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 */ 016package org.kuali.ole.select.batch.service.impl; 017 018import org.kuali.ole.OLEConstants; 019import org.kuali.ole.module.purap.PurapParameterConstants; 020import org.kuali.ole.module.purap.document.RequisitionDocument; 021import org.kuali.ole.select.OleSelectConstant; 022import org.kuali.ole.select.batch.service.OleRequisitionCreateDocumentService; 023import org.kuali.ole.select.batch.service.RequisitionLoadTransactionsService; 024import org.kuali.ole.select.businessobject.BibInfoBean; 025import org.kuali.ole.select.service.impl.BuildVendorBibInfoBean; 026import org.kuali.ole.sys.batch.service.BatchInputFileService; 027import org.kuali.ole.sys.context.SpringContext; 028import org.kuali.rice.core.api.config.property.ConfigurationService; 029import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator; 030import org.kuali.rice.coreservice.api.parameter.Parameter; 031import org.kuali.rice.coreservice.api.parameter.ParameterKey; 032import org.kuali.rice.coreservice.framework.parameter.ParameterService; 033import org.kuali.rice.kew.api.exception.WorkflowException; 034import org.kuali.rice.krad.UserSession; 035import org.kuali.rice.krad.util.GlobalVariables; 036import org.springframework.beans.factory.InitializingBean; 037 038import java.util.ArrayList; 039import java.util.List; 040import java.util.Properties; 041 042public class RequisitionLoadTransactionsServiceImpl implements RequisitionLoadTransactionsService, InitializingBean { 043 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RequisitionLoadTransactionsServiceImpl.class); 044 045 protected BatchInputFileService batchInputFileService; 046 protected OleRequisitionCreateDocumentService oleRequisitionCreateDocumentService; 047 protected BuildVendorBibInfoBean buildVendorBibInfoBean; 048 protected Properties properties = null; 049 protected ConfigurationService kualiConfigurationService; 050 051 protected String determinePurchaseOrderTransmissionMethod() { 052 053 return SpringContext.getBean(ParameterService.class).getParameterValueAsString(RequisitionDocument.class, PurapParameterConstants.PURAP_DEFAULT_PO_TRANSMISSION_CODE); 054 } 055 056 public BatchInputFileService getBatchInputFileService() { 057 return batchInputFileService; 058 } 059 060 public void setBatchInputFileService(BatchInputFileService batchInputFileService) { 061 this.batchInputFileService = batchInputFileService; 062 } 063 064 public OleRequisitionCreateDocumentService getOleRequisitionCreateDocumentService() { 065 return oleRequisitionCreateDocumentService; 066 } 067 068 069 public void setOleRequisitionCreateDocumentService(OleRequisitionCreateDocumentService oleRequisitionCreateDocumentService) { 070 this.oleRequisitionCreateDocumentService = oleRequisitionCreateDocumentService; 071 } 072 073 public BuildVendorBibInfoBean getBuildVendorBibInfoBean() { 074 return buildVendorBibInfoBean; 075 } 076 077 public void setBuildVendorBibInfoBean(BuildVendorBibInfoBean buildVendorBibInfoBean) { 078 this.buildVendorBibInfoBean = buildVendorBibInfoBean; 079 } 080 081 public ConfigurationService getConfigurationService() { 082 return kualiConfigurationService; 083 } 084 085 public void setConfigurationService(ConfigurationService kualiConfigurationService) { 086 this.kualiConfigurationService = kualiConfigurationService; 087 } 088 089 @Override 090 public void afterPropertiesSet() throws Exception { 091 //properties = loadPropertiesFromClassPath("org/kuali/ole/select/batch/service/impl/bibinfo.properties"); 092 } 093 094 /** 095 * To load the XML File for the vendor. 096 * 097 * @param fileName String 098 * @return boolean 099 */ 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}