001/*
002 * Copyright 2007-2008 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 */
016
017package org.kuali.ole.module.purap.document.service.impl;
018
019import org.kuali.ole.module.purap.PurapConstants;
020import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
021import org.kuali.ole.module.purap.document.service.FaxBatchDocumentsService;
022import org.kuali.ole.module.purap.document.service.FaxService;
023import org.kuali.ole.module.purap.document.service.PurapService;
024import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
025import org.kuali.ole.sys.context.SpringContext;
026import org.kuali.rice.core.api.datetime.DateTimeService;
027import org.kuali.rice.kew.api.exception.WorkflowException;
028import org.kuali.rice.krad.service.DocumentService;
029import org.kuali.rice.krad.util.GlobalVariables;
030
031import java.util.Collection;
032import java.util.Iterator;
033
034public class FaxBatchDocumentsServiceImpl implements FaxBatchDocumentsService {
035    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FaxBatchDocumentsServiceImpl.class);
036
037    private PurchaseOrderService purchaseOrderService;
038    private FaxService faxService;
039    private PurapService purapService;
040    private DateTimeService dateTimeService;
041
042    /**
043     * Faxes pending documents.  Currently only PO documents set to Pending Fax
044     * Status inside workflow.
045     *
046     * @return Collection of ServiceError objects
047     */
048    public boolean faxPendingPurchaseOrders() {
049
050        Collection<PurchaseOrderDocument> pendingPOs = purchaseOrderService.getPendingPurchaseOrderFaxes();
051        boolean result = true;
052
053        for (Iterator<PurchaseOrderDocument> iter = pendingPOs.iterator(); iter.hasNext(); ) {
054
055            PurchaseOrderDocument po = iter.next();
056
057            if (!po.getDocumentHeader().hasWorkflowDocument()) {
058                try {
059                    po = (PurchaseOrderDocument) SpringContext.getBean(DocumentService.class).getByDocumentHeaderId(po.getDocumentNumber());
060                } catch (WorkflowException e) {
061                    throw new RuntimeException(e);
062                }
063            }
064
065            GlobalVariables.getMessageMap().clearErrorMessages();
066            faxService.faxPurchaseOrderPdf(po, false);
067
068            if (GlobalVariables.getMessageMap().hasErrors()) {
069                try {
070                    po.updateAndSaveAppDocStatus(PurapConstants.PurchaseOrderStatuses.APPDOC_OPEN);
071                } catch (WorkflowException we) {
072                    String errorMsg = "Workflow Exception caught trying to create and save PO document of type PurchaseOrderSplitDocument using source document with doc id '" + po.getDocumentNumber() + "'";
073                    LOG.error(errorMsg, we);
074                    throw new RuntimeException(errorMsg, we);
075                }
076
077                po.setPurchaseOrderInitialOpenTimestamp(dateTimeService.getCurrentTimestamp());
078                po.setPurchaseOrderLastTransmitTimestamp(dateTimeService.getCurrentTimestamp());
079                purapService.saveDocumentNoValidation(po);
080            } else {
081                result = false;
082            }
083        }
084
085        return result;
086    }
087
088    public void setPurchaseOrderService(PurchaseOrderService purchaseOrderService) {
089        this.purchaseOrderService = purchaseOrderService;
090    }
091
092    public void setFaxService(FaxService faxService) {
093        this.faxService = faxService;
094    }
095
096    public void setPurapService(PurapService purapService) {
097        this.purapService = purapService;
098    }
099
100    public void setDateTimeService(DateTimeService dateTimeService) {
101        this.dateTimeService = dateTimeService;
102    }
103
104}