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.base.Function;
28  import com.google.common.base.Joiner;
29  import fr.ifremer.tutti.persistence.entities.referential.TuttiReferentialEntity;
30  import fr.ifremer.tutti.service.referential.ReferentialImportRequest;
31  import fr.ifremer.tutti.service.referential.ReferentialImportResult;
32  
33  import java.util.ArrayList;
34  import java.util.Collection;
35  import java.util.Collections;
36  import java.util.List;
37  import java.util.Map;
38  import java.util.Set;
39  import java.util.TreeMap;
40  
41  /**
42   * Created on 2/15/15.
43   *
44   * @author Tony Chemit - chemit@codelutin.com
45   * @since 3.14
46   */
47  public class GenericFormatReferentialImportResult<E extends TuttiReferentialEntity, K extends Comparable<K>> extends GenericFormatCsvFileResult {
48  
49      private static final long serialVersionUID = 1L;
50  
51      protected Map<String, E> entitiesAdded;
52  
53      protected Map<String, E> entitiesLinked;
54  
55      private Map<String, String> idTranslationMap;
56  
57      public GenericFormatReferentialImportResult(String filename, boolean found) {
58          super(filename, false, found);
59          this.entitiesAdded = Collections.emptyMap();
60          this.entitiesLinked = Collections.emptyMap();
61          this.idTranslationMap = Collections.emptyMap();
62      }
63  
64      public void flushResult(ReferentialImportRequest<E, K> importRequest, ReferentialImportResult<E> importResult) {
65  
66          Map<String, String> translationMap = new TreeMap<>();
67  
68          Function<E, K> entityToIdFunction = importRequest.getEntityToIdFunction();
69  
70          int nbRefAdded = importResult.getNbRefAdded();
71          List<E> toAdds = importRequest.getEntitiesToAdd();
72          List<E> addeds = importResult.getRefAdded();
73          this.entitiesAdded = merge(entityToIdFunction, nbRefAdded, toAdds, addeds, translationMap);
74  
75          int nbRefLinked = importResult.getNbRefLinked();
76          List<E> toLinks = importRequest.getEntitiesToLink();
77          List<E> linkeds = importResult.getRefLinked();
78          this.entitiesLinked = merge(entityToIdFunction, nbRefLinked, toLinks, linkeds, translationMap);
79  
80          this.idTranslationMap = Collections.unmodifiableMap(translationMap);
81  
82      }
83  
84      public Set<Map.Entry<String, E>> getEntitiesAddedEntries() {
85          return entitiesAdded.entrySet();
86      }
87  
88      public Collection<E> getEntitiesAdded() {
89          return entitiesAdded.values();
90      }
91  
92      public Set<Map.Entry<String, E>> getEntitiesLinkedEntries() {
93          return entitiesLinked.entrySet();
94      }
95  
96      public Map<String, String> getIdTranslationMap() {
97          return idTranslationMap;
98      }
99  
100     protected Map<String, E> merge(Function<E, K> entityToIdFunction,
101                                    int nbEntities,
102                                    List<E> request,
103                                    List<E> result,
104                                    Map<String, String> translationMap) {
105 
106         Map<String, E> mergeMap = new TreeMap<>();
107 
108         for (int index = 0; index < nbEntities; index++) {
109 
110             E toAdd = request.get(index);
111             K toAddId = entityToIdFunction.apply(toAdd);
112             String originalId = String.valueOf(toAddId);
113 
114             E added = result.get(index);
115             K addedId = entityToIdFunction.apply(added);
116             String targetId = String.valueOf(addedId);
117 
118             mergeMap.put(originalId, added);
119             translationMap.put(originalId, targetId);
120 
121         }
122 
123         return Collections.unmodifiableMap(mergeMap);
124 
125     }
126 
127     public String getReport() {
128 
129         StringBuilder builder = new StringBuilder();
130 
131         Set<Map.Entry<Long, Set<String>>> errors = getErrorsEntries();
132 
133         if (!errors.isEmpty()) {
134 
135             builder.append(String.format("%s lines in error:", errors.size()));
136             for (Map.Entry<Long, Set<String>> entry : errors) {
137                 Long lineNumber = entry.getKey();
138                 Set<String> error = entry.getValue();
139                 builder.append(String.format("\nline %s : \n%s", lineNumber, Joiner.on("\n\t").join(error)));
140             }
141 
142         }
143 
144         List<String> added = getMapReport(entitiesAdded);
145         if (!added.isEmpty()) {
146             builder.append(String.format("\n%s entities added: \n%s", added.size(), Joiner.on("\n").join(added)));
147         }
148 
149         List<String> linked = getMapReport(entitiesLinked);
150         if (!linked.isEmpty()) {
151             builder.append(String.format("\n%s entities linked: \n%s", linked.size(), Joiner.on("\n").join(linked)));
152         }
153         return builder.toString();
154 
155     }
156 
157     protected List<String> getMapReport(Map<String, E> map) {
158 
159         List<String> list = new ArrayList<>();
160         for (Map.Entry<String, String> entry : idTranslationMap.entrySet()) {
161             String originalId = entry.getKey();
162             String targetId = entry.getValue();
163             if (map.containsKey(originalId)) {
164                 list.add(String.format("original id: %s -> persist id: %s", originalId, targetId));
165             }
166 
167         }
168         return list;
169 
170     }
171 }