]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/DefaultPasteHandler.java
Clean up and support internal seed resources in tg export
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / DefaultPasteHandler.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.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.Map;
19 import java.util.Set;
20
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.WriteGraph;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.adapter.PasteHandlerAdapter;
26 import org.simantics.db.layer0.internal.SimanticsInternal;
27 import org.simantics.db.layer0.util.ClipboardUtils;
28 import org.simantics.db.layer0.util.ModelTransferableGraphSource;
29 import org.simantics.db.layer0.util.PasteEventHandler;
30 import org.simantics.db.layer0.util.RemoverUtil;
31 import org.simantics.db.layer0.util.SimanticsClipboard;
32 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
33 import org.simantics.db.layer0.util.SimanticsKeys;
34 import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec;
35 import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec.SeedSpecType;
36 import org.simantics.graph.db.IImportAdvisor;
37 import org.simantics.graph.db.IImportAdvisor2;
38 import org.simantics.graph.db.IImportAdvisor2.RootInfo;
39 import org.simantics.graph.db.TransferableGraphException;
40 import org.simantics.graph.db.TransferableGraphSource;
41 import org.simantics.graph.db.TransferableGraphs;
42 import org.simantics.graph.representation.Identity;
43 import org.simantics.graph.representation.Root;
44 import org.simantics.graph.representation.TransferableGraph1;
45 import org.simantics.graph.representation.TransferableGraphUtils;
46 import org.simantics.layer0.Layer0;
47
48 public class DefaultPasteHandler extends PasteHandlerAdapter {
49
50     protected Resource resource;
51
52     public DefaultPasteHandler(Resource resource) {
53         this.resource = resource;
54     }
55
56     public static void defaultExecute(TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException, TransferableGraphException {
57         TransferableGraphs.importGraph1(SimanticsInternal.getSession(), tg, advisor);
58     }
59
60     public static void defaultExecute(WriteGraph graph, TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException {
61         TransferableGraphs.importGraph1(graph, tg, advisor);
62     }
63
64     public void execute(WriteGraph graph, TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException {
65         defaultExecute(graph, tg, resource, advisor);
66     }
67
68     /**
69      * Override this in your inherited class if post processing is done.
70      */
71     public void onPasteBegin(WriteGraph graph) throws DatabaseException {
72     }
73
74     /**
75      * Override this in your inherited class if post processing is done
76      * advisor.getTarget() returns an object under which data is copied
77      * advisor.getRoot() returns the object which have been pasted.
78      * @param representations TODO
79      */
80     public void onPaste(WriteGraph graph, IImportAdvisor2 advisor, Set<Representation> representations) throws DatabaseException {
81     }
82
83     /**
84      * Override this in your inherited class if post processing is done.
85      */
86     public void onPasteEnd(WriteGraph graph) throws DatabaseException {
87     }
88     
89     public IImportAdvisor2 getAdvisor(ReadGraph graph, TransferableGraph1 tg, Resource target, PasteEventHandler handler) throws DatabaseException {
90         
91         Collection<Identity> roots = TransferableGraphUtils.getRoots(tg);
92         if(roots.size() == 1) {
93                 Root root = (Root)roots.iterator().next().definition;
94                 Resource type = graph.getPossibleResource(root.type);
95                 ImportAdvisorFactory factory = graph.getPossibleAdapter(type, ImportAdvisorFactory.class);
96                 if(factory != null) {
97                         if(handler != null)
98                                 return handler.createAdvisor(graph, factory, target);
99                         else
100                                 return factory.create(graph, target, Collections.<String,Object>emptyMap());
101                 }
102         }
103
104         return new DefaultPasteImportAdvisor(target);
105     }
106
107     public Collection<Resource> pasteObject(WriteGraph graph, Set<Representation> object, PasteEventHandler handler) throws DatabaseException {
108         Collection<Resource> result = new ArrayList<>();
109         TransferableGraphSource tgs = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH_SOURCE);
110         if (tgs != null) {
111                 TransferableGraph1 tg = TransferableGraphs.create(graph, tgs);
112             IImportAdvisor2 advisor = getAdvisor(graph, tg, resource, handler); 
113             execute(graph, tg, resource, advisor);
114             Collection<RootInfo> roots = advisor.getRootInfo();
115             if(handler != null) {
116                 for(RootInfo r : roots) handler.postProcess(graph, r.resource);
117             }
118             onPaste(graph, advisor, object);
119             if(tgs instanceof ModelTransferableGraphSource) {
120                 
121                 ModelTransferableGraphSource mtgs = (ModelTransferableGraphSource)tgs;
122                 
123                 loop: for(SeedSpec spec : mtgs.getConfiguration().seeds) {
124                         if(SeedSpecType.SPECIAL_ROOT.equals(spec.specType)) continue;
125                         for(RootInfo info : roots) {
126                                 if(spec.name.equals(info.root.name)) {
127                                         result.add(info.resource);
128                                         continue loop;
129                                 }
130                         }
131                                 // Fallback
132                                 result.clear();
133                                 result.addAll(advisor.getRoots());
134                                 break;
135                 }
136                 
137             } else {
138                 result.addAll(advisor.getRoots());
139             }
140         }
141         return result;
142     }
143
144     @Override
145     public Collection<Resource> pasteFromClipboard(WriteGraph graph, SimanticsClipboard clipboard, PasteEventHandler handler) throws DatabaseException {
146         
147         Collection<Resource> result = new ArrayList<>();
148         
149         Map<String,Object> hints = Collections.singletonMap(ClipboardUtils.HINT_TARGET_RESOURCE, resource);
150         
151         onPasteBegin(graph);
152
153         final Set<Resource> cuts = new HashSet<>();
154         for(Set<Representation> object : clipboard.getContents()) {
155             Collection<Resource> cut = ClipboardUtils.accept(object, SimanticsKeys.KEY_CUT_RESOURCES);
156             TransferableGraphSource tg = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH_SOURCE, hints);
157             if(tg != null) {
158                 result.addAll(pasteObject(graph, object, handler));
159                 if (cut != null)
160                     cuts.addAll(cut);
161             } else {
162                 Layer0 L0 = Layer0.getInstance(graph);
163                 for(Resource r : cut) {
164                         graph.deny(r, L0.PartOf);
165                         graph.claim(resource, L0.ConsistsOf, L0.PartOf, r);
166                         result.add(r);
167                 }
168             }
169         }
170
171         onPasteEnd(graph);
172
173         if(!cuts.isEmpty()) {
174                 for (Resource cut : cuts)
175                         RemoverUtil.remove(graph, cut);
176         }
177         
178         return result;
179         
180     }
181
182     @SuppressWarnings("rawtypes")
183     @Override
184     public Object getAdapter(Class adapter) {
185         if(Resource.class == adapter) return resource;
186         return null;
187     }
188
189 }