]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.objmap.structural;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.List;\r
16 \r
17 import org.simantics.db.ReadGraph;\r
18 import org.simantics.db.Resource;\r
19 import org.simantics.db.exception.DatabaseException;\r
20 import org.simantics.layer0.Layer0;\r
21 \r
22 /**\r
23  * An object representing structural Resource.\r
24  * \r
25  * \r
26  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
27  *\r
28  */\r
29 public class StructuralResource  {\r
30 \r
31         private Resource resource;\r
32         private List<Resource> context = new ArrayList<Resource>(1);\r
33         \r
34         private Resource typeResource = null;\r
35         \r
36         public StructuralResource(Resource resource) {\r
37                 assert(resource != null);\r
38                 this.resource = resource;\r
39         }\r
40         public StructuralResource(ReadGraph g, Resource resource, Resource context) throws DatabaseException {\r
41                 assert(resource != null);\r
42                 this.resource = resource;\r
43                 this.context.add(context);\r
44                 resolveType(g);\r
45         }\r
46         \r
47         public StructuralResource(ReadGraph g, Resource resource, Resource... context) throws DatabaseException {\r
48                 assert(resource != null);\r
49                 this.resource = resource;\r
50                 for (Resource r : context)\r
51                         this.context.add(r);\r
52                 resolveType(g);\r
53         }\r
54         \r
55         public StructuralResource(ReadGraph g, Resource resource, List<Resource> context) throws DatabaseException {\r
56                 assert(resource != null);\r
57                 this.resource = resource;\r
58                 for (Resource r : context)\r
59                         this.context.add(r);\r
60                 resolveType(g); \r
61         }\r
62         public StructuralResource(ReadGraph g, Resource resource, List<Resource> context, Resource context2) throws DatabaseException {\r
63                 assert(resource != null);\r
64                 this.resource = resource;\r
65                 for (Resource r : context)\r
66                         this.context.add(r);\r
67                 this.context.add(context2);\r
68                 resolveType(g);\r
69         }\r
70         \r
71         private void resolveType(ReadGraph g) throws DatabaseException {\r
72                 if (this.context.contains(resource)) {\r
73                         Layer0 l0 = Layer0.getInstance(g);\r
74                         typeResource = g.getSingleObject(resource, l0.InstanceOf);\r
75                 } \r
76         }\r
77         \r
78         \r
79         /**\r
80          * The Resource in the DB.\r
81          * @return\r
82          */\r
83         public Resource getResource() {\r
84                 return resource;\r
85         }\r
86         \r
87         /**\r
88          * Context in which this resource is accessed. Each context resource represents a structural model instance. \r
89          * @return\r
90          */\r
91         public List<Resource> getContext() {\r
92                 return context;\r
93         }\r
94         \r
95         /**\r
96          * If the resource is structural model instance, this returns the type Resource. Otherwise returns null.\r
97          * @return\r
98          */\r
99         public Resource getTypeResource() {\r
100                 return typeResource;\r
101         }\r
102         \r
103         /**\r
104          * Returns true, if the resource is structural, \r
105          * @return\r
106          */\r
107         public boolean isStructural() {\r
108                 return context.size() > 0;\r
109         }\r
110         \r
111         /**\r
112          * Returns true is the Resource is root of Structural Model instance.\r
113          * In this case the resource instance is editable.\r
114          * \r
115          * @return\r
116          */\r
117         public boolean isStructuralRoot() {\r
118                 return (context.size() == 1 && context.get(0).equals(resource));\r
119         }\r
120         \r
121         /**\r
122          * Returns true,  the resource is structural model instance.\r
123          * @return\r
124          */\r
125         public boolean isStructuralInstance() {\r
126                 return typeResource != null;\r
127         }\r
128         \r
129         @Override\r
130         public int hashCode() {\r
131                 int hashCode = resource.hashCode();\r
132                 for (Resource ctx : context)\r
133                         hashCode += ctx.hashCode();\r
134                 return hashCode;\r
135         }\r
136         \r
137         @Override\r
138         public boolean equals(Object obj) {\r
139                 if (obj == this)\r
140                         return true;\r
141                 if (obj == null)\r
142                         return false;\r
143                 if (obj.getClass() != getClass())\r
144                         return false;\r
145                 StructuralResource other = (StructuralResource)obj;\r
146                 if (!resource.equals(other.resource))\r
147                         return false;\r
148                 if (context.size() != other.context.size())\r
149                         return false;\r
150                 for (int i = 0; i < context.size(); i++) {\r
151                         if (!context.get(i).equals(other.context.get(i)))\r
152                                 return false;\r
153                 }\r
154                 return true;\r
155         }\r
156         \r
157         @Override\r
158         public String toString() {\r
159                 String s = "Res: " + resource + " Context:";\r
160                 for (Resource ctx : context) \r
161                         s+= " "+ ctx;\r
162                 if (typeResource != null)\r
163                         s+= " Type: " + typeResource;\r
164                 return s;\r
165         }\r
166 \r
167 }\r