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.service.impl;
17  
18  import org.kuali.ole.select.OleSelectConstant;
19  import org.kuali.ole.select.service.FileProcessingService;
20  import org.kuali.ole.sys.context.SpringContext;
21  import org.kuali.rice.core.api.config.property.ConfigurationService;
22  
23  import java.io.*;
24  import java.util.HashMap;
25  
26  public class FileProcessingServiceImpl implements FileProcessingService {
27      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FileProcessingServiceImpl.class);
28  
29      @Override
30      public HashMap<String, String> getFileContentAndDeleteFile(HashMap<String, String> dataMap) throws Exception {
31          StringBuilder strigObj = new StringBuilder();
32          String xmlString = null;
33          String filePath = dataMap.get(OleSelectConstant.FILEPATH) + dataMap.get(OleSelectConstant.FILENAME) + OleSelectConstant.XML_FILE_TYPE_EXTENSION;
34          boolean isFileExist = true;
35          boolean isBibEdit = false;
36          if (isCreateFileExist(dataMap)) {
37              isFileExist = true;
38          } else if (isEditFileExist(dataMap)) {
39              isFileExist = true;
40              isBibEdit = true;
41              filePath = dataMap.get(OleSelectConstant.FILEPATH) + dataMap.get(OleSelectConstant.FILENAME) + "edit" + OleSelectConstant.XML_FILE_TYPE_EXTENSION;
42          }
43  
44          if (LOG.isInfoEnabled())
45              LOG.info("FileProcessingServiceImpl.getFileContentAndDeleteFile filePath--------->" + filePath);
46          FileInputStream fstream = null;
47          DataInputStream in = null;
48          BufferedReader br = null;
49          try {
50              if (isFileExist) {
51                  fstream = new FileInputStream(filePath);
52                  in = new DataInputStream(fstream);
53                  br = new BufferedReader(new InputStreamReader(in));
54                  String strLine;
55                  while ((strLine = br.readLine()) != null) {
56                      strigObj.append(strLine);
57                  }
58                  xmlString = strigObj.toString();
59              }
60          } catch (FileNotFoundException fnfe) {
61              throw new FileNotFoundException("The file mentioned in particular path is not available");
62          } catch (Exception e) {
63              LOG.error("Exception while getting the file content of the file...",e);
64              throw new RuntimeException(e);
65          } finally {
66              if (in != null && br != null) {
67                  fstream.close();
68                  in.close();
69                  br.close();
70              }
71          }
72          File file = new File(filePath);
73          file.delete();
74          dataMap.put(OleSelectConstant.XML_FILE_CONTENT, xmlString);
75          dataMap.put(OleSelectConstant.IS_BIB_EDIT, String.valueOf(isBibEdit));
76          return dataMap;
77      }
78  
79      public String getMarcXMLFileDirLocation() throws Exception {
80          ConfigurationService kualiConfigurationService = SpringContext.getBean(ConfigurationService.class);
81          String externaleDirectory = kualiConfigurationService.getPropertyValueAsString(OleSelectConstant.BIBMARCXML_DIR);
82          return externaleDirectory;
83      }
84  
85      public boolean isCreateFileExist(HashMap<String, String> dataMap) throws Exception {
86          String filePath = dataMap.get(OleSelectConstant.FILEPATH) + dataMap.get(OleSelectConstant.FILENAME) + OleSelectConstant.XML_FILE_TYPE_EXTENSION;
87          File file = new File(filePath);
88          boolean isFileExist = true;
89          if (!file.exists()) {
90              isFileExist = false;
91          }
92          return isFileExist;
93      }
94  
95      public boolean isEditFileExist(HashMap<String, String> dataMap) throws Exception {
96          String filePath = dataMap.get(OleSelectConstant.FILEPATH) + dataMap.get(OleSelectConstant.FILENAME) + OleSelectConstant.XML_FILE_TYPE_EXTENSION;
97          File file = new File(filePath);
98          boolean isFileExist = true;
99          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 }