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