1 package fr.ifremer.coser.result.repository.echobase;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import com.google.common.base.Function;
26 import com.google.common.base.Preconditions;
27 import com.google.common.base.Predicate;
28 import com.google.common.collect.Lists;
29 import com.google.common.collect.Sets;
30 import fr.ifremer.coser.bean.EchoBaseProject;
31 import fr.ifremer.coser.bean.SpeciesMap;
32 import fr.ifremer.coser.result.ResultType;
33 import fr.ifremer.coser.result.repository.ResultRepository;
34 import fr.ifremer.coser.result.request.CoserRequestExtractTypeListAware;
35 import fr.ifremer.coser.result.request.CoserRequestFacadeAware;
36 import fr.ifremer.coser.result.request.CoserRequestRepositoryResultTypeAware;
37 import fr.ifremer.coser.result.request.CoserRequestRepositoryTypeAware;
38 import fr.ifremer.coser.result.request.CoserRequestZoneAware;
39 import fr.ifremer.coser.result.request.CoserRequestZoneListAware;
40 import fr.ifremer.coser.storage.DataStorage;
41 import fr.ifremer.coser.storage.DataStorages;
42 import fr.ifremer.coser.util.DataType;
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45
46 import java.io.File;
47 import java.io.FilenameFilter;
48 import java.util.List;
49 import java.util.Map;
50 import java.util.Set;
51
52
53
54
55
56
57
58 public class EchoBaseResultRepository implements ResultRepository {
59
60
61 private static final Log log = LogFactory.getLog(EchoBaseResultRepository.class);
62
63
64
65
66 protected final EchoBaseProject project;
67
68
69
70
71 protected final Function<File, String> mapFileToSpeciesCode;
72
73
74
75
76 protected final Function<String, String> speciesCodeToMapFile;
77
78
79
80
81 protected final FilenameFilter mapSpeciesFilenameFilter;
82
83
84
85
86 protected SpeciesMap speciesMap;
87
88
89
90
91 protected final String id;
92
93 public EchoBaseResultRepository(EchoBaseProject project) {
94 Preconditions.checkNotNull(project);
95 this.project = project;
96
97 String surveyName = project.getSurveyName();
98 this.mapFileToSpeciesCode = EchoBaseProject.newMapFileToSpeciesCode(surveyName);
99 this.speciesCodeToMapFile = EchoBaseProject.newSpeciesCodeToMapFileName(surveyName);
100 this.mapSpeciesFilenameFilter = EchoBaseProject.newMapSpeciesFilenameFilter(surveyName);
101 this.id = EchoBaseResultRepositoryType.ID + "::" + project.getBasedir();
102
103 if (log.isInfoEnabled()) {
104 log.info("New result repository: " + id);
105 }
106 }
107
108
109
110
111
112 @Override
113 public String getId() {
114 return id;
115 }
116
117 @Override
118 public EchoBaseResultRepositoryType getResultRepositoryType() {
119 return EchoBaseResultRepositoryType.INSTANCE;
120 }
121
122 @Override
123 public File getBasedir() {
124 return project.getBasedir();
125 }
126
127 @Override
128 public String getProjectName() {
129 return project.getName();
130 }
131
132 @Override
133 public String getSurveyName() {
134 return project.getSurveyName();
135 }
136
137 @Override
138 public String getZone() {
139 return project.getZoneName();
140 }
141
142 @Override
143 public boolean isMapsResult() {
144 return true;
145 }
146
147 @Override
148 public boolean isIndicatorsResult() {
149 return true;
150 }
151
152 @Override
153 public boolean isDataResult() {
154 return true;
155 }
156
157 @Override
158 public boolean isPubliableResult() {
159 return project.isPubliableResult();
160 }
161
162 public EchoBaseProject getProject() {
163 return project;
164 }
165
166
167
168
169
170 public boolean matchFacade(CoserRequestFacadeAware request) {
171 return getProject().getFacadeName().equals(request.getFacade());
172 }
173
174 public boolean matchZone(CoserRequestZoneAware request) {
175 return getZone().equals(request.getZone());
176 }
177
178 public boolean matchZone(CoserRequestZoneListAware request) {
179 List<String> zoneList = request.getZoneList();
180 boolean result = zoneList.contains(getZone());
181 return result;
182 }
183
184 public boolean matchRepositoryType(CoserRequestRepositoryTypeAware request) {
185 boolean result = request.getRepositoryType().equals(EchoBaseResultRepositoryType.ID);
186 return result;
187 }
188
189 public boolean matchResultType(CoserRequestRepositoryResultTypeAware request) {
190 ResultType resultType = request.getResultType();
191 boolean result = ResultType.MAP_AND_INDICATOR == resultType;
192 return result;
193 }
194
195 public boolean matchExtractTypeList(CoserRequestExtractTypeListAware request) {
196 boolean result = false;
197 List<DataType> extractTypeList = request.getExtractTypeList();
198 if (extractTypeList.contains(DataType.MAP)) {
199 result = isMapsResult();
200 }
201
202 if (!result && (extractTypeList.contains(DataType.POPULATION) ||
203 extractTypeList.contains(DataType.COMMUNITY))) {
204 result = isIndicatorsResult();
205 }
206 if (!result && extractTypeList.contains(DataType.SOURCE)) {
207 result = isDataResult();
208 }
209 return result;
210 }
211
212 public boolean matchCommunity(Predicate<String[]> predicate) {
213 Preconditions.checkNotNull(predicate);
214
215 DataStorage storage = getCommunityIndicatorStorage();
216 boolean result = DataStorages.match(storage, predicate, true);
217
218 return result;
219 }
220
221 public boolean matchPopulation(Predicate<String[]> predicate) {
222 Preconditions.checkNotNull(predicate);
223
224 DataStorage storage = getPopulationIndicatorStorage();
225 boolean result = DataStorages.match(storage, predicate, true);
226
227 return result;
228 }
229
230
231
232
233
234 public File getMapSpeciesFile(String species) {
235 String fileName = speciesCodeToMapFile.apply(species);
236 File file = fileName == null ? null : new File(project.getMapsDirectory(), fileName);
237 return file;
238 }
239
240 public Map<String, String> getMapSpecies() {
241 Set<String> speciesList = Sets.newHashSet();
242 File[] files = project.getMapsDirectory().listFiles(mapSpeciesFilenameFilter);
243 if (files != null) {
244 List<String> transform = Lists.transform(Lists.newArrayList(files), mapFileToSpeciesCode);
245 speciesList.addAll(transform);
246 }
247 Map<String, String> result = getSpeciesMap().getSpeciesSubMap(speciesList);
248 return result;
249 }
250
251
252
253
254
255 public SpeciesMap getSpeciesMap() {
256 if (speciesMap == null) {
257 File file = project.getSpeciesDefinitionFile();
258 speciesMap = new SpeciesMap(file);
259 }
260 return speciesMap;
261 }
262
263
264
265
266
267 public DataStorage getPopulationIndicatorStorage() {
268 File file = project.getPopulationIndicatorsFile();
269
270 DataStorage result = DataStorages.load(file);
271 return result;
272 }
273
274 public DataStorage getCommunityIndicatorStorage() {
275 File file = project.getCommunityIndicatorsFile();
276
277 DataStorage result = DataStorages.load(file);
278 return result;
279 }
280
281 }