1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.util;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.util.tree.Node;
20 import org.kuali.rice.krms.api.repository.LogicalOperator;
21 import org.kuali.rice.krms.api.repository.proposition.PropositionParameterType;
22 import org.kuali.rice.krms.api.repository.proposition.PropositionType;
23 import org.kuali.rice.krms.dto.PropositionEditor;
24 import org.kuali.rice.krms.dto.PropositionParameterEditor;
25 import org.kuali.rice.krms.dto.RuleEditor;
26 import org.kuali.rice.krms.tree.node.RuleEditorTreeNode;
27
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31
32
33
34
35
36
37 public class PropositionTreeUtil {
38
39 public static final String DOC_NEW_DATAOBJECT_PATH = "document.newMaintainableObject.dataObject";
40 public static final String EDIT_TREE_NEW_COLLECTION_LINE = DOC_NEW_DATAOBJECT_PATH + ".editTree";
41
42 public static String getBindingPath(PropositionEditor proposition, String propertyName) {
43 return getBindingPrefix(proposition) + "proposition." + propertyName;
44 }
45
46 public static String getBindingPrefix(PropositionEditor proposition) {
47 return DOC_NEW_DATAOBJECT_PATH + "." + proposition.getBindingPath() + ".";
48 }
49
50 public static Node<RuleEditorTreeNode, String> findParentPropositionNode(Node<RuleEditorTreeNode, String> currentNode, String selectedPropKey) {
51 Node<RuleEditorTreeNode, String> bingo = null;
52 if (selectedPropKey != null) {
53
54 List<Node<RuleEditorTreeNode, String>> children = currentNode.getChildren();
55 for (Node<RuleEditorTreeNode, String> child : children) {
56 RuleEditorTreeNode dataNode = child.getData();
57 if (selectedPropKey.equalsIgnoreCase(dataNode.getProposition().getKey())) {
58 return currentNode;
59 }
60 }
61
62
63 for (Node<RuleEditorTreeNode, String> kid : children) {
64 bingo = findParentPropositionNode(kid, selectedPropKey);
65 if (bingo != null) {
66 break;
67 }
68 }
69 }
70 return bingo;
71 }
72
73
74
75
76 public static PropositionEditor getProposition(RuleEditor ruleEditor) {
77
78 if (ruleEditor != null) {
79 String selectedPropKey = ruleEditor.getSelectedKey();
80 return findProposition(ruleEditor.getEditTree().getRootElement(), selectedPropKey);
81 }
82
83 return null;
84 }
85
86 public static PropositionEditor findProposition(Node<RuleEditorTreeNode, String> currentNode, String selectedPropKey) {
87
88 if ((selectedPropKey == null) || (selectedPropKey.isEmpty())) {
89 return null;
90 }
91
92
93 for (Node<RuleEditorTreeNode, String> child : currentNode.getChildren()) {
94 PropositionEditor proposition = child.getData().getProposition();
95 if (selectedPropKey.equalsIgnoreCase(proposition.getKey())) {
96 return proposition;
97 } else if ("S".equals(proposition.getPropositionTypeCode()) && proposition.isEditMode()) {
98 return proposition;
99 } else if (!proposition.isEditMode()) {
100
101 proposition = findProposition(child, selectedPropKey);
102 if (proposition != null) {
103 return proposition;
104 }
105 }
106 }
107
108 return null;
109 }
110
111
112
113
114
115
116
117 public static Node<RuleEditorTreeNode, String> findEditedProposition(Node<RuleEditorTreeNode, String> node) {
118 Node<RuleEditorTreeNode, String> result = null;
119 if (node.getData() != null && node.getData().getProposition() != null && node.getData().getProposition()
120 .isEditMode()) {
121 result = node;
122 } else {
123 for (Node<RuleEditorTreeNode, String> child : node.getChildren()) {
124 result = findEditedProposition(child);
125 if (result != null) {
126 break;
127 }
128 }
129 }
130 return result;
131 }
132
133
134
135
136
137
138 public static boolean resetEditModeOnPropositionTree(PropositionEditor proposition) {
139 if(proposition==null){
140 return false;
141 }
142
143 boolean editMode = proposition.isEditMode();
144 proposition.setEditMode(false);
145 if (proposition.getCompoundEditors() != null) {
146 for (PropositionEditor childProp : proposition.getCompoundEditors()) {
147 if(resetEditModeOnPropositionTree(childProp)) {
148 editMode = true;
149 }
150 }
151 }
152 return editMode;
153 }
154
155
156
157
158
159
160
161 public static String configureLogicExpression(PropositionEditor proposition) {
162
163 if (PropositionType.COMPOUND.getCode().equalsIgnoreCase(proposition.getPropositionTypeCode())) {
164 StringBuilder logicExpression = new StringBuilder();
165 boolean first = true;
166 for (PropositionEditor child : proposition.getCompoundEditors()) {
167
168 if (!first) {
169 logicExpression.append(" " + getLabelForOperator(proposition.getCompoundOpCode()) + " ");
170 }
171 first = false;
172
173 String compoundExpression = configureLogicExpression(child);
174 if (compoundExpression.length() > 1) {
175 logicExpression.append("(" + compoundExpression + ")");
176 } else {
177 logicExpression.append(compoundExpression);
178 }
179 }
180 return logicExpression.toString();
181 } else {
182 return proposition.getKey();
183 }
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199 public static PropositionEditor createSimplePropositionBoStub(PropositionEditor sibling, Class<? extends PropositionEditor> propClass) throws IllegalAccessException, InstantiationException {
200
201 PropositionEditor prop = propClass.newInstance();
202 prop.setPropositionTypeCode(PropositionType.SIMPLE.getCode());
203 prop.setNewProp(true);
204 prop.setEditMode(true);
205 if (sibling != null) {
206 prop.setRuleId(sibling.getRuleId());
207 }
208
209 prop.setParameters(createParameterList());
210
211 return prop;
212 }
213
214 public static List<PropositionParameterEditor> createParameterList() {
215
216 PropositionParameterEditor pTerm = new PropositionParameterEditor(PropositionParameterType.TERM.getCode(), Integer.valueOf("0"));
217 PropositionParameterEditor pOp = new PropositionParameterEditor(PropositionParameterType.OPERATOR.getCode(), Integer.valueOf("2"));
218 PropositionParameterEditor pConst = new PropositionParameterEditor(PropositionParameterType.CONSTANT.getCode(), Integer.valueOf("1"));
219
220 return Arrays.asList(pTerm, pConst, pOp);
221 }
222
223 public static PropositionEditor createCompoundPropositionBoStub(PropositionEditor existing, boolean addNewChild, Class<? extends PropositionEditor> propClass) throws InstantiationException, IllegalAccessException {
224
225 PropositionEditor prop = createCompoundPropositionBoStub(existing, propClass);
226
227 if (addNewChild) {
228 PropositionEditor newProp = createSimplePropositionBoStub(existing, propClass);
229 prop.getCompoundEditors().add(newProp);
230 prop.setEditMode(false);
231 }
232
233 return prop;
234 }
235
236 public static PropositionEditor createCompoundPropositionBoStub(PropositionEditor existing, Class<? extends PropositionEditor> propClass) throws IllegalAccessException, InstantiationException {
237
238 PropositionEditor prop = propClass.newInstance();
239 prop.setNewProp(true);
240 prop.setPropositionTypeCode(PropositionType.COMPOUND.getCode());
241 prop.setRuleId(existing.getRuleId());
242 prop.setEditMode(false);
243
244 List<PropositionEditor> components = new ArrayList<PropositionEditor>();
245 components.add(existing);
246 prop.setCompoundEditors(components);
247 return prop;
248 }
249
250 public static void cancelNewProp(PropositionEditor proposition) {
251 int i = 0;
252 if (proposition.getCompoundEditors() != null) {
253 while (i < proposition.getCompoundEditors().size()) {
254 PropositionEditor child = proposition.getCompoundEditors().get(i);
255 if (child.isNewProp() && child.isEditMode()) {
256 proposition.getCompoundEditors().remove(child);
257 continue;
258 } else {
259 cancelNewProp(child);
260 }
261 i++;
262 }
263 }
264 }
265
266
267
268
269
270
271
272 public static void removeCompoundProp(RuleEditor rule) {
273
274 PropositionEditor root = rule.getPropositionEditor();
275 if (root.getCompoundEditors() != null) {
276 if(root.getCompoundEditors().size() == 1) {
277 rule.setProposition(root.getCompoundEditors().get(0));
278 }
279
280
281 removeCompoundProp(rule.getPropositionEditor());
282 }
283
284 }
285
286
287
288
289
290
291
292
293
294 public static void removeCompoundProp(PropositionEditor proposition){
295 if (proposition.getCompoundEditors() != null) {
296
297
298 if(proposition.getCompoundEditors().size()==1){
299 PropositionEditor child = proposition.getCompoundEditors().get(0);
300 if(PropositionType.COMPOUND.getCode().equalsIgnoreCase(child.getPropositionTypeCode())) {
301 proposition.setCompoundEditors(child.getCompoundEditors());
302 }
303 }
304
305
306 for(int i=proposition.getCompoundEditors().size()-1;i>=0;i--){
307 PropositionEditor child = proposition.getCompoundEditors().get(i);
308 if(PropositionType.COMPOUND.getCode().equalsIgnoreCase(child.getPropositionTypeCode())) {
309 if (child.getCompoundEditors().isEmpty()) {
310 proposition.getCompoundEditors().remove(i);
311 } else {
312 if (child.getCompoundEditors().size() == 1) {
313 proposition.getCompoundEditors().set(i, child.getCompoundEditors().get(0));
314 }
315 removeCompoundProp(child);
316 }
317 }
318 }
319 }
320 }
321
322 public static void resetNewProp(PropositionEditor proposition) {
323 proposition.setNewProp(false);
324 if (proposition.getCompoundEditors() != null) {
325 for (PropositionEditor child : proposition.getCompoundEditors()) {
326 resetNewProp(child);
327 }
328 }
329 }
330
331
332
333
334
335
336
337 public static PropositionParameterEditor getTermParameter(List<PropositionParameterEditor> parameters) {
338 return getParameterForType(parameters, PropositionParameterType.TERM);
339 }
340
341
342
343
344
345
346
347 public static PropositionParameterEditor getConstantParameter(List<PropositionParameterEditor> parameters) {
348 return getParameterForType(parameters, PropositionParameterType.CONSTANT);
349 }
350
351
352
353
354
355
356
357 public static PropositionParameterEditor getOperatorParameter(List<PropositionParameterEditor> parameters) {
358 return getParameterForType(parameters, PropositionParameterType.OPERATOR);
359 }
360
361
362
363
364
365
366
367
368 public static PropositionParameterEditor getParameterForType(List<PropositionParameterEditor> parameters, PropositionParameterType type) {
369
370 if (parameters == null) {
371 return null;
372 }
373
374 for (PropositionParameterEditor parameter : parameters) {
375 if (type.getCode().equals(parameter.getParameterType())) {
376 return parameter;
377 }
378 }
379 return null;
380 }
381
382
383
384
385
386
387
388 public static String getLabelForOperator(String opCode) {
389 if (LogicalOperator.AND.getCode().equalsIgnoreCase(opCode)) {
390 return "AND";
391 } else if (LogicalOperator.OR.getCode().equalsIgnoreCase(opCode)) {
392 return "OR";
393 }
394 return StringUtils.EMPTY;
395 }
396
397 public static boolean isSimpleCompounds(PropositionEditor propositionEditor) {
398 for (int index = 0; index < propositionEditor.getCompoundEditors().size(); index++) {
399 if (propositionEditor.getCompoundEditors().get(index).getPropositionTypeCode().equals("C")) {
400 return false;
401 }
402 }
403 return true;
404 }
405 }