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