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