1 package net.sumaris.core.dao.referential.location;
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.base.Preconditions;
26 import net.sumaris.core.dao.technical.Daos;
27 import net.sumaris.core.dao.technical.hibernate.HibernateDaoSupport;
28 import net.sumaris.core.dao.technical.model.IEntity;
29 import net.sumaris.core.model.referential.Status;
30 import net.sumaris.core.model.referential.StatusEnum;
31 import net.sumaris.core.model.referential.ValidityStatus;
32 import net.sumaris.core.model.referential.ValidityStatusEnum;
33 import net.sumaris.core.model.referential.location.Location;
34 import net.sumaris.core.model.referential.location.LocationAssociation;
35 import net.sumaris.core.model.referential.location.LocationLevel;
36 import net.sumaris.core.util.Beans;
37 import net.sumaris.core.vo.referential.LocationVO;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.stereotype.Repository;
41
42 import javax.persistence.EntityManager;
43 import javax.persistence.Query;
44 import javax.persistence.TypedQuery;
45 import javax.persistence.criteria.CriteriaBuilder;
46 import javax.persistence.criteria.CriteriaQuery;
47 import javax.persistence.criteria.ParameterExpression;
48 import javax.persistence.criteria.Root;
49 import java.sql.Timestamp;
50 import java.util.List;
51 import java.util.stream.Collectors;
52
53 @Repository("locationDao")
54 public class LocationDaoImpl extends HibernateDaoSupport implements LocationDao {
55
56
57
58
59 private static final Logger log =
60 LoggerFactory.getLogger(LocationDaoImpl.class);
61
62 @Override
63 public LocationVO findByLabel(final String label) {
64
65 try {
66 return getByLabel(label);
67 } catch (Exception e) {
68 log.error(e.getMessage(), e);
69 return null;
70 }
71 }
72
73 @Override
74 public LocationVO getByLabel(final String label) {
75 CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
76 CriteriaQuery<Location> query = builder.createQuery(Location.class);
77 Root<Location> root = query.from(Location.class);
78
79 ParameterExpression<String> labelParam = builder.parameter(String.class);
80
81 query.select(root)
82 .where(builder.equal(root.get(Location.Fields.LABEL), labelParam));
83
84 TypedQuery<Location> q = getEntityManager().createQuery(query)
85 .setParameter(labelParam, label);
86 return toLocationVO(q.getSingleResult());
87 }
88
89 @Override
90 public List<LocationVO> getByLocationLevel(int locationLevelId) {
91 CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
92 CriteriaQuery<Location> query = builder.createQuery(Location.class);
93 Root<Location> root = query.from(Location.class);
94
95 ParameterExpression<Integer> levelIdParam = builder.parameter(Integer.class);
96
97 query.select(root)
98 .where(builder.equal(root.get(Location.Fields.LOCATION_LEVEL).get(LocationLevel.Fields.ID), levelIdParam));
99
100 TypedQuery<Location> q = getEntityManager().createQuery(query)
101 .setParameter(levelIdParam, locationLevelId);
102 return q.getResultStream().map(this::toLocationVO).collect(Collectors.toList());
103 }
104
105 @Override
106 public Location"../../../../../../net/sumaris/core/model/referential/location/Location.html#Location">Location create(Location location) {
107 Preconditions.checkNotNull(location);
108 Preconditions.checkNotNull(location.getLabel());
109 Preconditions.checkNotNull(location.getName());
110
111
112 if (location.getStatus() == null) {
113 location.setStatus(load(Status.class, StatusEnum.ENABLE.getId()));
114 }
115
116
117 if (location.getValidityStatus() == null) {
118 location.setValidityStatus(load(ValidityStatus.class, ValidityStatusEnum.VALID.getId()));
119 }
120
121 getEntityManager().persist(location);
122
123 return location;
124 }
125
126 @Override
127 public Location"../../../../../../net/sumaris/core/model/referential/location/Location.html#Location">Location update(Location location) {
128 Preconditions.checkNotNull(location);
129 Preconditions.checkNotNull(location.getId());
130 Preconditions.checkNotNull(location.getLabel());
131 Preconditions.checkNotNull(location.getName());
132
133
134 if (location.getStatus() == null) {
135 location.setStatus(load(Status.class, StatusEnum.ENABLE.getId()));
136 }
137
138
139 if (location.getValidityStatus() == null) {
140 location.setValidityStatus(load(ValidityStatus.class, ValidityStatusEnum.VALID.getId()));
141 }
142
143 return getEntityManager().merge(location);
144 }
145
146 @Override
147 public LocationVO toLocationVO(Location source) {
148 if (source == null) return null;
149
150 LocationVOntial/LocationVO.html#LocationVO">LocationVO target = new LocationVO();
151
152 Beans.copyProperties(source, target);
153
154 if (source.getLocationLevel() != null) {
155 target.setLevelId(source.getLocationLevel().getId());
156 }
157
158 if (source.getStatus() != null) {
159 target.setStatusId(source.getStatus().getId());
160 }
161
162 if (source.getValidityStatus() != null) {
163 target.setValidityStatusId(source.getValidityStatus().getId());
164 }
165
166 return target;
167 }
168
169 @Override
170 public void updateLocationHierarchy() {
171
172
173 if (Daos.isHsqlDatabase(config.getJdbcURL())) {
174 log.warn("Skipping location hierarchy (Stored procedure P_FILL_LOCATION_HIERARCHY not exists)");
175 return;
176 }
177
178 Query q = getEntityManager().createNamedQuery("fillLocationHierarchy");
179 q.getResultList();
180 }
181
182 @Override
183 public boolean hasAssociation(int childLocationId, int parentLocationId) {
184 EntityManager em = getEntityManager();
185 CriteriaBuilder builder = em.getCriteriaBuilder();
186 CriteriaQuery<Long> query = builder.createQuery(Long.class);
187 Root<LocationAssociation> root = query.from(LocationAssociation.class);
188
189 ParameterExpression<Integer> childIdParam = builder.parameter(Integer.class);
190 ParameterExpression<Integer> parentIdParam = builder.parameter(Integer.class);
191
192 query.select(builder.count(root))
193 .where(
194 builder.and(
195 builder.equal(root.get(LocationAssociation.Fields.CHILD_LOCATION).get(IEntity.Fields.ID), childIdParam),
196 builder.equal(root.get(LocationAssociation.Fields.PARENT_LOCATION).get(IEntity.Fields.ID), parentIdParam)
197 )
198 );
199
200 return em.createQuery(query)
201 .setParameter(childIdParam, childLocationId)
202 .setParameter(parentIdParam, parentLocationId)
203 .getSingleResult() > 0;
204 }
205
206 @Override
207 public void addAssociation(int childLocationId, int parentLocationId, double childSurfaceRatio) {
208 LocationAssociationlocation/LocationAssociation.html#LocationAssociation">LocationAssociation entity = new LocationAssociation();
209 entity.setChildLocation(load(Location.class, childLocationId));
210 entity.setParentLocation(load(Location.class, parentLocationId));
211 entity.setChildSurfaceRatio(childSurfaceRatio);
212
213
214 Timestamp newUpdateDate = getDatabaseCurrentTimestamp();
215 entity.setUpdateDate(newUpdateDate);
216
217 getEntityManager().persist(entity);
218 }
219
220 @Override
221 public Location get(int id) {
222 return load(Location.class, id);
223 }
224
225
226
227 }