]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.project/src/org/simantics/project/management/OntologySpec.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / management / OntologySpec.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.Collection;
15 import java.util.List;
16 import java.util.TreeMap;
17 import java.util.regex.Pattern;
18
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.DatabaseManagementResource;
24 import org.simantics.layer0.Layer0;
25
26 /**
27  * A description of an graph bundle
28  *
29  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
30  */
31 public class OntologySpec {
32
33         /** Versioned Id pattern */     
34         static String ID_PATTERN_STRING =                "[a-zA-Z_0-9]+(?:\\.[a-zA-Z_0-9]+)*";
35         static String VERSION_PATTERN_STRING =           "(\\d+).(\\d+).(\\d+).([a-zA-Z_0-9\\-]+)";
36         static Pattern ID_PATTERN = Pattern.compile(ID_PATTERN_STRING);
37         static Pattern VERSION_PATTERN = Pattern.compile(VERSION_PATTERN_STRING);
38         static Pattern VERSIONED_ID_PATTERN = Pattern.compile("(" + ID_PATTERN_STRING + ")/" + VERSION_PATTERN_STRING + "");
39         
40         /** Display name */
41         String name;    
42         /** OSGi versioned ID - id/major.minor.micro(.qualifier) */
43         String versionedId;
44         
45         /**
46          * Create new bundle spec 
47          * 
48          * @param name optional name
49          * @param versionedId
50          */
51         public OntologySpec(String name, String versionedId) {
52                 if (!VERSIONED_ID_PATTERN.matcher(versionedId).matches()) 
53                         throw new IllegalArgumentException("not versioned id");
54                 this.versionedId = versionedId;
55                 this.name = name == null ? versionedId : name;
56         }
57         
58         public String getName() {
59                 return name;
60         }
61
62         public String getVersionedId() {
63                 return versionedId;
64         }
65
66         @Override
67         public int hashCode() {
68                 return versionedId.hashCode();
69         }
70         
71         @Override
72         public boolean equals(Object obj) {
73                 if (obj == null) return false;
74                 if (obj instanceof OntologySpec == false) return false;
75                 OntologySpec other = (OntologySpec) obj;
76                 return other.versionedId.equals(versionedId);
77         }
78         
79         @Override
80         public String toString() {
81                 return name + "("+versionedId+")";
82         }
83
84         /** A query that reads all bundles from the database, result is ordered by versioned id */
85         static Read<List<OntologySpec>> QUERY = new Read<List<OntologySpec>>() {
86                 @Override
87                 public List<OntologySpec> perform(ReadGraph g) throws DatabaseException {
88                         DatabaseManagementResource DatabaseManagement = DatabaseManagementResource.getInstance(g);
89                         Layer0 L0 = Layer0.getInstance(g);
90                         Collection<Resource> tgs = g.getObjects(DatabaseManagement.InstalledGraphBundles, L0.ConsistsOf);
91                         TreeMap<String, OntologySpec> result = new TreeMap<String, OntologySpec>();                     
92                         for (Resource tg : tgs) {
93                                 if ( !g.isInstanceOf(tg, DatabaseManagement.GraphBundle) ) continue; 
94                                 try {
95                                         String name = g.getPossibleRelatedValue(tg, L0.HasName);
96                                         String versionedId = g.getPossibleRelatedValue(tg, DatabaseManagement.HasVersionedId);
97                                         OntologySpec bs = new OntologySpec(name, versionedId);
98                                         result.put( bs.versionedId, bs );
99                                 } catch (IllegalArgumentException iae) {
100                                         continue;
101                                 }
102                         }                                       
103                         return new ArrayList<OntologySpec>(result.values());
104                 }
105         };
106         
107 }
108