1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.kuali.ole.vnd.document;
19
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.kuali.ole.sys.context.SpringContext;
27 import org.kuali.ole.sys.document.FinancialSystemMaintainable;
28 import org.kuali.ole.vnd.VendorConstants;
29 import org.kuali.ole.vnd.VendorKeyConstants;
30 import org.kuali.ole.vnd.VendorParameterConstants;
31 import org.kuali.ole.vnd.VendorPropertyConstants;
32 import org.kuali.ole.vnd.VendorUtils;
33 import org.kuali.ole.vnd.businessobject.VendorDetail;
34 import org.kuali.ole.vnd.businessobject.VendorHeader;
35 import org.kuali.ole.vnd.businessobject.VendorTaxChange;
36 import org.kuali.ole.vnd.document.service.VendorService;
37 import org.kuali.rice.core.api.datetime.DateTimeService;
38 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
39 import org.kuali.rice.kew.api.WorkflowDocument;
40 import org.kuali.rice.kim.api.identity.Person;
41 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
42 import org.kuali.rice.kns.document.MaintenanceDocument;
43 import org.kuali.rice.krad.bo.DocumentHeader;
44 import org.kuali.rice.krad.bo.Note;
45 import org.kuali.rice.krad.bo.PersistableBusinessObject;
46 import org.kuali.rice.krad.maintenance.MaintenanceLock;
47 import org.kuali.rice.krad.service.BusinessObjectService;
48 import org.kuali.rice.krad.service.KRADServiceLocator;
49 import org.kuali.rice.krad.service.NoteService;
50 import org.kuali.rice.krad.util.GlobalVariables;
51 import org.kuali.rice.krad.util.ObjectUtils;
52
53 public class VendorMaintainableImpl extends FinancialSystemMaintainable {
54 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(VendorMaintainableImpl.class);
55
56
57
58
59 @Override
60 public void setGenerateDefaultValues(String docTypeName) {
61 super.setGenerateDefaultValues(docTypeName);
62
63 List<Note> notes = new ArrayList<Note>();
64
65 if (getBusinessObject().getObjectId() != null) {
66 NoteService noteService = KRADServiceLocator.getNoteService();
67 notes = noteService.getByRemoteObjectId(this.getBusinessObject().getObjectId());
68
69 if (notes.isEmpty()) {
70 notes.add(getNewBoNoteForAdding(VendorConstants.VendorCreateAndUpdateNotePrefixes.ADD));
71 }
72 }
73 }
74
75
76
77
78
79
80 @Override
81 public String getDocumentTitle(MaintenanceDocument document) {
82 String documentTitle = "";
83
84 if (SpringContext.getBean(ParameterService.class).getParameterValueAsBoolean(VendorDetail.class, VendorParameterConstants.OVERRIDE_VENDOR_DOC_TITLE)) {
85
86 if (document.isOldBusinessObjectInDocument()) {
87 documentTitle = "Edit Vendor - ";
88 }
89 else {
90 documentTitle = "New Vendor - ";
91 }
92
93 try {
94 Person initUser = KimApiServiceLocator.getPersonService().getPerson(document.getDocumentHeader().getWorkflowDocument().getInitiatorPrincipalId());
95 documentTitle += initUser.getCampusCode();
96 }
97 catch (Exception e) {
98 throw new RuntimeException("Document Initiator not found " + e.getMessage());
99 }
100
101 VendorDetail newBo = (VendorDetail) document.getNewMaintainableObject().getBusinessObject();
102
103 if (StringUtils.isNotBlank(newBo.getVendorName())) {
104 documentTitle += " '" + newBo.getVendorName() + "'";
105 }
106 else {
107 if (StringUtils.isNotBlank(newBo.getVendorFirstName())) {
108 documentTitle += " '" + newBo.getVendorFirstName() + " ";
109 if (StringUtils.isBlank(newBo.getVendorLastName())) {
110 documentTitle += "'";
111 }
112 }
113
114 if (StringUtils.isNotBlank(newBo.getVendorLastName())) {
115 if (StringUtils.isBlank(newBo.getVendorFirstName())) {
116 documentTitle += " '";
117 }
118 documentTitle += newBo.getVendorLastName() + "'";
119 }
120 }
121
122 if (newBo.getVendorHeader().getVendorForeignIndicator()) {
123 documentTitle += " (F)";
124 }
125
126 if (!newBo.isVendorParentIndicator()) {
127 documentTitle += " (D)";
128 }
129 }
130 else {
131 documentTitle = super.getDocumentTitle(document);
132 }
133 return documentTitle;
134 }
135
136 @Override
137 public void doRouteStatusChange(DocumentHeader header) {
138 super.doRouteStatusChange(header);
139 VendorDetail vendorDetail = (VendorDetail) getBusinessObject();
140 WorkflowDocument workflowDoc = header.getWorkflowDocument();
141
142
143 if (workflowDoc.isProcessed()) {
144
145 if (vendorDetail.isVendorParentIndicator() && vendorDetail.getVendorHeaderGeneratedIdentifier() != null) {
146 VendorDetail previousParent = SpringContext.getBean(VendorService.class).getParentVendor(vendorDetail.getVendorHeaderGeneratedIdentifier());
147
148
149 if (vendorDetail.getVendorDetailAssignedIdentifier() == null ||
150 previousParent.getVendorHeaderGeneratedIdentifier().intValue() != vendorDetail.getVendorHeaderGeneratedIdentifier().intValue() ||
151 previousParent.getVendorDetailAssignedIdentifier().intValue() != vendorDetail.getVendorDetailAssignedIdentifier().intValue()) {
152 previousParent.setVendorParentIndicator(false);
153 addNoteForParentIndicatorChange(vendorDetail, previousParent, header.getDocumentNumber());
154 SpringContext.getBean(BusinessObjectService.class).save(previousParent);
155 }
156 }
157
158
159
160 if (vendorDetail.isVendorParentIndicator()) {
161 VendorDetail oldVendorDetail = SpringContext.getBean(VendorService.class).getVendorDetail(vendorDetail.getVendorHeaderGeneratedIdentifier(), vendorDetail.getVendorDetailAssignedIdentifier());
162 if (ObjectUtils.isNotNull(oldVendorDetail)) {
163 VendorHeader oldVendorHeader = oldVendorDetail.getVendorHeader();
164 VendorHeader newVendorHeader = vendorDetail.getVendorHeader();
165
166 if (ObjectUtils.isNotNull(oldVendorHeader)) {
167 String oldVendorTaxNumber = oldVendorHeader.getVendorTaxNumber();
168 String oldVendorTaxTypeCode = oldVendorHeader.getVendorTaxTypeCode();
169
170 String vendorTaxNumber = newVendorHeader.getVendorTaxNumber();
171 String vendorTaxTypeCode = newVendorHeader.getVendorTaxTypeCode();
172
173 if ((!StringUtils.equals(vendorTaxNumber, oldVendorTaxNumber)) || (!StringUtils.equals(vendorTaxTypeCode, oldVendorTaxTypeCode))) {
174 VendorTaxChange taxChange = new VendorTaxChange(vendorDetail.getVendorHeaderGeneratedIdentifier(), SpringContext.getBean(DateTimeService.class).getCurrentTimestamp(), oldVendorTaxNumber, oldVendorTaxTypeCode, GlobalVariables.getUserSession().getPerson().getPrincipalId());
175 SpringContext.getBean(BusinessObjectService.class).save(taxChange);
176 }
177 }
178 }
179 }
180
181 }
182 }
183
184
185
186
187
188
189
190
191 private void addNoteForParentIndicatorChange(VendorDetail newVendorDetail, VendorDetail oldVendorDetail, String docNumber) {
192 String noteText = VendorUtils.buildMessageText(VendorKeyConstants.MESSAGE_VENDOR_PARENT_TO_DIVISION, docNumber, newVendorDetail.getVendorName() + " (" + newVendorDetail.getVendorNumber() + ")");
193 Note newBONote = new Note();
194 newBONote.setNoteText(noteText);
195 try {
196 NoteService noteService = SpringContext.getBean(NoteService.class);
197 newBONote = noteService.createNote(newBONote, oldVendorDetail, GlobalVariables.getUserSession().getPrincipalId());
198 newBONote.setNotePostedTimestampToCurrent();
199
200 noteService.save(newBONote);
201 }
202 catch (Exception e) {
203 throw new RuntimeException("Caught Exception While Trying To Add Note to Vendor", e);
204 }
205
206 NoteService noteService = KRADServiceLocator.getNoteService();
207 List<Note> notes = noteService.getByRemoteObjectId(oldVendorDetail.getObjectId());
208 notes.add(newBONote);
209 }
210
211
212
213
214
215
216
217
218 @Override
219 public void refresh(String refreshCaller, Map fieldValues, MaintenanceDocument document) {
220 PersistableBusinessObject oldBo = (PersistableBusinessObject) document.getOldMaintainableObject().getBusinessObject();
221 if (ObjectUtils.isNotNull(oldBo)) {
222 oldBo.refreshNonUpdateableReferences();
223 }
224 VendorDetail newBo = (VendorDetail) document.getNewMaintainableObject().getBusinessObject();
225
226
227
228 VendorHeader tempHeader = newBo.getVendorHeader();
229 newBo.refreshNonUpdateableReferences();
230 newBo.setVendorHeader(tempHeader);
231 super.refresh(refreshCaller, fieldValues, document);
232 }
233
234
235
236
237
238 public void refreshBusinessObject() {
239 VendorDetail vd = (VendorDetail) getBusinessObject();
240
241
242
243 VendorHeader tempHeader = vd.getVendorHeader();
244 vd.refreshNonUpdateableReferences();
245 vd.setVendorHeader(tempHeader);
246 }
247
248
249
250
251
252
253
254
255
256
257
258
259
260 @Override
261 public void saveBusinessObject() {
262 VendorDetail vendorDetail = (VendorDetail) super.getBusinessObject();
263 VendorHeader vendorHeader = vendorDetail.getVendorHeader();
264
265
266 setVendorName(vendorDetail);
267 vendorHeader.setVendorHeaderGeneratedIdentifier(vendorDetail.getVendorHeaderGeneratedIdentifier());
268 if (ObjectUtils.isNull(vendorDetail.getVendorDetailAssignedIdentifier())) {
269 setDetailAssignedId(vendorDetail);
270 }
271 if (vendorDetail.isVendorParentIndicator()) {
272 SpringContext.getBean(VendorService.class).saveVendorHeader(vendorDetail);
273 }
274 super.saveBusinessObject();
275 }
276
277
278
279
280 @Override
281 public void processAfterEdit( MaintenanceDocument document, Map<String,String[]> parameters ) {
282
283 List<Note> notes = new ArrayList<Note>();
284 if (document.getOldMaintainableObject().getBusinessObject().getObjectId() != null) {
285 NoteService noteService = KRADServiceLocator.getNoteService();
286 notes = noteService.getByRemoteObjectId(this.getBusinessObject().getObjectId());
287 }
288
289 setVendorCreateAndUpdateNote(notes, VendorConstants.VendorCreateAndUpdateNotePrefixes.CHANGE);
290 document.setNotes(notes);
291
292 super.processAfterEdit(document, parameters);
293 }
294
295
296
297
298
299
300
301 private void setVendorCreateAndUpdateNote(List<Note> notes, String prefix) {
302 boolean shouldAddNote = true;
303
304 if (prefix.equals(VendorConstants.VendorCreateAndUpdateNotePrefixes.CHANGE)) {
305
306 if (!notes.isEmpty()) {
307 Note previousNote = notes.get(notes.size() - 1 );
308 if (previousNote.getNoteText().contains(getDocumentNumber())) {
309 shouldAddNote = false;
310 }
311 }
312 }
313 if (shouldAddNote) {
314 notes.add(getNewBoNoteForAdding(prefix));
315 }
316 }
317
318
319
320
321
322
323 protected Note getNewBoNoteForAdding(String prefix) {
324 Note newBoNote = new Note();
325 newBoNote.setNoteText(prefix + " vendor document ID " + getDocumentNumber());
326 newBoNote.setNotePostedTimestampToCurrent();
327
328 try {
329 newBoNote = SpringContext.getBean(NoteService.class).createNote(newBoNote, this.getBusinessObject(), GlobalVariables.getUserSession().getPrincipalId());
330 }
331 catch (Exception e) {
332 throw new RuntimeException("Caught Exception While Trying To Add Note to Vendor", e);
333 }
334
335 return newBoNote;
336 }
337
338
339
340
341
342
343
344 private void setVendorName(VendorDetail vendorDetail) {
345 if (vendorDetail.isVendorFirstLastNameIndicator()) {
346 vendorDetail.setVendorName(vendorDetail.getVendorLastName() + VendorConstants.NAME_DELIM + vendorDetail.getVendorFirstName());
347 }
348 }
349
350
351
352
353
354
355
356
357 @Override
358 public void setBusinessObject(PersistableBusinessObject bo) {
359 VendorDetail originalBo = (VendorDetail) bo;
360
361 String vendorName = originalBo.getVendorName();
362 if (originalBo.isVendorFirstLastNameIndicator() && ObjectUtils.isNotNull(vendorName)) {
363 int start = vendorName.indexOf(VendorConstants.NAME_DELIM);
364 if (start >= 0) {
365 String lastName = vendorName.substring(0, start);
366 String firstName = new String();
367 if (start + VendorConstants.NAME_DELIM.length() <= vendorName.length()) {
368 firstName = vendorName.substring(start + VendorConstants.NAME_DELIM.length(), vendorName.length());
369 }
370
371 originalBo.setVendorFirstName((ObjectUtils.isNotNull(firstName) ? firstName.trim() : firstName));
372 originalBo.setVendorLastName((ObjectUtils.isNotNull(lastName) ? lastName.trim() : lastName));
373 originalBo.setVendorName(null);
374 }
375 }
376
377 super.setBusinessObject(originalBo);
378 }
379
380
381
382
383
384
385
386
387
388
389
390
391 private void setDetailAssignedId(VendorDetail vendorDetail) {
392
393 if (ObjectUtils.isNull(vendorDetail.getVendorHeaderGeneratedIdentifier())) {
394 vendorDetail.setVendorDetailAssignedIdentifier(new Integer(0));
395 }
396 else {
397
398 Map criterias = new HashMap();
399 criterias.put(VendorPropertyConstants.VENDOR_HEADER_GENERATED_ID, vendorDetail.getVendorHeaderGeneratedIdentifier());
400 BusinessObjectService boService = SpringContext.getBean(BusinessObjectService.class);
401 int count = boService.countMatching(VendorDetail.class, criterias);
402 boolean validId = false;
403 while (!validId) {
404 criterias.put(VendorPropertyConstants.VENDOR_DETAIL_ASSIGNED_ID, count);
405 int result = boService.countMatching(VendorDetail.class, criterias);
406 if (result > 0) {
407
408 count++;
409 }
410 else {
411
412 validId = true;
413 vendorDetail.setVendorDetailAssignedIdentifier(new Integer(count));
414 }
415 }
416 }
417 }
418
419
420
421
422
423
424
425
426 @Override
427 public List<MaintenanceLock> generateMaintenanceLocks() {
428 if (ObjectUtils.isNotNull(((VendorDetail) getBusinessObject()).getVendorDetailAssignedIdentifier())) {
429 return super.generateMaintenanceLocks();
430 }
431 else {
432 return new ArrayList();
433 }
434 }
435
436
437
438
439
440
441
442
443 @Override
444 public void setupNewFromExisting( MaintenanceDocument document, Map<String,String[]> parameters ) {
445 super.setupNewFromExisting(document, parameters);
446
447 ((VendorDetail) super.getBusinessObject()).setVendorParentIndicator(false);
448 ((VendorDetail) super.getBusinessObject()).setActiveIndicator(true);
449
450 List<Note> notes = new ArrayList<Note>();
451
452 if (getBusinessObject().getObjectId() != null) {
453 NoteService noteService = KRADServiceLocator.getNoteService();
454 notes = noteService.getByRemoteObjectId(this.getBusinessObject().getObjectId());
455 }
456
457 setVendorCreateAndUpdateNote(notes, VendorConstants.VendorCreateAndUpdateNotePrefixes.ADD);
458
459 document.setNotes(notes);
460 }
461
462
463
464
465 @Override
466 protected boolean isRelationshipRefreshable(Class boClass, String relationshipName) {
467 if (VendorDetail.class.isAssignableFrom(boClass) && VendorConstants.VENDOR_HEADER_ATTR.equals(relationshipName)) {
468 return false;
469 }
470 return super.isRelationshipRefreshable(boClass, relationshipName);
471 }
472
473
474
475
476 @Override
477 protected boolean answerSplitNodeQuestion(String nodeName) throws UnsupportedOperationException {
478 if (nodeName.equals("RequiresApproval")) return SpringContext.getBean(VendorService.class).shouldVendorRouteForApproval(getDocumentNumber());
479 return super.answerSplitNodeQuestion(nodeName);
480 }
481 }
482