1 package fr.ifremer.quadrige3.core.dao.technical.hibernate;
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
27
28 import com.google.common.base.Joiner;
29 import com.google.common.base.Splitter;
30 import com.google.common.collect.ArrayListMultimap;
31 import com.google.common.collect.Lists;
32 import com.google.common.collect.Multimap;
33 import fr.ifremer.quadrige3.core.dao.technical.Dates;
34 import org.apache.commons.lang3.StringUtils;
35
36 import java.io.File;
37 import java.util.*;
38
39
40
41
42
43
44
45
46 public class ConfigurationHelper {
47
48
49
50
51
52
53
54
55 public static Integer getInteger(String key, Properties properties) {
56 return org.hibernate.internal.util.config.ConfigurationHelper.getInteger(key, properties);
57 }
58
59
60
61
62
63
64
65
66 public static String getString(String key, Properties properties) {
67 return org.hibernate.internal.util.config.ConfigurationHelper.getString(key, properties);
68 }
69
70
71
72
73
74
75
76
77
78 public static String getString(String key, Properties properties, String defaultValue) {
79 return org.hibernate.internal.util.config.ConfigurationHelper.getString(key, properties, defaultValue);
80 }
81
82
83
84
85
86
87
88
89 public static Multimap<String, String> getMultimap(String key, Properties properties) {
90 String property = getString(key, properties);
91 return getMultimap(property);
92 }
93
94
95
96
97
98
99
100
101
102 public static long getLong(String key, Properties properties, int defaultValue) {
103 return org.hibernate.internal.util.config.ConfigurationHelper.getLong(key, properties, defaultValue);
104 }
105
106
107
108
109
110
111
112
113
114 public static boolean getBoolean(String key, Properties properties, boolean defaultValue) {
115 return org.hibernate.internal.util.config.ConfigurationHelper.getBoolean(key, properties, defaultValue);
116 }
117
118
119
120
121
122
123
124
125
126 public static int getInt(String key, Properties properties, int defaultValue) {
127 return org.hibernate.internal.util.config.ConfigurationHelper.getInt(key, properties, defaultValue);
128 }
129
130
131
132
133
134
135
136 public static Multimap<String, String> getMultimap(String property) {
137 if (StringUtils.isBlank(property)) {
138 return null;
139 }
140
141 Multimap<String, String> result = ArrayListMultimap.create();
142
143 Map<String, String> valuesStrByKey = Splitter
144 .on(',')
145 .trimResults()
146 .withKeyValueSeparator(Splitter.on('|').trimResults())
147 .split(property);
148 for (String mapKey: valuesStrByKey.keySet()) {
149 String valuesStr = valuesStrByKey.get(mapKey);
150 List<String> valuesList = Splitter.on(';').trimResults().splitToList(valuesStr);
151
152
153 result.putAll(mapKey, valuesList);
154 }
155
156 return result;
157 }
158
159
160
161
162
163
164
165
166 public static void setMultimap(String key, Multimap<String, String> multimap, Properties properties) {
167 if (multimap == null || multimap.isEmpty()) {
168 properties.remove(key);
169 return;
170 }
171
172 StringBuilder propertyBuilder = new StringBuilder();
173 for (String mapKey : multimap.keySet()) {
174 Collection<String> mapValues = multimap.get(mapKey);
175 propertyBuilder.append(',')
176 .append(mapKey.toUpperCase())
177 .append('|');
178 Joiner.on(';')
179 .appendTo(propertyBuilder, mapValues);
180 }
181
182 properties.setProperty(key, propertyBuilder.substring(1));
183 }
184
185
186
187
188
189
190
191
192 public static Collection<String> getList(String key, Properties properties) {
193 String valueStr = properties.getProperty(key);
194 if (valueStr == null || valueStr.length() == 0) {
195 return null;
196 }
197
198 return Lists.newArrayList(Splitter.on(',').split(valueStr));
199 }
200
201
202
203
204
205
206
207
208 public static void setList(String key, Iterable<?> values, Properties properties) {
209 if (values == null) {
210 properties.remove(key);
211 return;
212 }
213
214 properties.setProperty(key, Joiner.on(',').join(values));
215 }
216
217
218
219
220
221
222
223
224
225
226 public static void setDate(String key, Date date, String pattern, boolean withTime, Properties properties) {
227 if (date == null) {
228 properties.remove(key);
229 return;
230 }
231 if (!withTime) {
232 date = Dates.truncate(date, Calendar.DAY_OF_MONTH);
233 }
234 String property = Dates.formatDate(date, pattern);
235 properties.setProperty(key, property);
236 }
237
238
239
240
241
242
243
244
245 public static void setString(String key, String property, Properties properties) {
246 if (property == null) {
247 properties.remove(key);
248 return;
249 }
250 properties.setProperty(key, property);
251 }
252
253
254
255
256
257
258
259
260 public static void setBoolean(String key, Boolean property, Properties properties) {
261 if (property == null) {
262 properties.remove(key);
263 return;
264 }
265 properties.setProperty(key, property.toString());
266 }
267
268
269
270
271
272
273
274
275 public static void setFile(String key, File file, Properties properties) {
276 if (file == null) {
277 properties.remove(key);
278 return;
279 }
280 properties.setProperty(key, file.getAbsolutePath());
281 }
282
283
284 }