1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.module.cg.document;
17
18 import static org.kuali.ole.sys.OLEPropertyConstants.AWARD_ACCOUNTS;
19 import static org.kuali.ole.sys.OLEPropertyConstants.AWARD_PROJECT_DIRECTORS;
20 import static org.kuali.ole.sys.OLEPropertyConstants.AWARD_SUBCONTRACTORS;
21 import static org.kuali.ole.sys.OLEPropertyConstants.DOCUMENT;
22 import static org.kuali.ole.sys.OLEPropertyConstants.NEW_MAINTAINABLE_OBJECT;
23
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.apache.commons.lang.StringUtils;
31 import org.kuali.ole.module.cg.businessobject.Award;
32 import org.kuali.ole.module.cg.businessobject.AwardAccount;
33 import org.kuali.ole.module.cg.businessobject.AwardOrganization;
34 import org.kuali.ole.module.cg.businessobject.AwardProjectDirector;
35 import org.kuali.ole.module.cg.businessobject.AwardSubcontractor;
36 import org.kuali.ole.module.cg.businessobject.CGProjectDirector;
37 import org.kuali.ole.module.cg.businessobject.Proposal;
38 import org.kuali.ole.module.cg.document.validation.impl.AwardRuleUtil;
39 import org.kuali.ole.sys.OLEConstants;
40 import org.kuali.ole.sys.OLEKeyConstants;
41 import org.kuali.ole.sys.OLEPropertyConstants;
42 import org.kuali.ole.sys.context.SpringContext;
43 import org.kuali.ole.sys.document.FinancialSystemMaintainable;
44 import org.kuali.rice.kew.api.WorkflowDocument;
45 import org.kuali.rice.kim.api.identity.Person;
46 import org.kuali.rice.kim.api.identity.PersonService;
47 import org.kuali.rice.kns.document.MaintenanceDocument;
48 import org.kuali.rice.krad.bo.DocumentHeader;
49 import org.kuali.rice.krad.bo.PersistableBusinessObject;
50 import org.kuali.rice.krad.maintenance.MaintenanceLock;
51 import org.kuali.rice.krad.service.BusinessObjectService;
52 import org.kuali.rice.krad.util.GlobalVariables;
53 import org.kuali.rice.krad.util.ObjectUtils;
54
55
56
57
58 public class AwardMaintainableImpl extends FinancialSystemMaintainable {
59 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AwardMaintainableImpl.class);
60
61
62
63 public AwardMaintainableImpl() {
64 super();
65 }
66
67
68
69
70
71
72 public AwardMaintainableImpl(Award award) {
73 super(award);
74 this.setBoClass(award.getClass());
75 }
76
77
78
79
80
81 @Override
82 public void processAfterRetrieve() {
83 refreshAward(false);
84 super.processAfterRetrieve();
85 }
86
87
88
89
90
91 @Override
92 public void prepareForSave() {
93 refreshAward(false);
94 List<AwardProjectDirector> directors = getAward().getAwardProjectDirectors();
95 if (directors.size() == 1) {
96 directors.get(0).setAwardPrimaryProjectDirectorIndicator(true);
97 }
98 List<AwardOrganization> organizations = getAward().getAwardOrganizations();
99 if (organizations.size() == 1) {
100 organizations.get(0).setAwardPrimaryOrganizationIndicator(true);
101 }
102
103
104
105 List<AwardSubcontractor> awardSubcontractors = getAward().getAwardSubcontractors();
106 if (awardSubcontractors != null && !awardSubcontractors.isEmpty()) {
107
108 Map<String,List<AwardSubcontractor>> subcontractorAwardMap = new HashMap<String, List<AwardSubcontractor>>();
109 List<AwardSubcontractor> newSubcontractorRecords = new ArrayList<AwardSubcontractor>();
110 for (AwardSubcontractor awardSubcontractor : awardSubcontractors) {
111 if ( !StringUtils.isBlank(awardSubcontractor.getAwardSubcontractorNumber()) ) {
112
113 if ( !subcontractorAwardMap.containsKey(awardSubcontractor.getSubcontractorNumber()) ) {
114 subcontractorAwardMap.put(awardSubcontractor.getSubcontractorNumber(), new ArrayList<AwardSubcontractor>() );
115 }
116 subcontractorAwardMap.get(awardSubcontractor.getSubcontractorNumber()).add(awardSubcontractor);
117 } else {
118
119 newSubcontractorRecords.add(awardSubcontractor);
120 }
121 }
122
123
124 for (AwardSubcontractor awardSubcontractor : newSubcontractorRecords) {
125 String awardSubcontractorNumber = "1";
126 String awardSubcontractorAmendmentNumber = "1";
127
128 List<AwardSubcontractor> oldSubcontractors = subcontractorAwardMap.get(awardSubcontractor.getSubcontractorNumber());
129 if ( oldSubcontractors != null ) {
130
131
132 boolean[][] nums = new boolean[100][100];
133 for ( AwardSubcontractor oldSub : oldSubcontractors ) {
134 try {
135 nums[Integer.valueOf( oldSub.getAwardSubcontractorNumber() )][Integer.valueOf( oldSub.getAwardSubcontractorAmendmentNumber() )] = true;
136 } catch ( NumberFormatException ex ) {
137
138 LOG.warn( "Unexpected non-integer award subcontractor / amendment number: " + oldSub.getAwardSubcontractorNumber() + " / " + oldSub.getAwardSubcontractorAmendmentNumber() );
139 }
140 }
141
142
143 boolean foundNumbers = false;
144 for ( int i = 1; i <= 99; i++ ) {
145 for ( int j = 1; j <= 99; j++ ) {
146 if ( !nums[j][i] ) {
147
148 awardSubcontractorNumber = Integer.toString(j);
149 awardSubcontractorAmendmentNumber = Integer.toString(i);
150
151 nums[j][i] = true;
152
153 foundNumbers = true;
154 break;
155 }
156 }
157 if ( foundNumbers ) {
158 break;
159 }
160
161
162 }
163 }
164 awardSubcontractor.setAwardSubcontractorNumber(awardSubcontractorNumber);
165 awardSubcontractor.setAwardSubcontractorAmendmentNumber(awardSubcontractorAmendmentNumber);
166 }
167
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186 super.prepareForSave();
187 }
188
189
190
191
192
193
194
195 @SuppressWarnings("unchecked")
196 @Override
197 public void refresh(String refreshCaller, Map fieldValues, MaintenanceDocument document) {
198 if (StringUtils.equals("proposalLookupable", (String) fieldValues.get(OLEConstants.REFRESH_CALLER))) {
199
200 boolean awarded = AwardRuleUtil.isProposalAwarded(getAward());
201 if (awarded) {
202 String pathToMaintainable = DOCUMENT + "." + NEW_MAINTAINABLE_OBJECT;
203 GlobalVariables.getMessageMap().addToErrorPath(pathToMaintainable);
204 GlobalVariables.getMessageMap().putError(OLEPropertyConstants.PROPOSAL_NUMBER, OLEKeyConstants.ERROR_AWARD_PROPOSAL_AWARDED, new String[] { getAward().getProposalNumber().toString() });
205 GlobalVariables.getMessageMap().removeFromErrorPath(pathToMaintainable);
206 }
207
208
209
210
211
212
213
214
215 if (!awarded) {
216 refreshAward(true);
217 fieldValues.put(OLEConstants.REFERENCES_TO_REFRESH, "proposal");
218 super.refresh(refreshCaller, fieldValues, document);
219 getAward().populateFromProposal(getAward().getProposal());
220 refreshAward(true);
221 }
222 } else {
223 refreshAward(OLEConstants.KUALI_LOOKUPABLE_IMPL.equals(fieldValues.get(OLEConstants.REFRESH_CALLER)));
224 super.refresh(refreshCaller, fieldValues, document);
225 }
226
227 }
228
229
230
231
232
233
234 private void refreshAward(boolean refreshFromLookup) {
235 Award award = getAward();
236 award.refreshNonUpdateableReferences();
237
238 getNewCollectionLine(AWARD_SUBCONTRACTORS).refreshNonUpdateableReferences();
239 getNewCollectionLine(AWARD_PROJECT_DIRECTORS).refreshNonUpdateableReferences();
240 getNewCollectionLine(AWARD_ACCOUNTS).refreshNonUpdateableReferences();
241
242
243 refreshNonUpdateableReferences(award.getAwardOrganizations());
244 refreshNonUpdateableReferences(award.getAwardAccounts());
245 refreshNonUpdateableReferences(award.getAwardSubcontractors());
246 refreshAwardProjectDirectors(refreshFromLookup);
247 }
248
249
250
251
252
253
254 private void refreshAwardProjectDirectors(boolean refreshFromLookup) {
255 if (refreshFromLookup) {
256 getNewCollectionLine(AWARD_PROJECT_DIRECTORS).refreshNonUpdateableReferences();
257 refreshNonUpdateableReferences(getAward().getAwardProjectDirectors());
258
259 getNewCollectionLine(AWARD_ACCOUNTS).refreshNonUpdateableReferences();
260 refreshNonUpdateableReferences(getAward().getAwardAccounts());
261 }
262 else {
263 refreshWithSecondaryKey((AwardProjectDirector) getNewCollectionLine(AWARD_PROJECT_DIRECTORS));
264 for (AwardProjectDirector projectDirector : getAward().getAwardProjectDirectors()) {
265 refreshWithSecondaryKey(projectDirector);
266 }
267
268 refreshWithSecondaryKey((AwardAccount) getNewCollectionLine(AWARD_ACCOUNTS));
269 for (AwardAccount account : getAward().getAwardAccounts()) {
270 refreshWithSecondaryKey(account);
271 }
272 }
273 }
274
275
276
277
278 private static void refreshNonUpdateableReferences(Collection<? extends PersistableBusinessObject> collection) {
279 for (PersistableBusinessObject item : collection) {
280 item.refreshNonUpdateableReferences();
281 }
282 }
283
284
285
286
287
288
289
290
291
292
293 private static void refreshWithSecondaryKey(CGProjectDirector director) {
294 Person cgdir = director.getProjectDirector();
295 if (ObjectUtils.isNotNull(cgdir)) {
296 String secondaryKey = cgdir.getPrincipalName();
297 if (StringUtils.isNotBlank(secondaryKey)) {
298 Person dir = SpringContext.getBean(PersonService.class).getPersonByPrincipalName(secondaryKey);
299 director.setPrincipalId(dir == null ? null : dir.getPrincipalId());
300 }
301 if (StringUtils.isNotBlank(director.getPrincipalId())) {
302 Person person = SpringContext.getBean(PersonService.class).getPerson(director.getPrincipalId());
303 if (person != null) {
304 ((PersistableBusinessObject) director).refreshNonUpdateableReferences();
305 }
306 }
307 }
308 }
309
310
311
312
313
314
315 public Award getAward() {
316 return (Award) getBusinessObject();
317 }
318
319
320
321
322
323
324
325 @Override
326 public void addNewLineToCollection(String collectionName) {
327 refreshAward(false);
328 super.addNewLineToCollection(collectionName);
329 }
330
331
332
333
334
335
336
337 @Override
338 public void doRouteStatusChange(DocumentHeader header) {
339 super.doRouteStatusChange(header);
340
341 Award award = getAward();
342 WorkflowDocument workflowDoc = header.getWorkflowDocument();
343
344
345 if (workflowDoc.isProcessed()) {
346 Proposal proposal = award.getProposal();
347 proposal.setProposalStatusCode(Proposal.AWARD_CODE);
348 SpringContext.getBean(BusinessObjectService.class).save(proposal);
349 }
350
351 }
352
353 public List<MaintenanceLock> generateMaintenanceLocks() {
354 List<MaintenanceLock> locks = super.generateMaintenanceLocks();
355 return locks;
356 }
357 }