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
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.project.management;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.TreeMap;
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;
27 * Project specification.
29 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
31 public class ProjectSpec {
36 /** Project database unique identifier */
39 /** Feature specification of the project. The list is sorted by versionedId */
40 List<FeatureSpec> features;
41 transient List<FeatureSpec> features_read_only_view;
45 * Create a new project specification
47 * @param name optional Name
48 * @param URI project unique identifier
49 * @param features features of the project
51 public ProjectSpec(String name, String URI, List<FeatureSpec> features) {
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();
64 public String getName() {
68 public String getURI() {
73 public int hashCode() {
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;
93 * Get a view to projects features
95 * @return features, list sorted by versionedId
97 public List<FeatureSpec> getFeatures() {
101 /** A query that reads all bundles from the database, result ordered by URI */
102 static Read<List<ProjectSpec>> QUERY = new Read<List<ProjectSpec>>() {
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);
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);
124 ProjectSpec spec = new ProjectSpec(name, URI, new ArrayList<FeatureSpec>(features.values()));
125 result.put(URI, spec);
127 return new ArrayList<ProjectSpec>(result.values());