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     */
016    package org.kuali.kfs.module.purap.document.service.impl;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    import java.util.HashMap;
021    import java.util.List;
022    import java.util.Map;
023    
024    import org.apache.commons.lang.StringUtils;
025    import org.apache.log4j.Logger;
026    import org.kuali.kfs.module.purap.PurapConstants;
027    import org.kuali.kfs.module.purap.businessobject.PurchaseOrderContractLanguage;
028    import org.kuali.kfs.module.purap.businessobject.PurchaseOrderVendorQuote;
029    import org.kuali.kfs.module.purap.dataaccess.ImageDao;
030    import org.kuali.kfs.module.purap.document.PurchaseOrderDocument;
031    import org.kuali.kfs.module.purap.document.service.FaxService;
032    import org.kuali.kfs.module.purap.exception.FaxServerUnavailableError;
033    import org.kuali.kfs.module.purap.exception.FaxSubmissionError;
034    import org.kuali.kfs.module.purap.exception.PurError;
035    import org.kuali.kfs.module.purap.exception.PurapConfigurationException;
036    import org.kuali.kfs.module.purap.pdf.PurchaseOrderParameters;
037    import org.kuali.kfs.module.purap.pdf.PurchaseOrderPdf;
038    import org.kuali.kfs.module.purap.pdf.PurchaseOrderQuotePdf;
039    import org.kuali.kfs.module.purap.pdf.PurchaseOrderTransmitParameters;
040    import org.kuali.kfs.sys.KFSConstants;
041    import org.kuali.kfs.sys.KFSPropertyConstants;
042    import org.kuali.kfs.sys.context.SpringContext;
043    import org.kuali.kfs.sys.service.impl.KfsParameterConstants;
044    import org.kuali.kfs.vnd.businessobject.CampusParameter;
045    import org.kuali.kfs.vnd.document.service.VendorService;
046    import org.kuali.rice.kim.bo.Person;
047    import org.kuali.rice.kns.bo.Country;
048    import org.kuali.rice.kns.service.BusinessObjectService;
049    import org.kuali.rice.kns.service.CountryService;
050    import org.kuali.rice.kns.service.KualiConfigurationService;
051    import org.kuali.rice.kns.service.ParameterService;
052    import org.kuali.rice.kns.util.GlobalVariables;
053    import org.springframework.transaction.annotation.Transactional;
054    
055    
056    @Transactional
057    public class FaxServiceImpl implements FaxService {
058    
059        private static final Logger LOG = Logger.getLogger(FaxServiceImpl.class);
060    
061        protected KualiConfigurationService kualiConfigurationService;
062        protected ParameterService parameterService;
063        protected VendorService vendorService;
064        protected BusinessObjectService businessObjectService;
065        protected CountryService countryService;
066        protected ImageDao imageDao;
067    
068        
069    
070        /**
071         * Create the Purchase Order Pdf document and send it via fax to the recipient in the PO
072         * 
073         * @param po PurchaseOrder that holds the Quote
074         * @param isRetransmit if passed true then PO is being retransmitted
075         * @return Collection of ServiceError objects
076         */
077        public void faxPurchaseOrderPdf(PurchaseOrderDocument po, boolean isRetransmit) {
078            LOG.debug("faxPurchaseOrderPdf(po,reTransmit) started");
079            String pdfFileLocation = parameterService.getParameterValue(KfsParameterConstants.PURCHASING_DOCUMENT.class, PurapConstants.PDF_DIRECTORY);
080            if (pdfFileLocation == null) {
081                throw new RuntimeException("Application Setting PDF_DIRECTORY is missing.");
082            }
083    
084            String imageTempLocation = kualiConfigurationService.getPropertyString(KFSConstants.TEMP_DIRECTORY_KEY) + "/";
085            if (imageTempLocation == null) {
086                throw new RuntimeException("Application Setting TEMP_DIRECTORY_KEY is missing.");
087            }
088    
089            LOG.debug("faxPurchaseOrderPdf() ended");
090            this.faxPurchaseOrderPdf(po, pdfFileLocation, imageTempLocation, isRetransmit);
091        }
092    
093      
094    
095        /**
096         * Create the Purchase Order Pdf document and send it via fax to the recipient in the PO
097         * 
098         * @param po PurchaseOrder that holds the Quote
099         * @param isRetransmit if passed true then PO is being retransmitted
100         * @return Collection of ServiceError objects
101         */
102        public void faxPurchaseOrderPdf(PurchaseOrderDocument po, String pdfFileLocation, String imageTempLocation, boolean isRetransmit) {
103            LOG.debug("faxPurchaseOrderPdf() started with locations");
104         
105            // Get the vendor's country name.
106            if (po.getVendorCountryCode() != null) {
107                Country vendorCountry = countryService.getByPrimaryId(po.getVendorCountryCode());
108                if (vendorCountry != null) {
109                    po.setVendorCountryCode(vendorCountry.getPostalCountryCode());
110                }
111                else {
112                    po.setVendorCountryCode("NA");
113                }
114            }
115            else {
116                po.setVendorCountryCode("NA");
117            }
118            
119           
120            PurchaseOrderParameters purchaseOrderParameters = getPurchaseOrderParameters();
121            purchaseOrderParameters.setPurchaseOrderPdfAndFaxParameters(po);
122            
123            PurchaseOrderPdf poPdf = null;
124            try {
125                String environment = kualiConfigurationService.getPropertyString(KFSConstants.ENVIRONMENT_KEY);
126                poPdf = new PurchaseOrderPdf();
127                poPdf.savePdf(po, purchaseOrderParameters , isRetransmit, environment);
128            }
129            catch (PurError e) {
130                GlobalVariables.getMessageMap().putError("errors", "error.blank");
131                                                                                                      // only need to call once.
132                LOG.debug("faxPurchaseOrderPdf() ended");
133            }
134            catch (Throwable e) {
135                LOG.error("faxPurchaseOrderPdf() Faxing Failed on PDF creation - Exception was " + e.getMessage(), e);
136                GlobalVariables.getMessageMap().putError("errors", "error.blank", "Faxing Error.  Unable to save pdf file. Please Contact Purchasing");
137                                                                                                      // only need to call once.
138                LOG.debug("faxPurchaseOrderPdf() ended");
139            }
140    
141            PurchaseOrderTransmitParameters transmitParameters =  (PurchaseOrderTransmitParameters)purchaseOrderParameters;
142            String[] files = new String[1];
143            files[0] = transmitParameters.getPdfFileName();
144    
145            try {
146                this.faxPDF(files,purchaseOrderParameters); 
147            }
148            catch (FaxSubmissionError e) {
149                GlobalVariables.getMessageMap().putError("errors", "error.blank");
150            }
151            catch (FaxServerUnavailableError e) {
152                GlobalVariables.getMessageMap().putError("errors", "error.blank");
153            }
154            catch (PurError e) {
155                GlobalVariables.getMessageMap().putError("errors", "error.blank");
156            }
157            catch (Throwable e) {
158                LOG.error("faxPurchaseOrderPdf() Faxing Failed Exception was " + e.getMessage(), e);
159                GlobalVariables.getMessageMap().putError("errors", "error.blank", "Faxing Error.  Please Contact Purchasing");
160            }
161            finally {
162                try {
163                    if (poPdf != null) {
164                        poPdf.deletePdf(pdfFileLocation, transmitParameters.getPdfFileName());
165                    }
166                    else {
167                        // ignore - PDF can't be deleted if PDF doesn't exist
168                    }
169                }
170                catch (Throwable e) {
171                    LOG.error("faxPurchaseOrderPdf() Error deleting PDF - Exception was " + e.getMessage(), e);
172                    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");
173                }
174            }
175    
176            LOG.debug("faxPurchaseOrderPdf() ended");
177        }
178    
179        public void faxPurchaseOrderQuotePdf(PurchaseOrderDocument po, PurchaseOrderVendorQuote povq) {
180            LOG.debug("faxPurchaseOrderQuotePdf() started");
181            
182           
183            PurchaseOrderParameters purchaseOrderParameters = getPurchaseOrderParameters();
184            purchaseOrderParameters.setPurchaseOrderPdfAndFaxParameters(po,povq);
185            String environmentCode = kualiConfigurationService.getPropertyString(KFSConstants.ENVIRONMENT_KEY);
186            
187            PurchaseOrderQuotePdf poQuotePdf = new PurchaseOrderQuotePdf();
188            Collection errors = new ArrayList();
189            
190            
191            try {
192              
193              // Get the vendor's country name.
194              if (povq.getVendorCountryCode() != null) {
195                  Country vendorCountry = countryService.getByPrimaryId(po.getVendorCountryCode());
196                  if (vendorCountry != null) {
197                      povq.setVendorCountryCode(vendorCountry.getPostalCountryCode());
198                  }
199                  else {
200                      povq.setVendorCountryCode("NA");
201                  }
202              }
203              else {
204                  povq.setVendorCountryCode("NA");
205              }
206    
207              poQuotePdf.savePOQuotePDF(po, povq, purchaseOrderParameters, environmentCode);
208            } catch (PurError e) {
209              GlobalVariables.getMessageMap().putError("errors", "error.blank");
210              LOG.debug("faxPurchaseOrderQuotePdf() ended");
211              
212            } catch (Throwable e) {
213              LOG.error("faxPurchaseOrderQuotePdf() Faxing Failed on PDF creation - Exception was " + e.getMessage(), e);
214              LOG.error("faxPurchaseOrderQuotePdf() Faxing Failed on PDF creation - Exception was " + e.getMessage(), e);
215              GlobalVariables.getMessageMap().putError("errors", "error.blank", "Faxing Error.  Unable to save pdf file. Please Contact Purchasing");
216            }
217    
218            LOG.debug("faxPurchaseOrderQuotePdf() Quote PDF saved successfully for PO " + po.getPurapDocumentIdentifier() + " and Quote ID " + povq.getPurchaseOrderVendorQuoteIdentifier());
219    
220           
221         
222            
223            PurchaseOrderTransmitParameters transmitParameters =  (PurchaseOrderTransmitParameters)purchaseOrderParameters;
224            String pdfFileLocation = transmitParameters.getPdfFileLocation();
225            String pdfFileName     = transmitParameters.getPdfFileName();
226            String[] files = new String[1];
227            files[0] = pdfFileName;
228            
229            try {
230              this.faxPDF(files,transmitParameters);
231            } catch (FaxSubmissionError e) {
232                LOG.error("faxPurchaseOrderQuotePdf() Error faxing Quote PDF" + pdfFileName + " - Exception was " + e.getMessage(), e);
233                GlobalVariables.getMessageMap().putError("errors", "error.blank");
234                
235            } catch (FaxServerUnavailableError e) {
236                LOG.error("faxPurchaseOrderQuotePdf() Error faxing Quote PDF" + pdfFileName + " - Exception was " + e.getMessage(), e);
237                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");
238                
239            } catch (PurError e) {
240                LOG.error("faxPurchaseOrderQuotePdf() Error faxing Quote PDF" + pdfFileName + " - Exception was " + e.getMessage(), e);
241                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");
242                
243            } catch (Throwable e) {
244                LOG.error("faxPurchaseOrderQuotePdf() Error faxing Quote PDF" + pdfFileName + " - Exception was " + e.getMessage(), e);
245                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");
246                
247            } finally {
248              try {
249                poQuotePdf.deletePdf(pdfFileLocation, pdfFileName);
250              } catch (Throwable e) {
251                LOG.error("faxPurchaseOrderQuotePdf() Error deleting Quote PDF" + pdfFileName + " - Exception was " + e.getMessage(), e);
252                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");
253            
254              }
255            }
256            
257            
258            LOG.debug("faxPurchaseOrderQuotePdf() ended");
259            
260        
261        }
262     
263        
264        /**
265         * Here is where the PDF is actually faxed, needs to be implemented at each institution
266         */
267        protected void faxPDF(String[] files, PurchaseOrderParameters transmitParameters ) {
268            LOG.info("faxPDF() NEEDS TO BE IMPLEMENTED!");
269            throw new RuntimeException("faxPDF() NEEDS TO BE IMPLEMENTED!");
270        }
271        
272      
273        
274        public KualiConfigurationService getKualiConfigurationService() {
275            return kualiConfigurationService;
276        }
277    
278        public void setKualiConfigurationService(KualiConfigurationService kualiConfigurationService) {
279            this.kualiConfigurationService = kualiConfigurationService;
280        }
281    
282        public ParameterService getParameterService() {
283            return parameterService;
284        }
285    
286        public void setParameterService(ParameterService parameterService) {
287            this.parameterService = parameterService;
288        }
289    
290        public VendorService getVendorService() {
291            return vendorService;
292        }
293    
294        public void setVendorService(VendorService vendorService) {
295            this.vendorService = vendorService;
296        }
297    
298        public BusinessObjectService getBusinessObjectService() {
299            return businessObjectService;
300        }
301    
302        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
303            this.businessObjectService = businessObjectService;
304        }
305    
306        public CountryService getCountryService() {
307            return countryService;
308        }
309        
310        public PurchaseOrderParameters getPurchaseOrderParameters() {
311            return SpringContext.getBean(PurchaseOrderParameters.class);
312        }
313    
314        public void setCountryService(CountryService countryService) {
315            this.countryService = countryService;
316        }
317    
318        public ImageDao getImageDao() {
319            return imageDao;
320        }
321    
322        public void setImageDao(ImageDao imageDao) {
323            this.imageDao = imageDao;
324        }
325        
326    
327    
328    
329    }