]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/ge/FixAllReferencesAction.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.document.linking.ui / src / org / simantics / document / linking / ge / FixAllReferencesAction.java
1 package org.simantics.document.linking.ge;\r
2 \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
7 \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
23 \r
24 public class FixAllReferencesAction implements ActionFactory {\r
25         \r
26         @Override\r
27         public Runnable create(final Object target) {\r
28                 return new Runnable() {\r
29                         \r
30                         @Override\r
31                         public void run() {\r
32                                 System.out.println(target);\r
33                                 try {\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
43                                                 dialog.open();\r
44                                                 return;\r
45                                         }\r
46                                         MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(), dialogTitle, null, dialogMessage, MessageDialog.CONFIRM, dialogButtonLabels, defaultIndex);\r
47                                         if (dialog.open() != 0)\r
48                                                 return;\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
53                                 }\r
54                         }\r
55                 };\r
56         }\r
57         \r
58         public static class FindFixable implements Read<Pair<Collection<Resource>, Collection<Resource>>> {\r
59                 \r
60                 List<Resource> old;\r
61                 List<Resource> removed;\r
62                 Object target;\r
63                 \r
64                 public FindFixable(Object target) {\r
65                         this.target = target;\r
66                 }\r
67                 \r
68                 @Override\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
72                         \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
76                         stack.add(target);\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
81                                 \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
86                                                 process(graph, r);\r
87                                         } else if (o instanceof Resource) {\r
88                                                 Resource r = (Resource)o;\r
89                                                 process(graph, r);\r
90                                         }\r
91                                 } else {\r
92                                         stack.addAll(children);\r
93                                 }\r
94                                 \r
95                         }\r
96                         \r
97                         Pair<Collection<Resource>, Collection<Resource>> result =  new Pair<Collection<Resource>, Collection<Resource>>(old, removed);\r
98                         old = null;\r
99                         removed = null;\r
100                         return result;\r
101                 }\r
102                 \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
107                                                 old.add(r);\r
108                                 }\r
109                         } else if (SourceLinkUtil.isSource(graph, r)){\r
110                                 if (!removed.contains(r))\r
111                                         removed.add(r);\r
112                         }\r
113                 }\r
114         }\r
115         \r
116         public static class FixAll extends WriteRequest {\r
117                 Pair<Collection<Resource>, Collection<Resource>> refs;\r
118                 \r
119                 public FixAll(Pair<Collection<Resource>, Collection<Resource>> refs) {\r
120                         super();\r
121                         this.refs = refs;\r
122                 }\r
123 \r
124                 @Override\r
125                 public void perform(WriteGraph graph) throws DatabaseException {\r
126                         Collection<Resource> old = refs.first;;\r
127                         Collection<Resource> removed = refs.second;\r
128                         \r
129                         for (Resource r : old)\r
130                                 SourceLinkUtil.updateToLatest(graph, r);\r
131                         for (Resource r : removed)\r
132                                 graph.deny(r);\r
133                 }\r
134         }\r
135         \r
136         \r
137         public static class FixAllSilent extends WriteRequest {\r
138                 Resource target;\r
139                 \r
140                 public FixAllSilent(Resource target) {\r
141                         this.target = target;\r
142                 }\r
143                 \r
144                 @Override\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
150                         stack.add(target);\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
155                                 \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
160                                                 process(graph, r);\r
161                                         } else if (o instanceof Resource) {\r
162                                                 Resource r = (Resource)o;\r
163                                                 process(graph, r);\r
164                                         }\r
165                                 } else {\r
166                                         stack.addAll(children);\r
167                                 }\r
168                                 \r
169                         }\r
170                         \r
171                 }\r
172                 \r
173                 void process(WriteGraph graph, Resource r) throws DatabaseException{\r
174                         if (SourceLinkUtil.isValidSource(graph, r))\r
175                                 SourceLinkUtil.updateToLatest(graph, r);\r
176                         else\r
177                                 graph.deny(r);\r
178                 }\r
179         }\r
180 \r
181 }\r