]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.project/src/org/simantics/project/management/ProjectSpec.java
Include acorn db in db.client feature and make it the default db driver
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / management / ProjectSpec.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.project.management;\r
12 \r
13 import java.util.ArrayList;\r
14 import java.util.Collections;\r
15 import java.util.List;\r
16 import java.util.TreeMap;\r
17 \r
18 import org.simantics.databoard.Bindings;\r
19 import org.simantics.db.ReadGraph;\r
20 import org.simantics.db.Resource;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.db.request.Read;\r
23 import org.simantics.layer0.Layer0;\r
24 import org.simantics.project.ontology.ProjectResource;\r
25 \r
26 /**\r
27  * Project specification.\r
28  *\r
29  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>\r
30  */\r
31 public class ProjectSpec {\r
32  \r
33         /** Display Name */\r
34         String name;\r
35         \r
36         /** Project database unique identifier */\r
37         String URI;\r
38         \r
39         /** Feature specification of the project. The list is sorted by versionedId */\r
40         List<FeatureSpec> features;\r
41         transient List<FeatureSpec> features_read_only_view;\r
42         transient int hash;\r
43         \r
44         /**\r
45          * Create a new project specification\r
46          * \r
47          * @param name optional Name\r
48          * @param URI project unique identifier\r
49          * @param features features of the project\r
50          */\r
51         public ProjectSpec(String name, String URI, List<FeatureSpec> features) {\r
52                 this.name = name;\r
53                 this.URI = URI;\r
54                 TreeMap<String, FeatureSpec> sortedList = new TreeMap<String, FeatureSpec>();\r
55                 for (FeatureSpec fs : features) sortedList.put(fs.versionedId, fs);\r
56                 this.features = new ArrayList<FeatureSpec>(sortedList.values());\r
57                 this.features_read_only_view = Collections.unmodifiableList(this.features);\r
58                 hash = URI.hashCode();\r
59                 for (FeatureSpec fs : this.features) {\r
60                         hash = 13*hash + fs.hashCode();\r
61                 }\r
62         }       \r
63 \r
64         public String getName() {\r
65                 return name;\r
66         }\r
67 \r
68         public String getURI() {\r
69                 return URI;\r
70         }\r
71         \r
72         @Override\r
73         public int hashCode() {\r
74                 return hash;\r
75         }\r
76         \r
77         @Override\r
78         public boolean equals(Object obj) {\r
79                 if (obj == null) return false;\r
80                 if (obj instanceof ProjectSpec == false) return false;\r
81                 if (obj==this) return true;\r
82                 ProjectSpec other = (ProjectSpec) obj;\r
83                 if (!other.URI.equals(URI)) return false;\r
84                 if (other.features.size() != features.size()) return false;\r
85                 int c = features.size();\r
86                 for (int i=0; i<c; i++) {\r
87                         if (!features.get(i).equals(other.features.get(i))) return false;\r
88                 }\r
89                 return true;\r
90         }\r
91 \r
92         /**\r
93          * Get a view to projects features\r
94          * \r
95          * @return features, list sorted by versionedId\r
96          */\r
97         public List<FeatureSpec> getFeatures() {\r
98                 return features;\r
99         }\r
100         \r
101         /** A query that reads all bundles from the database, result ordered by URI */\r
102         static Read<List<ProjectSpec>> QUERY = new Read<List<ProjectSpec>>() {\r
103                 @Override\r
104                 public List<ProjectSpec> perform(ReadGraph g) throws DatabaseException {\r
105                         ProjectResource pr = ProjectResource.getInstance(g);\r
106                         Layer0 L0 = Layer0.getInstance(g);\r
107                 Resource root = g.getResource("http://Projects");\r
108                 TreeMap<String, ProjectSpec> result = new TreeMap<String, ProjectSpec>();\r
109                 TreeMap<String, FeatureSpec> features = new TreeMap<String, FeatureSpec>();\r
110                         for (Resource r : g.getObjects(root, L0.ConsistsOf) ) {\r
111                                 String URI = g.getURI( r );\r
112                                 String name = g.getPossibleRelatedValue(r, L0.HasName);\r
113                                 \r
114                                 features.clear();                               \r
115                                 for (Resource r2 : g.getObjects(r, pr.HasFeature)) {\r
116                                         FeatureSpec spec = new FeatureSpec();\r
117                                         spec.versionedId = g.getPossibleRelatedValue(r2, pr.HasGroupId, Bindings.STRING);\r
118                                         spec.name = g.getPossibleRelatedValue(r2, L0.HasName, Bindings.STRING);\r
119                                         Boolean b = g.getPossibleRelatedValue(r2, pr.IsRequired, Bindings.BOOLEAN);\r
120                                         spec.required = b!=null ? b : true;                                     \r
121                                         features.put(spec.versionedId, spec);\r
122                                 }\r
123                                 \r
124                                 ProjectSpec spec = new ProjectSpec(name, URI, new ArrayList<FeatureSpec>(features.values()));\r
125                                 result.put(URI, spec);\r
126                         }\r
127                         return new ArrayList<ProjectSpec>(result.values());\r
128                 }\r
129         };\r
130                 \r
131         \r
132 }\r
133 \r