]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphfile/src/org/simantics/graphfile/adapters/SystemResourcePasteImportAdvisor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graphfile / src / org / simantics / graphfile / adapters / SystemResourcePasteImportAdvisor.java
1 /*******************************************************************************\r
2  * Copyright (c) 2013 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.graphfile.adapters;\r
13 \r
14 import java.util.HashMap;\r
15 import java.util.Map;\r
16 \r
17 import org.simantics.databoard.Bindings;\r
18 import org.simantics.db.ReadGraph;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.db.WriteGraph;\r
21 import org.simantics.db.WriteOnlyGraph;\r
22 import org.simantics.db.common.request.FreshEscapedName;\r
23 import org.simantics.db.exception.DatabaseException;\r
24 import org.simantics.db.layer0.request.PossibleModel;\r
25 import org.simantics.graph.db.IImportAdvisor;\r
26 import org.simantics.graph.representation.Root;\r
27 import org.simantics.graphfile.ontology.GraphFileResource;\r
28 import org.simantics.layer0.Layer0;\r
29 \r
30 public class SystemResourcePasteImportAdvisor implements IImportAdvisor {\r
31 \r
32         protected Resource root;\r
33         protected Resource library;\r
34         protected Resource model;\r
35         protected String singleType = null;\r
36         protected Map<String, String> nameMappings = new HashMap<String, String>();\r
37         \r
38         public SystemResourcePasteImportAdvisor(ReadGraph graph, Resource library) throws DatabaseException {\r
39                 this.library = library;\r
40                 this.model = graph.syncRequest(new PossibleModel(library));\r
41         }\r
42 \r
43         public Resource getTarget() {\r
44                 return library;\r
45         }\r
46 \r
47         public Resource analyzeType(ReadGraph graph, Root root) throws DatabaseException {\r
48                 return null;\r
49         }\r
50         \r
51         @Override\r
52         public Resource analyzeRoot(ReadGraph graph, Root root) throws DatabaseException {\r
53                 \r
54                 if("%model".equals(root.name)) return model;\r
55                 \r
56                 analyzeType(graph, root);\r
57 \r
58                 String type = root.type;\r
59                 if(singleType != null) {\r
60                         if(!type.equals(singleType)) throw new DatabaseException("Paste of a set of different types of objects is not supported.");\r
61                 } else {\r
62                         singleType = type;\r
63                 }\r
64                 \r
65                 String newName = newName(graph, library, root.name);\r
66                 nameMappings.put(root.name, newName);\r
67                 \r
68                 return null;\r
69 \r
70         }\r
71         \r
72         public String newName(ReadGraph graph, Resource library, String name) throws DatabaseException {\r
73                 return graph.syncRequest(new FreshEscapedName(library, GraphFileResource.getInstance(graph).HasSystemResource, name));\r
74         }\r
75 \r
76         @Override\r
77         public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {\r
78                 \r
79                 Layer0 l0 = graph.getService(Layer0.class);\r
80 \r
81                 this.root = graph.newResource();\r
82                 String name = root.name;\r
83                 String newName = nameMappings.get(name);\r
84                 graph.addLiteral(this.root, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING);\r
85                 return this.root;\r
86 \r
87         }\r
88         \r
89         public Resource getRoot() {\r
90                 return root;\r
91         }\r
92         \r
93         public void attachRoot(WriteGraph graph) throws DatabaseException {\r
94                 GraphFileResource gf = GraphFileResource.getInstance(graph);\r
95                 if (graph.isInstanceOf(library, gf.Folder)) {\r
96                         if (graph.isInstanceOf(root, gf.Folder)) {\r
97                                 graph.claim(library, gf.HasFolder, root);\r
98                                 return;\r
99                         }  else if (graph.isInstanceOf(root, gf.File)) {\r
100                                 graph.claim(library, gf.HasFile, root);\r
101                                 return;\r
102                         }\r
103                 }\r
104                 throw new DatabaseException("Unknown type, cannot attach copied resource " + root);\r
105         }\r
106 \r
107 }\r