View Javadoc
1   package net.sumaris.core.dao.data;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Core
6    * %%
7    * Copyright (C) 2018 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.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      /** Logger. */
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              // Check update date
79              checkUpdateDateForUpdate(source, entity);
80  
81              // Lock entityName
82              lockForUpdate(entity);
83          }
84  
85          imageAttachmentVOToEntity(source, entity, true);
86  
87          // Update update_dt
88          Timestamp newUpdateDate = getDatabaseCurrentTimestamp();
89          entity.setUpdateDate(newUpdateDate);
90  
91          // Save entityName
92          if (isNew) {
93              // Force creation date
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         // Department
126         DepartmentVO department = departmentDao.toDepartmentVO(source.getRecorderDepartment());
127         target.setRecorderDepartment(department);
128 
129         // Status
130         target.setQualityFlagId(source.getQualityFlag().getId());
131 
132         return target;
133     }
134 
135     /* -- protected methods -- */
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         // Recorder department & person
149         copyRecorderDepartment(source, target, copyIfNull);
150         copyRecorderPerson(source, target, copyIfNull);
151 
152         // Quality flag
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 }