View Javadoc
1   package fr.ifremer.tutti.service.genericformat;
2   
3   /*
4    * #%L
5    * Tutti :: Service
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import com.google.common.collect.ImmutableMap;
28  import fr.ifremer.tutti.persistence.entities.referential.Species;
29  
30  import java.util.Map;
31  import java.util.TreeMap;
32  
33  /**
34   * Created on 4/17/15.
35   *
36   * @author Tony Chemit - chemit@codelutin.com
37   * @since 4.0-RC3
38   */
39  public class GenericFormatReferentialSpeciesImportResult extends GenericFormatReferentialImportResult<Species, Integer> {
40  
41      private static final long serialVersionUID = 1L;
42  
43      private ImmutableMap<Integer, Integer> referenceTaxonIdTranslationMap;
44  
45      public GenericFormatReferentialSpeciesImportResult(String filename, boolean found) {
46          super(filename, found);
47          this.referenceTaxonIdTranslationMap = ImmutableMap.of();
48      }
49  
50      public Map<Integer, Integer> getReferenceTaxonIdTranslationMap() {
51          return referenceTaxonIdTranslationMap;
52      }
53  
54      public void flushObsoleteReferenceTaxonIds(Map<Integer, Integer> referenceTaxonIdById) {
55  
56          referenceTaxonIdTranslationMap = ImmutableMap.copyOf(referenceTaxonIdById);
57  
58      }
59  
60      public void flushReferenceTaxonIds(Map<Integer, Integer> referenceTaxonIdById) {
61  
62          Map<Integer, Integer> referenceTaxonIdMap = new TreeMap<>();
63  
64          for (Map.Entry<Integer, Integer> entry : referenceTaxonIdById.entrySet()) {
65              Integer oldId = entry.getKey();
66              String originalId = String.valueOf(oldId);
67              Species species = entitiesAdded.get(originalId);
68              if (species == null) {
69                  species = entitiesLinked.get(originalId);
70              }
71  
72              // il s'agit d'un taxon importé, on effectue la correspondance
73              Integer oldReferenceTaxonId = entry.getValue();
74              Integer newReferenceTaxonId = species.getReferenceTaxonId();
75  
76              referenceTaxonIdMap.put(oldReferenceTaxonId, newReferenceTaxonId);
77  
78          }
79  
80          referenceTaxonIdTranslationMap = ImmutableMap
81                  .<Integer, Integer>builder()
82                  .putAll(referenceTaxonIdTranslationMap)
83                  .putAll(referenceTaxonIdMap)
84                  .build();
85  
86      }
87  
88  }