]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/DefaultPasteImportAdvisor.java
Sync git svn branch with SVN repository r33389.
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / DefaultPasteImportAdvisor.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.Collections;\r
15 import java.util.HashMap;\r
16 import java.util.Map;\r
17 \r
18 import org.simantics.databoard.Bindings;\r
19 import org.simantics.db.ReadGraph;\r
20 import org.simantics.db.Resource;\r
21 import org.simantics.db.WriteOnlyGraph;\r
22 import org.simantics.db.common.request.FreshEscapedName;\r
23 import org.simantics.db.common.request.PossibleIndexRoot;\r
24 import org.simantics.db.common.utils.Versions;\r
25 import org.simantics.db.exception.DatabaseException;\r
26 import org.simantics.db.service.SerialisationSupport;\r
27 import org.simantics.graph.db.AbstractImportAdvisor2;\r
28 import org.simantics.graph.db.ImportAdvisors;\r
29 import org.simantics.graph.db.TransferableGraphImporter;\r
30 import org.simantics.graph.representation.Root;\r
31 import org.simantics.layer0.Layer0;\r
32 \r
33 public class DefaultPasteImportAdvisor extends AbstractImportAdvisor2 {\r
34 \r
35         protected Resource library;\r
36         protected Resource temp;\r
37         protected Resource model;\r
38         protected String singleType = null;\r
39         protected long[] ids;\r
40         protected Map<String, String> nameMappings = new HashMap<String, String>();\r
41         \r
42         final protected Map<String, Object> context;\r
43 \r
44         public DefaultPasteImportAdvisor(ReadGraph graph, Resource library) throws DatabaseException {\r
45                 this(library, graph.syncRequest(new PossibleIndexRoot(library)), Collections.<String,Object>emptyMap());\r
46         }\r
47 \r
48         public DefaultPasteImportAdvisor(Resource library) {\r
49                 this(library, library, Collections.<String,Object>emptyMap());\r
50         }\r
51 \r
52         public DefaultPasteImportAdvisor(Resource library, Map<String, Object> context) {\r
53                 this(library, library, context);\r
54         }\r
55 \r
56         public DefaultPasteImportAdvisor(Resource library, Resource model, Map<String, Object> context) {\r
57                 this.library = library;\r
58                 this.model = model;\r
59                 this.context = context;\r
60         }\r
61 \r
62         @Override\r
63         public void redirect(Resource temp) {\r
64                 this.temp = temp;\r
65         }\r
66         \r
67         private Resource getRedirectTarget() {\r
68                 if(temp != null) return temp;\r
69                 else return library;\r
70         }\r
71         \r
72         public Resource getTarget() {\r
73                 return library;\r
74         }\r
75         \r
76         public void analyzeType(ReadGraph graph, Root root) throws DatabaseException {\r
77         }\r
78         \r
79         @Override\r
80         public Resource analyzeRoot(ReadGraph graph, Root root) throws DatabaseException {\r
81                 \r
82                 if("%model".equals(root.name)) {\r
83                         return model;\r
84                 }\r
85 \r
86                 analyzeType(graph, root);\r
87 \r
88                 String type = root.type;\r
89                 if(singleType != null) {\r
90                         if(!type.equals(singleType)) throw new DatabaseException("Paste of a set of different types of objects is not supported.");\r
91                 } else {\r
92                         singleType = type;\r
93                 }\r
94                 \r
95                 if(library != null) {\r
96                         String newName = newName(graph, library, root.name);\r
97                         nameMappings.put(root.name, newName);\r
98                 }\r
99                 \r
100                 return null;\r
101 \r
102         }\r
103         \r
104         public String newName(ReadGraph graph, Resource library, String name) throws DatabaseException {\r
105                 \r
106                 Map<String,String> renameMap = (Map<String,String>)context.get(ImportAdvisors.RENAME_MAP);\r
107                 if(renameMap != null) {\r
108                         String renamed = renameMap.get(name);\r
109                         if(renamed != null) return renamed;\r
110                 }\r
111                 \r
112 \r
113                 String version = Versions.getVersion(name);\r
114                 if(version != null) {\r
115                         String baseName = Versions.getBaseName(name);\r
116                         String newBaseName = graph.syncRequest(new FreshEscapedName(library, Layer0.getInstance(graph).ConsistsOf, baseName));\r
117                         return Versions.make(newBaseName, Versions.getBaseVersion(version));\r
118                 } else {\r
119                         return graph.syncRequest(new FreshEscapedName(library, Layer0.getInstance(graph).ConsistsOf, name)); \r
120                 }\r
121                 \r
122         }\r
123 \r
124         @Override\r
125         public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {\r
126                 return createRoot(graph, root, null);\r
127         }\r
128         \r
129         public String getName(Root root) {\r
130                 String name = root.name;\r
131                 String newName = nameMappings.get(name);\r
132                 if(newName != null) name = newName;\r
133                 return name;\r
134         }\r
135         \r
136         @Override\r
137         public Resource createRoot(WriteOnlyGraph graph, Root root, Resource resource) throws DatabaseException {\r
138                 \r
139                 Layer0 l0 = graph.getService(Layer0.class);\r
140 \r
141                 if(resource == null) resource = graph.newResource();\r
142                 if(getRedirectTarget() != null) {\r
143                         graph.claim(getRedirectTarget(), l0.ConsistsOf, l0.PartOf, resource);\r
144                 }\r
145                 String newName = getName(root);\r
146                 graph.addLiteral(resource, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING);\r
147                 \r
148                 addRootInfo(root, newName, resource);\r
149                 \r
150                 return resource;\r
151                 \r
152         }\r
153 \r
154     @Override\r
155     public void beforeWrite(WriteOnlyGraph graph, TransferableGraphImporter process)\r
156             throws DatabaseException {\r
157     }\r
158 \r
159     @Override\r
160     public void afterWrite(WriteOnlyGraph graph, TransferableGraphImporter process)\r
161             throws DatabaseException {\r
162         ids = process.getResourceIds(graph.getService(SerialisationSupport.class));\r
163     }\r
164     \r
165     @Override\r
166     public boolean allowImmutableModifications() {\r
167         return false;\r
168     }\r
169         \r
170     @Override\r
171     public Resource createChild(WriteOnlyGraph graph, TransferableGraphImporter process, Resource parent,\r
172             String name) throws DatabaseException {\r
173         return process.createChild(graph, parent, null, name);\r
174     }\r
175     \r
176     @Override\r
177     public Resource createChild(WriteOnlyGraph graph, TransferableGraphImporter process, Resource parent, Resource child,\r
178             String name) throws DatabaseException {\r
179         return process.createChild(graph, parent, child, name);\r
180     }\r
181     \r
182     public long[] getResourceIds() {\r
183         return ids;\r
184     }\r
185     \r
186 }\r