001/*
002 * Copyright 2011 The Kuali Foundation.
003 * 
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 * http://www.opensource.org/licenses/ecl2.php
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.select.batch.service.impl;
017
018import org.kuali.ole.module.purap.PurapConstants;
019import org.kuali.ole.module.purap.businessobject.PurchaseOrderType;
020import org.kuali.ole.module.purap.document.RequisitionDocument;
021import org.kuali.ole.select.batch.service.RequisitionCreateDocumentService;
022import org.kuali.ole.sys.context.SpringContext;
023import org.kuali.ole.sys.document.validation.event.DocumentSystemSaveEvent;
024import org.kuali.rice.kew.framework.postprocessor.IDocumentEvent;
025import org.kuali.rice.kim.api.identity.Person;
026import org.kuali.rice.kim.api.identity.PersonService;
027import org.kuali.rice.krad.service.DocumentService;
028import org.kuali.rice.krad.util.GlobalVariables;
029import java.util.HashMap;
030import java.util.List;
031import java.util.Map;
032
033public class RequisitionCreateDocumentServiceImpl implements RequisitionCreateDocumentService {
034    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RequisitionCreateDocumentServiceImpl.class);
035
036    protected DocumentService documentService;
037
038    /**
039     * Gets the documentService attribute.
040     *
041     * @return Returns the documentService.
042     */
043    public DocumentService getDocumentService() {
044        return documentService;
045    }
046
047
048    /**
049     * Sets the documentService attribute value.
050     *
051     * @param documentService The documentService to set.
052     */
053    public void setDocumentService(DocumentService documentService) {
054        this.documentService = documentService;
055    }
056/*    public boolean saveRequisitionDocuments(RequisitionDocument reqDocument) {
057        try {
058            documentService.saveDocument(reqDocument, DocumentSystemSaveEvent.class);
059            //documentService.prepareWorkflowDocument(reqDocument);
060            if ( LOG.isInfoEnabled() ) {
061                LOG.info("Saved Requisition document. Document Number: "+reqDocument.getDocumentNumber());
062            }
063        }
064        catch (Exception e) {
065            LOG.error("Error persisting document # " + reqDocument.getDocumentHeader().getDocumentNumber() + " " + e.getMessage(), e);
066            throw new RuntimeException("Error persisting document # " + reqDocument.getDocumentHeader().getDocumentNumber() + " " + e.getMessage(), e);
067        }
068        return true;
069    }*/
070
071    public String saveRequisitionDocuments(RequisitionDocument reqDocument) {
072        try {
073            if (LOG.isInfoEnabled()) {
074                LOG.info("Calling saveRequisitionDocuments in RequisitionCreateDocumentServiceImpl >>>>" + reqDocument.getDocumentNumber());
075            }
076            try {
077                documentService.saveDocument(reqDocument, DocumentSystemSaveEvent.class);
078            } catch (Exception e) {
079                LOG.error("Exection while saving requisition document" + e);
080                e.printStackTrace();
081            }
082            reqDocument.populateDocumentForRouting();
083
084            Person principalPerson = SpringContext.getBean(PersonService.class).getPerson(GlobalVariables.getUserSession().getPerson().getPrincipalId());
085            // reqDocument.getDocumentHeader().setWorkflowDocument(KNSServiceLocator.getWorkflowDocumentService().createWorkflowDocument(new Long(reqDocument.getDocumentNumber()), principalPerson));
086
087            String purchaseOrderType = "";
088            if (reqDocument.getPurchaseOrderTypeId() != null) {
089
090                Map purchaseOrderTypeIdMap = new HashMap();
091                purchaseOrderTypeIdMap.put("purchaseOrderTypeId", reqDocument.getPurchaseOrderTypeId());
092                org.kuali.rice.krad.service.BusinessObjectService
093                        businessObject = SpringContext.getBean(org.kuali.rice.krad.service.BusinessObjectService.class);
094                List<PurchaseOrderType> purchaseOrderTypeDocumentList = (List) businessObject.findMatching(PurchaseOrderType.class, purchaseOrderTypeIdMap);
095                if (purchaseOrderTypeDocumentList != null && purchaseOrderTypeDocumentList.size() > 0) {
096                    PurchaseOrderType purchaseOrderTypeDoc = (PurchaseOrderType) purchaseOrderTypeDocumentList.get(0);
097                    purchaseOrderType = purchaseOrderTypeDoc.getPurchaseOrderType();
098                }
099                if (LOG.isDebugEnabled())
100                    LOG.debug("purchaseOrderType >>>>>>>>>>>" + purchaseOrderType);
101                //Modified for jira OLE-7278
102                //if (purchaseOrderType.equalsIgnoreCase(PurapConstants.ORDER_TYPE_FIRM)) {
103                    getDocumentService().routeDocument(reqDocument, null, null);
104                //}
105                LOG.debug("After Calling createWorkflowDocument >>>>>>>>>>>");
106            }
107            //  System.out.println("After Calling createWorkflowDocument >>>>>>>>>>>" + reqDocument.getDocumentHeader().getWorkflowDocument().getStatus());
108            LOG.debug(IDocumentEvent.BEFORE_PROCESS);
109            if (LOG.isInfoEnabled()) {
110                LOG.info("Saved Requisition document. Document Number: " + reqDocument.getDocumentNumber());
111            }
112        } catch (Exception e) {
113            LOG.error("Error persisting document # " + reqDocument.getDocumentHeader().getDocumentNumber() + " " + e.getMessage(), e);
114            throw new RuntimeException("Error persisting document # " + reqDocument.getDocumentHeader().getDocumentNumber() + " " + e.getMessage(), e);
115        }
116        return reqDocument.getDocumentNumber();
117    }
118
119}