]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/ge/FixAllReferencesAction.java
Externalize org.simantics.document.linking.ui
[simantics/platform.git] / bundles / org.simantics.document.linking.ui / src / org / simantics / document / linking / ge / FixAllReferencesAction.java
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.IDialogConstants;
9 import org.eclipse.jface.dialogs.MessageDialog;
10 import org.eclipse.osgi.util.NLS;
11 import org.eclipse.swt.widgets.Display;
12 import org.simantics.Simantics;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.Resource;
15 import org.simantics.db.WriteGraph;
16 import org.simantics.db.common.request.WriteRequest;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.layer0.adapter.ActionFactory;
19 import org.simantics.db.layer0.variable.Variable;
20 import org.simantics.db.request.Read;
21 import org.simantics.document.linking.ontology.DocumentLink;
22 import org.simantics.document.linking.utils.SourceLinkUtil;
23 import org.simantics.utils.datastructures.Pair;
24 import org.simantics.utils.ui.ExceptionUtils;
25
26 public class FixAllReferencesAction implements ActionFactory {
27         
28         @Override
29         public Runnable create(final Object target) {
30                 return new Runnable() {
31                         
32                         @Override
33                         public void run() {
34                                 System.out.println(target);
35                                 try {
36                                         Pair<Collection<Resource>, Collection<Resource>> refs = Simantics.getSession()
37                                                         .syncRequest(new FindFixable(target));
38                                         String dialogTitle = Messages.FixAllReferencesAction_FixReferences;
39                                         String dialogMessage = NLS.bind(Messages.FixAllReferencesAction_FixOldReferences,
40                                                         new Object[] { refs.first.size(), refs.second.size() });
41                                         String dialogButtonLabels[] = new String[] { IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL };
42                                         int defaultIndex = 0;
43                                         if (refs.first.size() == 0 && refs.second.size() == 0) {
44                                                 dialogMessage = Messages.FixAllReferencesAction_NothingToFix;
45                                                 dialogButtonLabels = new String[] { IDialogConstants.OK_LABEL };
46                                                 MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(), dialogTitle,
47                                                                 null, dialogMessage, MessageDialog.CONFIRM, dialogButtonLabels, defaultIndex);
48                                                 dialog.open();
49                                                 return;
50                                         }
51                                         MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(), dialogTitle, null,
52                                                         dialogMessage, MessageDialog.CONFIRM, dialogButtonLabels, defaultIndex);
53                                         if (dialog.open() != 0)
54                                                 return;
55                                         Simantics.getSession().markUndoPoint();
56                                         Simantics.getSession().syncRequest(new FixAll(refs));
57                                 } catch (DatabaseException e) {
58                                         ExceptionUtils.logAndShowError(Messages.FixAllReferencesAction_CannotFixReferences, e);
59                                 }
60                         }
61                 };
62         }
63         
64         public static class FindFixable implements Read<Pair<Collection<Resource>, Collection<Resource>>> {
65                 
66                 List<Resource> old;
67                 List<Resource> removed;
68                 Object target;
69                 
70                 public FindFixable(Object target) {
71                         this.target = target;
72                 }
73                 
74                 @Override
75                 public Pair<Collection<Resource>, Collection<Resource>> perform(ReadGraph graph) throws DatabaseException {
76                         old = new ArrayList<Resource>();
77                         removed = new ArrayList<Resource>();
78                         
79                         DocumentLink sl = DocumentLink.getInstance(graph);
80                         ModelChildRule modelChildRule = new ModelChildRule(graph, sl.ModelViewpointBrowseContext2_ChildRule);
81                         Stack<Object> stack = new Stack<Object>();
82                         stack.add(target);
83                         while (!stack.isEmpty()) {
84                                 Object o = stack.pop();
85                                 @SuppressWarnings("unchecked")
86                                 Collection<Object> children = (Collection<Object>) modelChildRule.getChildren(graph, o);
87                                 
88                                 if (children.size() == 0) {
89                                         if (o instanceof Variable) {
90                                                 Variable v = (Variable)o;
91                                                 Resource r = v.getPossibleRepresents(graph);
92                                                 process(graph, r);
93                                         } else if (o instanceof Resource) {
94                                                 Resource r = (Resource)o;
95                                                 process(graph, r);
96                                         }
97                                 } else {
98                                         stack.addAll(children);
99                                 }
100                                 
101                         }
102                         
103                         Pair<Collection<Resource>, Collection<Resource>> result =  new Pair<Collection<Resource>, Collection<Resource>>(old, removed);
104                         old = null;
105                         removed = null;
106                         return result;
107                 }
108                 
109                 void process(ReadGraph graph, Resource r) throws DatabaseException{
110                         if (SourceLinkUtil.isValidSource(graph, r)) {
111                                 if (!SourceLinkUtil.isUpToDateSource(graph, r)) {
112                                         if (!old.contains(r))
113                                                 old.add(r);
114                                 }
115                         } else if (SourceLinkUtil.isSource(graph, r)){
116                                 if (!removed.contains(r))
117                                         removed.add(r);
118                         }
119                 }
120         }
121         
122         public static class FixAll extends WriteRequest {
123                 Pair<Collection<Resource>, Collection<Resource>> refs;
124                 
125                 public FixAll(Pair<Collection<Resource>, Collection<Resource>> refs) {
126                         super();
127                         this.refs = refs;
128                 }
129
130                 @Override
131                 public void perform(WriteGraph graph) throws DatabaseException {
132                         Collection<Resource> old = refs.first;;
133                         Collection<Resource> removed = refs.second;
134                         
135                         for (Resource r : old)
136                                 SourceLinkUtil.updateToLatest(graph, r);
137                         for (Resource r : removed)
138                                 graph.deny(r);
139                 }
140         }
141         
142         
143         public static class FixAllSilent extends WriteRequest {
144                 Resource target;
145                 
146                 public FixAllSilent(Resource target) {
147                         this.target = target;
148                 }
149                 
150                 @Override
151                 public void perform(WriteGraph graph) throws DatabaseException {
152                         // TODO: using ModelChildRule makes the data processing very slow.
153                         DocumentLink sl = DocumentLink.getInstance(graph);
154                         ModelChildRule modelChildRule = new ModelChildRule(graph, sl.ModelViewpointBrowseContext2_ChildRule);
155                         Stack<Object> stack = new Stack<Object>();
156                         stack.add(target);
157                         while (!stack.isEmpty()) {
158                                 Object o = stack.pop();
159                                 @SuppressWarnings("unchecked")
160                                 Collection<Object> children = (Collection<Object>) modelChildRule.getChildren(graph, o);
161                                 
162                                 if (children.size() == 0) {
163                                         if (o instanceof Variable) {
164                                                 Variable v = (Variable)o;
165                                                 Resource r = v.getPossibleRepresents(graph);
166                                                 process(graph, r);
167                                         } else if (o instanceof Resource) {
168                                                 Resource r = (Resource)o;
169                                                 process(graph, r);
170                                         }
171                                 } else {
172                                         stack.addAll(children);
173                                 }
174                                 
175                         }
176                         
177                 }
178                 
179                 void process(WriteGraph graph, Resource r) throws DatabaseException{
180                         if (SourceLinkUtil.isValidSource(graph, r))
181                                 SourceLinkUtil.updateToLatest(graph, r);
182                         else
183                                 graph.deny(r);
184                 }
185         }
186
187 }