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.SortDirection;
28 import net.sumaris.core.dao.technical.hibernate.HibernateDaoSupport;
29 import net.sumaris.core.dao.technical.model.IEntity;
30 import net.sumaris.core.exception.SumarisTechnicalException;
31 import net.sumaris.core.model.administration.programStrategy.Program;
32 import net.sumaris.core.model.administration.user.Department;
33 import net.sumaris.core.model.administration.user.Person;
34 import net.sumaris.core.model.data.*;
35 import net.sumaris.core.model.referential.QualityFlag;
36 import net.sumaris.core.util.Beans;
37 import net.sumaris.core.vo.administration.user.DepartmentVO;
38 import net.sumaris.core.vo.administration.user.PersonVO;
39 import net.sumaris.core.vo.data.IDataVO;
40 import net.sumaris.core.vo.data.IRootDataVO;
41 import net.sumaris.core.vo.data.VesselSnapshotVO;
42 import org.apache.commons.collections4.CollectionUtils;
43 import org.apache.commons.collections4.MapUtils;
44 import org.apache.commons.lang3.StringUtils;
45
46 import javax.persistence.criteria.*;
47 import java.io.Serializable;
48 import java.util.Map;
49 import java.util.Objects;
50 import java.util.Set;
51
52
53
54
55 public abstract class BaseDataDaoImpl extends HibernateDaoSupport {
56
57 public <T extends Serializable> void copyRootDataProperties(IRootDataVO<T> source,
58 IRootDataEntity<T> target,
59 boolean copyIfNull) {
60
61
62 copyRootDataProperties(source, target, copyIfNull, (String) null);
63 }
64
65 public <T extends Serializable> void copyRootDataProperties(IRootDataVO<T> source,
66 IRootDataEntity<T> target,
67 boolean copyIfNull,
68 String... exceptProperties) {
69
70
71 copyDataProperties(source, target, copyIfNull, exceptProperties);
72
73
74 copyRecorderPerson(source, target, copyIfNull);
75
76
77 copyProgram(source, target, copyIfNull);
78 }
79
80 public <T extends Serializable> void copyDataProperties(IDataVO<T> source,
81 IDataEntity<T> target,
82 boolean copyIfNull) {
83
84
85 copyDataProperties(source, target, copyIfNull, (String) null);
86 }
87
88 public <T extends Serializable> void copyDataProperties(IDataVO<T> source,
89 IDataEntity<T> target,
90 boolean copyIfNull,
91 String... exceptProperties) {
92
93
94 Beans.copyProperties(source, target, exceptProperties);
95
96
97 copyRecorderDepartment(source, target, copyIfNull);
98
99
100 copyQualityFlag(source, target, copyIfNull);
101 }
102
103 public <T extends Serializable> void copyRecorderDepartment(IWithRecorderDepartmentEntity<T, DepartmentVO> source,
104 IWithRecorderDepartmentEntity<T, Department> target,
105 boolean copyIfNull) {
106
107 if (copyIfNull || source.getRecorderDepartment() != null) {
108 if (source.getRecorderDepartment() == null || source.getRecorderDepartment().getId() == null) {
109 target.setRecorderDepartment(null);
110 } else {
111 target.setRecorderDepartment(load(Department.class, source.getRecorderDepartment().getId()));
112 }
113 }
114 }
115
116 public <T extends Serializable> void copyRecorderPerson(IWithRecorderPersonEntity<T, PersonVO> source,
117 IWithRecorderPersonEntity<T, Person> target,
118 boolean copyIfNull) {
119 if (copyIfNull || source.getRecorderPerson() != null) {
120 if (source.getRecorderPerson() == null || source.getRecorderPerson().getId() == null) {
121 target.setRecorderPerson(null);
122 } else {
123 target.setRecorderPerson(load(Person.class, source.getRecorderPerson().getId()));
124 }
125 }
126 }
127
128 public <T extends Serializable> void copyObservers(IWithObserversEntity<T, PersonVO> source,
129 IWithObserversEntity<T, Person> target,
130 boolean copyIfNull) {
131
132 if (copyIfNull || source.getObservers() != null) {
133 if (CollectionUtils.isEmpty(source.getObservers())) {
134 if (target.getId() != null && CollectionUtils.isNotEmpty(target.getObservers())) {
135 target.getObservers().clear();
136 }
137 } else {
138 Set<Person> observers = target.getId() != null ? target.getObservers() : Sets.newHashSet();
139 Map<Integer, Person> observersToRemove = Beans.splitById(observers);
140 source.getObservers().stream()
141 .map(IEntity::getId)
142 .filter(Objects::nonNull)
143 .forEach(personId -> {
144 if (observersToRemove.remove(personId) == null) {
145
146 observers.add(load(Person.class, personId));
147 }
148 });
149
150
151 if (MapUtils.isNotEmpty(observersToRemove)) {
152 observers.removeAll(observersToRemove.values());
153 }
154 target.setObservers(observers);
155 }
156 }
157 }
158
159 public void copyVessel(IWithVesselSnapshotEntity<Integer, VesselSnapshotVO> source,
160 IWithVesselEntity<Integer, Vessel> target,
161 boolean copyIfNull) {
162
163 if (copyIfNull || (source.getVesselSnapshot() != null && source.getVesselSnapshot().getId() != null)) {
164 if (source.getVesselSnapshot() == null || source.getVesselSnapshot().getId() == null) {
165 target.setVessel(null);
166 } else {
167 target.setVessel(load(Vessel.class, source.getVesselSnapshot().getId()));
168 }
169 }
170 }
171
172 public <T extends Serializable> void copyQualityFlag(IDataVO<T> source,
173 IDataEntity<T> target,
174 boolean copyIfNull) {
175
176 if (copyIfNull || source.getQualityFlagId() != null) {
177 if (source.getQualityFlagId() == null) {
178 target.setQualityFlag(load(QualityFlag.class, SumarisConfiguration.getInstance().getDefaultQualityFlagId()));
179 } else {
180 target.setQualityFlag(load(QualityFlag.class, source.getQualityFlagId()));
181 }
182 }
183 }
184
185 public <T extends Serializable> void copyProgram(IRootDataVO<T> source,
186 IRootDataEntity<T> target,
187 boolean copyIfNull) {
188
189 if (copyIfNull || (source.getProgram() != null && (source.getProgram().getId() != null || source.getProgram().getLabel() != null))) {
190 if (source.getProgram() == null || (source.getProgram().getId() == null && source.getProgram().getLabel() == null)) {
191 target.setProgram(null);
192 }
193
194 else if (source.getProgram().getId() != null) {
195 target.setProgram(load(Program.class, source.getProgram().getId()));
196 }
197
198 else {
199 throw new SumarisTechnicalException("Missing program.id !");
200 }
201 }
202 }
203
204
205
206
207
208
209
210
211
212
213
214
215 protected <T> CriteriaQuery<T> addSorting(CriteriaQuery<T> query,
216 CriteriaBuilder cb,
217 Root<?> root, String sortAttribute, SortDirection sortDirection) {
218
219 if (StringUtils.isNotBlank(sortAttribute)) {
220 Expression<?> sortExpression = composePath(root, sortAttribute);
221 query.orderBy(SortDirection.DESC.equals(sortDirection) ?
222 cb.desc(sortExpression) :
223 cb.asc(sortExpression)
224 );
225 }
226 return query;
227 }
228
229
230
231
232
233
234
235
236
237 protected <X> Path<X> composePath(Root<?> root, String attributePath) {
238
239 String[] paths = attributePath.split("\\.");
240 From<?, ?> from = root;
241 Path<X> result = null;
242
243 for (int i = 0; i < paths.length; i++) {
244 String path = paths[i];
245
246 if (i == paths.length - 1) {
247
248 result = from.get(path);
249 } else {
250
251 Join join = from.getJoins().stream()
252 .filter(j -> j.getAttribute().getName().equals(path))
253 .findFirst().orElse(null);
254 if (join == null) {
255 throw new IllegalArgumentException(String.format("the join %s from %s doesn't exists", path, from.getClass().getSimpleName()));
256 }
257 from = join;
258 }
259 }
260
261 return result;
262 }
263 }