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