use of edu.cornell.kfs.module.purap.document.BatchIWantDocument in project cu-kfs by CU-CommunityApps.
the class IWantDocumentFeedServiceImpl method populateIWantDocument.
/**
* Populates an I Want document based on the input data.
*
* @param batchIWantDocument
*/
private void populateIWantDocument(BatchIWantDocument batchIWantDocument, String incomingFileName) {
boolean noErrors = true;
List<AdHocRoutePerson> adHocRoutePersons = new ArrayList<AdHocRoutePerson>();
LOG.info("Creating I Want document from data related to source number: " + batchIWantDocument.getSourceNumber());
try {
if (StringUtils.isBlank(batchIWantDocument.getInitiator())) {
LOG.error("Initiator net ID cannot be empty: " + batchIWantDocument.getInitiator());
noErrors = false;
}
// if initiator is blank we cannot create the I Want doc
if (noErrors) {
Person initiator = personService.getPersonByPrincipalName(batchIWantDocument.getInitiator());
if (ObjectUtils.isNull(initiator)) {
LOG.error("Initiator net ID is not valid: " + batchIWantDocument.getInitiator());
noErrors = false;
}
// if initiator is not valid we cannot create the I Want doc
if (noErrors) {
IWantDocument iWantDocument = (IWantDocument) documentService.getNewDocument(CUPurapConstants.IWNT_DOC_TYPE, batchIWantDocument.getInitiator());
iWantDocumentService.setUpIWantDocDefaultValues(iWantDocument, initiator);
if (StringUtils.isNotBlank(batchIWantDocument.getInitiatorNetID())) {
iWantDocument.setInitiatorNetID(batchIWantDocument.getInitiatorNetID());
}
iWantDocument.getDocumentHeader().setExplanation(batchIWantDocument.getBusinessPurpose());
iWantDocument.setExplanation(batchIWantDocument.getBusinessPurpose());
if (StringUtils.isNotBlank(batchIWantDocument.getCollegeLevelOrganization())) {
iWantDocument.setCollegeLevelOrganization(batchIWantDocument.getCollegeLevelOrganization());
}
if (StringUtils.isNotBlank(batchIWantDocument.getDepartmentLevelOrganization())) {
iWantDocument.setDepartmentLevelOrganization(batchIWantDocument.getDepartmentLevelOrganization());
}
iWantDocument.getDocumentHeader().setOrganizationDocumentNumber(batchIWantDocument.getSourceNumber());
// populate requester fields
populateIWantDocRequestorSection(initiator, batchIWantDocument, iWantDocument);
// populate deliver to section
populateIWantDocDeliverToSection(batchIWantDocument, iWantDocument);
// populate vendor data
if (StringUtils.isNotBlank(batchIWantDocument.getVendorNumber())) {
String[] vendorNumbers = batchIWantDocument.getVendorNumber().split("-");
if (vendorNumbers.length == 2) {
try {
Integer vendorHeaderId = new Integer(vendorNumbers[0]);
Integer vendorId = new Integer(vendorNumbers[1]);
String phoneNumber = "Phone: ";
Map<String, Object> fieldValues = new HashMap<String, Object>();
fieldValues.put("vendorHeaderGeneratedIdentifier", vendorHeaderId);
fieldValues.put("vendorDetailAssignedIdentifier", vendorId);
fieldValues.put("vendorPhoneTypeCode", "PH");
Collection<VendorPhoneNumber> vendorPhoneNumbers = businessObjectService.findMatching(VendorPhoneNumber.class, fieldValues);
if (ObjectUtils.isNotNull(vendorPhoneNumbers) && vendorPhoneNumbers.size() > 0) {
VendorPhoneNumber retrievedVendorPhoneNumber = vendorPhoneNumbers.toArray(new VendorPhoneNumber[1])[0];
phoneNumber += retrievedVendorPhoneNumber.getVendorPhoneNumber();
}
Map<String, Object> fieldValuesVendorDetail = new HashMap<String, Object>();
fieldValuesVendorDetail.put("vendorHeaderGeneratedIdentifier", vendorHeaderId);
fieldValuesVendorDetail.put("vendorDetailAssignedIdentifier", vendorId);
VendorDetail vendorDetail = businessObjectService.findByPrimaryKey(VendorDetail.class, fieldValuesVendorDetail);
if (ObjectUtils.isNotNull(vendorDetail)) {
iWantDocument.setVendorHeaderGeneratedIdentifier(vendorHeaderId);
iWantDocument.setVendorDetailAssignedIdentifier(vendorId);
iWantDocument.setVendorName(vendorDetail.getVendorName());
updateDefaultVendorAddress(vendorDetail);
// populate vendor info
String addressLine1 = vendorDetail.getDefaultAddressLine1() != null ? vendorDetail.getDefaultAddressLine1() : StringUtils.EMPTY;
String addressLine2 = vendorDetail.getDefaultAddressLine2() != null ? vendorDetail.getDefaultAddressLine2() : StringUtils.EMPTY;
String cityName = vendorDetail.getDefaultAddressCity() != null ? vendorDetail.getDefaultAddressCity() : StringUtils.EMPTY;
String stateCode = vendorDetail.getDefaultAddressStateCode() != null ? vendorDetail.getDefaultAddressStateCode() : StringUtils.EMPTY;
String countryCode = vendorDetail.getDefaultAddressCountryCode() != null ? vendorDetail.getDefaultAddressCountryCode() : StringUtils.EMPTY;
String postalCode = vendorDetail.getDefaultAddressPostalCode() != null ? vendorDetail.getDefaultAddressPostalCode() : StringUtils.EMPTY;
String faxNumber = "Fax: " + (vendorDetail.getDefaultFaxNumber() != null ? vendorDetail.getDefaultFaxNumber() : StringUtils.EMPTY);
String url = "URL: " + (vendorDetail.getVendorUrlAddress() != null ? vendorDetail.getVendorUrlAddress() : StringUtils.EMPTY);
String vendorInfo = new StringBuilder(100).append(addressLine1).append('\n').append(addressLine2).append('\n').append(cityName).append(", ").append(postalCode).append(", ").append(stateCode).append(", ").append(countryCode).append('\n').append(faxNumber).append('\n').append(phoneNumber).append(" \n").append(url).toString();
iWantDocument.setVendorDescription(vendorInfo);
} else {
// Invalid vendor number
LOG.error("Vendor with id: " + batchIWantDocument.getVendorNumber() + " does not exist.");
noErrors = false;
}
} catch (NumberFormatException e) {
LOG.error("Vendor id: " + batchIWantDocument.getVendorNumber() + " is not valid.");
noErrors = false;
}
} else {
// Invalid vendor number
LOG.error("Vendor ID is not valid: " + batchIWantDocument.getVendorNumber());
noErrors = false;
}
} else {
if (StringUtils.isNotEmpty(batchIWantDocument.getVendorName())) {
iWantDocument.setVendorName(batchIWantDocument.getVendorName());
}
if (StringUtils.isNotEmpty(batchIWantDocument.getVendorDescription())) {
iWantDocument.setVendorDescription(batchIWantDocument.getVendorDescription());
}
}
// add items
noErrors &= populateIWantDocItems(batchIWantDocument, iWantDocument);
// add accounts
noErrors &= populateIWantDocAccounts(batchIWantDocument, iWantDocument);
// account Description
if (StringUtils.isNotBlank(batchIWantDocument.getAccountDescriptionTxt())) {
iWantDocument.setAccountDescriptionTxt(batchIWantDocument.getAccountDescriptionTxt());
}
if (StringUtils.isNotBlank(batchIWantDocument.getCommentsAndSpecialInstructions())) {
iWantDocument.setCommentsAndSpecialInstructions(batchIWantDocument.getCommentsAndSpecialInstructions());
}
iWantDocument.setGoods(batchIWantDocument.isGoods());
if (StringUtils.isNotBlank(batchIWantDocument.getServicePerformedOnCampus())) {
iWantDocument.setServicePerformedOnCampus(batchIWantDocument.getServicePerformedOnCampus());
}
if (StringUtils.isNotBlank(batchIWantDocument.getCurrentRouteToNetId())) {
Person adHocRouteTo = personService.getPersonByPrincipalName(batchIWantDocument.getCurrentRouteToNetId());
if (ObjectUtils.isNull(adHocRouteTo)) {
LOG.error("Ad Hoc Route to net ID is not valid: " + batchIWantDocument.getCurrentRouteToNetId());
noErrors = false;
} else {
iWantDocument.setCurrentRouteToNetId(batchIWantDocument.getCurrentRouteToNetId());
AdHocRoutePerson recipient = new AdHocRoutePerson();
recipient.setId(iWantDocument.getCurrentRouteToNetId());
recipient.setActionRequested(KewApiConstants.ACTION_REQUEST_APPROVE_REQ);
adHocRoutePersons.add(recipient);
iWantDocument.setAdHocRoutePersons(adHocRoutePersons);
}
}
iWantDocumentService.setIWantDocumentDescription(iWantDocument);
// add notes
addNotes(iWantDocument, batchIWantDocument);
// add attachments
loadDocumentAttachments(iWantDocument, batchIWantDocument.getAttachments(), incomingFileName);
boolean rulePassed = true;
// call business rules
rulePassed &= ruleService.applyRules(new SaveDocumentEvent("", iWantDocument));
if (!rulePassed) {
LOG.error("I Want document " + iWantDocument.getDocumentNumber() + "not saved due to errors");
logErrorMessages();
} else if (noErrors) {
documentService.saveDocument(iWantDocument);
}
}
}
} catch (Exception e) {
LOG.error("error while creating I Want document: " + e.getMessage(), e);
throw new RuntimeException("Error encountered while attempting to create I Want document " + e.getMessage(), e);
}
}
Aggregations