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.kuali.ole.OLEConstants;
19  import org.kuali.ole.module.purap.PurapParameterConstants;
20  import org.kuali.ole.module.purap.document.RequisitionDocument;
21  import org.kuali.ole.select.OleSelectConstant;
22  import org.kuali.ole.select.batch.service.OleRequisitionCreateDocumentService;
23  import org.kuali.ole.select.batch.service.RequisitionLoadTransactionsService;
24  import org.kuali.ole.select.businessobject.BibInfoBean;
25  import org.kuali.ole.select.service.impl.BuildVendorBibInfoBean;
26  import org.kuali.ole.sys.batch.service.BatchInputFileService;
27  import org.kuali.ole.sys.context.SpringContext;
28  import org.kuali.rice.core.api.config.property.ConfigurationService;
29  import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
30  import org.kuali.rice.coreservice.api.parameter.Parameter;
31  import org.kuali.rice.coreservice.api.parameter.ParameterKey;
32  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
33  import org.kuali.rice.kew.api.exception.WorkflowException;
34  import org.kuali.rice.krad.UserSession;
35  import org.kuali.rice.krad.util.GlobalVariables;
36  import org.springframework.beans.factory.InitializingBean;
37  
38  import java.util.ArrayList;
39  import java.util.List;
40  import java.util.Properties;
41  
42  public class RequisitionLoadTransactionsServiceImpl implements RequisitionLoadTransactionsService, InitializingBean {
43      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RequisitionLoadTransactionsServiceImpl.class);
44  
45      protected BatchInputFileService batchInputFileService;
46      protected OleRequisitionCreateDocumentService oleRequisitionCreateDocumentService;
47      protected BuildVendorBibInfoBean buildVendorBibInfoBean;
48      protected Properties properties = null;
49      protected ConfigurationService kualiConfigurationService;
50  
51      protected String determinePurchaseOrderTransmissionMethod() {
52  
53          return SpringContext.getBean(ParameterService.class).getParameterValueAsString(RequisitionDocument.class, PurapParameterConstants.PURAP_DEFAULT_PO_TRANSMISSION_CODE);
54      }
55  
56      public BatchInputFileService getBatchInputFileService() {
57          return batchInputFileService;
58      }
59  
60      public void setBatchInputFileService(BatchInputFileService batchInputFileService) {
61          this.batchInputFileService = batchInputFileService;
62      }
63  
64      public OleRequisitionCreateDocumentService getOleRequisitionCreateDocumentService() {
65          return oleRequisitionCreateDocumentService;
66      }
67  
68  
69      public void setOleRequisitionCreateDocumentService(OleRequisitionCreateDocumentService oleRequisitionCreateDocumentService) {
70          this.oleRequisitionCreateDocumentService = oleRequisitionCreateDocumentService;
71      }
72  
73      public BuildVendorBibInfoBean getBuildVendorBibInfoBean() {
74          return buildVendorBibInfoBean;
75      }
76  
77      public void setBuildVendorBibInfoBean(BuildVendorBibInfoBean buildVendorBibInfoBean) {
78          this.buildVendorBibInfoBean = buildVendorBibInfoBean;
79      }
80  
81      public ConfigurationService getConfigurationService() {
82          return kualiConfigurationService;
83      }
84  
85      public void setConfigurationService(ConfigurationService kualiConfigurationService) {
86          this.kualiConfigurationService = kualiConfigurationService;
87      }
88  
89      @Override
90      public void afterPropertiesSet() throws Exception {
91          //properties = loadPropertiesFromClassPath("org/kuali/ole/select/batch/service/impl/bibinfo.properties");
92      }
93  
94      /**
95       * To load the XML File for the vendor.
96       *
97       * @param fileName String
98       * @return boolean
99       */
100     public boolean loadRequisitionFile(String fileName) {
101 
102         String userName = kualiConfigurationService.getPropertyValueAsString("userName");
103         GlobalVariables.setUserSession(new UserSession(userName));
104         try {
105             List<BibInfoBean> bibInfoBeanList = new ArrayList<BibInfoBean>();
106             bibInfoBeanList = buildVendorBibInfoBean.getBibInfoList(fileName);
107             setRequisitionParameterValue(bibInfoBeanList);
108             saveRequisitionDocument(bibInfoBeanList);
109         } catch (WorkflowException we) {
110             LOG.error("failed to create a new RequisitionDocument instance" + we, we);
111         } catch (Exception ex) {
112             LOG.error("faild to create the requisition document in RequisitionLoadTransactionsServiceImpl " + fileName, ex);
113             throw new RuntimeException("parsing error " + fileName + " " + ex.getMessage(), ex);
114         }
115 
116         return true;
117 
118     }
119 
120     private List<BibInfoBean> setRequisitionParameterValue(List<BibInfoBean> bibInfoBeanList) {
121         for (BibInfoBean bibInfoBean : bibInfoBeanList) {
122             //bibInfoBean.setRequestSource(OleSelectConstant.REQUEST_SRC_TYPE_BATCHINGEST);
123             bibInfoBean.setRequisitionSource(OleSelectConstant.REQUISITON_SRC_TYPE_MANUALINGEST);
124             bibInfoBean.setRequestorType(OleSelectConstant.BATCHINGEST_REQUEST);
125             bibInfoBean.setDocStoreOperation(OleSelectConstant.DOCSTORE_OPERATION_BATCHINGEST);
126         }
127         return bibInfoBeanList;
128     }
129 
130     /**
131      * Set the values for the Requisition Document and save.
132      *
133      * @param bibInfoBeanList ArrayList
134      */
135     public List saveRequisitionDocument(List<BibInfoBean> bibInfoBeanList) throws Exception {
136         boolean vendorRecordMappingFlag = false;
137         List reqList = new ArrayList(0);
138         String vendorRecordMappingProperty = getParameter("VENDOR_RECORD_TO_REQUISITION_MAPPING");
139         if (vendorRecordMappingProperty.equalsIgnoreCase("TRUE"))
140             vendorRecordMappingFlag = true;
141         oleRequisitionCreateDocumentService.saveRequisitionDocument(bibInfoBeanList, vendorRecordMappingFlag);
142         return (List) oleRequisitionCreateDocumentService.getReqList();
143 
144     }
145 
146 
147     /**
148      * To load the property file for the given path.
149      *
150      * @param classPath String
151      * @return properties
152      */
153 /*    public static Properties loadPropertiesFromClassPath(String classPath) {
154         ClassPathResource classPathResource = new ClassPathResource(classPath);
155 
156         Properties properties = new Properties();
157         try {
158             properties.load(classPathResource.getInputStream());
159         }
160         catch (IOException e) {
161             throw new RuntimeException("Invalid class path: " + classPath + e,e);
162         }
163 
164         return properties;
165     }*/
166 
167     public String getParameter(String name){
168         ParameterKey parameterKey = ParameterKey.create(OLEConstants.APPL_ID,OLEConstants.SELECT_NMSPC,OLEConstants.SELECT_CMPNT,name);
169         Parameter parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(parameterKey);
170         return parameter!=null?parameter.getValue():null;
171     }
172 
173 }