View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.purap.document.service.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.ole.module.purap.PurapConstants;
21  import org.kuali.ole.module.purap.businessobject.PurchaseOrderVendorQuote;
22  import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
23  import org.kuali.ole.module.purap.document.service.FaxService;
24  import org.kuali.ole.module.purap.exception.FaxServerUnavailableError;
25  import org.kuali.ole.module.purap.exception.FaxSubmissionError;
26  import org.kuali.ole.module.purap.exception.PurError;
27  import org.kuali.ole.module.purap.pdf.PurchaseOrderParameters;
28  import org.kuali.ole.module.purap.pdf.PurchaseOrderPdf;
29  import org.kuali.ole.module.purap.pdf.PurchaseOrderQuotePdf;
30  import org.kuali.ole.module.purap.pdf.PurchaseOrderTransmitParameters;
31  import org.kuali.ole.sys.OLEConstants;
32  import org.kuali.ole.sys.context.SpringContext;
33  import org.kuali.ole.sys.service.impl.OleParameterConstants;
34  import org.kuali.ole.vnd.document.service.VendorService;
35  import org.kuali.rice.core.api.config.property.ConfigurationService;
36  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
37  import org.kuali.rice.krad.service.BusinessObjectService;
38  import org.kuali.rice.krad.util.GlobalVariables;
39  import org.kuali.rice.location.api.country.Country;
40  import org.kuali.rice.location.api.country.CountryService;
41  import org.springframework.transaction.annotation.Transactional;
42  
43  import java.util.ArrayList;
44  import java.util.Collection;
45  
46  
47  @Transactional
48  public class FaxServiceImpl implements FaxService {
49  
50      private static final Logger LOG = Logger.getLogger(FaxServiceImpl.class);
51  
52      protected ConfigurationService kualiConfigurationService;
53      protected ParameterService parameterService;
54      protected VendorService vendorService;
55      protected BusinessObjectService businessObjectService;
56      protected CountryService countryService;
57  
58  
59      /**
60       * Create the Purchase Order Pdf document and send it via fax to the recipient in the PO
61       *
62       * @param po           PurchaseOrder that holds the Quote
63       * @param isRetransmit if passed true then PO is being retransmitted
64       * @return Collection of ServiceError objects
65       */
66      @Override
67      public void faxPurchaseOrderPdf(PurchaseOrderDocument po, boolean isRetransmit) {
68          LOG.debug("faxPurchaseOrderPdf(po,reTransmit) started");
69          String pdfFileLocation = getPdfFileLocation();
70          if (pdfFileLocation == null) {
71              throw new RuntimeException("Application Setting PDF_DIRECTORY is missing.");
72          }
73  
74          String imageTempLocation = kualiConfigurationService.getPropertyValueAsString(OLEConstants.TEMP_DIRECTORY_KEY) + "/";
75          if (imageTempLocation == null) {
76              throw new RuntimeException("Application Setting TEMP_DIRECTORY_KEY is missing.");
77          }
78  
79          LOG.debug("faxPurchaseOrderPdf() ended");
80          this.faxPurchaseOrderPdf(po, pdfFileLocation, imageTempLocation, isRetransmit);
81      }
82  
83  
84      /**
85       * Create the Purchase Order Pdf document and send it via fax to the recipient in the PO
86       *
87       * @param po           PurchaseOrder that holds the Quote
88       * @param isRetransmit if passed true then PO is being retransmitted
89       * @return Collection of ServiceError objects
90       */
91      @Override
92      public void faxPurchaseOrderPdf(PurchaseOrderDocument po, String pdfFileLocation, String imageTempLocation, boolean isRetransmit) {
93          LOG.debug("faxPurchaseOrderPdf() started with locations");
94  
95          // Get the vendor's country name.
96          if (StringUtils.isNotBlank(po.getVendorCountryCode())) {
97              Country vendorCountry = countryService.getCountry(po.getVendorCountryCode());
98              if (vendorCountry != null) {
99                  po.setVendorCountryCode(vendorCountry.getCode());
100             } else {
101                 po.setVendorCountryCode("NA");
102             }
103         } else {
104             po.setVendorCountryCode("NA");
105         }
106 
107 
108         PurchaseOrderParameters purchaseOrderParameters = getPurchaseOrderParameters();
109         purchaseOrderParameters.setPurchaseOrderPdfAndFaxParameters(po);
110 
111         PurchaseOrderPdf poPdf = null;
112         try {
113             String environment = kualiConfigurationService.getPropertyValueAsString(OLEConstants.ENVIRONMENT_KEY);
114             poPdf = new PurchaseOrderPdf();
115             poPdf.savePdf(po, purchaseOrderParameters, isRetransmit, environment);
116         } catch (PurError e) {
117             GlobalVariables.getMessageMap().putError("errors", "error.blank");
118             // only need to call once.
119             LOG.debug("faxPurchaseOrderPdf() ended");
120         } catch (Throwable e) {
121             LOG.error("faxPurchaseOrderPdf() Faxing Failed on PDF creation - Exception was " + e.getMessage(), e);
122             GlobalVariables.getMessageMap().putError("errors", "error.blank", "Faxing Error.  Unable to save pdf file. Please Contact Purchasing");
123             // only need to call once.
124             LOG.debug("faxPurchaseOrderPdf() ended");
125         }
126 
127         PurchaseOrderTransmitParameters transmitParameters = (PurchaseOrderTransmitParameters) purchaseOrderParameters;
128         String[] files = new String[1];
129         files[0] = transmitParameters.getPdfFileName();
130 
131         try {
132             this.faxPDF(files, purchaseOrderParameters);
133         } catch (FaxSubmissionError e) {
134             GlobalVariables.getMessageMap().putError("errors", "error.blank");
135         } catch (FaxServerUnavailableError e) {
136             GlobalVariables.getMessageMap().putError("errors", "error.blank");
137         } catch (PurError e) {
138             GlobalVariables.getMessageMap().putError("errors", "error.blank");
139         } catch (Throwable e) {
140             LOG.error("faxPurchaseOrderPdf() Faxing Failed Exception was " + e.getMessage(), e);
141             GlobalVariables.getMessageMap().putError("errors", "error.blank", "Faxing Error.  Please Contact Purchasing");
142         } finally {
143             try {
144                 if (poPdf != null) {
145                     poPdf.deletePdf(pdfFileLocation, transmitParameters.getPdfFileName());
146                 } else {
147                     // ignore - PDF can't be deleted if PDF doesn't exist
148                 }
149             } catch (Throwable e) {
150                 LOG.error("faxPurchaseOrderPdf() Error deleting PDF - Exception was " + e.getMessage(), e);
151                 GlobalVariables.getMessageMap().putError("errors", "error.blank", "Your fax was sent successfully but an error occurred that is unrelated to faxing. Please report this problem to Purchasing");
152             }
153         }
154 
155         LOG.debug("faxPurchaseOrderPdf() ended");
156     }
157 
158     @Override
159     public void faxPurchaseOrderQuotePdf(PurchaseOrderDocument po, PurchaseOrderVendorQuote povq) {
160         LOG.debug("faxPurchaseOrderQuotePdf() started");
161 
162 
163         PurchaseOrderParameters purchaseOrderParameters = getPurchaseOrderParameters();
164         purchaseOrderParameters.setPurchaseOrderPdfAndFaxParameters(po, povq);
165         String environmentCode = kualiConfigurationService.getPropertyValueAsString(OLEConstants.ENVIRONMENT_KEY);
166 
167         PurchaseOrderQuotePdf poQuotePdf = new PurchaseOrderQuotePdf();
168         Collection errors = new ArrayList();
169 
170 
171         try {
172 
173             // Get the vendor's country name.
174             if (StringUtils.isNotBlank(povq.getVendorCountryCode())) {
175                 Country vendorCountry = countryService.getCountry(po.getVendorCountryCode());
176                 if (vendorCountry != null) {
177                     povq.setVendorCountryCode(vendorCountry.getCode());
178                 } else {
179                     povq.setVendorCountryCode("NA");
180                 }
181             } else {
182                 povq.setVendorCountryCode("NA");
183             }
184 
185             poQuotePdf.savePOQuotePDF(po, povq, purchaseOrderParameters, environmentCode);
186         } catch (PurError e) {
187             GlobalVariables.getMessageMap().putError("errors", "error.blank");
188             LOG.debug("faxPurchaseOrderQuotePdf() ended");
189 
190         } catch (Throwable e) {
191             LOG.error("faxPurchaseOrderQuotePdf() Faxing Failed on PDF creation - Exception was " + e.getMessage(), e);
192             LOG.error("faxPurchaseOrderQuotePdf() Faxing Failed on PDF creation - Exception was " + e.getMessage(), e);
193             GlobalVariables.getMessageMap().putError("errors", "error.blank", "Faxing Error.  Unable to save pdf file. Please Contact Purchasing");
194         }
195 
196         if (LOG.isDebugEnabled()) {
197             LOG.debug("faxPurchaseOrderQuotePdf() Quote PDF saved successfully for PO " + po.getPurapDocumentIdentifier() + " and Quote ID " + povq.getPurchaseOrderVendorQuoteIdentifier());
198         }
199 
200         PurchaseOrderTransmitParameters transmitParameters = (PurchaseOrderTransmitParameters) purchaseOrderParameters;
201         String pdfFileLocation = transmitParameters.getPdfFileLocation();
202         String pdfFileName = transmitParameters.getPdfFileName();
203         String[] files = new String[1];
204         files[0] = pdfFileName;
205 
206         try {
207             this.faxPDF(files, transmitParameters);
208         } catch (FaxSubmissionError e) {
209             LOG.error("faxPurchaseOrderQuotePdf() Error faxing Quote PDF" + pdfFileName + " - Exception was " + e.getMessage(), e);
210             GlobalVariables.getMessageMap().putError("errors", "error.blank");
211 
212         } catch (FaxServerUnavailableError e) {
213             LOG.error("faxPurchaseOrderQuotePdf() Error faxing Quote PDF" + pdfFileName + " - Exception was " + e.getMessage(), e);
214             GlobalVariables.getMessageMap().putError("errors", "error.blank", "The document did not successfully transmit to the fax server. Report this to the Procurement Services Technology group, make note of the document you attempted to transmit and try again when the issue has been resolved");
215 
216         } catch (PurError e) {
217             LOG.error("faxPurchaseOrderQuotePdf() Error faxing Quote PDF" + pdfFileName + " - Exception was " + e.getMessage(), e);
218             GlobalVariables.getMessageMap().putError("errors", "error.blank", "The document did not successfully transmit to the fax server. Report this to the Procurement Services Technology group, make note of the document you attempted to transmit and try again when the issue has been resolved");
219 
220         } catch (Throwable e) {
221             LOG.error("faxPurchaseOrderQuotePdf() Error faxing Quote PDF" + pdfFileName + " - Exception was " + e.getMessage(), e);
222             GlobalVariables.getMessageMap().putError("errors", "error.blank", "The document did not successfully transmit to the fax server. Report this to the Procurement Services Technology group, make note of the document you attempted to transmit and try again when the issue has been resolved");
223 
224         } finally {
225             try {
226                 poQuotePdf.deletePdf(pdfFileLocation, pdfFileName);
227             } catch (Throwable e) {
228                 LOG.error("faxPurchaseOrderQuotePdf() Error deleting Quote PDF" + pdfFileName + " - Exception was " + e.getMessage(), e);
229                 GlobalVariables.getMessageMap().putError("errors", "error.blank", "Your fax was sent successfully but an error occurred that is unrelated to faxing. Please report this problem to Purchasing");
230 
231             }
232         }
233 
234 
235         LOG.debug("faxPurchaseOrderQuotePdf() ended");
236 
237 
238     }
239 
240 
241     /**
242      * Here is where the PDF is actually faxed, needs to be implemented at each institution
243      */
244     protected void faxPDF(String[] files, PurchaseOrderParameters transmitParameters) {
245         LOG.info("faxPDF() NEEDS TO BE IMPLEMENTED!");
246         throw new RuntimeException("faxPDF() NEEDS TO BE IMPLEMENTED!");
247     }
248 
249 
250     public ConfigurationService getConfigurationService() {
251         return kualiConfigurationService;
252     }
253 
254     public void setConfigurationService(ConfigurationService kualiConfigurationService) {
255         this.kualiConfigurationService = kualiConfigurationService;
256     }
257 
258     public ParameterService getParameterService() {
259         return parameterService;
260     }
261 
262     public void setParameterService(ParameterService parameterService) {
263         this.parameterService = parameterService;
264     }
265 
266     public VendorService getVendorService() {
267         return vendorService;
268     }
269 
270     public void setVendorService(VendorService vendorService) {
271         this.vendorService = vendorService;
272     }
273 
274     public BusinessObjectService getBusinessObjectService() {
275         return businessObjectService;
276     }
277 
278     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
279         this.businessObjectService = businessObjectService;
280     }
281 
282     public CountryService getCountryService() {
283         return countryService;
284     }
285 
286     public PurchaseOrderParameters getPurchaseOrderParameters() {
287         return SpringContext.getBean(PurchaseOrderParameters.class);
288     }
289 
290     public void setCountryService(CountryService countryService) {
291         this.countryService = countryService;
292     }
293 
294     public String getPdfFileLocation() {
295         return parameterService.getParameterValueAsString(OleParameterConstants.PURCHASING_DOCUMENT.class, PurapConstants.PDF_DIRECTORY);
296     }
297 
298 
299 }