View Javadoc
1   package fr.ifremer.quadrige3.core.dao.administration.strategy;
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  import fr.ifremer.quadrige3.core.dao.system.synchronization.SynchronizationStatus;
26  import fr.ifremer.quadrige3.core.dao.technical.Assert;
27  import fr.ifremer.quadrige3.core.exception.SaveForbiddenException;
28  import fr.ifremer.quadrige3.core.vo.administration.strategy.AppliedPeriodVO;
29  import org.hibernate.SessionFactory;
30  import org.hibernate.type.DateType;
31  import org.hibernate.type.IntegerType;
32  import org.hibernate.type.StringType;
33  import org.springframework.beans.factory.annotation.Autowired;
34  import org.springframework.context.annotation.Lazy;
35  import org.springframework.stereotype.Repository;
36  
37  import java.sql.Timestamp;
38  import java.util.Collection;
39  import java.util.Collections;
40  import java.util.Date;
41  
42  /**
43   * <p>
44   * AppliedPeriodDaoImpl class.
45   * </p>
46   * 
47   * @see AppliedPeriod
48   */
49  @Repository("appliedPeriodDao")
50  @Lazy
51  public class AppliedPeriodDaoImpl
52  		extends AppliedPeriodDaoBase
53  {
54  	/**
55  	 * Constructor used by Spring
56  	 * 
57  	 * @param sessionFactory
58  	 *            a {@link org.hibernate.SessionFactory} object.
59  	 */
60  	@Autowired
61  	public AppliedPeriodDaoImpl(SessionFactory sessionFactory) {
62  		super();
63  		setSessionFactory(sessionFactory);
64  	}
65  
66  	@Override
67  	protected AppliedPeriodVO handleSave(AppliedPeriodVO source, Timestamp updateDt) {
68  		Assert.notNull(source);
69  		Assert.notNull(source.getAppliedPeriodStartDt());
70  		Assert.notNull(source.getAppliedStratId());
71  
72  		AppliedPeriodPK pk = new AppliedPeriodPK();
73  		pk.setAppliedPeriodStartDt(source.getAppliedPeriodStartDt());
74  		AppliedStrategyImpl appliedStrategy = load(AppliedStrategyImpl.class, source.getAppliedStratId());
75  		pk.setAppliedStrategy(appliedStrategy);
76  
77  		AppliedPeriod entity = this.get(pk);
78  		boolean isNew = false;
79  		if (entity == null)
80  		{
81  			entity = AppliedPeriod.Factory.newInstance();
82  			entity.setAppliedPeriodPk(pk);
83  			isNew = true;
84  		}
85  
86  		if (!isNew) {
87  			// Check if this period has data outside (Mantis #48011)
88  			long count = queryCount("countSurveysByProgramLocationAndOutsideDates",
89  					"programCode", StringType.INSTANCE, appliedStrategy.getStrategy().getProgram().getProgCd(),
90  					"locationId", IntegerType.INSTANCE, appliedStrategy.getMonitoringLocation().getMonLocId(),
91  					"appliedStrategyId", IntegerType.INSTANCE, appliedStrategy.getAppliedStratId(),
92  					"startDate", DateType.INSTANCE, source.getAppliedPeriodStartDt(),
93  					"endDate", DateType.INSTANCE, source.getAppliedPeriodEndDt(),
94  					"synchronizationStatusToIgnore", StringType.INSTANCE, SynchronizationStatus.DELETED.getValue()
95  			);
96  
97  			if (count > 0) {
98  				throw new SaveForbiddenException(SaveForbiddenException.Type.ATTACHED_DATA, "surveys outside modified period",
99  						Collections.singleton(appliedStrategy.getStrategy().getStratNm()));
100 			}
101 		}
102 
103 		// update update_dt
104 		if (updateDt != null) {
105 			source.setUpdateDt(updateDt);
106 		}
107 
108 		// VO -> Entity
109 		appliedPeriodVOToEntity(source, entity, true);
110 
111 		// save entity
112 		if (isNew) {
113 			getSession().save(entity);
114 		} else {
115 			getSession().update(entity);
116 		}
117 
118 		return source;
119 	}
120 
121 	@Override
122 	protected void handleCheckCanRemove(Integer appliedStratId, String progCd, Integer monLocId, Date startDate, Date endDate) throws Exception {
123 
124 		// Check if this period has data inside (Mantis #48011)
125 		long count = queryCount("countSurveysByProgramLocationAndInsideDates",
126 			"programCode", StringType.INSTANCE, progCd,
127 			"locationId", IntegerType.INSTANCE, monLocId,
128 			"appliedStrategyId", IntegerType.INSTANCE, appliedStratId,
129 			"startDate", DateType.INSTANCE, startDate,
130 			"endDate", DateType.INSTANCE, endDate,
131 			"synchronizationStatusToIgnore", StringType.INSTANCE, SynchronizationStatus.DELETED.getValue()
132 		);
133 
134 		if (count > 0) {
135 			throw new SaveForbiddenException(SaveForbiddenException.Type.ATTACHED_DATA, "surveys inside deleted period",
136 				Collections.singleton(get(AppliedStrategyImpl.class, appliedStratId).getStrategy().getStratNm()));
137 		}
138 
139 	}
140 
141 	/** {@inheritDoc} */
142 	public void toAppliedPeriodVO(
143 			AppliedPeriod source,
144 			AppliedPeriodVO target)
145 	{
146 		super.toAppliedPeriodVO(source, target);
147 
148 		AppliedPeriodPK pk = source.getAppliedPeriodPk();
149 		if (pk != null) {
150 			target.setAppliedPeriodStartDt(pk.getAppliedPeriodStartDt());
151 			target.setAppliedStratId(pk.getAppliedStrategy().getAppliedStratId());
152 		}
153 
154 	}
155 
156 	/**
157 	 * Retrieves the entity object that is associated with the specified value object
158 	 * from the object store. If no such entity object exists in the object store,
159 	 * a new, blank entity is created
160 	 */
161 	private AppliedPeriod loadAppliedPeriodFromAppliedPeriodVO(AppliedPeriodVO source)
162 	{
163 		AppliedPeriodPK pk = new AppliedPeriodPK();
164 		pk.setAppliedPeriodStartDt(source.getAppliedPeriodStartDt());
165 		pk.setAppliedStrategy(load(AppliedStrategyImpl.class, source.getAppliedStratId()));
166 
167 		AppliedPeriod appliedPeriod = this.get(pk);
168 		if (appliedPeriod == null)
169 		{
170 			appliedPeriod = AppliedPeriod.Factory.newInstance();
171 			appliedPeriod.setAppliedPeriodPk(pk);
172 		}
173 		return appliedPeriod;
174 	}
175 
176 	/** {@inheritDoc} */
177 	public AppliedPeriod appliedPeriodVOToEntity(AppliedPeriodVO appliedPeriodVO)
178 	{
179 		AppliedPeriod entity = this.loadAppliedPeriodFromAppliedPeriodVO(appliedPeriodVO);
180 		this.appliedPeriodVOToEntity(appliedPeriodVO, entity, true);
181 		return entity;
182 	}
183 
184 	@Override
185 	public void remove(Collection<AppliedPeriod> entities) {
186 		Assert.notEmpty(entities);
187 
188 		entities.forEach(this::remove);
189 	}
190 }