View Javadoc
1   package fr.ifremer.coser.bean;
2   
3   /*
4    * #%L
5    * Coser :: Business
6    * %%
7    * Copyright (C) 2010 - 2014 Ifremer, Codelutin
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.collect.Maps;
26  import fr.ifremer.coser.storage.DataStorage;
27  import fr.ifremer.coser.storage.DataStorages;
28  import org.apache.commons.lang3.StringUtils;
29  
30  import java.io.File;
31  import java.io.Serializable;
32  import java.util.Collection;
33  import java.util.Collections;
34  import java.util.Iterator;
35  import java.util.Map;
36  
37  /**
38   * Created on 3/11/14.
39   *
40   * @author Tony Chemit <chemit@codelutin.com>
41   * @since 1.5
42   */
43  public class SpeciesMap implements Serializable {
44  
45      private static final long serialVersionUID = 1L;
46  
47      /**
48       * Species' storage.
49       */
50      protected final DataStorage storage;
51  
52      /**
53       * Cache of species definition.
54       */
55      protected Map<String, String> speciesMap;
56  
57  
58      public SpeciesMap(File speciesFile) {
59          storage = DataStorages.load(speciesFile);
60      }
61  
62      public DataStorage getStorage() {
63          return storage;
64      }
65  
66      public Iterator<String[]> iterator(boolean skipFirstLine) {
67          // "C_Perm","NumSys","NivSys","C_VALIDE","L_VALIDE","AA_VALIDE","C_TxP\u00E8re","Taxa"
68          Iterator<String[]> iterator = storage.iterator(skipFirstLine);
69          return iterator;
70      }
71  
72      public Map<String, String> getSpeciesMap() {
73          if (speciesMap == null) {
74              speciesMap = Maps.newTreeMap();
75  
76              // load species file
77              Iterator<String[]> iterator = iterator(true);
78              while (iterator.hasNext()) {
79                  String[] tuple = iterator.next();
80                  String speciesCode = tuple[3];
81                  String speciesLabel = tuple[4] + " " + tuple[5];
82                  speciesMap.put(speciesCode, speciesLabel);
83              }
84              speciesMap = Collections.unmodifiableMap(speciesMap);
85          }
86          return speciesMap;
87      }
88  
89      public String getSpeciesName(String species) {
90          return getSpeciesMap().get(species);
91      }
92  
93      public Map<String, String> getSpeciesSubMap(Collection<String> speciesList) {
94  
95          Map<String, String> result = Maps.newTreeMap();
96  
97          if (speciesList != null) {
98  
99              Map<String, String> map = getSpeciesMap();
100             for (String species : speciesList) {
101                 String speciesLabel = map.get(species);
102                 if (StringUtils.isNotEmpty(speciesLabel)) {
103                     result.put(species, speciesLabel);
104                 }
105             }
106         }
107 
108         return result;
109     }
110 
111     /**
112      * Retourne le nom d'affichage d'une especes pour les rapports.
113      *
114      * @param species species code
115      * @return species display name
116      * @since 1.5
117      */
118     public String getReportDisplayName(String species) {
119         String displayName = null;
120 
121         Iterator<String[]> iterator = iterator(true);
122         while (iterator.hasNext()) {
123             String[] tuple = iterator.next();
124 
125             // "C_Perm","NumSys","NivSys","C_VALIDE","L_VALIDE","AA_VALIDE","C_TxP\u00E8re","Taxa"
126             String speciesCode = tuple[3];
127             if (speciesCode.equals(species)) {
128                 // nom + auteur (sans ajout de parenthese : important)
129                 displayName = tuple[4] + " " + tuple[5];
130                 break;
131             }
132         }
133 
134         return displayName;
135     }
136 }