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