1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.vnd.document.service.impl;
17
18 import java.sql.Date;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.log4j.Logger;
28 import org.kuali.ole.sys.OLEConstants;
29 import org.kuali.ole.sys.businessobject.Building;
30 import org.kuali.ole.sys.context.SpringContext;
31 import org.kuali.ole.vnd.VendorConstants;
32 import org.kuali.ole.vnd.VendorPropertyConstants;
33 import org.kuali.ole.vnd.businessobject.VendorAddress;
34 import org.kuali.ole.vnd.businessobject.VendorContract;
35 import org.kuali.ole.vnd.businessobject.VendorContractOrganization;
36 import org.kuali.ole.vnd.businessobject.VendorDefaultAddress;
37 import org.kuali.ole.vnd.businessobject.VendorDetail;
38 import org.kuali.ole.vnd.businessobject.VendorHeader;
39 import org.kuali.ole.vnd.businessobject.VendorRoutingComparable;
40 import org.kuali.ole.vnd.dataaccess.VendorDao;
41 import org.kuali.ole.vnd.document.service.VendorService;
42 import org.kuali.rice.core.api.datetime.DateTimeService;
43 import org.kuali.rice.core.api.util.type.KualiDecimal;
44 import org.kuali.rice.kew.api.exception.WorkflowException;
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.Note;
49 import org.kuali.rice.krad.document.Document;
50 import org.kuali.rice.krad.service.BusinessObjectService;
51 import org.kuali.rice.krad.service.DocumentService;
52 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
53 import org.kuali.rice.krad.service.NoteService;
54 import org.kuali.rice.krad.util.GlobalVariables;
55 import org.kuali.rice.krad.util.KRADConstants;
56 import org.kuali.rice.krad.util.ObjectUtils;
57 import org.springframework.transaction.annotation.Transactional;
58
59 @Transactional
60 public class VendorServiceImpl implements VendorService {
61 private static final Logger LOG = Logger.getLogger(VendorServiceImpl.class);
62
63 protected BusinessObjectService businessObjectService;
64 protected DocumentService documentService;
65 protected DateTimeService dateTimeService;
66 protected VendorDao vendorDao;
67 protected NoteService noteService;
68
69
70
71
72 @Override
73 public void saveVendorHeader(VendorDetail vendorDetail) {
74 KRADServiceLocatorWeb.getLegacyDataAdapter().save(vendorDetail.getVendorHeader());
75 }
76
77
78
79
80 @Override
81 public VendorDetail getByVendorNumber(String vendorNumber) {
82 return getVendorDetail(vendorNumber);
83 }
84
85
86
87
88 @Override
89 public VendorDetail getVendorDetail(String vendorNumber) {
90 if (LOG.isDebugEnabled()) {
91 LOG.debug("Entering getVendorDetail for vendorNumber: " + vendorNumber);
92 }
93 if (StringUtils.isBlank(vendorNumber)) {
94 return null;
95 }
96
97 int dashInd = vendorNumber.indexOf('-');
98
99 if (dashInd > 0 && dashInd < vendorNumber.length() - 1) {
100 try {
101 Integer headerId = new Integer(vendorNumber.substring(0, dashInd));
102 Integer detailId = new Integer(vendorNumber.substring(dashInd + 1));
103 return getVendorDetail(headerId, detailId);
104 }
105 catch (NumberFormatException e) {
106
107 return null;
108 }
109 }
110
111 return null;
112 }
113
114
115
116
117 @Override
118 public VendorDetail getVendorDetail(Integer headerId, Integer detailId) {
119 if (LOG.isDebugEnabled()) {
120 LOG.debug("Entering getVendorDetail for headerId:" + headerId + ", detailId:" + detailId);
121 }
122 HashMap<String, Integer> keys = new HashMap<String, Integer>();
123 keys.put("vendorHeaderGeneratedIdentifier", headerId);
124 keys.put("vendorDetailAssignedIdentifier", detailId);
125 return KRADServiceLocatorWeb.getLegacyDataAdapter().findByPrimaryKey(VendorDetail.class, keys);
126 }
127
128
129
130
131 @Override
132 public KualiDecimal getApoLimitFromContract(Integer contractId, String chart, String org) {
133 if (LOG.isDebugEnabled()) {
134 LOG.debug("Entering getApoLimitFromContract with contractId:" + contractId + ", chart:" + chart + ", org:" + org);
135 }
136
137
138 if (ObjectUtils.isNotNull(contractId) && ObjectUtils.isNotNull(chart) && ObjectUtils.isNotNull(org)) {
139 Map<String,Object> pkFields = new HashMap<String, Object>(3);
140 pkFields.put("vendorContractGeneratedIdentifier", contractId);
141 pkFields.put("chartOfAccountsCode", chart);
142 pkFields.put("organizationCode", org);
143 VendorContractOrganization contractOrg = KRADServiceLocatorWeb.getLegacyDataAdapter().findByPrimaryKey(VendorContractOrganization.class, pkFields);
144
145 if (ObjectUtils.isNotNull(contractOrg)) {
146
147 if (!contractOrg.isVendorContractExcludeIndicator()) {
148 return contractOrg.getVendorContractPurchaseOrderLimitAmount();
149 }
150
151 else {
152 return null;
153 }
154 }
155 }
156
157
158
159 if ( contractId != null ) {
160 VendorContract contract = KRADServiceLocatorWeb.getLegacyDataAdapter().findBySinglePrimaryKey(VendorContract.class, contractId);
161 if (contract != null) {
162 return contract.getOrganizationAutomaticPurchaseOrderLimit();
163 }
164 }
165
166
167 return null;
168 }
169
170
171
172
173 @Override
174 public VendorDetail getParentVendor(Integer vendorHeaderGeneratedIdentifier) {
175 if (LOG.isDebugEnabled()) {
176 LOG.debug("Entering getParentVendor for vendorHeaderGeneratedIdentifier:" + vendorHeaderGeneratedIdentifier);
177 }
178 Collection<VendorDetail> vendors = KRADServiceLocatorWeb.getLegacyDataAdapter().findMatching(VendorDetail.class,
179 Collections.singletonMap("vendorHeaderGeneratedIdentifier", vendorHeaderGeneratedIdentifier));
180 VendorDetail result = null;
181 if (vendors == null || vendors.isEmpty() ) {
182 LOG.warn("Error: No vendors exist with vendor header " + vendorHeaderGeneratedIdentifier + ".");
183 }
184 else {
185 for (VendorDetail vendor : vendors) {
186 if (vendor.isVendorParentIndicator()) {
187 if (ObjectUtils.isNull(result)) {
188 result = vendor;
189 }
190 else {
191 LOG.error("Error: More than one parent vendor for vendor header " + vendorHeaderGeneratedIdentifier + ".");
192 throw new RuntimeException("Error: More than one parent vendor for vendor header " + vendorHeaderGeneratedIdentifier + ".");
193 }
194 }
195 }
196 if (ObjectUtils.isNull(result)) {
197 LOG.error("Error: No parent vendor for vendor header " + vendorHeaderGeneratedIdentifier + ".");
198 throw new RuntimeException("Error: No parent vendor for vendor header " + vendorHeaderGeneratedIdentifier + ".");
199 }
200 }
201 LOG.debug("Exiting getParentVendor normally.");
202 return result;
203 }
204
205
206
207
208 @Override
209 public VendorDetail getVendorByDunsNumber(String vendorDunsNumber) {
210 if (LOG.isDebugEnabled()) {
211 LOG.debug("Entering getVendorByDunsNumber for vendorDunsNumber:" + vendorDunsNumber);
212 }
213 HashMap<String, String> criteria = new HashMap<String, String>();
214 criteria.put(VendorPropertyConstants.VENDOR_DUNS_NUMBER, vendorDunsNumber);
215 Collection<VendorDetail> vds = KRADServiceLocatorWeb.getLegacyDataAdapter().findMatching(VendorDetail.class, criteria);
216 LOG.debug("Exiting getVendorByDunsNumber.");
217 if (vds.size() < 1) {
218 return null;
219 }
220 else {
221 return vds.iterator().next();
222 }
223 }
224
225
226
227
228 @Override
229 public VendorAddress getVendorDefaultAddress(Integer vendorHeaderId, Integer vendorDetailId, String addressType, String campus) {
230 if (LOG.isDebugEnabled()) {
231 LOG.debug("Entering getVendorDefaultAddress for vendorHeaderId:" + vendorHeaderId + ", vendorDetailId:" + vendorDetailId + ", addressType:" + addressType + ", campus:" + campus);
232 }
233 HashMap<String, Object> criteria = new HashMap<String, Object>();
234 criteria.put(VendorPropertyConstants.VENDOR_HEADER_GENERATED_ID, vendorHeaderId);
235 criteria.put(VendorPropertyConstants.VENDOR_DETAIL_ASSIGNED_ID, vendorDetailId);
236 criteria.put(VendorPropertyConstants.VENDOR_ADDRESS_TYPE_CODE, addressType);
237 Collection<VendorAddress> addresses = KRADServiceLocatorWeb.getLegacyDataAdapter().findMatching(VendorAddress.class, criteria);
238 LOG.debug("Exiting getVendorDefaultAddress.");
239 return getVendorDefaultAddress(addresses, addressType, campus);
240 }
241
242
243
244
245 @Override
246 public VendorAddress getVendorDefaultAddress(Collection<VendorAddress> addresses, String addressType, String campus) {
247 LOG.debug("Entering getVendorDefaultAddress.");
248 VendorAddress allDefaultAddress = null;
249 for (VendorAddress address : addresses) {
250
251 if (addressType.equals(address.getVendorAddressTypeCode())) {
252
253 if (StringUtils.isNotEmpty(campus) && address.getVendorDefaultAddresses() != null) {
254
255 for (VendorDefaultAddress defaultCampus : address.getVendorDefaultAddresses()) {
256 if (campus.equals(defaultCampus.getVendorCampusCode())) {
257
258 LOG.debug("Exiting getVendorDefaultAddress with single campus default.");
259 return address;
260 }
261 }
262 }
263
264
265 if (address.isVendorDefaultAddressIndicator()) {
266 allDefaultAddress = address;
267 }
268 }
269 }
270
271
272 LOG.debug("Exiting getVendorDefaultAddress with default set for all.");
273 return allDefaultAddress;
274 }
275
276
277
278
279 @Override
280 public boolean shouldVendorRouteForApproval(String documentId) {
281 LOG.debug("Entering shouldVendorRouteForApproval.");
282 boolean shouldRoute = true;
283 MaintenanceDocument document = null;
284 try {
285 document = (MaintenanceDocument) documentService.getByDocumentHeaderId(documentId);
286 }
287 catch (WorkflowException we) {
288 throw new RuntimeException("A WorkflowException was thrown which prevented the loading of the comparison document (" + documentId + ")", we);
289 }
290
291 if (document == null) {
292
293 LOG.error( "Unable to retrieve document in workflow: " + documentId);
294 return false;
295 }
296 String maintenanceAction = document.getNewMaintainableObject().getMaintenanceAction();
297 if ( StringUtils.equals(KRADConstants.MAINTENANCE_NEW_ACTION, maintenanceAction)
298 || StringUtils.equals(KRADConstants.MAINTENANCE_NEWWITHEXISTING_ACTION, maintenanceAction)
299 || StringUtils.equals(KRADConstants.MAINTENANCE_COPY_ACTION, maintenanceAction) ) {
300 return true;
301 }
302 VendorDetail oldVendorDetail = (VendorDetail)document.getOldMaintainableObject().getBusinessObject();
303 if ( oldVendorDetail == null ) {
304
305 return true;
306 }
307 VendorHeader oldVendorHeader = oldVendorDetail.getVendorHeader();
308 if ( ObjectUtils.isNull(oldVendorHeader) ) {
309
310 return true;
311 }
312
313 VendorDetail newVendorDetail = (VendorDetail)document.getNewMaintainableObject().getBusinessObject();
314 if ( newVendorDetail == null ) {
315
316 return true;
317 }
318 VendorHeader newVendorHeader = newVendorDetail.getVendorHeader();
319
320 if ( ObjectUtils.isNull(newVendorHeader) ) {
321
322 return true;
323 }
324 return !noRouteSignificantChangeOccurred(newVendorDetail, newVendorHeader, oldVendorDetail, oldVendorHeader);
325 }
326
327
328
329
330
331
332 @Override
333 public boolean noRouteSignificantChangeOccurred(VendorDetail newVDtl, VendorHeader newVHdr, VendorDetail oldVDtl, VendorHeader oldVHdr) {
334 LOG.debug("Entering noRouteSignificantChangeOccurred.");
335
336
337 boolean unchanged = ((oldVHdr.isEqualForRouting(newVHdr))
338 && (equalMemberLists(oldVHdr.getVendorSupplierDiversities(), newVHdr.getVendorSupplierDiversities()))
339 && (oldVDtl.isEqualForRouting(newVDtl))
340 && (equalMemberLists(oldVDtl.getVendorAddresses(), newVDtl.getVendorAddresses()))
341 && (equalMemberLists(oldVDtl.getVendorContracts(), newVDtl.getVendorContracts()))
342 && (equalMemberLists(oldVDtl.getVendorShippingSpecialConditions(), newVDtl.getVendorShippingSpecialConditions())));
343
344 LOG.debug("Exiting noRouteSignificantChangeOccurred.");
345 return unchanged;
346 }
347
348
349
350
351 @Override
352 public boolean equalMemberLists(List<? extends VendorRoutingComparable> list_a, List<? extends VendorRoutingComparable> list_b) {
353 LOG.debug("Entering equalMemberLists.");
354 boolean result = true;
355 int listSize = list_a.size();
356 if (listSize != list_b.size()) {
357 LOG.debug("Exiting equalMemberLists because list sizes are unequal.");
358 return false;
359 }
360 VendorRoutingComparable aMember = null;
361 for (int i = 0; i < listSize; i++) {
362 aMember = list_a.get(i);
363 if (!aMember.isEqualForRouting(list_b.get(i))) {
364 result = false;
365 break;
366 }
367 }
368 LOG.debug("Exiting equalMemberLists.");
369 return result;
370 }
371
372
373
374
375 @Override
376 public boolean isVendorInstitutionEmployee(Integer vendorHeaderGeneratedIdentifier) {
377 VendorDetail vendorToUse = getParentVendor(vendorHeaderGeneratedIdentifier);
378 if (ObjectUtils.isNull(vendorToUse)) {
379 String errorMsg = "Vendor with header generated id '" + vendorHeaderGeneratedIdentifier + "' cannot be found in the system";
380 LOG.error(errorMsg);
381 throw new RuntimeException(errorMsg);
382 }
383 if (VendorConstants.TAX_TYPE_SSN.equals(vendorToUse.getVendorHeader().getVendorTaxTypeCode())) {
384 String ssnTaxId = vendorToUse.getVendorHeader().getVendorTaxNumber();
385 if (StringUtils.isNotBlank(ssnTaxId)) {
386 List<Person> personList = SpringContext.getBean(PersonService.class).getPersonByExternalIdentifier(org.kuali.rice.kim.api.KimConstants.PersonExternalIdentifierTypes.TAX, ssnTaxId);
387 if (personList != null && !personList.isEmpty()) {
388 return ObjectUtils.isNotNull(personList.get(0));
389 }
390 else {
391
392 return false;
393 }
394 }
395 }
396 return false;
397 }
398
399 public void createVendorNote(VendorDetail vendorDetail, String vendorNote) {
400 try {
401 if (StringUtils.isNotBlank(vendorNote)) {
402 Note newBONote = new Note();
403 newBONote.setNoteText(vendorNote);
404 newBONote.setNotePostedTimestampToCurrent();
405 newBONote.setNoteTypeCode(OLEConstants.NoteTypeEnum.BUSINESS_OBJECT_NOTE_TYPE.getCode());
406 Note note = noteService.createNote(newBONote, vendorDetail, GlobalVariables.getUserSession().getPrincipalId());
407 noteService.save(note);
408 }
409 } catch (Exception e){
410 throw new RuntimeException("Problems creating note for Vendor " + vendorDetail);
411 }
412 }
413
414 @Override
415 public List<Note> getVendorNotes(VendorDetail vendorDetail) {
416 List<Note> notes = new ArrayList<Note>();
417 if (ObjectUtils.isNotNull(vendorDetail)) {
418 notes = noteService.getByRemoteObjectId(vendorDetail.getObjectId());
419 }
420 return notes;
421 }
422
423
424
425
426 @Override
427 public boolean isVendorForeign(Integer vendorHeaderGeneratedIdentifier) {
428 VendorDetail vendorToUse = getParentVendor(vendorHeaderGeneratedIdentifier);
429 if (ObjectUtils.isNull(vendorToUse)) {
430 String errorMsg = "Vendor with header generated id '" + vendorHeaderGeneratedIdentifier + "' cannot be found in the system";
431 LOG.error(errorMsg);
432 throw new RuntimeException(errorMsg);
433 }
434 return vendorToUse.getVendorHeader().getVendorForeignIndicator();
435 }
436
437
438
439
440 @Override
441 public boolean isSubjectPaymentVendor(Integer vendorHeaderGeneratedIdentifier) {
442 VendorDetail vendorToUse = getParentVendor(vendorHeaderGeneratedIdentifier);
443 if (ObjectUtils.isNull(vendorToUse)) {
444 String errorMsg = "Vendor with header generated id '" + vendorHeaderGeneratedIdentifier + "' cannot be found in the system";
445 LOG.error(errorMsg);
446 throw new RuntimeException(errorMsg);
447 }
448 return VendorConstants.VendorTypes.SUBJECT_PAYMENT.equals(vendorToUse.getVendorHeader().getVendorTypeCode());
449 }
450
451
452
453
454 @Override
455 public boolean isRevolvingFundCodeVendor(Integer vendorHeaderGeneratedIdentifier) {
456 VendorDetail vendorToUse = getParentVendor(vendorHeaderGeneratedIdentifier);
457 if (ObjectUtils.isNull(vendorToUse)) {
458 String errorMsg = "Vendor with header generated id '" + vendorHeaderGeneratedIdentifier + "' cannot be found in the system";
459 LOG.error(errorMsg);
460 throw new RuntimeException(errorMsg);
461 }
462 return VendorConstants.VendorTypes.REVOLVING_FUND.equals(vendorToUse.getVendorHeader().getVendorTypeCode());
463 }
464
465
466
467
468
469 @Override
470 public Building getBuildingDetails(String campusCode, String buildingCode) {
471
472 Map keys = new HashMap();
473 keys.put("campusCode", campusCode);
474 keys.put("buildingCode", buildingCode);
475 return KRADServiceLocatorWeb.getLegacyDataAdapter().findByPrimaryKey(Building.class, keys);
476 }
477
478
479
480
481 @Override
482 public boolean isVendorContractExpired(Document document, VendorDetail vendorDetail) {
483 boolean isExpired = false;
484
485 Date currentDate = SpringContext.getBean(DateTimeService.class).getCurrentSqlDate();
486
487 List<VendorContract> vendorContracts = vendorDetail.getVendorContracts();
488 List<Note> notes = document.getNotes();
489
490 for (VendorContract vendorContract : vendorContracts) {
491 if (currentDate.compareTo(vendorContract.getVendorContractEndDate()) > 0 || !vendorContract.isActive()) {
492 Note newNote = new Note();
493 newNote.setNoteText("Vendor Contract: " + vendorContract.getVendorContractName() + " contract has expired contract end date.");
494 newNote.setNotePostedTimestampToCurrent();
495 newNote.setNoteTypeCode(OLEConstants.NoteTypeEnum.BUSINESS_OBJECT_NOTE_TYPE.getCode());
496 Note note = noteService.createNote(newNote, vendorDetail, GlobalVariables.getUserSession().getPrincipalId());
497 notes.add(note);
498 return true;
499 }
500 }
501
502 return isExpired;
503 }
504
505 @Override
506 public VendorContract getVendorB2BContract(VendorDetail vendorDetail, String campus) {
507 return SpringContext.getBean(org.kuali.ole.vnd.dataaccess.VendorDao.class).getVendorB2BContract(vendorDetail, campus, dateTimeService.getCurrentSqlDate());
508 }
509
510 public void setBusinessObjectService(BusinessObjectService boService) {
511 this.businessObjectService = boService;
512 }
513
514 public void setDocumentService(DocumentService documentService) {
515 this.documentService = documentService;
516 }
517
518 public void setVendorDao(VendorDao vendorDao) {
519 this.vendorDao = vendorDao;
520 }
521
522 public VendorDao getVendorDao() {
523 return this.vendorDao ;
524 }
525
526 public void setDateTimeService(DateTimeService dateTimeService) {
527 this.dateTimeService = dateTimeService;
528 }
529
530 }