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.service.impl; 017 018import org.kuali.ole.select.OleSelectConstant; 019import org.kuali.ole.select.service.FileProcessingService; 020import org.kuali.ole.sys.context.SpringContext; 021import org.kuali.rice.core.api.config.property.ConfigurationService; 022 023import java.io.*; 024import java.util.HashMap; 025 026public class FileProcessingServiceImpl implements FileProcessingService { 027 protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FileProcessingServiceImpl.class); 028 029 @Override 030 public HashMap<String, String> getFileContentAndDeleteFile(HashMap<String, String> dataMap) throws Exception { 031 StringBuilder strigObj = new StringBuilder(); 032 String xmlString = null; 033 String filePath = dataMap.get(OleSelectConstant.FILEPATH) + dataMap.get(OleSelectConstant.FILENAME) + OleSelectConstant.XML_FILE_TYPE_EXTENSION; 034 boolean isFileExist = true; 035 boolean isBibEdit = false; 036 if (isCreateFileExist(dataMap)) { 037 isFileExist = true; 038 } else if (isEditFileExist(dataMap)) { 039 isFileExist = true; 040 isBibEdit = true; 041 filePath = dataMap.get(OleSelectConstant.FILEPATH) + dataMap.get(OleSelectConstant.FILENAME) + "edit" + OleSelectConstant.XML_FILE_TYPE_EXTENSION; 042 } 043 044 if (LOG.isInfoEnabled()) 045 LOG.info("FileProcessingServiceImpl.getFileContentAndDeleteFile filePath--------->" + filePath); 046 FileInputStream fstream = null; 047 DataInputStream in = null; 048 BufferedReader br = null; 049 try { 050 if (isFileExist) { 051 fstream = new FileInputStream(filePath); 052 in = new DataInputStream(fstream); 053 br = new BufferedReader(new InputStreamReader(in)); 054 String strLine; 055 while ((strLine = br.readLine()) != null) { 056 strigObj.append(strLine); 057 } 058 xmlString = strigObj.toString(); 059 } 060 } catch (FileNotFoundException fnfe) { 061 throw new FileNotFoundException("The file mentioned in particular path is not available"); 062 } catch (Exception e) { 063 LOG.error("Exception while getting the file content of the file...",e); 064 throw new RuntimeException(e); 065 } finally { 066 if (in != null && br != null) { 067 fstream.close(); 068 in.close(); 069 br.close(); 070 } 071 } 072 File file = new File(filePath); 073 file.delete(); 074 dataMap.put(OleSelectConstant.XML_FILE_CONTENT, xmlString); 075 dataMap.put(OleSelectConstant.IS_BIB_EDIT, String.valueOf(isBibEdit)); 076 return dataMap; 077 } 078 079 public String getMarcXMLFileDirLocation() throws Exception { 080 ConfigurationService kualiConfigurationService = SpringContext.getBean(ConfigurationService.class); 081 String externaleDirectory = kualiConfigurationService.getPropertyValueAsString(OleSelectConstant.BIBMARCXML_DIR); 082 return externaleDirectory; 083 } 084 085 public boolean isCreateFileExist(HashMap<String, String> dataMap) throws Exception { 086 String filePath = dataMap.get(OleSelectConstant.FILEPATH) + dataMap.get(OleSelectConstant.FILENAME) + OleSelectConstant.XML_FILE_TYPE_EXTENSION; 087 File file = new File(filePath); 088 boolean isFileExist = true; 089 if (!file.exists()) { 090 isFileExist = false; 091 } 092 return isFileExist; 093 } 094 095 public boolean isEditFileExist(HashMap<String, String> dataMap) throws Exception { 096 String filePath = dataMap.get(OleSelectConstant.FILEPATH) + dataMap.get(OleSelectConstant.FILENAME) + OleSelectConstant.XML_FILE_TYPE_EXTENSION; 097 File file = new File(filePath); 098 boolean isFileExist = true; 099 if (!file.exists()) { 100 filePath = dataMap.get(OleSelectConstant.FILEPATH) + dataMap.get(OleSelectConstant.FILENAME) + "edit" + OleSelectConstant.XML_FILE_TYPE_EXTENSION; 101 file = new File(filePath); 102 if (file.exists()) { 103 isFileExist = true; 104 } else { 105 isFileExist = false; 106 } 107 } 108 return isFileExist; 109 } 110 111}