1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.select.document.validation.impl;
17
18 import org.kuali.ole.select.OleSelectConstant;
19 import org.kuali.ole.select.businessobject.OleDefaultValue;
20 import org.kuali.ole.select.constants.OleSelectPropertyConstants;
21 import org.kuali.ole.sys.context.SpringContext;
22 import org.kuali.rice.core.api.membership.MemberType;
23 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
24 import org.kuali.rice.kns.document.MaintenanceDocument;
25 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
26 import org.kuali.rice.krad.service.BusinessObjectService;
27 import org.kuali.rice.krad.util.GlobalVariables;
28
29 import java.util.*;
30
31
32
33 public class OleDefaultValueRule extends MaintenanceDocumentRuleBase {
34
35 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleDefaultValueRule.class);
36
37 @Override
38 protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
39
40 boolean valid = processValidation(document);
41 return valid & super.processCustomRouteDocumentBusinessRules(document);
42 }
43
44 private boolean processValidation(MaintenanceDocument document) {
45 boolean valid = true;
46 valid &= processUsersValidation(document);
47 valid &= processRolesValidation(document);
48 valid &= processUserIdValidation(document);
49 valid &= processDefaultValueForValidation(document);
50 valid &= processSystemValidation(document);
51 return valid;
52 }
53
54 private boolean processUserIdValidation(MaintenanceDocument document) {
55 boolean valid = true;
56
57 boolean roleFlag = false;
58 String role = getRole();
59 List<String> roleImpl = getRolesForPrincipal(GlobalVariables.getUserSession().getPrincipalId());
60 Iterator itr = roleImpl.iterator();
61 while (itr.hasNext()) {
62 String kimrole = itr.next().toString();
63 if (KimApiServiceLocator.getRoleService().getRole(kimrole).getName().equalsIgnoreCase(role)) {
64 roleFlag = true;
65 }
66 }
67 if (!roleFlag) {
68 OleDefaultValue oleDefaultValue = (OleDefaultValue) document.getNewMaintainableObject().getBusinessObject();
69 if (!GlobalVariables.getUserSession().getPrincipalId().equalsIgnoreCase(oleDefaultValue.getUserId())) {
70 valid = false;
71 putFieldError(OleSelectConstant.DEFAULT_VALUE_USERID, OleSelectPropertyConstants.ERROR_USERID_SHOULD_BE_LOGGED_IN_USERID);
72 }
73 }
74 return valid;
75 }
76
77 private boolean processUsersValidation(MaintenanceDocument document) {
78 boolean valid = true;
79 OleDefaultValue oleDefaultValue = (OleDefaultValue) document.getNewMaintainableObject().getBusinessObject();
80 String role = getRole();
81 List<String> roleImpl = getRolesForPrincipal(GlobalVariables.getUserSession().getPrincipalId());
82 Iterator itr = roleImpl.iterator();
83 while (itr.hasNext()) {
84 String kimrole = itr.next().toString();
85 if (!KimApiServiceLocator.getRoleService().getRole(kimrole).getName().equalsIgnoreCase(role)) {
86 String userId = GlobalVariables.getUserSession().getPrincipalId();
87 Map<String, Object> users = new HashMap<String, Object>();
88 users.put("userId", userId);
89 List<OleDefaultValue> usersList = (List) SpringContext.getBean(BusinessObjectService.class).findMatching(OleDefaultValue.class, users);
90 if (usersList.size() > 0) {
91 for (int i = 0; i < usersList.size(); i++) {
92 if (usersList.get(i).getUserId().equalsIgnoreCase(oleDefaultValue.getUserId())) {
93 if (usersList.get(i).getDefaultTableColumnId().intValue() == oleDefaultValue.getDefaultTableColumnId().intValue()) {
94 if (oleDefaultValue.getDefaultValueId() == null) {
95 valid = false;
96 putFieldError(OleSelectConstant.DEFAULT_VALUE_USERID, OleSelectPropertyConstants.ERROR_USERID_EXIST);
97 } else {
98 if (!usersList.get(i).getDefaultValueId().toString().equalsIgnoreCase(oleDefaultValue.getDefaultValueId().toString())) {
99 valid = false;
100 putFieldError(OleSelectConstant.DEFAULT_VALUE_USERID, OleSelectPropertyConstants.ERROR_USERID_EXIST);
101 }
102 }
103 }
104 }
105 }
106 }
107 }
108 }
109
110 return valid;
111 }
112
113 private boolean processRolesValidation(MaintenanceDocument document) {
114 boolean valid = true;
115 OleDefaultValue newOleDefaultValue = (OleDefaultValue) document.getNewMaintainableObject().getBusinessObject();
116 String role = getRole();
117 List<String> roleImpl = getRolesForPrincipal(GlobalVariables.getUserSession().getPrincipalId());
118 Iterator itr = roleImpl.iterator();
119 while (itr.hasNext()) {
120 String kimrole = itr.next().toString();
121 if (KimApiServiceLocator.getRoleService().getRole(kimrole).getName().equalsIgnoreCase(role)) {
122 if (newOleDefaultValue.getDefaultValueFor().equalsIgnoreCase(OleSelectConstant.DEFAULT_VALUE_ROLE)) {
123 String defaultValueFor = OleSelectConstant.DEFAULT_VALUE_ROLE;
124 Map<String, Object> roles = new HashMap<String, Object>();
125 roles.put("defaultValueFor", defaultValueFor);
126 List<OleDefaultValue> rolesList = (List) SpringContext.getBean(BusinessObjectService.class).findMatching(OleDefaultValue.class, roles);
127 if (rolesList.size() > 0) {
128 for (int i = 0; i < rolesList.size(); i++) {
129 if (rolesList.get(i).getDefaultTableColumnId().intValue() == newOleDefaultValue.getDefaultTableColumnId().intValue() && rolesList.get(i).getRoleId().equalsIgnoreCase(newOleDefaultValue.getRoleId())) {
130 if (newOleDefaultValue.getDefaultValueId() == null) {
131 valid = false;
132 putFieldError(OleSelectConstant.DEFAULT_VALUE_ROLE_ID, OleSelectPropertyConstants.ERROR_ROLE_EXIST);
133 } else {
134 if (!rolesList.get(i).getDefaultValueId().toString().equalsIgnoreCase(newOleDefaultValue.getDefaultValueId().toString())) {
135 valid = false;
136 putFieldError(OleSelectConstant.DEFAULT_VALUE_ROLE_ID, OleSelectPropertyConstants.ERROR_ROLE_EXIST);
137 }
138 }
139 }
140 }
141 }
142 }
143 }
144 }
145 return valid;
146 }
147
148 private boolean processDefaultValueForValidation(MaintenanceDocument document) {
149 boolean valid = true;
150 OleDefaultValue newOleDefaultValue = (OleDefaultValue) document.getNewMaintainableObject().getBusinessObject();
151 String role = getRole();
152 List<String> roleImpl = getRolesForPrincipal(GlobalVariables.getUserSession().getPrincipalId());
153 Iterator itr = roleImpl.iterator();
154 while (itr.hasNext()) {
155 String kimrole = itr.next().toString();
156 if (KimApiServiceLocator.getRoleService().getRole(kimrole).getName().equalsIgnoreCase(role)) {
157 if (OleSelectConstant.DEFAULT_VALUE_ROLE.equalsIgnoreCase(newOleDefaultValue.getDefaultValueFor())) {
158 if (newOleDefaultValue.getUserId() != null) {
159 valid = false;
160 putFieldError(OleSelectConstant.DEFAULT_VALUE_USERID, OleSelectPropertyConstants.ERROR_USERID_SHOULD_BE_EMPTY_FOR_ROLE);
161 }
162 if (newOleDefaultValue.getRoleId() == null) {
163 putFieldError(OleSelectConstant.DEFAULT_VALUE_ROLE_ID, OleSelectPropertyConstants.ERROR_ROLEID_SHOULDNOT_BE_EMPTY);
164 }
165 } else {
166 if (newOleDefaultValue.getUserId() != null || newOleDefaultValue.getRoleId() != null) {
167 valid = false;
168 putFieldError(OleSelectConstant.DEFAULT_VALUE_USERID, OleSelectPropertyConstants.ERROR_USERID_SHOULD_BE_EMPTY_FOR_SYSTEM);
169 putFieldError(OleSelectConstant.DEFAULT_VALUE_ROLE_ID, OleSelectPropertyConstants.ERROR_ROLEID_SHOULD_BE_EMPTY_FOR_SYSTEM);
170 }
171 }
172 }
173 }
174 return valid;
175 }
176
177 public boolean processSystemValidation(MaintenanceDocument document) {
178 boolean valid = true;
179 OleDefaultValue newOleDefaultValue = (OleDefaultValue) document.getNewMaintainableObject().getBusinessObject();
180 String role = getRole();
181 List<String> roleImpl = getRolesForPrincipal(GlobalVariables.getUserSession().getPrincipalId());
182 Iterator itr = roleImpl.iterator();
183 while (itr.hasNext()) {
184 String kimrole = itr.next().toString();
185 if (KimApiServiceLocator.getRoleService().getRole(kimrole).getName().equalsIgnoreCase(role)) {
186 if (OleSelectConstant.DEFAULT_VALUE_SYSTEM.equalsIgnoreCase(newOleDefaultValue.getDefaultValueFor())) {
187 String defaultValueFor = OleSelectConstant.DEFAULT_VALUE_SYSTEM;
188 Map<String, Object> system = new HashMap<String, Object>();
189 system.put("defaultValueFor", defaultValueFor);
190 List<OleDefaultValue> systemList = (List) SpringContext.getBean(BusinessObjectService.class).findMatching(OleDefaultValue.class, system);
191 if (systemList.size() > 0) {
192 for (int i = 0; i < systemList.size(); i++) {
193 if (systemList.get(i).getDefaultTableColumnId().intValue() == newOleDefaultValue.getDefaultTableColumnId().intValue()) {
194 if (newOleDefaultValue.getDefaultValueId() == null) {
195 valid = false;
196 putFieldError(OleSelectConstant.DEFAULT_VALUE_FOR, OleSelectPropertyConstants.ERROR_SYSTEM_EXIST);
197 } else {
198 if (!systemList.get(i).getDefaultValueId().toString().equalsIgnoreCase(newOleDefaultValue.getDefaultValueId().toString())) {
199 valid = false;
200 putFieldError(OleSelectConstant.DEFAULT_VALUE_FOR, OleSelectPropertyConstants.ERROR_SYSTEM_EXIST);
201 }
202 }
203 }
204 }
205 }
206 }
207 }
208 }
209 return valid;
210 }
211
212 @SuppressWarnings("unchecked")
213 private List<String> getRolesForPrincipal(String principalId) {
214 if (principalId == null) {
215 return new ArrayList<String>();
216 }
217
218
219
220
221 return (List<String>) KimApiServiceLocator.getRoleService().getMemberParentRoleIds(MemberType.PRINCIPAL.getCode(), principalId);
222 }
223
224 public String getRole() {
225 return OleSelectConstant.SYSTEM_USER_ROLE_NAME;
226 }
227
228 }