1 package fr.ifremer.dali.dao.technical;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
44
45
46
47
48 public class Daos extends fr.ifremer.quadrige3.core.dao.technical.Daos {
49
50
51
52 public final static String SURVEY_OBJECT_TYPE = "PASS";
53
54 public final static String SAMPLING_OPERATION_OBJECT_TYPE = "PREL";
55
56 static {
57
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
66
67 protected Daos() {
68
69 }
70
71
72
73
74
75
76
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
87
88
89
90
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
99
100
101
102
103 public static Integer convertToInteger(Number numericalValue) {
104 if (numericalValue == null) {
105 return null;
106 }
107 return numericalValue.intValue();
108 }
109
110
111
112
113
114
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
127
128
129
130
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
143
144
145
146
147
148
149
150
151
152
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 }