View Javadoc
1   package fr.ifremer.tutti.service.referential;
2   
3   /*
4    * #%L
5    * Tutti :: Service
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2014 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.collect.Lists;
29  import com.google.common.collect.Maps;
30  import com.google.common.collect.Sets;
31  import fr.ifremer.tutti.persistence.entities.referential.TuttiReferentialEntity;
32  
33  import java.util.HashSet;
34  import java.util.List;
35  import java.util.Map;
36  import java.util.Set;
37  import java.util.stream.Collectors;
38  
39  /**
40   * Created on 11/16/14.
41   *
42   * @author Tony Chemit - chemit@codelutin.com
43   * @since 3.10
44   */
45  public class ReferentialImportRequest<E extends TuttiReferentialEntity, K> {
46  
47      private final List<E> toAdd = Lists.newArrayList();
48  
49      private final List<E> toUpdate = Lists.newArrayList();
50  
51      private final List<E> toLink = Lists.newArrayList();
52  
53      private final List<K> toDelete = Lists.newArrayList();
54  
55      private final Map<K, E> existingEntitiesById;
56  
57      private final Set<String> existingNaturalIds;
58  
59      private final Set<K> newIds;
60  
61      private final Set<String> newNaturalIds;
62  
63      private final Function<E, K> entityToIdFunction;
64  
65      public ReferentialImportRequest(List<E> existingEntities, Function<E, K> entityToIdFunction, Function<E, String> naturalIdFunction) {
66  
67          this.entityToIdFunction = entityToIdFunction;
68          this.existingEntitiesById = Maps.uniqueIndex(existingEntities, entityToIdFunction);
69          this.existingNaturalIds = Sets.newHashSet(existingEntities.stream().map(naturalIdFunction::apply).collect(Collectors.toList()));
70          this.newIds = new HashSet<>();
71          this.newNaturalIds = new HashSet<>();
72      }
73  
74      public void addEntityToAdd(E entityToAdd) {
75          toAdd.add(entityToAdd);
76          K id = entityToIdFunction.apply(entityToAdd);
77          newIds.add(id);
78      }
79  
80      public void addEntityToUpdate(E entityToUpdate) {
81          toUpdate.add(entityToUpdate);
82      }
83  
84      public void addEntityToLink(E entityToLink) {
85          toLink.add(entityToLink);
86      }
87  
88      public void addIdToDelete(K entityToDelete) {
89          toDelete.add(entityToDelete);
90      }
91  
92      public boolean withEntitiesToAdd() {
93          return !toAdd.isEmpty();
94      }
95  
96      public boolean withEntitiesToUpdate() {
97          return !toUpdate.isEmpty();
98      }
99  
100     public boolean withEntitiesToLink() {
101         return !toLink.isEmpty();
102     }
103 
104     public boolean withEntitiesToDelete() {
105         return !toDelete.isEmpty();
106     }
107 
108     public List<K> getIdsToDelete() {
109         return Lists.newArrayList(toDelete);
110     }
111 
112     public List<E> getEntitiesToAdd() {
113         return Lists.newArrayList(toAdd);
114     }
115 
116     public List<E> getEntitiesToUpdate() {
117         return Lists.newArrayList(toUpdate);
118     }
119 
120     public List<E> getEntitiesToLink() {
121         return Lists.newArrayList(toLink);
122     }
123 
124     public E getExistingEntityById(K id) {
125         return existingEntitiesById.get(id);
126     }
127 
128     public boolean addExistingNaturalId(String naturalId) {
129         boolean added = existingNaturalIds.add(naturalId);
130         if (added) {
131             newNaturalIds.add(naturalId);
132         }
133         return added;
134     }
135 
136     public boolean isNaturalIdAlreadyAdded(String naturalId) {
137         return newNaturalIds.contains(naturalId);
138     }
139 
140     public boolean isIdAlreadyAdded(K id) {
141         return newIds.contains(id);
142     }
143 
144     public void removeExistingNaturalId(String naturalId) {
145         existingNaturalIds.remove(naturalId);
146     }
147 
148     public Function<E, K> getEntityToIdFunction() {
149         return entityToIdFunction;
150     }
151 
152 }