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