View Javadoc
1   package net.sumaris.core.dao.data;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Core
6    * %%
7    * Copyright (C) 2018 - 2019 SUMARiS Consortium
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU 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 Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.collect.Sets;
26  import net.sumaris.core.config.SumarisConfiguration;
27  import net.sumaris.core.dao.technical.Daos;
28  import net.sumaris.core.dao.technical.model.IEntity;
29  import net.sumaris.core.exception.SumarisTechnicalException;
30  import net.sumaris.core.model.administration.programStrategy.Program;
31  import net.sumaris.core.model.administration.user.Department;
32  import net.sumaris.core.model.administration.user.Person;
33  import net.sumaris.core.model.data.*;
34  import net.sumaris.core.model.referential.QualityFlag;
35  import net.sumaris.core.util.Beans;
36  import net.sumaris.core.vo.administration.user.DepartmentVO;
37  import net.sumaris.core.vo.administration.user.PersonVO;
38  import net.sumaris.core.vo.data.IDataVO;
39  import net.sumaris.core.vo.data.IRootDataVO;
40  import net.sumaris.core.vo.data.VesselSnapshotVO;
41  import org.apache.commons.collections4.CollectionUtils;
42  import org.apache.commons.collections4.MapUtils;
43  import org.hibernate.Session;
44  
45  import javax.persistence.EntityManager;
46  import java.io.Serializable;
47  import java.util.Map;
48  import java.util.Objects;
49  import java.util.Set;
50  
51  /**
52   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>*
53   */
54  public class DataDaos extends Daos {
55  
56      protected DataDaos() {
57          super();
58          // helper class does not instantiate
59      }
60  
61      public static <T extends Serializable> void copyRootDataProperties(EntityManager entityManager,
62                                                                         IRootDataVO<T> source,
63                                                                         IRootDataEntity<T> target,
64                                                                         boolean copyIfNull) {
65          copyDataProperties(entityManager, source, target, copyIfNull);
66  
67          // Recorder person
68          copyRecorderPerson(entityManager, source, target, copyIfNull);
69  
70          // Program
71          copyProgram(entityManager, source, target, copyIfNull);
72      }
73  
74      public static <T extends Serializable> void copyDataProperties(EntityManager entityManager,
75                                                                     IDataVO<T> source,
76                                                                     IDataEntity<T> target,
77                                                                     boolean copyIfNull) {
78  
79          Beans.copyProperties(source, target);
80  
81          // Recorder department
82          copyRecorderDepartment(entityManager, source, target, copyIfNull);
83  
84          // Quality flag
85          copyQualityFlag(entityManager, source, target, copyIfNull);
86      }
87  
88      public static <T extends Serializable> void copyRecorderDepartment(EntityManager entityManager,
89                                                                         IWithRecorderDepartmentEntity<T, DepartmentVO> source,
90                                                                         IWithRecorderDepartmentEntity<T, Department> target,
91                                                                         boolean copyIfNull) {
92          // Recorder department
93          if (copyIfNull || source.getRecorderDepartment() != null) {
94              if (source.getRecorderDepartment() == null || source.getRecorderDepartment().getId() == null) {
95                  target.setRecorderDepartment(null);
96              } else {
97                  target.setRecorderDepartment(load(entityManager, Department.class, source.getRecorderDepartment().getId()));
98              }
99          }
100     }
101 
102     public static <T extends Serializable> void copyRecorderPerson(EntityManager entityManager,
103                                                                    IWithRecorderPersonEntity<T, PersonVO> source,
104                                                                    IWithRecorderPersonEntity<T, Person> target,
105                                                                    boolean copyIfNull) {
106         if (copyIfNull || source.getRecorderPerson() != null) {
107             if (source.getRecorderPerson() == null || source.getRecorderPerson().getId() == null) {
108                 target.setRecorderPerson(null);
109             } else {
110                 target.setRecorderPerson(load(entityManager, Person.class, source.getRecorderPerson().getId()));
111             }
112         }
113     }
114 
115     public static <T extends Serializable> void copyObservers(EntityManager entityManager,
116                                                               IWithObserversEntity<T, PersonVO> source,
117                                                               IWithObserversEntity<T, Person> target,
118                                                               boolean copyIfNull) {
119         // Observers
120         if (copyIfNull || source.getObservers() != null) {
121             if (CollectionUtils.isEmpty(source.getObservers())) {
122                 if (target.getId() != null && CollectionUtils.isNotEmpty(target.getObservers())) {
123                     target.getObservers().clear();
124                 }
125             } else {
126                 Set<Person> observers = target.getId() != null ? target.getObservers() : Sets.newHashSet();
127                 Map<Integer, Person> observersToRemove = Beans.splitById(observers);
128                 source.getObservers().stream()
129                         .map(IEntity::getId)
130                         .filter(Objects::nonNull)
131                         .forEach(personId -> {
132                             if (observersToRemove.remove(personId) == null) {
133                                 // Add new item
134                                 observers.add(load(entityManager, Person.class, personId));
135                             }
136                         });
137 
138                 // Remove deleted tableNames
139                 if (MapUtils.isNotEmpty(observersToRemove)) {
140                     observers.removeAll(observersToRemove.values());
141                 }
142                 target.setObservers(observers);
143             }
144         }
145     }
146 
147     public static <T extends Serializable> void copyVessel(EntityManager entityManager,
148                                                            IWithVesselSnapshotEntity<T, ? extends VesselSnapshotVO> source,
149                                                            IWithVesselEntity<T, Vessel> target,
150                                                            boolean copyIfNull) {
151         // Vessel
152         if (copyIfNull || (source.getVesselSnapshot() != null && source.getVesselSnapshot().getId() != null)) {
153             if (source.getVesselSnapshot() == null || source.getVesselSnapshot().getId() == null) {
154                 target.setVessel(null);
155             } else {
156                 target.setVessel(load(entityManager, Vessel.class, source.getVesselSnapshot().getId()));
157             }
158         }
159     }
160 
161     public static <T extends Serializable> void copyQualityFlag(EntityManager entityManager,
162                                                                 IDataVO<T> source,
163                                                                 IDataEntity<T> target,
164                                                                 boolean copyIfNull) {
165         // Quality flag
166         if (copyIfNull || source.getQualityFlagId() != null) {
167             if (source.getQualityFlagId() == null) {
168                 target.setQualityFlag(load(entityManager, QualityFlag.class, SumarisConfiguration.getInstance().getDefaultQualityFlagId()));
169             } else {
170                 target.setQualityFlag(load(entityManager, QualityFlag.class, source.getQualityFlagId()));
171             }
172         }
173     }
174 
175     public static <T extends Serializable> void copyProgram(EntityManager entityManager,
176                                                             IRootDataVO<T> source,
177                                                             IRootDataEntity<T> target,
178                                                             boolean copyIfNull) {
179         // Program
180         if (copyIfNull || (source.getProgram() != null && (source.getProgram().getId() != null || source.getProgram().getLabel() != null))) {
181             if (source.getProgram() == null || (source.getProgram().getId() == null && source.getProgram().getLabel() == null)) {
182                 target.setProgram(null);
183             }
184             // Load by id
185             else if (source.getProgram().getId() != null) {
186                 target.setProgram(load(entityManager, Program.class, source.getProgram().getId()));
187             }
188             // Load by label
189             else {
190                 if (copyIfNull) {
191                     throw new SumarisTechnicalException("Missing program.id !");
192                 }
193                 else {
194                     target.setProgram(new Program());
195                     Beans.copyProperties(source.getProgram(), target.getProgram());
196                 }
197             }
198         }
199     }
200 
201     /* -- protected method -- */
202 
203     protected static <C> C load(EntityManager em, Class<? extends C> clazz, Serializable id) {
204         return em.unwrap(Session.class).load(clazz, id);
205     }
206 
207 }