1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.layer0.adapter.impl;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.WriteGraph;
24 import org.simantics.db.common.utils.NameUtils;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.layer0.adapter.PasteHandlerAdapter;
27 import org.simantics.db.layer0.internal.SimanticsInternal;
28 import org.simantics.db.layer0.util.ClipboardUtils;
29 import org.simantics.db.layer0.util.ModelTransferableGraphSource;
30 import org.simantics.db.layer0.util.PasteEventHandler;
31 import org.simantics.db.layer0.util.RemoverUtil;
32 import org.simantics.db.layer0.util.SimanticsClipboard;
33 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
34 import org.simantics.db.layer0.util.SimanticsKeys;
35 import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec;
36 import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec.SeedSpecType;
37 import org.simantics.graph.db.IImportAdvisor;
38 import org.simantics.graph.db.IImportAdvisor2;
39 import org.simantics.graph.db.IImportAdvisor2.RootInfo;
40 import org.simantics.graph.db.TransferableGraphException;
41 import org.simantics.graph.db.TransferableGraphSource;
42 import org.simantics.graph.db.TransferableGraphs;
43 import org.simantics.graph.representation.Identity;
44 import org.simantics.graph.representation.Root;
45 import org.simantics.graph.representation.TransferableGraph1;
46 import org.simantics.graph.representation.TransferableGraphUtils;
47 import org.simantics.layer0.Layer0;
49 public class DefaultPasteHandler extends PasteHandlerAdapter {
51 protected Resource resource;
53 public DefaultPasteHandler(Resource resource) {
54 this.resource = resource;
57 public static void defaultExecute(TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException, TransferableGraphException {
58 TransferableGraphs.importGraph1(SimanticsInternal.getSession(), tg, advisor);
61 public static void defaultExecute(WriteGraph graph, TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException {
62 TransferableGraphs.importGraph1(graph, tg, advisor);
65 public void execute(WriteGraph graph, TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException {
66 defaultExecute(graph, tg, resource, advisor);
70 * Override this in your inherited class if post processing is done.
72 public void onPasteBegin(WriteGraph graph) throws DatabaseException {
76 * Override this in your inherited class if post processing is done
77 * advisor.getTarget() returns an object under which data is copied
78 * advisor.getRoot() returns the object which have been pasted.
79 * @param representations TODO
81 public void onPaste(WriteGraph graph, IImportAdvisor2 advisor, Set<Representation> representations) throws DatabaseException {
85 * Override this in your inherited class if post processing is done.
87 public void onPasteEnd(WriteGraph graph) throws DatabaseException {
90 public IImportAdvisor2 getAdvisor(ReadGraph graph, TransferableGraph1 tg, Resource target, PasteEventHandler handler) throws DatabaseException {
92 Collection<Identity> roots = TransferableGraphUtils.getRoots(tg);
93 if(roots.size() == 1) {
94 Root root = (Root)roots.iterator().next().definition;
95 Resource type = graph.getPossibleResource(root.type);
96 ImportAdvisorFactory factory = graph.getPossibleAdapter(type, ImportAdvisorFactory.class);
99 return handler.createAdvisor(graph, factory, target);
101 return factory.create(graph, target, Collections.<String,Object>emptyMap());
105 return new DefaultPasteImportAdvisor(target);
108 public Collection<Resource> pasteObject(WriteGraph graph, Set<Representation> object, PasteEventHandler handler) throws DatabaseException {
109 Collection<Resource> result = new ArrayList<>();
110 TransferableGraphSource tgs = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH_SOURCE);
112 TransferableGraph1 tg = TransferableGraphs.create(graph, tgs);
113 IImportAdvisor2 advisor = getAdvisor(graph, tg, resource, handler);
114 execute(graph, tg, resource, advisor);
115 Collection<RootInfo> roots = advisor.getRootInfo();
116 if(handler != null) {
117 for(RootInfo r : roots) handler.postProcess(graph, r.resource);
119 onPaste(graph, advisor, object);
120 if(tgs instanceof ModelTransferableGraphSource) {
122 ModelTransferableGraphSource mtgs = (ModelTransferableGraphSource)tgs;
124 loop: for(SeedSpec spec : mtgs.getConfiguration().seeds) {
125 if(SeedSpecType.SPECIAL_ROOT.equals(spec.specType)) continue;
126 for(RootInfo info : roots) {
127 if(spec.name.equals(info.root.name)) {
128 result.add(info.resource);
134 result.addAll(advisor.getRoots());
139 result.addAll(advisor.getRoots());
146 public Collection<Resource> pasteFromClipboard(WriteGraph graph, SimanticsClipboard clipboard, PasteEventHandler handler) throws DatabaseException {
148 Collection<Resource> result = new ArrayList<>();
151 // Check if root resource is allowed for pasting
152 checkIfRootAllowsPaste(graph);
154 Map<String,Object> hints = Collections.singletonMap(ClipboardUtils.HINT_TARGET_RESOURCE, resource);
158 final Set<Resource> cuts = new HashSet<>();
159 for(Set<Representation> object : clipboard.getContents()) {
160 Collection<Resource> cut = ClipboardUtils.accept(object, SimanticsKeys.KEY_CUT_RESOURCES);
161 TransferableGraphSource tg = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH_SOURCE, hints);
163 result.addAll(pasteObject(graph, object, handler));
167 Layer0 L0 = Layer0.getInstance(graph);
168 for(Resource r : cut) {
169 graph.deny(r, L0.PartOf);
170 graph.claim(resource, L0.ConsistsOf, L0.PartOf, r);
178 if(!cuts.isEmpty()) {
179 for (Resource cut : cuts)
180 RemoverUtil.remove(graph, cut);
187 protected void checkIfRootAllowsPaste(ReadGraph graph) throws DatabaseException {
188 if (resource == null)
190 Layer0 L0 = Layer0.getInstance(graph);
191 // check if root is published
192 Boolean published = graph.getPossibleRelatedValue(resource, L0.Entity_published);
193 if (published != null && published) {
194 throw new DatabaseException("Target resource " + NameUtils.getSafeName(graph, resource) + " is published and does not allow paste.");
198 @SuppressWarnings("rawtypes")
200 public Object getAdapter(Class adapter) {
201 if(Resource.class == adapter) return resource;