View Javadoc
1   package fr.ifremer.quadrige3.core.dao.system.generalCondition;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Server API
6    * %%
7    * Copyright (C) 2017 - 2024 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (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 Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  import com.google.common.collect.Maps;
25  import fr.ifremer.quadrige3.core.dao.referential.StatusCode;
26  import fr.ifremer.quadrige3.core.dao.technical.Assert;
27  import fr.ifremer.quadrige3.core.dao.technical.jdbc.OptionalDataSourceJdbcDaoSupport;
28  import fr.ifremer.quadrige3.core.vo.system.generalCondition.GeneralConditionVO;
29  import org.springframework.beans.factory.InitializingBean;
30  import org.springframework.stereotype.Repository;
31  
32  import javax.annotation.Resource;
33  import javax.sql.DataSource;
34  import java.sql.ResultSet;
35  import java.sql.SQLException;
36  import java.util.Map;
37  import java.util.Properties;
38  
39  @Repository("generalConditionJdbcDao")
40  public class GeneralConditionJdbcDaoImpl extends OptionalDataSourceJdbcDaoSupport
41      implements InitializingBean {
42  
43      private final static String QUERIES_FILE_PATH = "queries.jdbc.xml";
44  
45      @Resource(name = "queriesJdbcProperties")
46      private Properties queriesJdbcProperties;
47  
48      public GeneralConditionJdbcDaoImpl(DataSource dataSource) {
49          super(dataSource);
50      }
51  
52      public GeneralConditionVO getLast() {
53          String sql = queriesJdbcProperties.getProperty("lastGeneralCondition");
54          Map<String, Object> paramMap = Maps.newHashMap();
55          paramMap.put("statusCd", StatusCode.ENABLE.getValue());
56          return query(null, sql, paramMap, this::toGeneralConditionVO);
57      }
58  
59      public boolean isAcceptedForUser(int id, int userId) {
60          String sql = queriesJdbcProperties.getProperty("acceptedGeneralCondition");
61          Map<String, Object> paramMap = Maps.newHashMap();
62          paramMap.put("id", id);
63          paramMap.put("userId", userId);
64          Long count = queryCount(null, sql, paramMap);
65          return count >= 1;
66      }
67  
68  
69      public void accept(int id, int userId) {
70          String sql = queriesJdbcProperties.getProperty("acceptGeneralCondition");
71          Map<String, Object> paramMap = Maps.newHashMap();
72          paramMap.put("id", id);
73          paramMap.put("userId", userId);
74          queryUpdateAndCommit(null, sql, paramMap);
75      }
76  
77      protected GeneralConditionVO toGeneralConditionVO(ResultSet source) throws SQLException {
78          GeneralConditionVO target = new GeneralConditionVO();
79          int col = 0;
80          target.setId(safeGetInteger(source, ++col));
81          target.setContent(source.getString(++col));
82          return target;
83      }
84  
85      @Override
86      public void afterPropertiesSet() throws Exception {
87          checkQueryExists("lastGeneralCondition");
88          checkQueryExists("acceptedGeneralCondition");
89          checkQueryExists("acceptGeneralCondition");
90      }
91  
92      protected void checkQueryExists(String queryName) {
93          Assert.notBlank(queriesJdbcProperties.getProperty(queryName),
94              "Property with name [%s] not exists on JDBC queries file");
95      }
96  }