1 package net.sumaris.core.dao.data;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
53
54 public class DataDaos extends Daos {
55
56 protected DataDaos() {
57 super();
58
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
68 copyRecorderPerson(entityManager, source, target, copyIfNull);
69
70
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
82 copyRecorderDepartment(entityManager, source, target, copyIfNull);
83
84
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
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
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
134 observers.add(load(entityManager, Person.class, personId));
135 }
136 });
137
138
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
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
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
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
185 else if (source.getProgram().getId() != null) {
186 target.setProgram(load(entityManager, Program.class, source.getProgram().getId()));
187 }
188
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
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 }