1 package org.simantics.document.linking.ge;
3 import java.util.ArrayList;
4 import java.util.Collection;
6 import java.util.Stack;
8 import org.eclipse.jface.dialogs.MessageDialog;
9 import org.eclipse.swt.widgets.Display;
10 import org.simantics.Simantics;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.WriteGraph;
14 import org.simantics.db.common.request.WriteRequest;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.adapter.ActionFactory;
17 import org.simantics.db.layer0.variable.Variable;
18 import org.simantics.db.request.Read;
19 import org.simantics.document.linking.ontology.DocumentLink;
20 import org.simantics.document.linking.utils.SourceLinkUtil;
21 import org.simantics.utils.datastructures.Pair;
22 import org.simantics.utils.ui.ExceptionUtils;
24 public class FixAllReferencesAction implements ActionFactory {
27 public Runnable create(final Object target) {
28 return new Runnable() {
32 System.out.println(target);
34 Pair<Collection<Resource>, Collection<Resource>> refs = Simantics.getSession().syncRequest(new FindFixable(target));
35 String dialogTitle = "Fix References";
36 String dialogMessage = "Fix " + refs.first.size() + " old references and " + refs.second.size() + " removed references?";
37 String dialogButtonLabels[] = new String[]{"Ok","Cancel"};
39 if (refs.first.size() == 0 && refs.second.size() == 0) {
40 dialogMessage = "Nothing to fix.";
41 dialogButtonLabels = new String[]{"OK"};
42 MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(), dialogTitle, null, dialogMessage, MessageDialog.CONFIRM, dialogButtonLabels, defaultIndex);
46 MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(), dialogTitle, null, dialogMessage, MessageDialog.CONFIRM, dialogButtonLabels, defaultIndex);
47 if (dialog.open() != 0)
49 Simantics.getSession().markUndoPoint();
50 Simantics.getSession().syncRequest(new FixAll(refs));
51 } catch (DatabaseException e) {
52 ExceptionUtils.logAndShowError("Cannot fix references", e);
58 public static class FindFixable implements Read<Pair<Collection<Resource>, Collection<Resource>>> {
61 List<Resource> removed;
64 public FindFixable(Object target) {
69 public Pair<Collection<Resource>, Collection<Resource>> perform(ReadGraph graph) throws DatabaseException {
70 old = new ArrayList<Resource>();
71 removed = new ArrayList<Resource>();
73 DocumentLink sl = DocumentLink.getInstance(graph);
74 ModelChildRule modelChildRule = new ModelChildRule(graph, sl.ModelViewpointBrowseContext2_ChildRule);
75 Stack<Object> stack = new Stack<Object>();
77 while (!stack.isEmpty()) {
78 Object o = stack.pop();
79 @SuppressWarnings("unchecked")
80 Collection<Object> children = (Collection<Object>) modelChildRule.getChildren(graph, o);
82 if (children.size() == 0) {
83 if (o instanceof Variable) {
84 Variable v = (Variable)o;
85 Resource r = v.getPossibleRepresents(graph);
87 } else if (o instanceof Resource) {
88 Resource r = (Resource)o;
92 stack.addAll(children);
97 Pair<Collection<Resource>, Collection<Resource>> result = new Pair<Collection<Resource>, Collection<Resource>>(old, removed);
103 void process(ReadGraph graph, Resource r) throws DatabaseException{
104 if (SourceLinkUtil.isValidSource(graph, r)) {
105 if (!SourceLinkUtil.isUpToDateSource(graph, r)) {
106 if (!old.contains(r))
109 } else if (SourceLinkUtil.isSource(graph, r)){
110 if (!removed.contains(r))
116 public static class FixAll extends WriteRequest {
117 Pair<Collection<Resource>, Collection<Resource>> refs;
119 public FixAll(Pair<Collection<Resource>, Collection<Resource>> refs) {
125 public void perform(WriteGraph graph) throws DatabaseException {
126 Collection<Resource> old = refs.first;;
127 Collection<Resource> removed = refs.second;
129 for (Resource r : old)
130 SourceLinkUtil.updateToLatest(graph, r);
131 for (Resource r : removed)
137 public static class FixAllSilent extends WriteRequest {
140 public FixAllSilent(Resource target) {
141 this.target = target;
145 public void perform(WriteGraph graph) throws DatabaseException {
146 // TODO: using ModelChildRule makes the data processing very slow.
147 DocumentLink sl = DocumentLink.getInstance(graph);
148 ModelChildRule modelChildRule = new ModelChildRule(graph, sl.ModelViewpointBrowseContext2_ChildRule);
149 Stack<Object> stack = new Stack<Object>();
151 while (!stack.isEmpty()) {
152 Object o = stack.pop();
153 @SuppressWarnings("unchecked")
154 Collection<Object> children = (Collection<Object>) modelChildRule.getChildren(graph, o);
156 if (children.size() == 0) {
157 if (o instanceof Variable) {
158 Variable v = (Variable)o;
159 Resource r = v.getPossibleRepresents(graph);
161 } else if (o instanceof Resource) {
162 Resource r = (Resource)o;
166 stack.addAll(children);
173 void process(WriteGraph graph, Resource r) throws DatabaseException{
174 if (SourceLinkUtil.isValidSource(graph, r))
175 SourceLinkUtil.updateToLatest(graph, r);