1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
51
52
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
68
69
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
86
87
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
136
137
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
176
177
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
189
190
191 @Required
192 public void setImporterConfig(ImporterConfig importerConfig) {
193 this.importerConfig = importerConfig;
194 }
195 }