]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/SharedOntologyImportAdvisor.java
Sync git svn branch with SVN repository r33389.
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / SharedOntologyImportAdvisor.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.layer0.adapter.impl;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collections;\r
16 import java.util.HashMap;\r
17 import java.util.HashSet;\r
18 import java.util.List;\r
19 import java.util.Map;\r
20 import java.util.Set;\r
21 \r
22 import org.simantics.databoard.Bindings;\r
23 import org.simantics.databoard.util.URIStringUtils;\r
24 import org.simantics.db.ReadGraph;\r
25 import org.simantics.db.Resource;\r
26 import org.simantics.db.WriteOnlyGraph;\r
27 import org.simantics.db.exception.DatabaseException;\r
28 import org.simantics.db.layer0.util.Layer0Utils;\r
29 import org.simantics.db.service.XSupport;\r
30 import org.simantics.graph.db.AbstractImportAdvisor2;\r
31 import org.simantics.graph.db.ImportAdvisors;\r
32 import org.simantics.graph.db.MissingDependencyException;\r
33 import org.simantics.graph.db.TransferableGraphImporter;\r
34 import org.simantics.graph.representation.Root;\r
35 import org.simantics.layer0.Layer0;\r
36 import org.simantics.utils.datastructures.Triple;\r
37 \r
38 public class SharedOntologyImportAdvisor extends AbstractImportAdvisor2 {\r
39 \r
40         private Layer0 L0;\r
41         \r
42         private Map<Root, Triple<Resource, Resource, List<String>>> toCreate = new HashMap<Root, Triple<Resource, Resource, List<String>>>();\r
43         private Map<Root, Resource> created = new HashMap<Root, Resource>();\r
44 \r
45         final private boolean published;\r
46 \r
47         final private Map<String,Object> context;\r
48         \r
49         public SharedOntologyImportAdvisor(boolean published) {\r
50                 this(Collections.<String,Object>emptyMap(), published);\r
51         }\r
52 \r
53         public SharedOntologyImportAdvisor(Map<String,Object> context, boolean published) {\r
54                 this.context = context;\r
55                 this.published = published;\r
56         }\r
57 \r
58         private Resource getNames(ReadGraph graph, String uri, List<String> names) throws DatabaseException {\r
59                 \r
60         if("http://".equals(uri) || "http:/".equals(uri)) {\r
61                 return graph.getRootLibrary();\r
62         }\r
63         \r
64         final String[] parts = URIStringUtils.splitURI(uri);\r
65         if(parts != null && parts.length == 2) {\r
66                 Resource base = graph.getPossibleResource(parts[0]);\r
67                 if(base == null) base = getNames(graph, parts[0], names);\r
68                 names.add(parts[1]);\r
69                 return base;\r
70         }\r
71         \r
72         return null;\r
73                 \r
74         }\r
75         \r
76         \r
77         @Override\r
78         public Resource analyzeRoot(ReadGraph graph, Root r) throws DatabaseException {\r
79 \r
80                 L0 = Layer0.getInstance(graph);\r
81                 \r
82                 String uri = r.name;\r
83                 Map<String,String> renameMap = (Map<String,String>)context.get(ImportAdvisors.RENAME_MAP);\r
84                 if(renameMap != null) {\r
85                         String renamed = renameMap.get(uri);\r
86                         if(renamed != null) uri = renamed;\r
87                 }\r
88                 \r
89                 Resource existing = graph.getPossibleResource(uri);\r
90                 if(existing != null) throw new DatabaseException("Shared library " + uri + " exists already.");\r
91                 \r
92                 Resource type = graph.getPossibleResource(r.type);\r
93                 if(type == null) { \r
94                     Set<String> types = new HashSet<String>();\r
95                     types.add(r.type);\r
96                     throw new MissingDependencyException(types);\r
97                 }\r
98                 //if(type == null) throw new DatabaseException("Shared library type " + r.type + " was not found.");\r
99                 \r
100                 List<String> namesToCreate = new ArrayList<String>();\r
101                 Resource base = getNames(graph, uri, namesToCreate);\r
102                 toCreate.put(r, Triple.make(base, type, namesToCreate));\r
103                 \r
104                 return null;\r
105                 \r
106         }\r
107 \r
108         @Override\r
109         public Resource getTarget() {\r
110                 return null;\r
111         }\r
112         \r
113         @Override\r
114         public Resource createRoot(WriteOnlyGraph graph, Root r) throws DatabaseException {\r
115             return createRoot(graph, r, null);\r
116         }\r
117         \r
118         @Override\r
119         public Resource createRoot(WriteOnlyGraph graph, Root root, Resource resource) throws DatabaseException {\r
120                 return created.get(root);\r
121         }\r
122         \r
123     @Override\r
124     public void beforeWrite(WriteOnlyGraph graph, TransferableGraphImporter process)\r
125             throws DatabaseException {\r
126         \r
127         graph.markUndoPoint();\r
128         if(published) {\r
129                 XSupport xs = graph.getService(XSupport.class);\r
130                 xs.setServiceMode(false, true);\r
131         }\r
132         \r
133         graph.setClusterSet4NewResource(graph.getRootLibrary());\r
134         graph.flushCluster();\r
135 \r
136         for(Map.Entry<Root, Triple<Resource, Resource, List<String>>> entry : toCreate.entrySet()) {\r
137                 \r
138             Triple<Resource, Resource, List<String>> recipe = entry.getValue();\r
139 \r
140             Resource base = recipe.first;\r
141             for(int i=0;i<recipe.third.size()-1;i++) {\r
142                 Resource lib = graph.newResource();\r
143                 graph.claim(lib, L0.InstanceOf, null, L0.Library);\r
144                 graph.addLiteral(lib, L0.HasName, L0.NameOf, URIStringUtils.unescape( recipe.third.get(i) ), Bindings.STRING);\r
145                 graph.claim(base, L0.ConsistsOf, L0.PartOf, lib);\r
146                 base = lib;\r
147             }\r
148 \r
149             Resource lib = graph.newResource();\r
150             graph.newClusterSet(lib);\r
151             graph.setClusterSet4NewResource(lib);\r
152             graph.claim(lib, L0.InstanceOf, null, recipe.second);\r
153             String name = URIStringUtils.unescape( recipe.third.get(recipe.third.size()-1) );\r
154             graph.addLiteral(lib, L0.HasName, L0.NameOf, name, Bindings.STRING);\r
155             graph.claim(base, L0.ConsistsOf, L0.PartOf, lib);\r
156 \r
157             addRootInfo(entry.getKey(), name, lib);\r
158             \r
159             created.put(entry.getKey(), lib);\r
160             \r
161         }\r
162         \r
163         graph.flushCluster();\r
164         \r
165     }\r
166 \r
167     @Override\r
168     public void afterWrite(WriteOnlyGraph graph, TransferableGraphImporter process)\r
169             throws DatabaseException {\r
170         \r
171         String name = getRootInfo().iterator().next().name;\r
172         Resource resource = getRootInfo().iterator().next().resource;\r
173         Layer0Utils.addCommentMetadata(graph, "Imported Shared Ontology " + name + " " + resource);\r
174         \r
175         if(published) {\r
176                 XSupport xs = graph.getService(XSupport.class);\r
177                 xs.setServiceMode(false, false);\r
178         }\r
179 \r
180         graph.setClusterSet4NewResource(graph.getRootLibrary());\r
181         graph.flushCluster();\r
182         \r
183     }\r
184     \r
185     @Override\r
186     public boolean allowImmutableModifications() {\r
187         return false;\r
188     }\r
189         \r
190     @Override\r
191     public Resource createChild(WriteOnlyGraph graph, TransferableGraphImporter process, Resource parent,\r
192             String name) throws DatabaseException {\r
193         return process.createChild(graph, parent, null, name);\r
194     }\r
195     \r
196     @Override\r
197     public Resource createChild(WriteOnlyGraph graph, TransferableGraphImporter process, Resource parent, Resource child,\r
198             String name) throws DatabaseException {\r
199         return process.createChild(graph, parent, child, name);\r
200     }\r
201     \r
202 }\r