1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.module.cg.document.validation.impl;
17
18 import java.sql.Date;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.List;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.kuali.ole.module.cg.businessobject.Agency;
26 import org.kuali.ole.module.cg.businessobject.CGProjectDirector;
27 import org.kuali.ole.module.cg.businessobject.Primaryable;
28 import org.kuali.ole.sys.OLEConstants;
29 import org.kuali.ole.sys.OLEKeyConstants;
30 import org.kuali.ole.sys.OLEPropertyConstants;
31 import org.kuali.ole.sys.context.SpringContext;
32 import org.kuali.rice.kim.api.identity.CodedAttribute;
33 import org.kuali.rice.kim.api.identity.PersonService;
34 import org.kuali.rice.kim.api.role.RoleService;
35 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
36 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
37 import org.kuali.rice.kns.service.DataDictionaryService;
38 import org.kuali.rice.krad.bo.BusinessObject;
39 import org.kuali.rice.krad.util.ObjectUtils;
40
41
42
43
44 public class CGMaintenanceDocumentRuleBase extends MaintenanceDocumentRuleBase {
45
46 protected static final String PROJECT_DIRECTOR_DECEASED = "D";
47 protected static final String[] PROJECT_DIRECTOR_INVALID_STATUSES = { PROJECT_DIRECTOR_DECEASED };
48
49 protected static final String AGENCY_TYPE_CODE_FEDERAL = "F";
50
51
52
53
54
55
56
57
58
59 protected boolean checkEndAfterBegin(Date begin, Date end, String propertyName) {
60 boolean success = true;
61 if (ObjectUtils.isNotNull(begin) && ObjectUtils.isNotNull(end) && !end.after(begin)) {
62 putFieldError(propertyName, OLEKeyConstants.ERROR_ENDING_DATE_NOT_AFTER_BEGIN);
63 success = false;
64 }
65 return success;
66 }
67
68
69
70
71
72
73
74
75
76 protected <E extends Primaryable> boolean checkPrimary(Collection<E> primaryables, Class<E> elementClass, String collectionName, Class<? extends BusinessObject> boClass) {
77 boolean success = true;
78 int count = 0;
79 for (Primaryable p : primaryables) {
80 if (p.isPrimary()) {
81 count++;
82 }
83 }
84 if (count != 1) {
85 success = false;
86 String elementLabel = SpringContext.getBean(DataDictionaryService.class).getCollectionElementLabel(boClass.getName(), collectionName, elementClass);
87 switch (count) {
88 case 0:
89 putFieldError(collectionName, OLEKeyConstants.ERROR_NO_PRIMARY, elementLabel);
90 break;
91 default:
92 putFieldError(collectionName, OLEKeyConstants.ERROR_MULTIPLE_PRIMARY, elementLabel);
93 }
94
95 }
96 return success;
97 }
98
99
100
101
102
103
104
105
106 protected <T extends CGProjectDirector> boolean checkProjectDirectorsExist(List<T> projectDirectors, Class<T> elementClass, String collectionName) {
107 boolean success = true;
108 final String personUserPropertyName = OLEPropertyConstants.PROJECT_DIRECTOR + "." + OLEPropertyConstants.PERSON_USER_IDENTIFIER;
109 String label = SpringContext.getBean(DataDictionaryService.class).getAttributeLabel(elementClass, personUserPropertyName);
110 int i = 0;
111 for (T pd : projectDirectors) {
112 String propertyName = collectionName + "[" + (i++) + "]." + personUserPropertyName;
113 String id = pd.getPrincipalId();
114 if (StringUtils.isBlank(id) || (SpringContext.getBean(PersonService.class).getPerson(id) == null)) {
115 putFieldError(propertyName, OLEKeyConstants.ERROR_EXISTENCE, label);
116 success = false;
117 }
118 }
119 return success;
120 }
121
122
123
124
125
126
127
128
129 protected <T extends CGProjectDirector> boolean checkProjectDirectorsAreDirectors(List<T> projectDirectors, Class<T> elementClass, String collectionName) {
130 boolean success = true;
131 final String personUserPropertyName = OLEPropertyConstants.PROJECT_DIRECTOR + "." + OLEPropertyConstants.PERSON_USER_IDENTIFIER;
132 String label = SpringContext.getBean(DataDictionaryService.class).getAttributeLabel(elementClass, personUserPropertyName);
133 RoleService roleService = KimApiServiceLocator.getRoleService();
134
135 List<String> roleId = new ArrayList<String>();
136 roleId.add(roleService.getRoleIdByNamespaceCodeAndName(OLEConstants.CoreModuleNamespaces.OLE, OLEConstants.SysKimApiConstants.CONTRACTS_AND_GRANTS_PROJECT_DIRECTOR));
137
138 int i = 0;
139 for (T pd : projectDirectors) {
140 String propertyName = collectionName + "[" + (i++) + "]." + personUserPropertyName;
141 String id = pd.getProjectDirector().getPrincipalId();
142 if (!roleService.principalHasRole(id, roleId, null)) {
143 putFieldError(propertyName, OLEKeyConstants.ERROR_NOT_A_PROJECT_DIRECTOR, id);
144 success = false;
145 }
146 }
147 return success;
148 }
149
150
151
152
153
154
155
156
157
158
159
160
161 protected <T extends CGProjectDirector> boolean checkProjectDirectorsStatuses(List<T> projectDirectors, Class<T> elementClass, String propertyName) {
162 boolean success = true;
163 for (T pd : projectDirectors) {
164 String pdEmplStatusCode = pd.getProjectDirector().getEmployeeStatusCode();
165 if (StringUtils.isBlank(pdEmplStatusCode) || Arrays.asList(PROJECT_DIRECTOR_INVALID_STATUSES).contains(pdEmplStatusCode)) {
166 String pdEmplStatusName = "INVALID STATUS CODE " + pdEmplStatusCode;
167 if ( StringUtils.isNotBlank(pdEmplStatusCode) ) {
168 CodedAttribute empStatus = KimApiServiceLocator.getIdentityService().getEmploymentStatus(pdEmplStatusCode);
169 if ( empStatus != null ) {
170 pdEmplStatusName = empStatus.getName();
171 }
172 }
173 String[] errors = { pd.getProjectDirector().getName(), pdEmplStatusCode + " - " + pdEmplStatusName };
174 putFieldError(propertyName, OLEKeyConstants.ERROR_INVALID_PROJECT_DIRECTOR_STATUS, errors);
175 success = false;
176 }
177 }
178 return success;
179 }
180
181
182
183
184
185
186
187
188
189
190 protected boolean checkAgencyNotEqualToFederalPassThroughAgency(Agency agency, Agency federalPassThroughAgency, String agencyPropertyName, String fedPassThroughAgencyPropertyName) {
191 boolean success = true;
192 if (ObjectUtils.isNotNull(agency) && ObjectUtils.isNotNull(federalPassThroughAgency) && agency.equals(federalPassThroughAgency)) {
193 putFieldError(agencyPropertyName, OLEKeyConstants.ERROR_AGENCY_EQUALS_FEDERAL_PASS_THROUGH_AGENCY);
194 putFieldError(fedPassThroughAgencyPropertyName, OLEKeyConstants.ERROR_FEDERAL_PASS_THROUGH_AGENCY_EQUALS_AGENCY);
195 success = false;
196 }
197 return success;
198 }
199
200
201
202
203
204
205 protected boolean checkFederalPassThrough(boolean federalPassThroughIndicator, Agency primaryAgency, String federalPassThroughAgencyNumber, Class propertyClass, String federalPassThroughIndicatorFieldName) {
206 boolean success = true;
207
208
209 boolean primaryAgencyIsFederal = false;
210
211 if (ObjectUtils.isNotNull(primaryAgency)) {
212 primaryAgencyIsFederal = AGENCY_TYPE_CODE_FEDERAL.equalsIgnoreCase(primaryAgency.getAgencyTypeCode());
213 }
214
215 String indicatorLabel = SpringContext.getBean(DataDictionaryService.class).getAttributeErrorLabel(propertyClass, federalPassThroughIndicatorFieldName);
216 String agencyLabel = SpringContext.getBean(DataDictionaryService.class).getAttributeErrorLabel(propertyClass, OLEPropertyConstants.FEDERAL_PASS_THROUGH_AGENCY_NUMBER);
217
218 if (primaryAgencyIsFederal) {
219 if (federalPassThroughIndicator) {
220
221 putFieldError(federalPassThroughIndicatorFieldName, OLEKeyConstants.ERROR_PRIMARY_AGENCY_IS_FEDERAL_AND_FPT_INDICATOR_IS_CHECKED, new String[] { primaryAgency.getAgencyNumber(), AGENCY_TYPE_CODE_FEDERAL });
222 success = false;
223 }
224 if (!StringUtils.isBlank(federalPassThroughAgencyNumber)) {
225
226 putFieldError(OLEPropertyConstants.FEDERAL_PASS_THROUGH_AGENCY_NUMBER, OLEKeyConstants.ERROR_PRIMARY_AGENCY_IS_FEDERAL_AND_FPT_AGENCY_IS_NOT_BLANK, new String[] { primaryAgency.getAgencyNumber(), AGENCY_TYPE_CODE_FEDERAL });
227 success = false;
228 }
229 }
230 else {
231 if (federalPassThroughIndicator && StringUtils.isBlank(federalPassThroughAgencyNumber)) {
232
233 putFieldError(OLEPropertyConstants.FEDERAL_PASS_THROUGH_AGENCY_NUMBER, OLEKeyConstants.ERROR_FPT_AGENCY_NUMBER_REQUIRED);
234 success = false;
235 }
236 else if (!federalPassThroughIndicator && !StringUtils.isBlank(federalPassThroughAgencyNumber)) {
237
238 putFieldError(OLEPropertyConstants.FEDERAL_PASS_THROUGH_AGENCY_NUMBER, OLEKeyConstants.ERROR_FPT_AGENCY_NUMBER_NOT_BLANK);
239 success = false;
240 }
241 }
242
243 return success;
244 }
245
246 }
247