View Javadoc
1   // Generated by: hibernate/SpringHibernateDaoImpl.vsl in andromda-spring-cartridge.
2   // license-header java merge-point
3   /**
4    * This is only generated once! It will never be overwritten.
5    * You can (and have to!) safely modify it by hand.
6    */
7   package fr.ifremer.quadrige2.core.dao.data.survey;
8   
9   /*-
10   * #%L
11   * Quadrige2 Core :: Quadrige2 Server Core
12   * %%
13   * Copyright (C) 2017 Ifremer
14   * %%
15   * This program is free software: you can redistribute it and/or modify
16   * it under the terms of the GNU Affero General Public License as published by
17   * the Free Software Foundation, either version 3 of the License, or
18   * (at your option) any later version.
19   * 
20   * This program is distributed in the hope that it will be useful,
21   * but WITHOUT ANY WARRANTY; without even the implied warranty of
22   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23   * GNU General Public License for more details.
24   * 
25   * You should have received a copy of the GNU Affero General Public License
26   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27   * #L%
28   */
29  
30  import com.google.common.base.Function;
31  import com.google.common.base.Preconditions;
32  import com.google.common.collect.Lists;
33  import fr.ifremer.quadrige2.core.dao.administration.user.DepartmentImpl;
34  import fr.ifremer.quadrige2.core.dao.administration.user.Quser;
35  import fr.ifremer.quadrige2.core.dao.administration.user.QuserImpl;
36  import fr.ifremer.quadrige2.core.dao.referential.monitoringLocation.PositionningSystemImpl;
37  import fr.ifremer.quadrige2.core.dao.system.synchronization.DeletedItemHistoryExtendDao;
38  import fr.ifremer.quadrige2.core.dao.technical.Daos;
39  import fr.ifremer.quadrige2.core.dao.technical.Dates;
40  import fr.ifremer.quadrige2.core.exception.BadUpdateDtException;
41  import fr.ifremer.quadrige2.core.vo.data.survey.OccasionVO;
42  import org.apache.commons.collections4.CollectionUtils;
43  import org.apache.commons.lang3.ArrayUtils;
44  import org.hibernate.SessionFactory;
45  import org.nuiton.i18n.I18n;
46  import org.springframework.beans.factory.annotation.Autowired;
47  import org.springframework.context.annotation.Lazy;
48  import org.springframework.stereotype.Repository;
49  
50  import javax.annotation.Nullable;
51  import javax.annotation.Resource;
52  import java.sql.Timestamp;
53  import java.util.Collection;
54  import java.util.List;
55  import java.util.Objects;
56  
57  /**
58   * @see Occasion
59   */
60  @Repository("occasionDao")
61  @Lazy
62  public class OccasionDaoImpl
63          extends OccasionDaoBase {
64      @Resource(name = "deletedItemHistoryDao")
65      private DeletedItemHistoryExtendDao deletedItemHistoryDao;
66  
67      /**
68       * Constructor used by Spring
69       */
70      @Autowired
71      public OccasionDaoImpl(SessionFactory sessionFactory) {
72          super();
73          setSessionFactory(sessionFactory);
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public void remove(Collection<Occasion> entities) {
81          Preconditions.checkNotNull(entities);
82          Preconditions.checkArgument(entities.size() > 0);
83  
84          for (Occasion entity : entities) {
85              remove(entity);
86          }
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      @Override
93      public void remove(Occasion entity) {
94  
95          // Remove link to moratoria
96          if (CollectionUtils.isNotEmpty(entity.getMoratoria())) {
97              entity.getMoratoria().clear();
98          }
99  
100         // Remove link to Quser
101         if (CollectionUtils.isNotEmpty(entity.getQusers())) {
102             entity.getQusers().clear();
103         }
104 
105         // Insert into DeleteItemHistory
106         deletedItemHistoryDao.insertDeletedItem(OccasionImpl.class, entity);
107 
108         super.remove(entity);
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     protected OccasionVO handleSave(OccasionVO source, Timestamp updateDt) {
115         Preconditions.checkNotNull(source);
116         Preconditions.checkNotNull(source.getCampaignId());
117 
118         // Load parent
119         Campaign parent = get(CampaignImpl.class, source.getCampaignId());
120 
121         // Load entity
122         Occasion entity = null;
123         boolean isNew = false;
124         if (source.getOccasId() != null) {
125             entity = get(source.getOccasId());
126         }
127         if (entity == null) {
128             entity = Occasion.Factory.newInstance();
129             parent.addOccasions(entity);
130             entity.setCampaign(parent);
131             isNew = true;
132         }
133 
134         // Check update_dt
135         if (!isNew) {
136             if (entity.getUpdateDt() != null) {
137                 Timestamp serverUpdateDtNoMillisecond = Dates.resetMillisecond(entity.getUpdateDt());
138                 if (source.getUpdateDt() == null) {
139                     throw new BadUpdateDtException(I18n.t("quadrige2.dao.occasion.badUpdateDt", source.getOccasId(),
140                             serverUpdateDtNoMillisecond,
141                             "null"));
142                 }
143                 Timestamp sourceUpdateDtNoMillisecond = Dates.resetMillisecond(source.getUpdateDt());
144                 if (!Objects.equals(sourceUpdateDtNoMillisecond, serverUpdateDtNoMillisecond)) {
145                     throw new BadUpdateDtException(I18n.t("quadrige2.dao.occasion.badUpdateDt", source.getOccasId(), serverUpdateDtNoMillisecond,
146                             sourceUpdateDtNoMillisecond));
147                 }
148             }
149         }
150 
151         // update update_dt
152         Timestamp newUpdateDt = updateDt;
153         if (newUpdateDt == null) {
154             newUpdateDt = getDatabaseCurrentTimestamp();
155         }
156         source.setUpdateDt(newUpdateDt);
157 
158         // VO -> Entity
159         occasionVOToEntity(source, entity, true);
160 
161         // Save entity
162         if (isNew) {
163             Integer occasId = (Integer) getSession().save(entity);
164             source.setOccasId(occasId);
165         } else {
166             getSession().update(entity);
167         }
168 
169         return source;
170     }
171 
172     /**
173      * {@inheritDoc}
174      */
175     protected void handleRemoveByIds(Collection<Integer> occasIds) {
176         for (Integer occasId : occasIds) {
177             remove(occasId);
178         }
179     }
180 
181     /**
182      * {@inheritDoc}
183      */
184     public void toOccasionVO(
185             Occasion source,
186             OccasionVO target) {
187         // copy base attributes
188         super.toOccasionVO(source, target);
189 
190         // copy recorder department
191         target.setRecDepId(source.getRecorderDepartment() != null ? source.getRecorderDepartment().getDepId() : null);
192 
193         // copy pos system
194         target.setPosSystemId(source.getPositionningSystem() != null ? source.getPositionningSystem().getPosSystemId() : null);
195 
196         // copy ship
197         target.setShipId(source.getShip() != null ? source.getShip().getShipId() : null);
198 
199         // Resp qusers
200         if (CollectionUtils.isEmpty(source.getQusers())) {
201             target.setQuserIds(null);
202         } else {
203             List<Integer> quserIds = Lists.transform(Lists.newArrayList(source.getQusers()),
204                     new Function<Quser, Integer>() {
205                         @Nullable
206                         @Override
207                         public Integer apply(Quser source) {
208                             return source.getQuserId();
209                         }
210                     });
211             target.setQuserIds(quserIds.toArray(new Integer[quserIds.size()]));
212         }
213     }
214 
215     /**
216      * Retrieves the entity object that is associated with the specified value object
217      * from the object store. If no such entity object exists in the object store,
218      * a new, blank entity is created
219      */
220     private Occasion loadOccasionFromOccasionVO(OccasionVO occasionVO) {
221         Occasion occasion = null;
222         if (occasionVO.getOccasId() != null) {
223             occasion = this.get(occasionVO.getOccasId());
224         }
225         if (occasion == null) {
226             occasion = Occasion.Factory.newInstance();
227         }
228         return occasion;
229     }
230 
231     /**
232      * {@inheritDoc}
233      */
234     public Occasion occasionVOToEntity(OccasionVO occasionVO) {
235         Occasion entity = this.loadOccasionFromOccasionVO(occasionVO);
236         this.occasionVOToEntity(occasionVO, entity, true);
237         return entity;
238     }
239 
240     /**
241      * {@inheritDoc}
242      */
243     @Override
244     public void occasionVOToEntity(
245             OccasionVO source,
246             Occasion target,
247             boolean copyIfNull) {
248         super.occasionVOToEntity(source, target, copyIfNull);
249 
250         // Campaign
251         if (copyIfNull || source.getCampaignId() != null) {
252             if (source.getCampaignId() == null) {
253                 target.setCampaign(null);
254             } else {
255                 target.setCampaign(load(CampaignImpl.class, source.getCampaignId()));
256             }
257         }
258 
259         // Recorder department
260         if (copyIfNull || source.getRecDepId() != null) {
261             target.setRecorderDepartment(source.getRecDepId() != null ? load(DepartmentImpl.class, source.getRecDepId()) : null);
262         }
263 
264         // Positionning system
265         if (copyIfNull || source.getPosSystemId() != null) {
266             target.setPositionningSystem(source.getPosSystemId() != null ? load(PositionningSystemImpl.class, source.getPosSystemId()) : null);
267         }
268 
269         // Ship
270         if (copyIfNull || source.getShipId() != null) {
271             target.setShip(source.getShipId() != null ? load(ShipImpl.class, source.getShipId()) : null);
272         }
273 
274         // Resp qusers
275         if (copyIfNull || ArrayUtils.isNotEmpty(source.getQuserIds())) {
276             if (ArrayUtils.isEmpty(source.getQuserIds())) {
277                 target.getQusers().clear();
278             } else {
279                 Daos.replaceEntities(
280                         target.getQusers(),
281                         source.getQuserIds(),
282                         quserId -> load(QuserImpl.class, quserId));
283             }
284         }
285     }
286 
287     /* -- protected methods -- */
288 
289 }