]> gerrit.simantics Code Review - simantics/platform.git/blob
3a19d21294a5e525f463ab7de21e72d8bd8ad1c0
[simantics/platform.git] /
1 package org.simantics.document.linking.ge;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.List;
6 import java.util.Stack;
7
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;
23
24 public class FixAllReferencesAction implements ActionFactory {
25         
26         @Override
27         public Runnable create(final Object target) {
28                 return new Runnable() {
29                         
30                         @Override
31                         public void run() {
32                                 System.out.println(target);
33                                 try {
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"};
38                                         int defaultIndex = 0;
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);
43                                                 dialog.open();
44                                                 return;
45                                         }
46                                         MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(), dialogTitle, null, dialogMessage, MessageDialog.CONFIRM, dialogButtonLabels, defaultIndex);
47                                         if (dialog.open() != 0)
48                                                 return;
49                                         Simantics.getSession().markUndoPoint();
50                                         Simantics.getSession().syncRequest(new FixAll(refs));
51                                 } catch (DatabaseException e) {
52                                         ExceptionUtils.logAndShowError("Cannot fix references", e);
53                                 }
54                         }
55                 };
56         }
57         
58         public static class FindFixable implements Read<Pair<Collection<Resource>, Collection<Resource>>> {
59                 
60                 List<Resource> old;
61                 List<Resource> removed;
62                 Object target;
63                 
64                 public FindFixable(Object target) {
65                         this.target = target;
66                 }
67                 
68                 @Override
69                 public Pair<Collection<Resource>, Collection<Resource>> perform(ReadGraph graph) throws DatabaseException {
70                         old = new ArrayList<Resource>();
71                         removed = new ArrayList<Resource>();
72                         
73                         DocumentLink sl = DocumentLink.getInstance(graph);
74                         ModelChildRule modelChildRule = new ModelChildRule(graph, sl.ModelViewpointBrowseContext2_ChildRule);
75                         Stack<Object> stack = new Stack<Object>();
76                         stack.add(target);
77                         while (!stack.isEmpty()) {
78                                 Object o = stack.pop();
79                                 @SuppressWarnings("unchecked")
80                                 Collection<Object> children = (Collection<Object>) modelChildRule.getChildren(graph, o);
81                                 
82                                 if (children.size() == 0) {
83                                         if (o instanceof Variable) {
84                                                 Variable v = (Variable)o;
85                                                 Resource r = v.getPossibleRepresents(graph);
86                                                 process(graph, r);
87                                         } else if (o instanceof Resource) {
88                                                 Resource r = (Resource)o;
89                                                 process(graph, r);
90                                         }
91                                 } else {
92                                         stack.addAll(children);
93                                 }
94                                 
95                         }
96                         
97                         Pair<Collection<Resource>, Collection<Resource>> result =  new Pair<Collection<Resource>, Collection<Resource>>(old, removed);
98                         old = null;
99                         removed = null;
100                         return result;
101                 }
102                 
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))
107                                                 old.add(r);
108                                 }
109                         } else if (SourceLinkUtil.isSource(graph, r)){
110                                 if (!removed.contains(r))
111                                         removed.add(r);
112                         }
113                 }
114         }
115         
116         public static class FixAll extends WriteRequest {
117                 Pair<Collection<Resource>, Collection<Resource>> refs;
118                 
119                 public FixAll(Pair<Collection<Resource>, Collection<Resource>> refs) {
120                         super();
121                         this.refs = refs;
122                 }
123
124                 @Override
125                 public void perform(WriteGraph graph) throws DatabaseException {
126                         Collection<Resource> old = refs.first;;
127                         Collection<Resource> removed = refs.second;
128                         
129                         for (Resource r : old)
130                                 SourceLinkUtil.updateToLatest(graph, r);
131                         for (Resource r : removed)
132                                 graph.deny(r);
133                 }
134         }
135         
136         
137         public static class FixAllSilent extends WriteRequest {
138                 Resource target;
139                 
140                 public FixAllSilent(Resource target) {
141                         this.target = target;
142                 }
143                 
144                 @Override
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>();
150                         stack.add(target);
151                         while (!stack.isEmpty()) {
152                                 Object o = stack.pop();
153                                 @SuppressWarnings("unchecked")
154                                 Collection<Object> children = (Collection<Object>) modelChildRule.getChildren(graph, o);
155                                 
156                                 if (children.size() == 0) {
157                                         if (o instanceof Variable) {
158                                                 Variable v = (Variable)o;
159                                                 Resource r = v.getPossibleRepresents(graph);
160                                                 process(graph, r);
161                                         } else if (o instanceof Resource) {
162                                                 Resource r = (Resource)o;
163                                                 process(graph, r);
164                                         }
165                                 } else {
166                                         stack.addAll(children);
167                                 }
168                                 
169                         }
170                         
171                 }
172                 
173                 void process(WriteGraph graph, Resource r) throws DatabaseException{
174                         if (SourceLinkUtil.isValidSource(graph, r))
175                                 SourceLinkUtil.updateToLatest(graph, r);
176                         else
177                                 graph.deny(r);
178                 }
179         }
180
181 }