View Javadoc
1   /*
2    * Copyright 2011 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.select.batch.service.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.module.purap.PurapConstants;
20  import org.kuali.ole.module.purap.businessobject.PurchaseOrderType;
21  import org.kuali.ole.module.purap.document.RequisitionDocument;
22  import org.kuali.ole.select.batch.service.RequisitionCreateDocumentService;
23  import org.kuali.ole.select.bo.OLEEResourceOrderRecord;
24  import org.kuali.ole.select.businessobject.OleRequisitionItem;
25  import org.kuali.ole.select.document.OleRequisitionDocument;
26  import org.kuali.ole.select.document.service.OleRequisitionDocumentService;
27  import org.kuali.ole.sys.OLEConstants;
28  import org.kuali.ole.sys.context.SpringContext;
29  import org.kuali.ole.sys.document.validation.event.DocumentSystemSaveEvent;
30  import org.kuali.rice.kew.framework.postprocessor.IDocumentEvent;
31  import org.kuali.rice.kim.api.identity.Person;
32  import org.kuali.rice.kim.api.identity.PersonService;
33  import org.kuali.rice.krad.service.DocumentService;
34  import org.kuali.rice.krad.util.GlobalVariables;
35  
36  import java.sql.Date;
37  import java.text.SimpleDateFormat;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  public class RequisitionCreateDocumentServiceImpl implements RequisitionCreateDocumentService {
43      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RequisitionCreateDocumentServiceImpl.class);
44  
45      protected DocumentService documentService;
46      private OleRequisitionDocumentService oleRequisitionDocumentService;
47  
48      /**
49       * Gets the documentService attribute.
50       *
51       * @return Returns the documentService.
52       */
53      public DocumentService getDocumentService() {
54          return documentService;
55      }
56  
57  
58      /**
59       * Sets the documentService attribute value.
60       *
61       * @param documentService The documentService to set.
62       */
63      public void setDocumentService(DocumentService documentService) {
64          this.documentService = documentService;
65      }
66  /*    public boolean saveRequisitionDocuments(RequisitionDocument reqDocument) {
67          try {
68              documentService.saveDocument(reqDocument, DocumentSystemSaveEvent.class);
69              //documentService.prepareWorkflowDocument(reqDocument);
70              if ( LOG.isInfoEnabled() ) {
71                  LOG.info("Saved Requisition document. Document Number: "+reqDocument.getDocumentNumber());
72              }
73          }
74          catch (Exception e) {
75              LOG.error("Error persisting document # " + reqDocument.getDocumentHeader().getDocumentNumber() + " " + e.getMessage(), e);
76              throw new RuntimeException("Error persisting document # " + reqDocument.getDocumentHeader().getDocumentNumber() + " " + e.getMessage(), e);
77          }
78          return true;
79      }*/
80  
81      public String saveRequisitionDocuments(RequisitionDocument reqDocument) {
82          try {
83              if (LOG.isInfoEnabled()) {
84                  LOG.info("Calling saveRequisitionDocuments in RequisitionCreateDocumentServiceImpl >>>>" + reqDocument.getDocumentNumber());
85              }
86              try {
87                  documentService.saveDocument(reqDocument, DocumentSystemSaveEvent.class);
88              } catch (Exception e) {
89                  LOG.error("Exection while saving requisition document" + e);
90                  e.printStackTrace();
91              }
92              reqDocument.populateDocumentForRouting();
93  
94              Person principalPerson = SpringContext.getBean(PersonService.class).getPerson(GlobalVariables.getUserSession().getPerson().getPrincipalId());
95              // reqDocument.getDocumentHeader().setWorkflowDocument(KNSServiceLocator.getWorkflowDocumentService().createWorkflowDocument(new Long(reqDocument.getDocumentNumber()), principalPerson));
96  
97              String purchaseOrderType = "";
98              if (reqDocument.getPurchaseOrderTypeId() != null) {
99  
100                 Map purchaseOrderTypeIdMap = new HashMap();
101                 purchaseOrderTypeIdMap.put("purchaseOrderTypeId", reqDocument.getPurchaseOrderTypeId());
102                 org.kuali.rice.krad.service.BusinessObjectService
103                         businessObject = SpringContext.getBean(org.kuali.rice.krad.service.BusinessObjectService.class);
104                 List<PurchaseOrderType> purchaseOrderTypeDocumentList = (List) businessObject.findMatching(PurchaseOrderType.class, purchaseOrderTypeIdMap);
105                 if (purchaseOrderTypeDocumentList != null && purchaseOrderTypeDocumentList.size() > 0) {
106                     PurchaseOrderType purchaseOrderTypeDoc = (PurchaseOrderType) purchaseOrderTypeDocumentList.get(0);
107                     purchaseOrderType = purchaseOrderTypeDoc.getPurchaseOrderType();
108                 }
109                 if (LOG.isDebugEnabled())
110                     LOG.debug("purchaseOrderType >>>>>>>>>>>" + purchaseOrderType);
111                 //Modified for jira OLE-7278
112                 //if (purchaseOrderType.equalsIgnoreCase(PurapConstants.ORDER_TYPE_FIRM)) {
113                     getDocumentService().routeDocument(reqDocument, null, null);
114                 //}
115                 LOG.debug("After Calling createWorkflowDocument >>>>>>>>>>>");
116             }
117             //  System.out.println("After Calling createWorkflowDocument >>>>>>>>>>>" + reqDocument.getDocumentHeader().getWorkflowDocument().getStatus());
118             LOG.debug(IDocumentEvent.BEFORE_PROCESS);
119             if (LOG.isInfoEnabled()) {
120                 LOG.info("Saved Requisition document. Document Number: " + reqDocument.getDocumentNumber());
121             }
122         } catch (Exception e) {
123             LOG.error("Error persisting document # " + reqDocument.getDocumentHeader().getDocumentNumber() + " " + e.getMessage(), e);
124             throw new RuntimeException("Error persisting document # " + reqDocument.getDocumentHeader().getDocumentNumber() + " " + e.getMessage(), e);
125         }
126         return reqDocument.getDocumentNumber();
127     }
128 
129     public OleRequisitionDocument updateParamaterValue(OleRequisitionDocument requisitionDocument,List<PurchaseOrderType> purchaseOrderTypeDocumentList,OLEEResourceOrderRecord oleEResourceOrderRecord) {
130 
131         if(!purchaseOrderTypeDocumentList.get(0).getPurchaseOrderType().equals(OLEConstants.APPROVAL) && !purchaseOrderTypeDocumentList.get(0).getPurchaseOrderType().equals(OLEConstants.ORD_TYPE_FIRM_FIX)) {
132             try {
133                 requisitionDocument.setRecurringPaymentTypeCode(getOleRequisitionDocumentService().getParameter(OLEConstants.RECURRING_PAY_TYP));
134                 SimpleDateFormat format = new SimpleDateFormat(OLEConstants.DATE_FORM_PO_BEGN_DT);
135                 java.util.Date utilDate = (java.util.Date) format.parse(OLEConstants.DATE_FORM_PO_END_DT);
136                 java.sql.Date poEndDate = new java.sql.Date(utilDate.getTime());
137                 java.util.Date date = new java.util.Date();
138                 Date poCreateDate = new Date(date.getTime());
139                 requisitionDocument.setPurchaseOrderBeginDate(poCreateDate);
140                 requisitionDocument.setPurchaseOrderEndDate(poEndDate);
141             } catch (Exception e) {
142                 LOG.error("Error Occured hwile setting the system parameter value to requisition document :" + e.getMessage());
143             }
144         }
145         return requisitionDocument;
146     }
147 
148     public void updateCopyNumber() {
149 
150     }
151 
152 
153     public OleRequisitionDocumentService getOleRequisitionDocumentService() {
154         if(oleRequisitionDocumentService == null) {
155             return  SpringContext.getBean(OleRequisitionDocumentService.class);
156         }
157 
158         return oleRequisitionDocumentService;
159     }
160 
161 
162 }