]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/StructuralResource.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / structural / StructuralResource.java
diff --git a/bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/StructuralResource.java b/bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/StructuralResource.java
new file mode 100644 (file)
index 0000000..857d0e4
--- /dev/null
@@ -0,0 +1,167 @@
+/*******************************************************************************\r
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.objmap.structural;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+\r
+/**\r
+ * An object representing structural Resource.\r
+ * \r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ *\r
+ */\r
+public class StructuralResource  {\r
+\r
+       private Resource resource;\r
+       private List<Resource> context = new ArrayList<Resource>(1);\r
+       \r
+       private Resource typeResource = null;\r
+       \r
+       public StructuralResource(Resource resource) {\r
+               assert(resource != null);\r
+               this.resource = resource;\r
+       }\r
+       public StructuralResource(ReadGraph g, Resource resource, Resource context) throws DatabaseException {\r
+               assert(resource != null);\r
+               this.resource = resource;\r
+               this.context.add(context);\r
+               resolveType(g);\r
+       }\r
+       \r
+       public StructuralResource(ReadGraph g, Resource resource, Resource... context) throws DatabaseException {\r
+               assert(resource != null);\r
+               this.resource = resource;\r
+               for (Resource r : context)\r
+                       this.context.add(r);\r
+               resolveType(g);\r
+       }\r
+       \r
+       public StructuralResource(ReadGraph g, Resource resource, List<Resource> context) throws DatabaseException {\r
+               assert(resource != null);\r
+               this.resource = resource;\r
+               for (Resource r : context)\r
+                       this.context.add(r);\r
+               resolveType(g); \r
+       }\r
+       public StructuralResource(ReadGraph g, Resource resource, List<Resource> context, Resource context2) throws DatabaseException {\r
+               assert(resource != null);\r
+               this.resource = resource;\r
+               for (Resource r : context)\r
+                       this.context.add(r);\r
+               this.context.add(context2);\r
+               resolveType(g);\r
+       }\r
+       \r
+       private void resolveType(ReadGraph g) throws DatabaseException {\r
+               if (this.context.contains(resource)) {\r
+                       Layer0 l0 = Layer0.getInstance(g);\r
+                       typeResource = g.getSingleObject(resource, l0.InstanceOf);\r
+               } \r
+       }\r
+       \r
+       \r
+       /**\r
+        * The Resource in the DB.\r
+        * @return\r
+        */\r
+       public Resource getResource() {\r
+               return resource;\r
+       }\r
+       \r
+       /**\r
+        * Context in which this resource is accessed. Each context resource represents a structural model instance. \r
+        * @return\r
+        */\r
+       public List<Resource> getContext() {\r
+               return context;\r
+       }\r
+       \r
+       /**\r
+        * If the resource is structural model instance, this returns the type Resource. Otherwise returns null.\r
+        * @return\r
+        */\r
+       public Resource getTypeResource() {\r
+               return typeResource;\r
+       }\r
+       \r
+       /**\r
+        * Returns true, if the resource is structural, \r
+        * @return\r
+        */\r
+       public boolean isStructural() {\r
+               return context.size() > 0;\r
+       }\r
+       \r
+       /**\r
+        * Returns true is the Resource is root of Structural Model instance.\r
+        * In this case the resource instance is editable.\r
+        * \r
+        * @return\r
+        */\r
+       public boolean isStructuralRoot() {\r
+               return (context.size() == 1 && context.get(0).equals(resource));\r
+       }\r
+       \r
+       /**\r
+        * Returns true,  the resource is structural model instance.\r
+        * @return\r
+        */\r
+       public boolean isStructuralInstance() {\r
+               return typeResource != null;\r
+       }\r
+       \r
+       @Override\r
+       public int hashCode() {\r
+               int hashCode = resource.hashCode();\r
+               for (Resource ctx : context)\r
+                       hashCode += ctx.hashCode();\r
+               return hashCode;\r
+       }\r
+       \r
+       @Override\r
+       public boolean equals(Object obj) {\r
+               if (obj == this)\r
+                       return true;\r
+               if (obj == null)\r
+                       return false;\r
+               if (obj.getClass() != getClass())\r
+                       return false;\r
+               StructuralResource other = (StructuralResource)obj;\r
+               if (!resource.equals(other.resource))\r
+                       return false;\r
+               if (context.size() != other.context.size())\r
+                       return false;\r
+               for (int i = 0; i < context.size(); i++) {\r
+                       if (!context.get(i).equals(other.context.get(i)))\r
+                               return false;\r
+               }\r
+               return true;\r
+       }\r
+       \r
+       @Override\r
+       public String toString() {\r
+               String s = "Res: " + resource + " Context:";\r
+               for (Resource ctx : context) \r
+                       s+= " "+ ctx;\r
+               if (typeResource != null)\r
+                       s+= " Type: " + typeResource;\r
+               return s;\r
+       }\r
+\r
+}\r