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  /**
24   * 
25   */
26  package com.gisgraphy.domain.repository;
27  
28  import java.io.BufferedOutputStream;
29  import java.io.BufferedReader;
30  import java.io.File;
31  import java.io.FileOutputStream;
32  import java.io.FileReader;
33  import java.io.IOException;
34  import java.io.OutputStream;
35  import java.io.OutputStreamWriter;
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  import org.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  import org.springframework.beans.factory.annotation.Required;
42  import org.springframework.stereotype.Repository;
43  
44  import com.gisgraphy.domain.geoloc.importer.ImporterConfig;
45  import com.gisgraphy.domain.valueobject.Constants;
46  import com.gisgraphy.domain.valueobject.ImporterStatus;
47  import com.gisgraphy.domain.valueobject.ImporterStatusDto;
48  
49  /**
50   * A data access object for {@link ImporterStatus}
51   * 
52   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
53   */
54  @Repository
55  public class ImporterStatusListDao implements IImporterStatusListDao {
56  
57      public static final String IMPORTER_STATUS_LIST_FILENAME = "importerStatusList";
58  
59      public static final String IMPORTER_METADATA_RELATIVE_PATH = "IMPORTER-METADATA-DO_NOT_REMOVE";
60  
61      ImporterConfig importerConfig;
62  
63      private Logger logger = LoggerFactory
64  	    .getLogger(ImporterStatusListDao.class);
65  
66      /*
67       * (non-Javadoc)
68       * 
69       * @see com.gisgraphy.domain.repository.IImporterStatusListDao#getFilePath()
70       */
71      public String getSavedFilePath() {
72  	String dirpath = importerConfig.getGeonamesDir()
73  		+ IMPORTER_METADATA_RELATIVE_PATH + File.separator;
74  	File directory = new File(dirpath);
75  	if (!directory.exists()) {
76  	    if (!directory.mkdir()) {
77  		throw new RuntimeException(
78  			"Can not create ImporterMetadataDirectory");
79  	    }
80  	}
81  	return dirpath + IMPORTER_STATUS_LIST_FILENAME;
82      }
83  
84      /*
85       * (non-Javadoc)
86       * 
87       * @see com.gisgraphy.domain.repository.IImporterStatusListDao#saveOrUpdate(java.util.List)
88       */
89      public List<ImporterStatusDto> saveOrUpdate(
90  	    List<ImporterStatusDto> importerStatusDtoList) {
91  	OutputStreamWriter writer = getWriter();
92  	try {
93  	    for (ImporterStatusDto dto : importerStatusDtoList) {
94  		writer.append(dto.toCSV());
95  	    }
96  	} catch (IOException e) {
97  	    throw new RuntimeException(e);
98  	} finally {
99  	    if (writer != null) {
100 		try {
101 		    writer.flush();
102 		    writer.close();
103 		} catch (IOException e) {
104 		    logger.error("error during flush or close");
105 		}
106 	    }
107 	}
108 
109 	return importerStatusDtoList;
110     }
111 
112     private OutputStreamWriter getWriter() {
113 	OutputStreamWriter writer = null;
114 	OutputStream outputStream = null;
115 	try {
116 
117 	    File file = new File(getSavedFilePath());
118 	    if (file.exists()) {
119 		file.delete();
120 	    }
121 	    if (!file.createNewFile()) {
122 		throw new RuntimeException("can not save to file "
123 			+ getSavedFilePath());
124 	    }
125 	    outputStream = new BufferedOutputStream(new FileOutputStream(file));
126 	    writer = new OutputStreamWriter(outputStream, Constants.CHARSET);
127 
128 	} catch (Exception e) {
129 	    throw new RuntimeException(e);
130 	}
131 	return writer;
132     }
133 
134     /*
135      * (non-Javadoc)
136      * 
137      * @see com.gisgraphy.domain.repository.IImporterStatusListDao#get()
138      */
139     public List<ImporterStatusDto> get() {
140 	List<ImporterStatusDto> result = new ArrayList<ImporterStatusDto>();
141 	FileReader fileReader = null;
142 	BufferedReader bufferReader = null;
143 	try {
144 	    File file = new File(getSavedFilePath());
145 	    if (!file.exists()){
146 		logger.warn("can not find file for importer metadata : "+file.getAbsolutePath());
147 		return new ArrayList<ImporterStatusDto>();
148 	    }
149 	    fileReader = new FileReader(file);
150 	    bufferReader = new BufferedReader(fileReader);
151 	    String line = bufferReader.readLine();
152 	    ImporterStatusDto importerStatusDto = null;
153 	    while (line != null) {
154 		importerStatusDto = new ImporterStatusDto(line);
155 		result.add(importerStatusDto);
156 		line = bufferReader.readLine();
157 	    }
158 	} catch (Exception e) {
159 	    logger.error(e.getMessage());
160 	    return new ArrayList<ImporterStatusDto>();
161 	} finally {
162 	    if (bufferReader != null) {
163 		try {
164 		    bufferReader.close();
165 		} catch (IOException e) {
166 		    logger.error("error during flush or close : "
167 			    + e.getMessage());
168 		}
169 	    }
170 	}
171 	return result;
172     }
173 
174     /*
175      * (non-Javadoc)
176      * 
177      * @see com.gisgraphy.domain.repository.IImporterStatusListDao#delete()
178      */
179     public boolean delete() {
180 	File file = new File(getSavedFilePath());
181 	if (file.exists()) {
182 	    return file.delete();
183 	}
184 	return true;
185     }
186 
187     /**
188      * @param importerConfig
189      *                the importerConfig to set
190      */
191     @Required
192     public void setImporterConfig(ImporterConfig importerConfig) {
193 	this.importerConfig = importerConfig;
194     }
195 }