1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.impl.rule;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
20 import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
21 import org.kuali.rice.krad.util.KRADConstants;
22 import org.kuali.rice.krms.api.repository.context.ContextDefinition;
23 import org.kuali.rice.krms.impl.repository.ContextBo;
24 import org.kuali.rice.krms.impl.repository.ContextBoService;
25 import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
26 import org.kuali.rice.krms.impl.util.KRMSPropertyConstants;
27
28 public class ContextBusRule extends MaintenanceDocumentRuleBase {
29 @Override
30 protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
31 boolean isValid = true;
32
33 ContextBo newContext = (ContextBo) document.getNewMaintainableObject().getDataObject();
34 ContextBo oldContext = (ContextBo) document.getOldMaintainableObject().getDataObject();
35 boolean isEditAction = KRADConstants.MAINTENANCE_EDIT_ACTION.equals(document.getNewMaintainableObject().getMaintenanceAction());
36
37 isValid &= validateId(oldContext, newContext, isEditAction);
38 isValid &= validateNameNamespace(newContext, isEditAction);
39
40 return isValid;
41 }
42
43 private boolean validateId(ContextBo oldContext, ContextBo newContext, boolean isEditAction) {
44 if (StringUtils.isBlank(newContext.getId())) {
45 this.putFieldError(KRMSPropertyConstants.Context.CONTEXT_ID, "error.context.blankId");
46 return false;
47 }
48 if (isEditAction) {
49 if (!oldContext.getId().equals(newContext.getId())) {
50 throw new IllegalStateException("The ID of a Context being edited must not change.");
51 }
52 } else {
53 ContextDefinition contextInDatabase = getContextBoService().getContextByContextId(newContext.getId());
54 if (contextInDatabase != null) {
55 this.putFieldError(KRMSPropertyConstants.Context.CONTEXT_ID, "error.context.duplicateId");
56 return false;
57 }
58 }
59
60 return true;
61 }
62
63
64
65
66
67
68 private boolean validateNameNamespace(ContextBo newContext, boolean isEditAction) {
69 if (isEditAction) {
70 ContextDefinition contextInDatabase = getContextBoService().getContextByNameAndNamespace(newContext.getName(), newContext
71 .getNamespace());
72 if ((contextInDatabase != null) && !contextInDatabase.getId().equals(newContext.getId())) {
73 this.putFieldError(KRMSPropertyConstants.Context.NAME, "error.context.duplicateNameNamespace");
74 return false;
75 }
76 } else if (StringUtils.isNotBlank(newContext.getName()) && StringUtils.isNotBlank(newContext.getNamespace())) {
77 ContextDefinition contextInDatabase = getContextBoService().getContextByNameAndNamespace(newContext.getName(), newContext
78 .getNamespace());
79 if(contextInDatabase != null) {
80 this.putFieldError(KRMSPropertyConstants.Context.NAME, "error.context.duplicateNameNamespace");
81 return false;
82 }
83 } else {
84 if (StringUtils.isBlank(newContext.getName())) {
85 this.putFieldError(KRMSPropertyConstants.Context.NAME, "error.context.blankName");
86 }
87 if (StringUtils.isBlank(newContext.getNamespace())) {
88 this.putFieldError(KRMSPropertyConstants.Context.NAME, "error.context.blankNamespace");
89 }
90 return false;
91 }
92
93 return true;
94 }
95
96 public ContextBoService getContextBoService() {
97 return KrmsRepositoryServiceLocator.getContextBoService();
98 }
99 }