Source Code Cross Referenced for PurchaseOrderServiceImpl.java in  » ERP-CRM-Financial » Kuali-Financial-System » org » kuali » module » purap » service » impl » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ERP CRM Financial » Kuali Financial System » org.kuali.module.purap.service.impl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * Copyright 2006-2007 The Kuali Foundation.
0003:         * 
0004:         * Licensed under the Educational Community License, Version 1.0 (the "License");
0005:         * you may not use this file except in compliance with the License.
0006:         * You may obtain a copy of the License at
0007:         * 
0008:         * http://www.opensource.org/licenses/ecl1.php
0009:         * 
0010:         * Unless required by applicable law or agreed to in writing, software
0011:         * distributed under the License is distributed on an "AS IS" BASIS,
0012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013:         * See the License for the specific language governing permissions and
0014:         * limitations under the License.
0015:         */
0016:        package org.kuali.module.purap.service.impl;
0017:
0018:        import java.io.ByteArrayOutputStream;
0019:        import java.sql.Date;
0020:        import java.util.ArrayList;
0021:        import java.util.Collection;
0022:        import java.util.HashSet;
0023:        import java.util.List;
0024:        import java.util.Set;
0025:
0026:        import org.apache.commons.lang.StringUtils;
0027:        import org.kuali.RicePropertyConstants;
0028:        import org.kuali.core.bo.AdHocRouteRecipient;
0029:        import org.kuali.core.bo.Note;
0030:        import org.kuali.core.document.DocumentBase;
0031:        import org.kuali.core.exceptions.UserNotFoundException;
0032:        import org.kuali.core.exceptions.ValidationException;
0033:        import org.kuali.core.service.BusinessObjectService;
0034:        import org.kuali.core.service.DateTimeService;
0035:        import org.kuali.core.service.DocumentService;
0036:        import org.kuali.core.service.KualiConfigurationService;
0037:        import org.kuali.core.service.KualiRuleService;
0038:        import org.kuali.core.service.NoteService;
0039:        import org.kuali.core.util.ErrorMap;
0040:        import org.kuali.core.util.GlobalVariables;
0041:        import org.kuali.core.util.KualiDecimal;
0042:        import org.kuali.core.util.ObjectUtils;
0043:        import org.kuali.core.util.TypedArrayList;
0044:        import org.kuali.core.workflow.service.KualiWorkflowDocument;
0045:        import org.kuali.core.workflow.service.KualiWorkflowInfo;
0046:        import org.kuali.core.workflow.service.WorkflowDocumentService;
0047:        import org.kuali.kfs.KFSConstants;
0048:        import org.kuali.kfs.context.SpringContext;
0049:        import org.kuali.kfs.rule.event.DocumentSystemSaveEvent;
0050:        import org.kuali.module.purap.PurapConstants;
0051:        import org.kuali.module.purap.PurapKeyConstants;
0052:        import org.kuali.module.purap.PurapConstants.POTransmissionMethods;
0053:        import org.kuali.module.purap.PurapConstants.PurchaseOrderDocTypes;
0054:        import org.kuali.module.purap.PurapConstants.PurchaseOrderStatuses;
0055:        import org.kuali.module.purap.PurapConstants.RequisitionSources;
0056:        import org.kuali.module.purap.PurapConstants.VendorChoice;
0057:        import org.kuali.module.purap.PurapWorkflowConstants.PurchaseOrderDocument.NodeDetailEnum;
0058:        import org.kuali.module.purap.bo.PurApItem;
0059:        import org.kuali.module.purap.bo.PurchaseOrderItem;
0060:        import org.kuali.module.purap.bo.PurchaseOrderQuoteStatus;
0061:        import org.kuali.module.purap.bo.PurchaseOrderVendorQuote;
0062:        import org.kuali.module.purap.dao.PurchaseOrderDao;
0063:        import org.kuali.module.purap.document.PurchaseOrderDocument;
0064:        import org.kuali.module.purap.document.RequisitionDocument;
0065:        import org.kuali.module.purap.service.LogicContainer;
0066:        import org.kuali.module.purap.service.PrintService;
0067:        import org.kuali.module.purap.service.PurApWorkflowIntegrationService;
0068:        import org.kuali.module.purap.service.PurapService;
0069:        import org.kuali.module.purap.service.PurchaseOrderService;
0070:        import org.kuali.module.purap.service.RequisitionService;
0071:        import org.kuali.module.purap.util.PurApObjectUtils;
0072:        import org.kuali.module.vendor.bo.VendorDetail;
0073:        import org.kuali.module.vendor.service.VendorService;
0074:        import org.springframework.transaction.annotation.Transactional;
0075:
0076:        import edu.iu.uis.eden.clientapp.vo.ActionRequestVO;
0077:        import edu.iu.uis.eden.exception.WorkflowException;
0078:
0079:        @Transactional
0080:        public class PurchaseOrderServiceImpl implements  PurchaseOrderService {
0081:            private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
0082:                    .getLogger(PurchaseOrderServiceImpl.class);
0083:
0084:            private BusinessObjectService businessObjectService;
0085:            private DateTimeService dateTimeService;
0086:            private DocumentService documentService;
0087:            private NoteService noteService;
0088:            private PurapService purapService;
0089:            private PrintService printService;
0090:            private PurchaseOrderDao purchaseOrderDao;
0091:            private WorkflowDocumentService workflowDocumentService;
0092:            private KualiConfigurationService kualiConfigurationService;
0093:            private KualiRuleService kualiRuleService;
0094:            private VendorService vendorService;
0095:            private RequisitionService requisitionService;
0096:
0097:            public void setBusinessObjectService(BusinessObjectService boService) {
0098:                this .businessObjectService = boService;
0099:            }
0100:
0101:            public void setDateTimeService(DateTimeService dateTimeService) {
0102:                this .dateTimeService = dateTimeService;
0103:            }
0104:
0105:            public void setDocumentService(DocumentService documentService) {
0106:                this .documentService = documentService;
0107:            }
0108:
0109:            public void setNoteService(NoteService noteService) {
0110:                this .noteService = noteService;
0111:            }
0112:
0113:            public void setPurapService(PurapService purapService) {
0114:                this .purapService = purapService;
0115:            }
0116:
0117:            public void setPrintService(PrintService printService) {
0118:                this .printService = printService;
0119:            }
0120:
0121:            public void setPurchaseOrderDao(PurchaseOrderDao purchaseOrderDao) {
0122:                this .purchaseOrderDao = purchaseOrderDao;
0123:            }
0124:
0125:            public void setWorkflowDocumentService(
0126:                    WorkflowDocumentService workflowDocumentService) {
0127:                this .workflowDocumentService = workflowDocumentService;
0128:            }
0129:
0130:            public void setKualiConfigurationService(
0131:                    KualiConfigurationService kualiConfigurationService) {
0132:                this .kualiConfigurationService = kualiConfigurationService;
0133:            }
0134:
0135:            public void setKualiRuleService(KualiRuleService kualiRuleService) {
0136:                this .kualiRuleService = kualiRuleService;
0137:            }
0138:
0139:            public void setVendorService(VendorService vendorService) {
0140:                this .vendorService = vendorService;
0141:            }
0142:
0143:            public void setRequisitionService(
0144:                    RequisitionService requisitionService) {
0145:                this .requisitionService = requisitionService;
0146:            }
0147:
0148:            /**
0149:             * @see org.kuali.module.purap.service.PurchaseOrderService#saveDocumentNoValidation(org.kuali.module.purap.document.PurchaseOrderDocument)
0150:             */
0151:            public void saveDocumentNoValidation(PurchaseOrderDocument document) {
0152:                try {
0153:                    documentService.saveDocument(document,
0154:                            DocumentSystemSaveEvent.class);
0155:                } catch (WorkflowException we) {
0156:                    String errorMsg = "Workflow Error saving document # "
0157:                            + document.getDocumentHeader().getDocumentNumber()
0158:                            + " " + we.getMessage();
0159:                    LOG.error(errorMsg, we);
0160:                    throw new RuntimeException(errorMsg, we);
0161:                }
0162:            }
0163:
0164:            /**
0165:             * Sets the error map to a new, empty error map before calling saveDocumentNoValidation to save the document.
0166:             * 
0167:             * @param document The purchase order document to be saved.
0168:             */
0169:            private void saveDocumentNoValidationUsingClearErrorMap(
0170:                    PurchaseOrderDocument document) {
0171:                ErrorMap errorHolder = GlobalVariables.getErrorMap();
0172:                GlobalVariables.setErrorMap(new ErrorMap());
0173:                try {
0174:                    saveDocumentNoValidation(document);
0175:                } finally {
0176:                    GlobalVariables.setErrorMap(errorHolder);
0177:                }
0178:            }
0179:
0180:            /**
0181:             * Calls the saveDocument method of documentService to save the document.
0182:             * 
0183:             * @param document The document to be saved.
0184:             */
0185:            private void saveDocumentStandardSave(PurchaseOrderDocument document) {
0186:                try {
0187:                    documentService.saveDocument(document);
0188:                } catch (WorkflowException we) {
0189:                    String errorMsg = "Workflow Error saving document # "
0190:                            + document.getDocumentHeader().getDocumentNumber()
0191:                            + " " + we.getMessage();
0192:                    LOG.error(errorMsg, we);
0193:                    throw new RuntimeException(errorMsg, we);
0194:                }
0195:            }
0196:
0197:            /**
0198:             * @see org.kuali.module.purap.service.PurchaseOrderService#createAutomaticPurchaseOrderDocument(org.kuali.module.purap.document.RequisitionDocument)
0199:             */
0200:            public void createAutomaticPurchaseOrderDocument(
0201:                    RequisitionDocument reqDocument) {
0202:                String newSessionUserId = KFSConstants.SYSTEM_USER;
0203:                try {
0204:                    LogicContainer logicToRun = new LogicContainer() {
0205:                        public Object runLogic(Object[] objects)
0206:                                throws Exception {
0207:                            RequisitionDocument doc = (RequisitionDocument) objects[0];
0208:                            // update REQ data
0209:                            doc
0210:                                    .setPurchaseOrderAutomaticIndicator(Boolean.TRUE);
0211:                            // create PO and populate with default data
0212:                            PurchaseOrderDocument po = generatePurchaseOrderFromRequisition(doc);
0213:                            po.setDefaultValuesForAPO();
0214:                            po
0215:                                    .setContractManagerCode(PurapConstants.APO_CONTRACT_MANAGER);
0216:                            documentService.routeDocument(po, null, null);
0217:                            return null;
0218:                        }
0219:                    };
0220:                    purapService.performLogicWithFakedUserSession(
0221:                            newSessionUserId, logicToRun,
0222:                            new Object[] { reqDocument });
0223:                } catch (WorkflowException e) {
0224:                    String errorMsg = "Workflow Exception caught: "
0225:                            + e.getLocalizedMessage();
0226:                    LOG.error(errorMsg, e);
0227:                    throw new RuntimeException(errorMsg, e);
0228:                } catch (UserNotFoundException e) {
0229:                    String errorMsg = "User not found for PersonUserIdentifier '"
0230:                            + newSessionUserId + "'";
0231:                    LOG.error(errorMsg, e);
0232:                    throw new RuntimeException(errorMsg, e);
0233:                } catch (Exception e) {
0234:                    throw new RuntimeException(e);
0235:                }
0236:            }
0237:
0238:            /**
0239:             * @see org.kuali.module.purap.service.PurchaseOrderService#createPurchaseOrderDocument(org.kuali.module.purap.document.RequisitionDocument,
0240:             *      java.lang.String, java.lang.Integer)
0241:             */
0242:            public PurchaseOrderDocument createPurchaseOrderDocument(
0243:                    RequisitionDocument reqDocument, String newSessionUserId,
0244:                    Integer contractManagerCode) {
0245:                try {
0246:                    LogicContainer logicToRun = new LogicContainer() {
0247:                        public Object runLogic(Object[] objects)
0248:                                throws Exception {
0249:                            RequisitionDocument doc = (RequisitionDocument) objects[0];
0250:                            PurchaseOrderDocument po = generatePurchaseOrderFromRequisition(doc);
0251:                            Integer cmCode = (Integer) objects[1];
0252:                            po.setContractManagerCode(cmCode);
0253:                            saveDocumentNoValidation(po);
0254:                            return po;
0255:                        }
0256:                    };
0257:                    return (PurchaseOrderDocument) purapService
0258:                            .performLogicWithFakedUserSession(newSessionUserId,
0259:                                    logicToRun, new Object[] { reqDocument,
0260:                                            contractManagerCode });
0261:                } catch (WorkflowException e) {
0262:                    String errorMsg = "Workflow Exception caught: "
0263:                            + e.getLocalizedMessage();
0264:                    LOG.error(errorMsg, e);
0265:                    throw new RuntimeException(errorMsg, e);
0266:                } catch (UserNotFoundException e) {
0267:                    String errorMsg = "User not found for PersonUserIdentifier '"
0268:                            + newSessionUserId + "'";
0269:                    LOG.error(errorMsg, e);
0270:                    throw new RuntimeException(errorMsg, e);
0271:                } catch (Exception e) {
0272:                    throw new RuntimeException(e);
0273:                }
0274:            }
0275:
0276:            /**
0277:             * Create Purchase Order and populate with data from Requisition and other default data
0278:             * 
0279:             * @param reqDocument The requisition document from which we create the purchase order document.
0280:             * @return The purchase order document created by this method.
0281:             * @throws WorkflowException
0282:             */
0283:            private PurchaseOrderDocument generatePurchaseOrderFromRequisition(
0284:                    RequisitionDocument reqDocument) throws WorkflowException {
0285:                PurchaseOrderDocument poDocument = null;
0286:                poDocument = (PurchaseOrderDocument) documentService
0287:                        .getNewDocument(PurchaseOrderDocTypes.PURCHASE_ORDER_DOCUMENT);
0288:                poDocument.populatePurchaseOrderFromRequisition(reqDocument);
0289:                poDocument.setStatusCode(PurchaseOrderStatuses.IN_PROCESS);
0290:                poDocument.setPurchaseOrderCurrentIndicator(true);
0291:                poDocument.setPendingActionIndicator(false);
0292:
0293:                if (RequisitionSources.B2B.equals(poDocument
0294:                        .getRequisitionSourceCode())) {
0295:                    poDocument
0296:                            .setPurchaseOrderVendorChoiceCode(VendorChoice.CONTRACTED_PRICE);
0297:                }
0298:
0299:                if (ObjectUtils.isNotNull(poDocument.getVendorContract())) {
0300:                    poDocument.setVendorPaymentTermsCode(poDocument
0301:                            .getVendorContract().getVendorPaymentTermsCode());
0302:                    poDocument.setVendorShippingPaymentTermsCode(poDocument
0303:                            .getVendorContract()
0304:                            .getVendorShippingPaymentTermsCode());
0305:                    poDocument.setVendorShippingTitleCode(poDocument
0306:                            .getVendorContract().getVendorShippingTitleCode());
0307:                } else {
0308:                    VendorDetail vendor = vendorService.getVendorDetail(
0309:                            poDocument.getVendorHeaderGeneratedIdentifier(),
0310:                            poDocument.getVendorDetailAssignedIdentifier());
0311:                    if (ObjectUtils.isNotNull(vendor)) {
0312:                        poDocument.setVendorPaymentTermsCode(vendor
0313:                                .getVendorPaymentTermsCode());
0314:                        poDocument.setVendorShippingPaymentTermsCode(vendor
0315:                                .getVendorShippingPaymentTermsCode());
0316:                        poDocument.setVendorShippingTitleCode(vendor
0317:                                .getVendorShippingTitleCode());
0318:                    }
0319:                }
0320:
0321:                purapService.addBelowLineItems(poDocument);
0322:
0323:                return poDocument;
0324:            }
0325:
0326:            /**
0327:             * @see org.kuali.module.purap.service.PurchaseOrderService#getInternalPurchasingDollarLimit(org.kuali.module.purap.document.PurchaseOrderDocument)
0328:             */
0329:            public KualiDecimal getInternalPurchasingDollarLimit(
0330:                    PurchaseOrderDocument document) {
0331:                if ((document.getVendorContract() != null)
0332:                        && (document.getContractManager() != null)) {
0333:                    KualiDecimal contractDollarLimit = vendorService
0334:                            .getApoLimitFromContract(document
0335:                                    .getVendorContract()
0336:                                    .getVendorContractGeneratedIdentifier(),
0337:                                    document.getChartOfAccountsCode(), document
0338:                                            .getOrganizationCode());
0339:                    KualiDecimal contractManagerLimit = document
0340:                            .getContractManager()
0341:                            .getContractManagerDelegationDollarLimit();
0342:                    if ((contractDollarLimit != null)
0343:                            && (contractManagerLimit != null)) {
0344:                        if (contractDollarLimit.compareTo(contractManagerLimit) > 0) {
0345:                            return contractDollarLimit;
0346:                        } else {
0347:                            return contractManagerLimit;
0348:                        }
0349:                    } else if (contractDollarLimit != null) {
0350:                        return contractDollarLimit;
0351:                    } else {
0352:                        return contractManagerLimit;
0353:                    }
0354:                } else if ((document.getVendorContract() == null)
0355:                        && (document.getContractManager() != null)) {
0356:                    return document.getContractManager()
0357:                            .getContractManagerDelegationDollarLimit();
0358:                } else if ((document.getVendorContract() != null)
0359:                        && (document.getContractManager() == null)) {
0360:                    return purapService.getApoLimit(document
0361:                            .getVendorContract()
0362:                            .getVendorContractGeneratedIdentifier(), document
0363:                            .getChartOfAccountsCode(), document
0364:                            .getOrganizationCode());
0365:                } else {
0366:                    String errorMsg = "No internal purchase order dollar limit found for purchase order '"
0367:                            + document.getPurapDocumentIdentifier() + "'.";
0368:                    LOG.warn(errorMsg);
0369:                    return null;
0370:                }
0371:            }
0372:
0373:            /**
0374:             * Loops through the collection of error messages and adding each of them to the error map.
0375:             * 
0376:             * @param errorKey The resource key used to retrieve the error text from the error message resource bundle.
0377:             * @param errors The collection of error messages.
0378:             */
0379:            private void addStringErrorMessagesToErrorMap(String errorKey,
0380:                    Collection<String> errors) {
0381:                if (ObjectUtils.isNotNull(errors)) {
0382:                    for (String error : errors) {
0383:                        LOG.error("Adding error message using error key '"
0384:                                + errorKey + "' with text '" + error + "'");
0385:                        GlobalVariables.getErrorMap().putError(
0386:                                KFSConstants.GLOBAL_ERRORS, errorKey, error);
0387:                    }
0388:                }
0389:            }
0390:
0391:            /**
0392:             * @see org.kuali.module.purap.service.PurchaseOrderService#printPurchaseOrderQuoteRequestsListPDF(org.kuali.module.purap.document.PurchaseOrderDocument,
0393:             *      java.io.ByteArrayOutputStream)
0394:             */
0395:            public boolean printPurchaseOrderQuoteRequestsListPDF(
0396:                    PurchaseOrderDocument po, ByteArrayOutputStream baosPDF) {
0397:                String environment = kualiConfigurationService
0398:                        .getPropertyString(KFSConstants.ENVIRONMENT_KEY);
0399:                Collection<String> generatePDFErrors = printService
0400:                        .generatePurchaseOrderQuoteRequestsListPdf(po, baosPDF);
0401:
0402:                if (generatePDFErrors.size() > 0) {
0403:                    addStringErrorMessagesToErrorMap(
0404:                            PurapKeyConstants.ERROR_PURCHASE_ORDER_PDF,
0405:                            generatePDFErrors);
0406:                    return false;
0407:                } else {
0408:                    saveDocumentStandardSave(po);
0409:                    return true;
0410:                }
0411:            }
0412:
0413:            /**
0414:             * @see org.kuali.module.purap.service.PurchaseOrderService#printPurchaseOrderQuotePDF(org.kuali.module.purap.document.PurchaseOrderDocument,
0415:             *      org.kuali.module.purap.bo.PurchaseOrderVendorQuote, java.io.ByteArrayOutputStream)
0416:             */
0417:            public boolean printPurchaseOrderQuotePDF(PurchaseOrderDocument po,
0418:                    PurchaseOrderVendorQuote povq, ByteArrayOutputStream baosPDF) {
0419:
0420:                String environment = kualiConfigurationService
0421:                        .getPropertyString(KFSConstants.ENVIRONMENT_KEY);
0422:                Collection<String> generatePDFErrors = printService
0423:                        .generatePurchaseOrderQuotePdf(po, povq, baosPDF,
0424:                                environment);
0425:
0426:                if (generatePDFErrors.size() > 0) {
0427:                    addStringErrorMessagesToErrorMap(
0428:                            PurapKeyConstants.ERROR_PURCHASE_ORDER_PDF,
0429:                            generatePDFErrors);
0430:                    return false;
0431:                } else {
0432:                    saveDocumentStandardSave(po);
0433:                    return true;
0434:                }
0435:            }
0436:
0437:            /**
0438:             * @see org.kuali.module.purap.service.PurchaseOrderService#performPurchaseOrderFirstTransmitViaPrinting(java.lang.String,
0439:             *      java.io.ByteArrayOutputStream)
0440:             */
0441:            public void performPurchaseOrderFirstTransmitViaPrinting(
0442:                    String documentNumber, ByteArrayOutputStream baosPDF) {
0443:                PurchaseOrderDocument po = getPurchaseOrderByDocumentNumber(documentNumber);
0444:                String environment = kualiConfigurationService
0445:                        .getPropertyString(KFSConstants.ENVIRONMENT_KEY);
0446:                Collection<String> generatePDFErrors = printService
0447:                        .generatePurchaseOrderPdf(po, baosPDF, environment,
0448:                                null);
0449:                if (!generatePDFErrors.isEmpty()) {
0450:                    addStringErrorMessagesToErrorMap(
0451:                            PurapKeyConstants.ERROR_PURCHASE_ORDER_PDF,
0452:                            generatePDFErrors);
0453:                    throw new ValidationException(
0454:                            "printing purchase order for first transmission failed");
0455:                }
0456:                if (ObjectUtils.isNotNull(po
0457:                        .getPurchaseOrderFirstTransmissionDate())) {
0458:                    // should not call this method for first transmission if document has already been transmitted
0459:                    String errorMsg = "Method to perform first transmit was called on document (doc id "
0460:                            + documentNumber
0461:                            + ") with already filled in 'first transmit date'";
0462:                    LOG.error(errorMsg);
0463:                    throw new RuntimeException(errorMsg);
0464:                }
0465:                Date currentDate = dateTimeService.getCurrentSqlDate();
0466:                po.setPurchaseOrderFirstTransmissionDate(currentDate);
0467:                po.setPurchaseOrderLastTransmitDate(currentDate);
0468:                po.setOverrideWorkflowButtons(Boolean.FALSE);
0469:                boolean performedAction = SpringContext
0470:                        .getBean(PurApWorkflowIntegrationService.class)
0471:                        .takeAllActionsForGivenCriteria(
0472:                                po,
0473:                                "Action taken automatically as part of document initial print transmission",
0474:                                NodeDetailEnum.DOCUMENT_TRANSMISSION.getName(),
0475:                                GlobalVariables.getUserSession()
0476:                                        .getUniversalUser(), null);
0477:                if (!performedAction) {
0478:                    SpringContext
0479:                            .getBean(PurApWorkflowIntegrationService.class)
0480:                            .takeAllActionsForGivenCriteria(
0481:                                    po,
0482:                                    "Action taken automatically as part of document initial print transmission by user "
0483:                                            + GlobalVariables.getUserSession()
0484:                                                    .getUniversalUser()
0485:                                                    .getPersonName(),
0486:                                    NodeDetailEnum.DOCUMENT_TRANSMISSION
0487:                                            .getName(), null,
0488:                                    KFSConstants.SYSTEM_USER);
0489:                }
0490:                po.setOverrideWorkflowButtons(Boolean.TRUE);
0491:                if (po.getStatusCode().equals(
0492:                        PurapConstants.PurchaseOrderStatuses.OPEN)) {
0493:                    saveDocumentNoValidation(po);
0494:                } else {
0495:                    attemptSetupOfInitialOpenOfDocument(po);
0496:                }
0497:            }
0498:
0499:            /**
0500:             * @see org.kuali.module.purap.service.PurchaseOrderService#performPrintPurchaseOrderPDFOnly(java.lang.String,
0501:             *      java.io.ByteArrayOutputStream)
0502:             */
0503:            public void performPrintPurchaseOrderPDFOnly(String documentNumber,
0504:                    ByteArrayOutputStream baosPDF) {
0505:                PurchaseOrderDocument po = getPurchaseOrderByDocumentNumber(documentNumber);
0506:                String environment = kualiConfigurationService
0507:                        .getPropertyString(KFSConstants.ENVIRONMENT_KEY);
0508:                Collection<String> generatePDFErrors = printService
0509:                        .generatePurchaseOrderPdf(po, baosPDF, environment,
0510:                                null);
0511:                if (!generatePDFErrors.isEmpty()) {
0512:                    addStringErrorMessagesToErrorMap(
0513:                            PurapKeyConstants.ERROR_PURCHASE_ORDER_PDF,
0514:                            generatePDFErrors);
0515:                    throw new ValidationException(
0516:                            "printing purchase order for first transmission failed");
0517:                }
0518:            }
0519:
0520:            /**
0521:             * Invokes the documentService to perform the appropriate workflow actions depending on the boolean flags on the
0522:             * workflowDocument of the documentHeader of the purchase order.
0523:             * 
0524:             * @param po The purchase order document upon which the workflow actions would be taken
0525:             * @param annotation The annotation String that we'll pass to workflow when we invoke superUserActionRequestApprove.
0526:             */
0527:            private void takeWorkflowActionsForDocumentTransmission(
0528:                    PurchaseOrderDocument po, String annotation) {
0529:                try {
0530:                    List<ActionRequestVO> docTransRequests = new ArrayList<ActionRequestVO>();
0531:                    ActionRequestVO[] actionRequests = SpringContext.getBean(
0532:                            KualiWorkflowInfo.class).getActionRequests(
0533:                            Long.valueOf(po.getDocumentNumber()));
0534:                    for (ActionRequestVO actionRequestVO : actionRequests) {
0535:                        if (actionRequestVO.isActivated()) {
0536:                            if (StringUtils.equals(actionRequestVO
0537:                                    .getNodeName(),
0538:                                    NodeDetailEnum.DOCUMENT_TRANSMISSION
0539:                                            .getName())) {
0540:                                docTransRequests.add(actionRequestVO);
0541:                            }
0542:                        }
0543:                    }
0544:                    if (!docTransRequests.isEmpty()) {
0545:                        for (ActionRequestVO actionRequest : docTransRequests) {
0546:                            po.getDocumentHeader().getWorkflowDocument()
0547:                                    .super UserActionRequestApprove(
0548:                                            actionRequest.getActionRequestId(),
0549:                                            annotation);
0550:                        }
0551:                    }
0552:                    if (po.getDocumentHeader().getWorkflowDocument()
0553:                            .isApprovalRequested()) {
0554:                        SpringContext.getBean(DocumentService.class)
0555:                                .approveDocument(po, null, new ArrayList());
0556:                    } else if (po.getDocumentHeader().getWorkflowDocument()
0557:                            .isAcknowledgeRequested()) {
0558:                        SpringContext.getBean(DocumentService.class)
0559:                                .acknowledgeDocument(po, null, new ArrayList());
0560:                    } else if (po.getDocumentHeader().getWorkflowDocument()
0561:                            .isFYIRequested()) {
0562:                        SpringContext.getBean(DocumentService.class)
0563:                                .clearDocumentFyi(po, new ArrayList());
0564:                    }
0565:                } catch (NumberFormatException nfe) {
0566:                    String errorMsg = "Exception trying to convert '"
0567:                            + po.getDocumentNumber() + "' into a number (Long)";
0568:                    LOG.error(errorMsg, nfe);
0569:                    throw new RuntimeException(errorMsg, nfe);
0570:                } catch (WorkflowException we) {
0571:                    String errorMsg = "Workflow Exception caught trying to take actions for document transmission node: "
0572:                            + we.getLocalizedMessage();
0573:                    LOG.error(errorMsg, we);
0574:                    throw new RuntimeException(errorMsg, we);
0575:                }
0576:            }
0577:
0578:            /**
0579:             * @see org.kuali.module.purap.service.PurchaseOrderService#retransmitPurchaseOrderPDF(org.kuali.module.purap.document.PurchaseOrderDocument,
0580:             *      java.io.ByteArrayOutputStream)
0581:             */
0582:            public void retransmitPurchaseOrderPDF(PurchaseOrderDocument po,
0583:                    ByteArrayOutputStream baosPDF) {
0584:
0585:                String environment = kualiConfigurationService
0586:                        .getPropertyString(KFSConstants.ENVIRONMENT_KEY);
0587:                List<PurchaseOrderItem> items = po.getItems();
0588:                List<PurchaseOrderItem> retransmitItems = new ArrayList<PurchaseOrderItem>();
0589:                for (PurchaseOrderItem item : items) {
0590:                    if (item.isItemSelectedForRetransmitIndicator()) {
0591:                        retransmitItems.add(item);
0592:                    }
0593:                }
0594:                Collection<String> generatePDFErrors = printService
0595:                        .generatePurchaseOrderPdfForRetransmission(po, baosPDF,
0596:                                environment, retransmitItems);
0597:
0598:                if (generatePDFErrors.size() > 0) {
0599:                    addStringErrorMessagesToErrorMap(
0600:                            PurapKeyConstants.ERROR_PURCHASE_ORDER_PDF,
0601:                            generatePDFErrors);
0602:                    throw new ValidationException(
0603:                            "found errors while trying to print po with doc id "
0604:                                    + po.getDocumentNumber());
0605:                }
0606:                po.setPurchaseOrderLastTransmitDate(dateTimeService
0607:                        .getCurrentSqlDate());
0608:                saveDocumentNoValidation(po);
0609:            }
0610:
0611:            /**
0612:             * This method creates a new Purchase Order Document using the given document type based off the given source document. This
0613:             * method will return null if the source document given is null.<br>
0614:             * <br> ** THIS METHOD DOES NOT SAVE EITHER THE GIVEN SOURCE DOCUMENT OR THE NEW DOCUMENT CREATED
0615:             * 
0616:             * @param sourceDocument - document the new Purchase Order Document should be based off of in terms of data
0617:             * @param docType - document type of the potential new Purchase Order Document
0618:             * @return the new Purchase Order Document of the given document type or null if the given source document is null
0619:             * @throws WorkflowException if a new document cannot be created using the given type
0620:             */
0621:            private PurchaseOrderDocument createPurchaseOrderDocumentFromSourceDocument(
0622:                    PurchaseOrderDocument sourceDocument, String docType)
0623:                    throws WorkflowException {
0624:                if (ObjectUtils.isNull(sourceDocument)) {
0625:                    String errorMsg = "Attempting to create new PO of type '"
0626:                            + docType + "' from source PO doc that is null";
0627:                    LOG.error(errorMsg);
0628:                    throw new RuntimeException(errorMsg);
0629:                }
0630:
0631:                PurchaseOrderDocument newPurchaseOrderChangeDocument = (PurchaseOrderDocument) documentService
0632:                        .getNewDocument(docType);
0633:
0634:                Set classesToExclude = new HashSet();
0635:                Class sourceObjectClass = DocumentBase.class;
0636:                classesToExclude.add(sourceObjectClass);
0637:                while (sourceObjectClass.getSuperclass() != null) {
0638:                    sourceObjectClass = sourceObjectClass.getSuperclass();
0639:                    classesToExclude.add(sourceObjectClass);
0640:                }
0641:                PurApObjectUtils.populateFromBaseWithSuper(sourceDocument,
0642:                        newPurchaseOrderChangeDocument,
0643:                        PurapConstants.UNCOPYABLE_FIELDS_FOR_PO,
0644:                        classesToExclude);
0645:                newPurchaseOrderChangeDocument.getDocumentHeader()
0646:                        .setFinancialDocumentDescription(
0647:                                sourceDocument.getDocumentHeader()
0648:                                        .getFinancialDocumentDescription());
0649:                newPurchaseOrderChangeDocument.getDocumentHeader()
0650:                        .setOrganizationDocumentNumber(
0651:                                sourceDocument.getDocumentHeader()
0652:                                        .getOrganizationDocumentNumber());
0653:
0654:                newPurchaseOrderChangeDocument
0655:                        .setPurchaseOrderCurrentIndicator(false);
0656:                newPurchaseOrderChangeDocument.setPendingActionIndicator(false);
0657:
0658:                // Need to find a way to make the ManageableArrayList to expand and populating the items and
0659:                // accounts, otherwise it will complain about the account on item 1 is missing.
0660:                for (PurApItem item : (List<PurApItem>) newPurchaseOrderChangeDocument
0661:                        .getItems()) {
0662:                    item.getSourceAccountingLines().iterator();
0663:                    // we only need to do this once to apply to all items, so we can break out of the loop now
0664:                    break;
0665:                }
0666:
0667:                newPurchaseOrderChangeDocument.refreshNonUpdateableReferences();
0668:                return newPurchaseOrderChangeDocument;
0669:            }
0670:
0671:            /**
0672:             * @see org.kuali.module.purap.service.PurchaseOrderService#createAndSavePotentialChangeDocument(java.lang.String,
0673:             *      java.lang.String, java.lang.String)
0674:             */
0675:            public PurchaseOrderDocument createAndSavePotentialChangeDocument(
0676:                    String documentNumber, String docType,
0677:                    String currentDocumentStatusCode) {
0678:                PurchaseOrderDocument currentDocument = SpringContext.getBean(
0679:                        PurchaseOrderService.class)
0680:                        .getPurchaseOrderByDocumentNumber(documentNumber);
0681:                try {
0682:                    PurchaseOrderDocument newDocument = createPurchaseOrderDocumentFromSourceDocument(
0683:                            currentDocument, docType);
0684:                    if (ObjectUtils.isNotNull(newDocument)) {
0685:                        newDocument
0686:                                .setStatusCode(PurchaseOrderStatuses.CHANGE_IN_PROCESS);
0687:                        // set status if needed
0688:                        if (StringUtils.isNotBlank(currentDocumentStatusCode)) {
0689:                            purapService.updateStatus(currentDocument,
0690:                                    currentDocumentStatusCode);
0691:                        }
0692:                        try {
0693:                            documentService.saveDocument(newDocument);
0694:                        }
0695:                        // if we catch a ValidationException it means the new PO doc found errors
0696:                        catch (ValidationException ve) {
0697:                            throw ve;
0698:                        }
0699:                        // if no validation exception was thrown then rules have passed and we are ok to edit the current PO
0700:                        currentDocument.setPendingActionIndicator(true);
0701:                        saveDocumentNoValidationUsingClearErrorMap(currentDocument);
0702:                        return newDocument;
0703:                    } else {
0704:                        String errorMsg = "Attempting to create new PO of type '"
0705:                                + docType
0706:                                + "' from source PO doc id "
0707:                                + documentNumber
0708:                                + " returned null for new document";
0709:                        LOG.error(errorMsg);
0710:                        throw new RuntimeException(errorMsg);
0711:                    }
0712:                } catch (WorkflowException we) {
0713:                    String errorMsg = "Workflow Exception caught trying to create and save PO document of type '"
0714:                            + docType
0715:                            + "' using source document with doc id '"
0716:                            + documentNumber + "'";
0717:                    LOG.error(errorMsg, we);
0718:                    throw new RuntimeException(errorMsg, we);
0719:                }
0720:            }
0721:
0722:            /**
0723:             * @see org.kuali.module.purap.service.PurchaseOrderService#createAndRoutePotentialChangeDocument(java.lang.String,
0724:             *      java.lang.String, java.lang.String, java.util.List, java.lang.String)
0725:             */
0726:            public PurchaseOrderDocument createAndRoutePotentialChangeDocument(
0727:                    String documentNumber, String docType, String annotation,
0728:                    List adhocRoutingRecipients,
0729:                    String currentDocumentStatusCode) {
0730:                PurchaseOrderDocument currentDocument = getPurchaseOrderByDocumentNumber(documentNumber);
0731:
0732:                purapService.updateStatus(currentDocument,
0733:                        currentDocumentStatusCode);
0734:                try {
0735:                    PurchaseOrderDocument newDocument = createPurchaseOrderDocumentFromSourceDocument(
0736:                            currentDocument, docType);
0737:                    newDocument
0738:                            .setStatusCode(PurchaseOrderStatuses.CHANGE_IN_PROCESS);
0739:                    if (ObjectUtils.isNotNull(newDocument)) {
0740:                        try {
0741:                            documentService.routeDocument(newDocument,
0742:                                    annotation, adhocRoutingRecipients);
0743:                        }
0744:                        // if we catch a ValidationException it means the new PO doc found errors
0745:                        catch (ValidationException ve) {
0746:                            throw ve;
0747:                        }
0748:                        // if no validation exception was thrown then rules have passed and we are ok to edit the current PO
0749:                        currentDocument.setPendingActionIndicator(true);
0750:                        saveDocumentNoValidationUsingClearErrorMap(currentDocument);
0751:                        return newDocument;
0752:                    } else {
0753:                        String errorMsg = "Attempting to create new PO of type '"
0754:                                + docType
0755:                                + "' from source PO doc id "
0756:                                + documentNumber
0757:                                + " returned null for new document";
0758:                        LOG.error(errorMsg);
0759:                        throw new RuntimeException(errorMsg);
0760:                    }
0761:                } catch (WorkflowException we) {
0762:                    String errorMsg = "Workflow Exception caught trying to create and route PO document of type '"
0763:                            + docType
0764:                            + "' using source document with doc id '"
0765:                            + documentNumber + "'";
0766:                    LOG.error(errorMsg, we);
0767:                    throw new RuntimeException(errorMsg, we);
0768:                }
0769:            }
0770:
0771:            /**
0772:             * Returns the current route node name.
0773:             * 
0774:             * @param wd The KualiWorkflowDocument object whose current route node we're trying to get.
0775:             * @return The current route node name.
0776:             * @throws WorkflowException
0777:             */
0778:            private String getCurrentRouteNodeName(KualiWorkflowDocument wd)
0779:                    throws WorkflowException {
0780:                String[] nodeNames = wd.getNodeNames();
0781:                if ((nodeNames == null) || (nodeNames.length == 0)) {
0782:                    return null;
0783:                } else {
0784:                    return nodeNames[0];
0785:                }
0786:            }
0787:
0788:            /**
0789:             * @see org.kuali.module.purap.service.PurchaseOrderService#completePurchaseOrder(org.kuali.module.purap.document.PurchaseOrderDocument)
0790:             */
0791:            public void completePurchaseOrder(PurchaseOrderDocument po) {
0792:                LOG.debug("completePurchaseOrder() started");
0793:                setCurrentAndPendingIndicatorsForApprovedPODocuments(po);
0794:                // if the document is set in a Pending Transmission status then don't OPEN the PO just leave it as is
0795:                if (!PurchaseOrderStatuses.STATUSES_BY_TRANSMISSION_TYPE
0796:                        .values().contains(po.getStatusCode())) {
0797:                    attemptSetupOfInitialOpenOfDocument(po);
0798:                }
0799:            }
0800:
0801:            /**
0802:             * @see org.kuali.module.purap.service.PurchaseOrderService#setupDocumentForPendingFirstTransmission(org.kuali.module.purap.document.PurchaseOrderDocument,
0803:             *      boolean)
0804:             */
0805:            public void setupDocumentForPendingFirstTransmission(
0806:                    PurchaseOrderDocument po,
0807:                    boolean hasActionRequestForDocumentTransmission) {
0808:                if (POTransmissionMethods.PRINT.equals(po
0809:                        .getPurchaseOrderTransmissionMethodCode())) {
0810:                    String newStatusCode = PurchaseOrderStatuses.STATUSES_BY_TRANSMISSION_TYPE
0811:                            .get(po.getPurchaseOrderTransmissionMethodCode());
0812:                    LOG
0813:                            .debug("setupDocumentForPendingFirstTransmission() Purchase Order Transmission Type is '"
0814:                                    + po
0815:                                            .getPurchaseOrderTransmissionMethodCode()
0816:                                    + "' setting status to '"
0817:                                    + newStatusCode
0818:                                    + "'");
0819:                    purapService.updateStatus(po, newStatusCode);
0820:                } else {
0821:                    if (hasActionRequestForDocumentTransmission) {
0822:                        /*
0823:                         * here we error out because the document generated a request for the doc transmission route level but the default
0824:                         * status to set is open... this prevents Open purchase orders that could be awaiting transmission by a valid method
0825:                         * (via the generated request)
0826:                         */
0827:                        String errorMessage = "An action request was generated for document id "
0828:                                + po.getDocumentNumber()
0829:                                + " with an unhandled transmission type '"
0830:                                + po.getPurchaseOrderTransmissionMethodCode()
0831:                                + "'";
0832:                        LOG.error(errorMessage);
0833:                        throw new RuntimeException(errorMessage);
0834:                    }
0835:                    LOG
0836:                            .info("setupDocumentForPendingFirstTransmission() Unhandled Transmission Status: "
0837:                                    + po
0838:                                            .getPurchaseOrderTransmissionMethodCode()
0839:                                    + " -- Defaulting Status to '"
0840:                                    + PurchaseOrderStatuses.OPEN + "'");
0841:                    attemptSetupOfInitialOpenOfDocument(po);
0842:                }
0843:            }
0844:
0845:            /**
0846:             * If the status of the purchase order is not OPEN and the initial open date is null, sets the initial open date to current date
0847:             * and update the status to OPEN, then save the purchase order.
0848:             * 
0849:             * @param po The purchase order document whose initial open date and status we want to update.
0850:             * @return boolean false if the initial open date, status update and the saving of the purchase order did not occur.
0851:             */
0852:            private boolean attemptSetupOfInitialOpenOfDocument(
0853:                    PurchaseOrderDocument po) {
0854:                LOG
0855:                        .debug("attemptSetupOfInitialOpenOfDocument() started using document with doc id "
0856:                                + po.getDocumentNumber());
0857:                boolean documentWasSaved = false;
0858:                if (!PurchaseOrderStatuses.OPEN.equals(po.getStatusCode())) {
0859:                    if (ObjectUtils
0860:                            .isNull(po.getPurchaseOrderInitialOpenDate())) {
0861:                        LOG
0862:                                .debug("attemptSetupOfInitialOpenOfDocument() setting initial open date on document");
0863:                        po.setPurchaseOrderInitialOpenDate(dateTimeService
0864:                                .getCurrentSqlDate());
0865:                    } else {
0866:                        throw new RuntimeException(
0867:                                "Document does not have status code '"
0868:                                        + PurchaseOrderStatuses.OPEN
0869:                                        + "' on it but value of initial open date is "
0870:                                        + po.getPurchaseOrderInitialOpenDate());
0871:                    }
0872:                    LOG
0873:                            .info("attemptSetupOfInitialOpenOfDocument() Setting po document id "
0874:                                    + po.getDocumentNumber()
0875:                                    + " status from '"
0876:                                    + po.getStatusCode()
0877:                                    + "' to '"
0878:                                    + PurchaseOrderStatuses.OPEN + "'");
0879:                    purapService.updateStatus(po, PurchaseOrderStatuses.OPEN);
0880:                    saveDocumentNoValidation(po);
0881:                    documentWasSaved = true;
0882:                } else {
0883:                    LOG
0884:                            .info("attemptSetupOfInitialOpenOfDocument() Found document already in '"
0885:                                    + PurchaseOrderStatuses.OPEN
0886:                                    + "' status... will not change or update");
0887:                }
0888:                return documentWasSaved;
0889:            }
0890:
0891:            /**
0892:             * @see org.kuali.module.purap.service.PurchaseOrderService#getCurrentPurchaseOrder(java.lang.Integer)
0893:             */
0894:            public PurchaseOrderDocument getCurrentPurchaseOrder(Integer id) {
0895:                return getPurchaseOrderByDocumentNumber(purchaseOrderDao
0896:                        .getDocumentNumberForCurrentPurchaseOrder(id));
0897:            }
0898:
0899:            /**
0900:             * @see org.kuali.module.purap.service.PurchaseOrderService#getPurchaseOrderByDocumentNumber(java.lang.String)
0901:             */
0902:            public PurchaseOrderDocument getPurchaseOrderByDocumentNumber(
0903:                    String documentNumber) {
0904:                if (ObjectUtils.isNotNull(documentNumber)) {
0905:                    try {
0906:                        PurchaseOrderDocument doc = (PurchaseOrderDocument) documentService
0907:                                .getByDocumentHeaderId(documentNumber);
0908:                        if (ObjectUtils.isNotNull(doc)) {
0909:                            KualiWorkflowDocument workflowDocument = doc
0910:                                    .getDocumentHeader().getWorkflowDocument();
0911:                            doc
0912:                                    .refreshReferenceObject(RicePropertyConstants.DOCUMENT_HEADER);
0913:                            doc.getDocumentHeader().setWorkflowDocument(
0914:                                    workflowDocument);
0915:                        }
0916:                        return doc;
0917:                    } catch (WorkflowException e) {
0918:                        String errorMessage = "Error getting purchase order document from document service";
0919:                        LOG.error("getPurchaseOrderByDocumentNumber() "
0920:                                + errorMessage, e);
0921:                        throw new RuntimeException(errorMessage, e);
0922:                    }
0923:                }
0924:                return null;
0925:            }
0926:
0927:            /**
0928:             * @see org.kuali.module.purap.service.PurchaseOrderService#getOldestPurchaseOrder(org.kuali.module.purap.document.PurchaseOrderDocument,
0929:             *      org.kuali.module.purap.document.PurchaseOrderDocument)
0930:             */
0931:            public PurchaseOrderDocument getOldestPurchaseOrder(
0932:                    PurchaseOrderDocument po,
0933:                    PurchaseOrderDocument documentBusinessObject) {
0934:                LOG.debug("entering getOldestPO(PurchaseOrderDocument)");
0935:                if (ObjectUtils.isNotNull(po)) {
0936:                    String oldestDocumentNumber = purchaseOrderDao
0937:                            .getOldestPurchaseOrderDocumentNumber(po
0938:                                    .getPurapDocumentIdentifier());
0939:                    if (StringUtils.equals(oldestDocumentNumber, po
0940:                            .getDocumentNumber())) {
0941:                        // manually set bo notes - this is mainly done for performance reasons (preferably we could call
0942:                        // retrieve doc notes in PersistableBusinessObjectBase but that is private)
0943:                        updateNotes(po, documentBusinessObject);
0944:                        LOG.debug("exiting getOldestPO(PurchaseOrderDocument)");
0945:                        return po;
0946:                    } else {
0947:                        PurchaseOrderDocument oldestPurchaseOrder = getPurchaseOrderByDocumentNumber(oldestDocumentNumber);
0948:                        updateNotes(oldestPurchaseOrder, documentBusinessObject);
0949:                        LOG.debug("exiting getOldestPO(PurchaseOrderDocument)");
0950:                        return oldestPurchaseOrder;
0951:                    }
0952:                }
0953:                return null;
0954:            }
0955:
0956:            /**
0957:             * If the purchase order's object id is not null (I think this means if it's an existing purchase order that had already been
0958:             * saved to the db previously), get the notes of the purchase order from the database, fix the notes' fields by calling the
0959:             * fixDbNoteFields, then set the notes to the purchase order. Otherwise (I think this means if it's a new purchase order), set
0960:             * the notes of this purchase order to be the notes of the documentBusinessObject.
0961:             * 
0962:             * @param po The current purchase order.
0963:             * @param documentBusinessObject The oldest purchase order whose purapDocumentIdentifier is the same as the po's
0964:             *        purapDocumentIdentifier.
0965:             */
0966:            private void updateNotes(PurchaseOrderDocument po,
0967:                    PurchaseOrderDocument documentBusinessObject) {
0968:                if (ObjectUtils.isNotNull(documentBusinessObject)) {
0969:                    if (ObjectUtils.isNotNull(po.getObjectId())) {
0970:                        List<Note> dbNotes = SpringContext.getBean(
0971:                                NoteService.class).getByRemoteObjectId(
0972:                                po.getObjectId());
0973:                        // need to set fields that are not ojb managed (i.e. the notes on the documentBusinessObject may have been modified
0974:                        // independently of the ones in the db)
0975:                        fixDbNoteFields(documentBusinessObject, dbNotes);
0976:                        po.setBoNotes(dbNotes);
0977:                    } else {
0978:                        po.setBoNotes(documentBusinessObject.getBoNotes());
0979:                    }
0980:                }
0981:            }
0982:
0983:            /**
0984:             * This method fixes non ojb managed missing fields from the db
0985:             * 
0986:             * @param documentBusinessObject The oldest purchase order whose purapDocumentIdentifier is the same as the po's
0987:             *        purapDocumentIdentifier.
0988:             * @param dbNotes The notes of the purchase order obtained from the database.
0989:             */
0990:            private void fixDbNoteFields(
0991:                    PurchaseOrderDocument documentBusinessObject,
0992:                    List<Note> dbNotes) {
0993:                for (int i = 0; i < dbNotes.size(); i++) {
0994:                    Note dbNote = dbNotes.get(i);
0995:                    List<Note> currentNotes = (List<Note>) documentBusinessObject
0996:                            .getBoNotes();
0997:                    if (i < currentNotes.size()) {
0998:                        Note currentNote = (currentNotes).get(i);
0999:                        // set the fyi from the current note if not empty
1000:                        AdHocRouteRecipient fyiNoteRecipient = currentNote
1001:                                .getAdHocRouteRecipient();
1002:                        if (ObjectUtils.isNotNull(fyiNoteRecipient)) {
1003:                            dbNote.setAdHocRouteRecipient(fyiNoteRecipient);
1004:                        }
1005:                    }
1006:                }
1007:            }
1008:
1009:            /**
1010:             * @see org.kuali.module.purap.service.PurchaseOrderService#getPurchaseOrderNotes(java.lang.Integer)
1011:             */
1012:            public ArrayList<Note> getPurchaseOrderNotes(Integer id) {
1013:                ArrayList notes = new TypedArrayList(Note.class);
1014:                PurchaseOrderDocument po = getPurchaseOrderByDocumentNumber(purchaseOrderDao
1015:                        .getOldestPurchaseOrderDocumentNumber(id));
1016:                if (ObjectUtils.isNotNull(po)) {
1017:                    notes = noteService.getByRemoteObjectId(po.getObjectId());
1018:                }
1019:                return notes;
1020:            }
1021:
1022:            /**
1023:             * @see org.kuali.module.purap.service.PurchaseOrderService#setCurrentAndPendingIndicatorsForApprovedPODocuments(org.kuali.module.purap.document.PurchaseOrderDocument)
1024:             */
1025:            public void setCurrentAndPendingIndicatorsForApprovedPODocuments(
1026:                    PurchaseOrderDocument newPO) {
1027:                // Get the "current PO" that's in the database, i.e. the PO row that contains current indicator = Y
1028:                PurchaseOrderDocument oldPO = getCurrentPurchaseOrder(newPO
1029:                        .getPurapDocumentIdentifier());
1030:
1031:                // If the document numbers between the oldPO and the newPO are different, then this is a PO change document.
1032:                if (!oldPO.getDocumentNumber()
1033:                        .equals(newPO.getDocumentNumber())) {
1034:                    // First, we set the indicators for the oldPO to : Current = N and Pending = N
1035:                    oldPO.setPurchaseOrderCurrentIndicator(false);
1036:                    oldPO.setPendingActionIndicator(false);
1037:
1038:                    // set the status and status history of the oldPO to retired version
1039:                    purapService
1040:                            .updateStatus(
1041:                                    oldPO,
1042:                                    PurapConstants.PurchaseOrderStatuses.RETIRED_VERSION);
1043:
1044:                    saveDocumentNoValidationUsingClearErrorMap(oldPO);
1045:                }
1046:
1047:                // Now, we set the "new PO" indicators so that Current = Y and Pending = N
1048:                newPO.setPurchaseOrderCurrentIndicator(true);
1049:                newPO.setPendingActionIndicator(false);
1050:            }
1051:
1052:            /**
1053:             * @see org.kuali.module.purap.service.PurchaseOrderService#setCurrentAndPendingIndicatorsForDisapprovedChangePODocuments(org.kuali.module.purap.document.PurchaseOrderDocument)
1054:             */
1055:            public void setCurrentAndPendingIndicatorsForDisapprovedChangePODocuments(
1056:                    PurchaseOrderDocument newPO) {
1057:                updateCurrentDocumentForNoPendingAction(
1058:                        newPO,
1059:                        PurapConstants.PurchaseOrderStatuses.DISAPPROVED_CHANGE,
1060:                        PurapConstants.PurchaseOrderStatuses.OPEN);
1061:            }
1062:
1063:            /**
1064:             * @see org.kuali.module.purap.service.PurchaseOrderService#setCurrentAndPendingIndicatorsForCancelledChangePODocuments(org.kuali.module.purap.document.PurchaseOrderDocument)
1065:             */
1066:            public void setCurrentAndPendingIndicatorsForCancelledChangePODocuments(
1067:                    PurchaseOrderDocument newPO) {
1068:                updateCurrentDocumentForNoPendingAction(newPO,
1069:                        PurapConstants.PurchaseOrderStatuses.CANCELLED_CHANGE,
1070:                        PurapConstants.PurchaseOrderStatuses.OPEN);
1071:            }
1072:
1073:            /**
1074:             * @see org.kuali.module.purap.service.PurchaseOrderService#setCurrentAndPendingIndicatorsForCancelledReopenPODocuments(org.kuali.module.purap.document.PurchaseOrderDocument)
1075:             */
1076:            public void setCurrentAndPendingIndicatorsForCancelledReopenPODocuments(
1077:                    PurchaseOrderDocument newPO) {
1078:                updateCurrentDocumentForNoPendingAction(newPO,
1079:                        PurapConstants.PurchaseOrderStatuses.CANCELLED_CHANGE,
1080:                        PurapConstants.PurchaseOrderStatuses.CLOSED);
1081:            }
1082:
1083:            /**
1084:             * @see org.kuali.module.purap.service.PurchaseOrderService#setCurrentAndPendingIndicatorsForDisapprovedReopenPODocuments(org.kuali.module.purap.document.PurchaseOrderDocument)
1085:             */
1086:            public void setCurrentAndPendingIndicatorsForDisapprovedReopenPODocuments(
1087:                    PurchaseOrderDocument newPO) {
1088:                updateCurrentDocumentForNoPendingAction(
1089:                        newPO,
1090:                        PurapConstants.PurchaseOrderStatuses.DISAPPROVED_CHANGE,
1091:                        PurapConstants.PurchaseOrderStatuses.CLOSED);
1092:            }
1093:
1094:            /**
1095:             * @see org.kuali.module.purap.service.PurchaseOrderService#setCurrentAndPendingIndicatorsForCancelledRemoveHoldPODocuments(org.kuali.module.purap.document.PurchaseOrderDocument)
1096:             */
1097:            public void setCurrentAndPendingIndicatorsForCancelledRemoveHoldPODocuments(
1098:                    PurchaseOrderDocument newPO) {
1099:                updateCurrentDocumentForNoPendingAction(newPO,
1100:                        PurapConstants.PurchaseOrderStatuses.CANCELLED_CHANGE,
1101:                        PurapConstants.PurchaseOrderStatuses.PAYMENT_HOLD);
1102:            }
1103:
1104:            /**
1105:             * @see org.kuali.module.purap.service.PurchaseOrderService#setCurrentAndPendingIndicatorsForDisapprovedRemoveHoldPODocuments(org.kuali.module.purap.document.PurchaseOrderDocument)
1106:             */
1107:            public void setCurrentAndPendingIndicatorsForDisapprovedRemoveHoldPODocuments(
1108:                    PurchaseOrderDocument newPO) {
1109:                updateCurrentDocumentForNoPendingAction(
1110:                        newPO,
1111:                        PurapConstants.PurchaseOrderStatuses.DISAPPROVED_CHANGE,
1112:                        PurapConstants.PurchaseOrderStatuses.PAYMENT_HOLD);
1113:            }
1114:
1115:            /**
1116:             * Update the statuses of both the old purchase order and the new purchase orders, then save the old and the new purchase
1117:             * orders.
1118:             * 
1119:             * @param newPO The new change purchase order document (e.g. the PurchaseOrderAmendmentDocument that was resulted from the user
1120:             *        clicking on the amend button).
1121:             * @param newPOStatus The status to be set on the new change purchase order document.
1122:             * @param oldPOStatus The status to be set on the existing (old) purchase order document.
1123:             */
1124:            private void updateCurrentDocumentForNoPendingAction(
1125:                    PurchaseOrderDocument newPO, String newPOStatus,
1126:                    String oldPOStatus) {
1127:                // Get the "current PO" that's in the database, i.e. the PO row that contains current indicator = Y
1128:                PurchaseOrderDocument oldPO = getCurrentPurchaseOrder(newPO
1129:                        .getPurapDocumentIdentifier());
1130:                // Set the Pending indicator for the oldPO to N
1131:                oldPO.setPendingActionIndicator(false);
1132:                purapService.updateStatus(oldPO, oldPOStatus);
1133:                purapService.updateStatus(newPO, newPOStatus);
1134:                saveDocumentNoValidationUsingClearErrorMap(oldPO);
1135:                saveDocumentNoValidationUsingClearErrorMap(newPO);
1136:            }
1137:
1138:            public ArrayList<PurchaseOrderQuoteStatus> getPurchaseOrderQuoteStatusCodes() {
1139:                ArrayList poQuoteStatuses = new TypedArrayList(
1140:                        PurchaseOrderQuoteStatus.class);
1141:                poQuoteStatuses = (ArrayList) businessObjectService
1142:                        .findAll(PurchaseOrderQuoteStatus.class);
1143:                return poQuoteStatuses;
1144:            }
1145:
1146:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.