View Javadoc
1   package fr.ifremer.coser.bean;
2   
3   /*
4    * #%L
5    * Coser :: Business
6    * %%
7    * Copyright (C) 2010 - 2014 Ifremer, Codelutin, Chemit Tony
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.base.Charsets;
26  import com.google.common.base.Function;
27  import com.google.common.base.Preconditions;
28  import com.google.common.io.Files;
29  import org.apache.commons.io.IOUtils;
30  import org.apache.commons.io.filefilter.RegexFileFilter;
31  import org.apache.commons.lang3.StringUtils;
32  
33  import java.io.File;
34  import java.io.FilenameFilter;
35  import java.io.IOException;
36  import java.io.Reader;
37  import java.io.Serializable;
38  import java.io.Writer;
39  import java.util.Date;
40  import java.util.Properties;
41  
42  /**
43   * Represent the storage of the EchoBase project published to CoserWeb application.
44   * <p/>
45   * Created on 3/4/14.
46   *
47   * @author Tony Chemit <chemit@codelutin.com>
48   * @since 1.5
49   */
50  public class EchoBaseProject implements Serializable {
51  
52      private static final long serialVersionUID = 1L;
53  
54      public static final String METADATA_FILE = "project.echobase";
55  
56      /**
57       * Base directory of the project.
58       */
59      protected final File basedir;
60  
61      protected String author;
62  
63      protected String facadeName;
64  
65      protected String zoneName;
66  
67      protected String surveyName;
68  
69      protected String comment;
70  
71      protected Date creationDate;
72  
73      /** Result publiable. */
74      protected boolean publiableResult;
75  
76      public EchoBaseProject(File basedir) {
77          Preconditions.checkNotNull(basedir);
78          this.basedir = basedir;
79      }
80  
81      public File getBasedir() {
82          return basedir;
83      }
84  
85      public String getName() {
86          return getBasedir().getName();
87      }
88  
89      public String getAuthor() {
90          return author;
91      }
92  
93      public void setAuthor(String author) {
94          this.author = author;
95      }
96  
97      public String getFacadeName() {
98          return facadeName;
99      }
100 
101     public void setFacadeName(String facadeName) {
102         this.facadeName = facadeName;
103     }
104 
105     public String getZoneName() {
106         return zoneName;
107     }
108 
109     public void setZoneName(String zoneName) {
110         this.zoneName = zoneName;
111     }
112 
113     public String getSurveyName() {
114         return surveyName;
115     }
116 
117     public void setSurveyName(String surveyName) {
118         this.surveyName = surveyName;
119     }
120 
121     public String getComment() {
122         return comment;
123     }
124 
125     public void setComment(String comment) {
126         this.comment = comment;
127     }
128 
129     public Date getCreationDate() {
130         return creationDate;
131     }
132 
133     public void setCreationDate(Date creationDate) {
134         this.creationDate = creationDate;
135     }
136 
137     public boolean isPubliableResult() {
138         return publiableResult;
139     }
140 
141     public void setPubliableResult(boolean publiableResult) {
142         this.publiableResult = publiableResult;
143     }
144 
145     public File getSpeciesDefinitionFile() {
146         return new File(basedir, "species.csv");
147     }
148 
149     public File getPopulationIndicatorsFile() {
150         return new File(basedir, "populationIndicators.csv");
151     }
152 
153     public File getCommunityIndicatorsFile() {
154         return new File(basedir, "communityIndicators.csv");
155     }
156 
157     public File getMapsDirectory() {
158         return new File(basedir, "maps");
159     }
160 
161     public File getRawDataDirectory() {
162         return new File(basedir, "source");
163     }
164 
165     /**
166      * Load a project from his basedir.
167      *
168      * @throws IOException if could not read the meta data file
169      */
170     public void load() throws IOException {
171         File metadataFile = getMetaDataFile();
172         Reader reader = Files.newReader(metadataFile, Charsets.UTF_8);
173         try {
174             Properties p = new Properties();
175             p.load(reader);
176             reader.close();
177             fromProperties(p);
178         } finally {
179             IOUtils.closeQuietly(reader);
180         }
181     }
182 
183     /**
184      * Save the project.
185      *
186      * @throws IOException if could not write the meta-data file
187      */
188     public void save() throws IOException {
189 
190         File metadataFile = getMetaDataFile();
191         Writer writer = Files.newWriter(metadataFile, Charsets.UTF_8);
192         try {
193             Properties p = toProperties();
194             p.store(writer, "Saved by " + EchoBaseProject.class.getName());
195             writer.close();
196         } finally {
197             IOUtils.closeQuietly(writer);
198         }
199     }
200 
201     @Override
202     public boolean equals(Object o) {
203         if (this == o) return true;
204         if (!(o instanceof EchoBaseProject)) return false;
205         EchoBaseProject that = (EchoBaseProject) o;
206         return basedir.equals(that.basedir);
207     }
208 
209     @Override
210     public int hashCode() {
211         return basedir.hashCode();
212     }
213 
214     protected File getMetaDataFile() {
215         return new File(basedir, METADATA_FILE);
216     }
217 
218     protected Properties toProperties() {
219         Properties props = new Properties();
220         if (author != null) {
221             props.setProperty("project.author", author);
222         }
223         if (facadeName != null) {
224             props.setProperty("project.facadeName", facadeName);
225         }
226         if (zoneName != null) {
227             props.setProperty("project.zoneName", zoneName);
228         }
229         if (surveyName != null) {
230             props.setProperty("project.surveyName", surveyName);
231         }
232         if (comment != null) {
233             props.setProperty("project.comment", comment);
234         }
235         if (creationDate != null) {
236             props.setProperty("project.creationDate", String.valueOf(creationDate.getTime()));
237         }
238         props.setProperty("project.publiableResult", String.valueOf(publiableResult));
239 
240         return props;
241     }
242 
243     protected void fromProperties(Properties props) {
244         if (props.containsKey("project.author")) {
245             setAuthor(props.getProperty("project.author"));
246         }
247         if (props.containsKey("project.facadeName")) {
248             setFacadeName(props.getProperty("project.facadeName"));
249         }
250         if (props.containsKey("project.zoneName")) {
251             setZoneName(props.getProperty("project.zoneName"));
252         }
253         if (props.containsKey("project.surveyName")) {
254             setSurveyName(props.getProperty("project.surveyName"));
255         }
256         if (props.containsKey("project.comment")) {
257             setComment(props.getProperty("project.comment"));
258         }
259         if (props.containsKey("project.creationDate")) {
260             Date date = new Date(Long.parseLong(props.getProperty("project.creationDate")));
261             setCreationDate(date);
262         }
263         if (props.containsKey("project.publiableResult")) {
264             boolean v = Boolean.valueOf(props.getProperty("project.publiableResult"));
265             setPubliableResult(v);
266         }
267     }
268 
269     public static FilenameFilter newMapSpeciesFilenameFilter(String surveyName) {
270         RegexFileFilter result = new RegexFileFilter("^" + surveyName + "_[^_]+?\\.png");
271         return result;
272     }
273 
274     public static Function<File, String> newMapFileToSpeciesCode(String surveyName) {
275         return new MapFileToSpeciesCodeFunction(surveyName);
276     }
277 
278     public static Function<String, String> newSpeciesCodeToMapFileName(String surveyName) {
279         return new SpeciesCodeToMapFileNameFunction(surveyName);
280     }
281 
282     protected static class MapFileToSpeciesCodeFunction implements Function<File, String> {
283 
284         private final String zoneName;
285 
286         public MapFileToSpeciesCodeFunction(String zoneName) {
287             this.zoneName = zoneName;
288         }
289 
290         @Override
291         public String apply(File input) {
292             String fileName = input.getName();
293             String species = fileName.substring(zoneName.length() + 1);
294             species = StringUtils.substringBefore(species, ".");
295             return species;
296         }
297     }
298 
299     protected static class SpeciesCodeToMapFileNameFunction implements Function<String, String> {
300 
301         private final String zoneName;
302 
303         public SpeciesCodeToMapFileNameFunction(String zoneName) {
304             this.zoneName = zoneName;
305         }
306 
307         @Override
308         public String apply(String input) {
309 
310             String fileName = zoneName + "_" + input + ".png";
311             return fileName;
312         }
313     }
314 }