View Javadoc
1   package fr.ifremer.quadrige3.core.dao.data.samplingoperation;
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.collect.ImmutableList;
27  import fr.ifremer.quadrige3.core.dao.technical.Assert;
28  import fr.ifremer.quadrige3.core.dao.technical.Daos;
29  import org.apache.commons.collections4.CollectionUtils;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.hibernate.SessionFactory;
33  import org.hibernate.type.StringType;
34  import org.springframework.beans.factory.annotation.Autowired;
35  import org.springframework.context.annotation.Lazy;
36  import org.springframework.stereotype.Repository;
37  
38  import java.util.List;
39  
40  /**
41   * <p>
42   * SamplingOperationDaoImpl class.
43   * </p>
44   * 
45   * @see fr.ifremer.quadrige3.core.dao.data.samplingoperation.SamplingOperation
46   */
47  @Repository("samplingOperationDao")
48  @Lazy
49  public class SamplingOperationDaoImpl
50  		extends SamplingOperationDaoBase
51  		implements SamplingOperationExtendDao
52  {
53  	/**
54  	 * Logger.
55  	 */
56  	private static final Log log = LogFactory.getLog(SamplingOperationDaoImpl.class);
57  
58  	/**
59  	 * Constructor used by Spring
60  	 * 
61  	 * @param sessionFactory
62  	 *            a {@link org.hibernate.SessionFactory} object.
63  	 */
64  	@Autowired
65  	public SamplingOperationDaoImpl(SessionFactory sessionFactory) {
66  		super();
67  		setSessionFactory(sessionFactory);
68  	}
69  
70  	/** {@inheritDoc} */
71  	@Override
72  	public int updateHasMeasFlagBySurveyId(int surveyId) {
73  		return updateHasMeasFlagBySurveyIds(ImmutableList.of(surveyId));
74  	}
75  
76  	/** {@inheritDoc} */
77  	@Override
78  	public int updateHasMeasFlagBySurveyIds(List<Integer> surveyIds) {
79  		Assert.notEmpty(surveyIds);
80  
81  		if (log.isDebugEnabled()) {
82  			log.debug(String.format("Updating HAS_MEAS flag o sampling operation, for parent surveys: %s", surveyIds.toString()));
83  		}
84  		int rowCount = 0;
85  
86  		// get get sampling operation WITH meas
87  		List<Integer> samplingOperIdsWithMeas = queryListTyped("samplingOperIdsWithMeasBySurveyIds",
88  				"surveyIds", null, surveyIds);
89  		boolean hasSamplingOperIdsWithMeas = CollectionUtils.isNotEmpty(samplingOperIdsWithMeas);
90  
91  		// get sampling operation WITHOUT meas
92  		List<Integer> samplingOperIdsNoMeas = queryListTyped("samplingOperIdsBySurveyIdsNotInIds",
93  				"surveyIds", null, surveyIds,
94  				"excludedSamplingOperIds", null, hasSamplingOperIdsWithMeas ? samplingOperIdsWithMeas : ImmutableList.of(-99999999));
95  
96  		// Update get sampling operation WITH meas
97  		if (hasSamplingOperIdsWithMeas) {
98  			rowCount += queryUpdate("updateSamplingOperHasMeas",
99  					"samplingOperHasMeas", StringType.INSTANCE, Daos.convertToString(true),
100 					"samplingOperIds", null, samplingOperIdsWithMeas);
101 		}
102 
103 		// Update get sampling operation WITHOUT meas
104 		if (CollectionUtils.isNotEmpty(samplingOperIdsNoMeas)) {
105 			rowCount += queryUpdate("updateSamplingOperHasMeas",
106 					"samplingOperHasMeas", StringType.INSTANCE, Daos.convertToString(false),
107 					"samplingOperIds", null, samplingOperIdsNoMeas);
108 		}
109 
110 		return rowCount;
111 	}
112 }