]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/SharedOntologyImportAdvisor.java
Make Write-interfaces as @FunctionalInterface for lambdas
[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.Triple;
37
38 public class SharedOntologyImportAdvisor extends AbstractImportAdvisor2 {
39
40         private Layer0 L0;
41         
42         private Map<Root, Triple<Resource, Resource, List<String>>> toCreate = new HashMap<Root, Triple<Resource, Resource, List<String>>>();
43         private Map<Root, Resource> created = new HashMap<Root, Resource>();
44
45         final private boolean published;
46
47         final private Map<String,Object> context;
48         
49         public SharedOntologyImportAdvisor(boolean published) {
50                 this(Collections.<String,Object>emptyMap(), published);
51         }
52
53         public SharedOntologyImportAdvisor(Map<String,Object> context, boolean published) {
54                 this.context = context;
55                 this.published = published;
56         }
57
58         private Resource getNames(ReadGraph graph, String uri, List<String> names) throws DatabaseException {
59                 
60         if("http://".equals(uri) || "http:/".equals(uri)) {
61                 return graph.getRootLibrary();
62         }
63         
64         final String[] parts = URIStringUtils.splitURI(uri);
65         if(parts != null && parts.length == 2) {
66                 Resource base = graph.getPossibleResource(parts[0]);
67                 if(base == null) base = getNames(graph, parts[0], names);
68                 names.add(parts[1]);
69                 return base;
70         }
71         
72         return null;
73                 
74         }
75         
76         
77         @Override
78         public Resource analyzeRoot(ReadGraph graph, Root r) throws DatabaseException {
79
80                 L0 = Layer0.getInstance(graph);
81                 
82                 String uri = r.name;
83                 Map<String,String> renameMap = (Map<String,String>)context.get(ImportAdvisors.RENAME_MAP);
84                 if(renameMap != null) {
85                         String renamed = renameMap.get(uri);
86                         if(renamed != null) uri = renamed;
87                 }
88                 
89                 Resource existing = graph.getPossibleResource(uri);
90                 if(existing != null) throw new DatabaseException("Shared library " + uri + " exists already.");
91                 
92                 Resource type = graph.getPossibleResource(r.type);
93                 if(type == null) { 
94                     Set<String> types = new HashSet<String>();
95                     types.add(r.type);
96                     throw new MissingDependencyException(types);
97                 }
98                 //if(type == null) throw new DatabaseException("Shared library type " + r.type + " was not found.");
99                 
100                 List<String> namesToCreate = new ArrayList<String>();
101                 Resource base = getNames(graph, uri, namesToCreate);
102                 toCreate.put(r, Triple.make(base, type, namesToCreate));
103                 
104                 return null;
105                 
106         }
107
108         @Override
109         public Resource getTarget() {
110                 return null;
111         }
112         
113         @Override
114         public Resource createRoot(WriteOnlyGraph graph, Root r) throws DatabaseException {
115             return createRoot(graph, r, null);
116         }
117         
118         @Override
119         public Resource createRoot(WriteOnlyGraph graph, Root root, Resource resource) throws DatabaseException {
120                 return created.get(root);
121         }
122         
123     @Override
124     public void beforeWrite(WriteOnlyGraph graph, TransferableGraphImporter process)
125             throws DatabaseException {
126         
127         graph.markUndoPoint();
128         if(published) {
129                 XSupport xs = graph.getService(XSupport.class);
130                 xs.setServiceMode(false, true);
131         }
132         
133         graph.setClusterSet4NewResource(graph.getRootLibrary());
134         graph.flushCluster();
135
136         for(Map.Entry<Root, Triple<Resource, Resource, List<String>>> entry : toCreate.entrySet()) {
137                 
138             Triple<Resource, Resource, List<String>> recipe = entry.getValue();
139
140             Resource base = recipe.first;
141             for(int i=0;i<recipe.third.size()-1;i++) {
142                 Resource lib = graph.newResource();
143                 graph.claim(lib, L0.InstanceOf, null, L0.Library);
144                 graph.addLiteral(lib, L0.HasName, L0.NameOf, URIStringUtils.unescape( recipe.third.get(i) ), Bindings.STRING);
145                 graph.claim(base, L0.ConsistsOf, L0.PartOf, lib);
146                 base = lib;
147             }
148
149             Resource lib = graph.newResource();
150             graph.newClusterSet(lib);
151             graph.setClusterSet4NewResource(lib);
152             graph.claim(lib, L0.InstanceOf, null, recipe.second);
153             String name = URIStringUtils.unescape( recipe.third.get(recipe.third.size()-1) );
154             graph.addLiteral(lib, L0.HasName, L0.NameOf, name, Bindings.STRING);
155             graph.claim(base, L0.ConsistsOf, L0.PartOf, lib);
156
157             addRootInfo(entry.getKey(), name, lib);
158             
159             created.put(entry.getKey(), lib);
160             
161         }
162         
163         graph.flushCluster();
164         
165     }
166
167     @Override
168     public void afterWrite(WriteOnlyGraph graph, TransferableGraphImporter process)
169             throws DatabaseException {
170         
171         String name = getRootInfo().iterator().next().name;
172         Resource resource = getRootInfo().iterator().next().resource;
173         Layer0Utils.addCommentMetadata(graph, "Imported Shared Ontology " + name + " " + resource);
174         
175         if(published) {
176                 XSupport xs = graph.getService(XSupport.class);
177                 xs.setServiceMode(false, false);
178         }
179
180         graph.setClusterSet4NewResource(graph.getRootLibrary());
181         graph.flushCluster();
182         
183     }
184     
185     @Override
186     public boolean allowImmutableModifications() {
187         return false;
188     }
189         
190     @Override
191     public Resource createChild(WriteOnlyGraph graph, TransferableGraphImporter process, Resource parent,
192             String name) throws DatabaseException {
193         return process.createChild(graph, parent, null, name);
194     }
195     
196     @Override
197     public Resource createChild(WriteOnlyGraph graph, TransferableGraphImporter process, Resource parent, Resource child,
198             String name) throws DatabaseException {
199         return process.createChild(graph, parent, child, name);
200     }
201     
202 }