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.base.Preconditions;
26 import net.sumaris.core.dao.administration.user.DepartmentDao;
27 import net.sumaris.core.model.data.ImageAttachment;
28 import net.sumaris.core.model.referential.QualityFlag;
29 import net.sumaris.core.util.Beans;
30 import net.sumaris.core.vo.administration.user.DepartmentVO;
31 import net.sumaris.core.vo.data.ImageAttachmentVO;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Repository;
36
37 import javax.persistence.EntityManager;
38 import java.sql.Timestamp;
39 import java.util.List;
40 import java.util.Objects;
41 import java.util.stream.Collectors;
42
43 @Repository("imageAttachmentDao")
44 public class ImageAttachmentDaoImpl extends BaseDataDaoImpl implements ImageAttachmentDao {
45
46
47 private static final Logger log =
48 LoggerFactory.getLogger(ImageAttachmentDaoImpl.class);
49
50 @Autowired
51 private DepartmentDao departmentDao;
52
53 @Override
54 public ImageAttachmentVO get(int id) {
55 return toImageAttachmentVO(get(ImageAttachment.class, id));
56 }
57
58 @Override
59 public ImageAttachmentVO../../../net/sumaris/core/vo/data/ImageAttachmentVO.html#ImageAttachmentVO">ImageAttachmentVO save(ImageAttachmentVO source) {
60 Preconditions.checkNotNull(source);
61 Preconditions.checkNotNull(source.getContent());
62 Preconditions.checkNotNull(source.getContentType());
63 Preconditions.checkNotNull(source.getDateTime());
64 Preconditions.checkNotNull(source.getRecorderDepartment());
65 Preconditions.checkNotNull(source.getRecorderDepartment().getId());
66
67 EntityManager entityManager = getEntityManager();
68 ImageAttachment entity = null;
69 if (source.getId() != null) {
70 entity = get(ImageAttachment.class, source.getId());
71 }
72 boolean isNew = (entity == null);
73 if (isNew) {
74 entity = new ImageAttachment();
75 }
76
77 if (!isNew) {
78
79 checkUpdateDateForUpdate(source, entity);
80
81
82 lockForUpdate(entity);
83 }
84
85 imageAttachmentVOToEntity(source, entity, true);
86
87
88 Timestamp newUpdateDate = getDatabaseCurrentTimestamp();
89 entity.setUpdateDate(newUpdateDate);
90
91
92 if (isNew) {
93
94 entity.setCreationDate(newUpdateDate);
95 source.setCreationDate(newUpdateDate);
96
97 entityManager.persist(entity);
98 source.setId(entity.getId());
99 } else {
100 entityManager.merge(entity);
101 }
102
103 source.setUpdateDate(newUpdateDate);
104
105 getEntityManager().flush();
106 getEntityManager().clear();
107
108 return source;
109 }
110
111 @Override
112 public void delete(int id) {
113
114 log.debug(String.format("Deleting image attachment {id=%s}...", id));
115 delete(ImageAttachment.class, id);
116 }
117
118 @Override
119 public ImageAttachmentVO toImageAttachmentVO(ImageAttachment source) {
120 if (source == null) return null;
121 ImageAttachmentVOmentVO.html#ImageAttachmentVO">ImageAttachmentVO target = new ImageAttachmentVO();
122
123 Beans.copyProperties(source, target);
124
125
126 DepartmentVO department = departmentDao.toDepartmentVO(source.getRecorderDepartment());
127 target.setRecorderDepartment(department);
128
129
130 target.setQualityFlagId(source.getQualityFlag().getId());
131
132 return target;
133 }
134
135
136
137 protected List<ImageAttachmentVO> toImageAttachmentVOs(List<ImageAttachment> source) {
138 return source.stream()
139 .map(this::toImageAttachmentVO)
140 .filter(Objects::nonNull)
141 .collect(Collectors.toList());
142 }
143
144 protected void imageAttachmentVOToEntity(ImageAttachmentVO source, ImageAttachment target, boolean copyIfNull) {
145
146 Beans.copyProperties(source, target);
147
148
149 copyRecorderDepartment(source, target, copyIfNull);
150 copyRecorderPerson(source, target, copyIfNull);
151
152
153 if (copyIfNull || source.getQualityFlagId() != null) {
154 if (source.getQualityFlagId() == null) {
155 target.setQualityFlag(null);
156 }
157 else {
158 target.setQualityFlag(load(QualityFlag.class, source.getQualityFlagId()));
159 }
160 }
161
162 }
163 }