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
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.project.management;
\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
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
27 * Project specification.
\r
29 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
\r
31 public class ProjectSpec {
\r
36 /** Project database unique identifier */
\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
45 * Create a new project specification
\r
47 * @param name optional Name
\r
48 * @param URI project unique identifier
\r
49 * @param features features of the project
\r
51 public ProjectSpec(String name, String URI, List<FeatureSpec> features) {
\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
64 public String getName() {
\r
68 public String getURI() {
\r
73 public int hashCode() {
\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
93 * Get a view to projects features
\r
95 * @return features, list sorted by versionedId
\r
97 public List<FeatureSpec> getFeatures() {
\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
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
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
124 ProjectSpec spec = new ProjectSpec(name, URI, new ArrayList<FeatureSpec>(features.values()));
\r
125 result.put(URI, spec);
\r
127 return new ArrayList<ProjectSpec>(result.values());
\r