]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/ui/SubscriptionDropActionFactory.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / ui / SubscriptionDropActionFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2014, 2017 Association for Decentralized Information Management in
3  * 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  *     Semantum Oy - #7313
12  *******************************************************************************/
13 package org.simantics.charts.ui;
14
15 import java.util.Collections;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Set;
19
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.simantics.Simantics;
23 import org.simantics.browsing.ui.common.ErrorLogger;
24 import org.simantics.charts.Activator;
25 import org.simantics.charts.internal.VariableUtils;
26 import org.simantics.databoard.util.ObjectUtils;
27 import org.simantics.db.ReadGraph;
28 import org.simantics.db.Resource;
29 import org.simantics.db.WriteGraph;
30 import org.simantics.db.common.request.WriteRequest;
31 import org.simantics.db.exception.DatabaseException;
32 import org.simantics.db.layer0.SelectionHints;
33 import org.simantics.db.layer0.adapter.DropActionFactory;
34 import org.simantics.db.layer0.request.PossibleModel;
35 import org.simantics.db.layer0.variable.RVI;
36 import org.simantics.db.layer0.variable.VariableReference;
37 import org.simantics.layer0.Layer0;
38 import org.simantics.modeling.ModelingResources;
39 import org.simantics.modeling.PropertyVariables;
40 import org.simantics.modeling.PropertyVariablesImpl;
41 import org.simantics.modeling.utils.VariableReferences;
42 import org.simantics.utils.ui.ISelectionUtils;
43
44 /**
45  * @author Tuukka Lehtonen
46  */
47 public class SubscriptionDropActionFactory implements DropActionFactory {
48
49     @Override
50     public Runnable create(ReadGraph g, Object target, Object source, int operation) throws DatabaseException {
51         //System.out.println("SUBSCRIPTION DROP: " + source + " -> " + target);
52
53         final Resource subscription = ISelectionUtils.getSinglePossibleKey(target, SelectionHints.KEY_MAIN, Resource.class);
54         if (subscription == null)
55             return null;
56         Resource targetModel = g.syncRequest(new PossibleModel(subscription));
57
58         if(source instanceof RVI) {
59             RVI rvi = (RVI)source;
60             List<VariableReference> refs = Collections.singletonList(new VariableReference(rvi, VariableUtils.getDatatype(g, targetModel, rvi), null));
61             return addSubscriptions(g, subscription, refs, Collections.<Resource>emptySet());
62         }
63
64         List<PropertyVariables> vars = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, PropertyVariables.class);
65         if (!vars.isEmpty()) {
66             // FIXME: this is a hack for indexed value support
67             vars = PropertyVariablesImpl.resolve(g, vars);
68             List<VariableReference> references = g.syncRequest(VariableReferences.toReferences(targetModel, vars));
69             if (!references.isEmpty())
70                 return addSubscriptions(g, subscription, references, Collections.<Resource>emptySet());
71         } else {
72             List<Resource> srcs = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Resource.class);
73             ModelingResources MOD = ModelingResources.getInstance(g);
74             Set<Resource> movedItems = new HashSet<>();
75             for (Resource src : srcs) {
76                 if (g.isInstanceOf(src, MOD.Subscription_Item)) {
77                     Resource model = g.syncRequest(new PossibleModel(src));
78                     if (ObjectUtils.objectEquals(targetModel, model))
79                         movedItems.add(src);
80                 }
81             }
82             if (!movedItems.isEmpty())
83                 return addSubscriptions(g, subscription, Collections.emptyList(), movedItems);
84         }
85
86         if (source instanceof String) {
87             return handleStringDrop(g, subscription, targetModel, (String) source);
88         }
89
90         return null;
91     }
92
93     private Runnable handleStringDrop(ReadGraph graph, Resource subscription, Resource targetModel, String source) throws DatabaseException {
94         try {
95             List<VariableReference> refs = VariableUtils.getVariableReferencesFromString(graph, targetModel, source);
96             return addSubscriptions(graph, subscription, refs, Collections.emptySet());
97         } catch (DatabaseException e) {
98             Activator.getDefault().getLog().log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, getClass().getSimpleName() + ": Unrecognized String input: " + source));
99             return null;
100         }
101     }
102
103         private Runnable addSubscriptions(ReadGraph graph, Resource subscription, List<VariableReference> references,
104             Set<Resource> movedSubscriptionItems) throws DatabaseException {
105         AddVariableToChartAction action = new AddVariableToChartAction(null, subscription, references).init(graph);
106         return () -> {
107             action.run();
108             if(!movedSubscriptionItems.isEmpty()) {
109                 Simantics.getSession().asyncRequest(new WriteRequest() {
110                     @Override
111                     public void perform(WriteGraph graph) throws DatabaseException {
112                         Layer0 L0 = Layer0.getInstance(graph);
113                         for (Resource item : movedSubscriptionItems) {
114                             graph.deny(item, L0.PartOf);
115                             graph.claim(subscription, L0.ConsistsOf, item);
116                         }
117                     }
118                 }, e -> {
119                     if (e != null)
120                         ErrorLogger.defaultLogError(e);
121                 });
122             }
123         };
124     }
125
126 }