1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.service.impl;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
20 import org.kuali.rice.core.api.util.tree.Tree;
21 import org.kuali.rice.krad.uif.UifConstants;
22 import org.kuali.rice.krad.uif.component.Component;
23 import org.kuali.rice.krad.uif.container.Container;
24 import org.kuali.rice.krad.uif.util.ComponentFactory;
25 import org.kuali.rice.krad.uif.util.ComponentUtils;
26 import org.kuali.rice.krad.uif.view.View;
27 import org.kuali.rice.krad.util.BeanPropertyComparator;
28 import org.kuali.rice.krad.util.ObjectUtils;
29 import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
30 import org.kuali.rice.krms.api.KrmsConstants;
31 import org.kuali.rice.krms.api.repository.LogicalOperator;
32 import org.kuali.rice.krms.api.repository.RuleManagementService;
33 import org.kuali.rice.krms.api.repository.proposition.PropositionType;
34 import org.kuali.rice.krms.api.repository.term.TermRepositoryService;
35 import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
36 import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
37 import org.kuali.rice.krms.builder.ComponentBuilder;
38 import org.kuali.rice.krms.dto.PropositionEditor;
39 import org.kuali.rice.krms.dto.PropositionParameterEditor;
40 import org.kuali.rice.krms.dto.RuleEditor;
41 import org.kuali.rice.krms.dto.RuleManagementWrapper;
42 import org.kuali.rice.krms.dto.TermEditor;
43 import org.kuali.rice.krms.dto.TermParameterEditor;
44 import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
45 import org.kuali.rice.krms.service.TemplateRegistry;
46 import org.kuali.rice.krms.tree.RuleViewTreeBuilder;
47 import org.kuali.rice.krms.tree.node.CompareTreeNode;
48 import org.kuali.rice.krms.tree.RuleCompareTreeBuilder;
49 import org.kuali.rice.krms.tree.RuleEditTreeBuilder;
50 import org.kuali.rice.krms.tree.RulePreviewTreeBuilder;
51 import org.kuali.rice.krms.util.KRMSConstants;
52 import org.kuali.rice.krms.util.NaturalLanguageHelper;
53 import org.kuali.rice.krms.util.PropositionTreeUtil;
54 import org.kuali.rice.krms.dto.TemplateInfo;
55 import org.kuali.rice.krms.service.RuleViewHelperService;
56 import org.kuali.student.common.uif.service.impl.KSViewHelperServiceImpl;
57 import org.kuali.student.r1.common.rice.StudentIdentityConstants;
58 import org.kuali.student.r2.core.constants.KSKRMSServiceConstants;
59 import org.springframework.beans.BeanUtils;
60
61 import javax.xml.namespace.QName;
62 import java.util.ArrayList;
63 import java.util.Arrays;
64 import java.util.List;
65 import java.util.Map;
66
67
68
69
70
71
72 public class RuleViewHelperServiceImpl extends KSViewHelperServiceImpl implements RuleViewHelperService {
73
74 private transient RuleManagementService ruleManagementService;
75 private transient KrmsTypeRepositoryService krmsTypeRepositoryService;
76 private transient TermRepositoryService termRepositoryService;
77
78 private RuleCompareTreeBuilder compareTreeBuilder;
79 private RuleEditTreeBuilder editTreeBuilder;
80 private RulePreviewTreeBuilder previewTreeBuilder;
81 private RuleViewTreeBuilder viewTreeBuilder;
82
83 private NaturalLanguageHelper naturalLanguageHelper;
84
85 private static TemplateRegistry templateRegistry;
86
87 protected RuleEditor getRuleEditor(Object model) {
88 if (model instanceof MaintenanceDocumentForm) {
89 MaintenanceDocumentForm maintenanceDocumentForm = (MaintenanceDocumentForm) model;
90 Object dataObject = maintenanceDocumentForm.getDocument().getNewMaintainableObject().getDataObject();
91
92 if (dataObject instanceof RuleEditor) {
93 return (RuleEditor) dataObject;
94 } else if (dataObject instanceof RuleManagementWrapper) {
95 RuleManagementWrapper wrapper = (RuleManagementWrapper) dataObject;
96 return wrapper.getRuleEditor();
97 }
98 }
99 return null;
100 }
101
102 @Override
103 public TemplateInfo getTemplateForType(String type) {
104 return this.getTemplateRegistry().getTemplateForType(type);
105 }
106
107 @Override
108 protected void addCustomContainerComponents(View view, Object model, Container container) {
109 if (KRMSConstants.KRMS_PROPOSITION_DETAILSECTION_ID.equals(container.getId())) {
110 customizePropositionEditSection(view, model, container);
111 }
112 }
113
114 private void customizePropositionEditSection(View view, Object model, Container container) {
115
116 MaintenanceDocumentForm maintenanceDocumentForm = (MaintenanceDocumentForm) model;
117 Object dataObject = maintenanceDocumentForm.getDocument().getNewMaintainableObject().getDataObject();
118
119 RuleEditor ruleEditor = ((RuleManagementWrapper) dataObject).getRuleEditor();
120 PropositionEditor propEditor = PropositionTreeUtil.getProposition(ruleEditor);
121
122 List<Component> components = new ArrayList<Component>();
123 if (propEditor != null) {
124
125 TemplateInfo template = this.getTemplateForType(propEditor.getType());
126
127 if (template != null && template.getComponentId() != null) {
128 Component component = ComponentFactory.getNewComponentInstance(template.getComponentId());
129 view.assignComponentIds(component);
130 if(container.getId().equals(maintenanceDocumentForm.getUpdateComponentId())){
131 String nodePath = view.getDefaultBindingObjectPath() + "." + propEditor.getBindingPath();
132 ComponentUtils.pushObjectToContext(component, UifConstants.ContextVariableNames.NODE_PATH, nodePath);
133 ComponentUtils.prefixBindingPathNested(component, propEditor.getBindingPath());
134 }
135
136
137 components.add(component);
138 }
139
140 if (template != null && template.getConstantComponentId() != null) {
141 Component component = ComponentFactory.getNewComponentInstance(template.getConstantComponentId());
142 view.assignComponentIds(component);
143
144
145 components.add(component);
146 }
147 }
148
149
150 if (components.size() == 0) {
151 container.getHeader().setRender(false);
152 }
153
154 container.setItems(components);
155 }
156
157
158
159
160
161
162 @Override
163 public void validateProposition(PropositionEditor proposition) {
164
165
166 ComponentBuilder builder = this.getTemplateRegistry().getComponentBuilderForType(proposition.getType());
167 if (builder != null) {
168
169 builder.validate(proposition);
170 }
171 }
172
173
174
175
176
177
178
179 @Override
180 public void resetDescription(PropositionEditor prop) {
181
182
183 if (prop.getType() == null) {
184 prop.setDescription(StringUtils.EMPTY);
185 prop.setTerm(null);
186 prop.getNaturalLanguage().clear();
187 return;
188 }
189
190
191 if (PropositionType.SIMPLE.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
192 Map<String, String> termParameters = null;
193 ComponentBuilder builder = this.getTemplateRegistry().getComponentBuilderForType(prop.getType());
194 if (builder != null) {
195 termParameters = builder.buildTermParameters(prop);
196 }
197
198 List<TermParameterEditor> parameters = new ArrayList<TermParameterEditor>();
199 if (termParameters != null) {
200 for (Map.Entry<String, String> entry : termParameters.entrySet()) {
201
202 TermParameterEditor parameterEditor = null;
203 if (prop.getTerm().getParameters() != null) {
204 for (TermParameterEditor parameter : prop.getTerm().getEditorParameters()) {
205
206 if (entry.getKey().equals(parameter.getName())) {
207 parameterEditor = parameter;
208 parameterEditor.setValue(entry.getValue());
209 break;
210 }
211 }
212 }
213
214
215 if (parameterEditor == null) {
216 parameterEditor = new TermParameterEditor();
217 parameterEditor.setName(entry.getKey());
218 parameterEditor.setValue(entry.getValue());
219 }
220 parameters.add(parameterEditor);
221 }
222 }
223
224 prop.getTerm().setParameters(parameters);
225
226
227 if (prop.getTerm().getSpecification() == null) {
228 String termSpecName = this.getTemplateRegistry().getTermSpecNameForType(prop.getType());
229 prop.getTerm().setSpecification(getTermRepositoryService().getTermSpecificationByNameAndNamespace(termSpecName, KSKRMSServiceConstants.NAMESPACE_CODE));
230 }
231
232 } else {
233 prop.setTerm(null);
234 }
235
236
237 prop.getNaturalLanguage().clear();
238 }
239
240 public void configurePropositionForType(PropositionEditor proposition) {
241
242 if (proposition != null) {
243
244 if (PropositionType.COMPOUND.getCode().equalsIgnoreCase(proposition.getPropositionTypeCode())) {
245 return;
246 }
247
248 String propositionTypeId = proposition.getTypeId();
249 if ((propositionTypeId == null) || (propositionTypeId.isEmpty())) {
250 proposition.setType(null);
251 return;
252 }
253
254 KrmsTypeDefinition type = KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService().getTypeById(propositionTypeId);
255 if (type != null) {
256 proposition.setType(type.getName());
257 }
258
259 if (proposition.getTerm() == null) {
260 proposition.setTerm(new TermEditor());
261 }
262
263 String termSpecName = this.getTemplateRegistry().getTermSpecNameForType(proposition.getType());
264 proposition.getTerm().setSpecification(getTermRepositoryService().getTermSpecificationByNameAndNamespace(termSpecName, KSKRMSServiceConstants.NAMESPACE_CODE));
265
266 }
267 }
268
269 @Override
270 public void refreshInitTrees(RuleEditor rule) {
271
272 if (rule == null) {
273 return;
274 }
275
276
277 rule.setEditTree(this.getEditTreeBuilder().buildTree(rule));
278 rule.setPreviewTree(this.getPreviewTreeBuilder().buildTree(rule));
279 }
280
281
282
283
284
285
286 @Override
287 public void refreshViewTree(RuleEditor rule) {
288
289 if (rule == null) {
290 return;
291 }
292
293
294 rule.setViewTree(this.getViewTreeBuilder().buildTree(rule));
295
296 }
297
298 @Override
299 public Tree<CompareTreeNode, String> buildCompareTree(RuleEditor original, RuleEditor compare) {
300
301
302 return this.getCompareTreeBuilder().buildTree(original, compare);
303
304 }
305
306 @Override
307 public Tree<CompareTreeNode, String> buildMultiViewTree(RuleEditor coRuleEditor, RuleEditor cluRuleEditor) {
308
309
310 return this.getCompareTreeBuilder().buildTree(coRuleEditor, cluRuleEditor);
311
312 }
313
314
315
316
317
318
319
320
321
322
323
324
325 @Override
326 public Boolean compareRules(RuleEditor original) {
327
328
329 RuleEditor compareEditor = original.getParent();
330 if ((compareEditor == null) || (compareEditor.getProposition() == null)) {
331 if (original.getProposition() != null) {
332 return false;
333 } else {
334 return true;
335 }
336 } else if (original.getProposition() == null) {
337 return false;
338 }
339
340
341 if (original.getProposition().getTypeId().equals(compareEditor.getProposition().getTypeId())) {
342 return compareProposition(original.getPropositionEditor(), compareEditor.getPropositionEditor());
343 } else {
344 return false;
345 }
346 }
347
348
349
350
351
352
353
354
355 @Override
356 public Boolean compareProposition(PropositionEditor original, PropositionEditor compare) {
357
358 BeanPropertyComparator propositionComparator = new BeanPropertyComparator(Arrays.asList("typeId"));
359 if (propositionComparator.compare(original, compare) != 0) {
360 return false;
361 }
362
363
364 if (PropositionType.SIMPLE.getCode().equalsIgnoreCase(original.getPropositionTypeCode())) {
365 TermEditor term = new TermEditor(PropositionTreeUtil.getTermParameter(compare.getParameters()).getTermValue());
366 if (!compareTerm(original.getTerm().getEditorParameters(), term.getEditorParameters())) {
367 return false;
368 }
369 }
370
371
372 return compareCompoundProposition(original.getCompoundEditors(), compare.getCompoundEditors());
373 }
374
375
376
377
378
379
380
381
382
383 @Override
384 public Boolean compareTerm(List<TermParameterEditor> original, List<TermParameterEditor> compare) {
385
386
387 int originalSize = original == null ? 0 : original.size();
388 if (originalSize != (compare == null ? 0 : compare.size())) {
389 return false;
390 } else if (originalSize > 0) {
391
392
393 BeanPropertyComparator termComparator = new BeanPropertyComparator(Arrays.asList("name","value"));
394 for (int index = 0; index < originalSize; index++) {
395 if (termComparator.compare(original.get(index), compare.get(index)) != 0) {
396 return false;
397 }
398 }
399 }
400
401 return true;
402
403 }
404
405
406
407
408
409
410
411
412 @Override
413 public Boolean compareCompoundProposition(List<PropositionEditor> original, List<PropositionEditor> compare) {
414
415
416 int originalSize = original == null ? 0 : original.size();
417 if (originalSize != (compare == null ? 0 : compare.size())) {
418 return false;
419 } else if (originalSize > 0) {
420
421
422 for (int index = 0; index < originalSize; index++) {
423 if (!compareProposition(original.get(index), compare.get(index))) {
424 return false;
425 }
426 }
427 }
428
429 return true;
430 }
431
432
433
434
435
436
437
438
439
440 @Override
441 public PropositionEditor copyProposition(PropositionEditor oldProposition) {
442 try {
443 PropositionEditor newProposition = this.copyPropositionEditor(oldProposition);
444
445
446 return (PropositionEditor) ObjectUtils.deepCopy(newProposition);
447 } catch (Exception e) {
448 return null;
449 }
450 }
451
452
453
454
455
456
457
458
459
460
461 protected PropositionEditor copyPropositionEditor(PropositionEditor oldProposition) {
462 PropositionEditor newProposition;
463 try {
464 newProposition = this.getPropositionEditorClass().newInstance();
465 } catch (Exception e) {
466 newProposition = new PropositionEditor();
467 }
468 BeanUtils.copyProperties(oldProposition, newProposition, new String[]{"key", "id", "term", "parameters"});
469
470 if (!oldProposition.getPropositionTypeCode().equals("C")) {
471 List<PropositionParameterEditor> propositionParameterEditors = new ArrayList<PropositionParameterEditor>();
472 for (PropositionParameterEditor parm : oldProposition.getParameters()) {
473 PropositionParameterEditor newParm = new PropositionParameterEditor();
474 BeanUtils.copyProperties(parm, newParm, new String[]{"termValue", "id", "versionNumber"});
475 propositionParameterEditors.add(newParm);
476 }
477
478 newProposition.setParameters(propositionParameterEditors);
479
480 TermEditor termEditor = new TermEditor();
481 List<TermParameterEditor> termParameterEditors = new ArrayList<TermParameterEditor>();
482 if( oldProposition.getTerm() != null) {
483 BeanUtils.copyProperties(oldProposition.getTerm(), termEditor, new String[]{"id", "versionNumber", "parameters"});
484 for (TermParameterEditor termParm : oldProposition.getTerm().getEditorParameters()) {
485 TermParameterEditor newTermParm = new TermParameterEditor();
486 BeanUtils.copyProperties(termParm, newTermParm, new String[]{"id", "versionNumber"});
487 termParameterEditors.add(newTermParm);
488 }
489 }
490 termEditor.setParameters(termParameterEditors);
491
492 newProposition.setTerm(termEditor);
493 this.resetDescription(newProposition);
494 }
495
496 if (newProposition.getCompoundEditors() != null) {
497 List<PropositionEditor> props = new ArrayList<PropositionEditor>();
498 for (PropositionEditor prop : newProposition.getCompoundEditors()) {
499 props.add(this.copyPropositionEditor(prop));
500 }
501 newProposition.setCompoundEditors(props);
502 }
503
504
505 return newProposition;
506 }
507
508 @Override
509 public PropositionEditor createCompoundPropositionBoStub(PropositionEditor existing, boolean addNewChild) {
510 try {
511 PropositionEditor compound = PropositionTreeUtil.createCompoundPropositionBoStub(existing, addNewChild, this.getPropositionEditorClass());
512 this.setTypeForCompoundOpCode(compound, LogicalOperator.AND.getCode());
513 return compound;
514 } catch (Exception e) {
515 return null;
516 }
517 }
518
519 @Override
520 public void setTypeForCompoundOpCode(PropositionEditor proposition, String compoundOpCode) {
521
522 if (compoundOpCode.equals(proposition.getCompoundOpCode())) {
523 return;
524 }
525
526
527 proposition.getNaturalLanguage().clear();
528 proposition.setCompoundOpCode(compoundOpCode);
529 }
530
531
532
533
534
535
536
537 @Override
538 public PropositionEditor createSimplePropositionBoStub(PropositionEditor sibling) {
539 try {
540 return PropositionTreeUtil.createSimplePropositionBoStub(sibling, this.getPropositionEditorClass());
541 } catch (Exception e) {
542 return null;
543 }
544 }
545
546
547
548
549
550
551 public Class<? extends PropositionEditor> getPropositionEditorClass() {
552 return PropositionEditor.class;
553 }
554
555 protected RuleManagementService getRuleManagementService() {
556 if (ruleManagementService == null) {
557 ruleManagementService = (RuleManagementService) GlobalResourceLoader.getService(QName.valueOf("ruleManagementService"));
558 }
559 return ruleManagementService;
560 }
561
562 protected RuleCompareTreeBuilder getCompareTreeBuilder() {
563 if (compareTreeBuilder == null) {
564 compareTreeBuilder = new RuleCompareTreeBuilder();
565 }
566 return compareTreeBuilder;
567 }
568
569 protected RuleEditTreeBuilder getEditTreeBuilder() {
570 if (editTreeBuilder == null) {
571 editTreeBuilder = new RuleEditTreeBuilder();
572 }
573 return editTreeBuilder;
574 }
575
576 protected RulePreviewTreeBuilder getPreviewTreeBuilder() {
577 if (previewTreeBuilder == null) {
578 previewTreeBuilder = new RulePreviewTreeBuilder();
579 }
580 return previewTreeBuilder;
581 }
582
583 protected RuleViewTreeBuilder getViewTreeBuilder() {
584 if (viewTreeBuilder == null) {
585 viewTreeBuilder = new RuleViewTreeBuilder();
586 }
587 return viewTreeBuilder;
588 }
589
590 protected NaturalLanguageHelper getNaturalLanguageHelper() {
591 if (naturalLanguageHelper == null) {
592 naturalLanguageHelper = new NaturalLanguageHelper();
593 naturalLanguageHelper.setRuleManagementService(this.getRuleManagementService());
594 }
595 return naturalLanguageHelper;
596 }
597
598 protected TemplateRegistry getTemplateRegistry() {
599 if (templateRegistry == null) {
600 templateRegistry = (TemplateRegistry) GlobalResourceLoader.getService(new QName("http://student.kuali.org/wsdl/templateResolverService", "templateResolverService"));
601 }
602 return templateRegistry;
603 }
604
605 protected KrmsTypeRepositoryService getKrmsTypeRepositoryService() {
606 if (krmsTypeRepositoryService == null) {
607 krmsTypeRepositoryService = (KrmsTypeRepositoryService) GlobalResourceLoader.getService(new QName(KrmsConstants.Namespaces.KRMS_NAMESPACE_2_0, "krmsTypeRepositoryService"));
608 }
609 return krmsTypeRepositoryService;
610 }
611
612 public TermRepositoryService getTermRepositoryService() {
613 if (termRepositoryService == null) {
614 termRepositoryService = (TermRepositoryService) GlobalResourceLoader.getService(new QName(KrmsConstants.Namespaces.KRMS_NAMESPACE_2_0, "termRepositoryService"));
615 }
616 return termRepositoryService;
617 }
618
619 }