View Javadoc
1   package fr.ifremer.dali.dao.technical;
2   
3   /*
4    * #%L
5    * Dali :: Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2013 - 2014 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 fr.ifremer.dali.dto.data.photo.PhotoDTO;
27  import fr.ifremer.quadrige3.core.dao.data.photo.Photo;
28  import fr.ifremer.quadrige3.core.dao.referential.StatusCode;
29  import fr.ifremer.quadrige3.core.dao.technical.Assert;
30  import org.apache.commons.io.FilenameUtils;
31  import org.apache.commons.lang3.StringUtils;
32  import org.hibernate.Query;
33  
34  import java.math.BigDecimal;
35  import java.util.Date;
36  import java.util.Iterator;
37  import java.util.List;
38  import java.util.Locale;
39  
40  import static org.nuiton.i18n.I18n.n;
41  
42  /**
43   * Helper class for all DAO operations
44   *
45   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
46   * @since 1.0
47   */
48  public class Daos extends fr.ifremer.quadrige3.core.dao.technical.Daos {
49  
50      // TODO get this constant by configuration
51      /** Constant <code>SURVEY_OBJECT_TYPE="PASS"</code> */
52      public final static String SURVEY_OBJECT_TYPE = "PASS";
53      /** Constant <code>SAMPLING_OPERATION_OBJECT_TYPE="PREL"</code> */
54      public final static String SAMPLING_OPERATION_OBJECT_TYPE = "PREL";
55  
56      static {
57          // reserve I18n entries
58          for (StatusCode statusCode : StatusCode.values()) {
59              String nameKey = String.format("dali.persistence.status.%s.name", statusCode.name());
60              n(nameKey);
61          }
62      }
63  
64      /**
65       * <p>Constructor for Daos.</p>
66       */
67      protected Daos() {
68          // Helper class = protected constructor
69      }
70  
71      /**
72       * <p>withStatus.</p>
73       *
74       * @param query a {@link Query} object.
75       * @param statusCodes a {@link List} object.
76       * @return a {@link Query} object.
77       */
78      public static Query withStatus(Query query, List<String> statusCodes) {
79          Assert.notEmpty(statusCodes);
80          Assert.nonNullElement(statusCodes);
81          query.setParameterList("statusCodes", statusCodes);
82          return query;
83      }
84  
85      /**
86       * <p>queryIteratorWithStatus.</p>
87       *
88       * @param query a {@link Query} object.
89       * @param statusCodes a {@link List} object.
90       * @return a {@link Iterator} object.
91       */
92      @SuppressWarnings("unchecked")
93      public static Iterator<Object[]> queryIteratorWithStatus(Query query, List<String> statusCodes) {
94          return withStatus(query, statusCodes).iterate();
95      }
96  
97      /**
98       * <p>convertToInteger.</p>
99       *
100      * @param numericalValue a {@link Float} object.
101      * @return a {@link Integer} object.
102      */
103     public static Integer convertToInteger(Number numericalValue) {
104         if (numericalValue == null) {
105             return null;
106         }
107         return numericalValue.intValue();
108     }
109 
110     /**
111      * <p>convertDateOnlyToSQLString.</p>
112      *
113      * @param date a {@link Date} object.
114      * @return a {@link String} object.
115      */
116     public static String convertDateOnlyToSQLString(Date date) {
117         Assert.notNull(date);
118         java.sql.Date jdbcDate = date instanceof java.sql.Date
119             ? (java.sql.Date) date
120             : new java.sql.Date(date.getTime());
121 
122         return "\'" + jdbcDate.toString() + " 00:00:00\'";
123     }
124 
125     /**
126      * <p>convertToBigDecimal.</p>
127      *
128      * @param value a {@link Double} object.
129      * @param digitNumber a {@link Integer} object.
130      * @return a {@link BigDecimal} object.
131      */
132     public static BigDecimal convertToBigDecimal(Double value, Integer digitNumber) {
133 
134         if (value == null) {
135             return null;
136         }
137 
138         int digitNb = digitNumber == null ? 0 : digitNumber;
139         return new BigDecimal(String.format(Locale.US, "%." + digitNb + "f", value));
140     }
141 
142     /* -- internal methods -- */
143     /**
144      * Build a file path for a Photo like Q² <br/>
145      * ex: PASS/OBJ60092549/PASS-OBJ60092549-60000320.jpg for the survey id=60092549 and photo id=60000320 <br/>
146      * or: PREL/OBJ60165512/PREL-OBJ60165512-60003120.jpg for the sampling operation id=60165512 and photo id=60003120
147      *
148      * Note: the ids are local only, need to recompute with remote ids for synchronization
149      *
150      * @param source the source photo to obtain file extension
151      * @param target the target photo (the entity must be persisted before calling this method)
152      * @return the local file path
153      */
154     public static String computePhotoFilePath(PhotoDTO source, Photo target) {
155         if (source == null || StringUtils.isBlank(source.getFullPath())) {
156             return null;
157         }
158         if (target == null || target.getPhotoId() == null || target.getObjectType() == null || target.getObjectId() == null) {
159             return null;
160         }
161 
162         return computePhotoFilePath(
163             target.getObjectType().getObjectTypeCd(),
164             target.getObjectId(),
165             target.getPhotoId(),
166             FilenameUtils.getExtension(source.getFullPath()));
167     }
168 
169 }