View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.domain.repository;
24  
25  import java.io.BufferedOutputStream;
26  import java.io.BufferedReader;
27  import java.io.File;
28  import java.io.FileOutputStream;
29  import java.io.FileReader;
30  import java.io.IOException;
31  import java.io.OutputStream;
32  import java.io.OutputStreamWriter;
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  import org.springframework.beans.factory.annotation.Required;
39  import org.springframework.stereotype.Repository;
40  
41  import com.gisgraphy.domain.valueobject.Constants;
42  import com.gisgraphy.domain.valueobject.ImporterStatus;
43  import com.gisgraphy.domain.valueobject.ImporterStatusDto;
44  import com.gisgraphy.importer.ImporterConfig;
45  
46  /**
47   * A data access object for {@link ImporterStatus}
48   * 
49   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
50   */
51  @Repository
52  public class ImporterStatusListDao implements IImporterStatusListDao {
53  
54      public static final String IMPORTER_STATUS_LIST_FILENAME = "importerStatusList";
55  
56      ImporterConfig importerConfig;
57  
58      private Logger logger = LoggerFactory
59  	    .getLogger(ImporterStatusListDao.class);
60  
61      /*
62       * (non-Javadoc)
63       * 
64       * @see com.gisgraphy.domain.repository.IImporterStatusListDao#getFilePath()
65       */
66      public String getSavedFilePath() {
67  	String dirpath = importerConfig.createImporterMetadataDirIfItDoesnTExist();
68  	return dirpath + IMPORTER_STATUS_LIST_FILENAME;
69      }
70  
71      /*
72       * (non-Javadoc)
73       * 
74       * @see com.gisgraphy.domain.repository.IImporterStatusListDao#saveOrUpdate(java.util.List)
75       */
76      public List<ImporterStatusDto> saveOrUpdate(
77  	    List<ImporterStatusDto> importerStatusDtoList) {
78  	OutputStreamWriter writer = getWriter();
79  	try {
80  	    for (ImporterStatusDto dto : importerStatusDtoList) {
81  		writer.append(dto.toCSV());
82  	    }
83  	} catch (IOException e) {
84  	    throw new RuntimeException(e);
85  	} finally {
86  	    if (writer != null) {
87  		try {
88  		    writer.flush();
89  		    writer.close();
90  		} catch (IOException e) {
91  		    logger.error("error during flush or close");
92  		}
93  	    }
94  	}
95  
96  	return importerStatusDtoList;
97      }
98  
99      private OutputStreamWriter getWriter() {
100 	OutputStreamWriter writer = null;
101 	OutputStream outputStream = null;
102 	try {
103 
104 	    File file = new File(getSavedFilePath());
105 	    if (file.exists()) {
106 		file.delete();
107 	    }
108 	    if (!file.createNewFile()) {
109 		throw new RuntimeException("can not save to file "
110 			+ getSavedFilePath());
111 	    }
112 	    outputStream = new BufferedOutputStream(new FileOutputStream(file));
113 	    writer = new OutputStreamWriter(outputStream, Constants.CHARSET);
114 
115 	} catch (Exception e) {
116 	    throw new RuntimeException(e);
117 	}
118 	return writer;
119     }
120 
121     /*
122      * (non-Javadoc)
123      * 
124      * @see com.gisgraphy.domain.repository.IImporterStatusListDao#get()
125      */
126     public List<ImporterStatusDto> get() {
127 	List<ImporterStatusDto> result = new ArrayList<ImporterStatusDto>();
128 	FileReader fileReader = null;
129 	BufferedReader bufferReader = null;
130 	try {
131 	    File file = new File(getSavedFilePath());
132 	    if (!file.exists()){
133 		logger.warn("can not find file for importer metadata : "+file.getAbsolutePath());
134 		return new ArrayList<ImporterStatusDto>();
135 	    }
136 	    fileReader = new FileReader(file);
137 	    bufferReader = new BufferedReader(fileReader);
138 	    String line = bufferReader.readLine();
139 	    ImporterStatusDto importerStatusDto = null;
140 	    while (line != null) {
141 		try {
142 			importerStatusDto = new ImporterStatusDto(line);
143 			result.add(importerStatusDto);
144 		} catch (Exception e) {
145 			logger.error(e.getMessage());
146 		}
147 		line = bufferReader.readLine();
148 	    }
149 	} catch (Exception e) {
150 	    logger.error(e.getMessage(),e);
151 	    return new ArrayList<ImporterStatusDto>();
152 	} finally {
153 	    if (bufferReader != null) {
154 		try {
155 		    bufferReader.close();
156 		} catch (IOException e) {
157 		    logger.error("error during flush or close : "
158 			    + e.getMessage());
159 		}
160 	    }
161 	}
162 	return result;
163     }
164 
165     /*
166      * (non-Javadoc)
167      * 
168      * @see com.gisgraphy.domain.repository.IImporterStatusListDao#delete()
169      */
170     public boolean delete() {
171 	File file = new File(getSavedFilePath());
172 	if (file.exists()) {
173 	    return file.delete();
174 	}
175 	return true;
176     }
177 
178     /**
179      * @param importerConfig
180      *                the importerConfig to set
181      */
182     @Required
183     public void setImporterConfig(ImporterConfig importerConfig) {
184 	this.importerConfig = importerConfig;
185     }
186 }