1 package fr.ifremer.quadrige3.core.dao.technical.hibernate;
2
3 /*-
4 * #%L
5 * Quadrige3 Core :: Quadrige3 Core Shared
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
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 * Collection of helper methods for dealing with configuration settings.
41 *
42 * @see org.hibernate.internal.util.config.ConfigurationHelper
43 * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
44 * @since 1.0
45 */
46 public class ConfigurationHelper {
47
48 /**
49 * <p>getInteger.</p>
50 *
51 * @param key a {@link java.lang.String} object.
52 * @param properties a {@link java.util.Properties} object.
53 * @return a {@link java.lang.Integer} object.
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 * <p>getString.</p>
61 *
62 * @param key a {@link java.lang.String} object.
63 * @param properties a {@link java.util.Properties} object.
64 * @return a {@link java.lang.String} object.
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 * <p>getString.</p>
72 *
73 * @param key a {@link java.lang.String} object.
74 * @param properties a {@link java.util.Properties} object.
75 * @param defaultValue a {@link java.lang.String} object.
76 * @return a {@link java.lang.String} object.
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 * <p>getMultimap.</p>
84 *
85 * @param key a {@link java.lang.String} object.
86 * @param properties a {@link java.util.Properties} object.
87 * @return a {@link com.google.common.collect.Multimap} object.
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 * <p>getLong.</p>
96 *
97 * @param key a {@link java.lang.String} object.
98 * @param properties a {@link java.util.Properties} object.
99 * @param defaultValue a int.
100 * @return a long.
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 * <p>getBoolean.</p>
108 *
109 * @param key a {@link java.lang.String} object.
110 * @param properties a {@link java.util.Properties} object.
111 * @param defaultValue a boolean.
112 * @return a boolean.
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 * <p>getInt.</p>
120 *
121 * @param key a {@link java.lang.String} object.
122 * @param properties a {@link java.util.Properties} object.
123 * @param defaultValue a int.
124 * @return a int.
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 * <p>getMultimap.</p>
132 *
133 * @param property a {@link java.lang.String} object.
134 * @return a {@link com.google.common.collect.Multimap} object.
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 // Add to result
153 result.putAll(mapKey, valuesList);
154 }
155
156 return result;
157 }
158
159 /**
160 * <p>setMultimap.</p>
161 *
162 * @param key a {@link java.lang.String} object.
163 * @param multimap a {@link com.google.common.collect.Multimap} object.
164 * @param properties a {@link java.util.Properties} object.
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 * <p>getList.</p>
187 *
188 * @param key a {@link java.lang.String} object.
189 * @param properties a {@link java.util.Properties} object.
190 * @return a {@link java.util.Collection} object.
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 * <p>setList.</p>
203 *
204 * @param key a {@link java.lang.String} object.
205 * @param values a {@link java.lang.Iterable} object.
206 * @param properties a {@link java.util.Properties} object.
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 * <p>setDate.</p>
219 *
220 * @param key a {@link java.lang.String} object.
221 * @param date a {@link java.util.Date} object.
222 * @param pattern a {@link java.lang.String} object.
223 * @param withTime a boolean.
224 * @param properties a {@link java.util.Properties} object.
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 * <p>setString.</p>
240 *
241 * @param key a {@link java.lang.String} object.
242 * @param property a {@link java.lang.String} object.
243 * @param properties a {@link java.util.Properties} object.
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 * <p>setBoolean.</p>
255 *
256 * @param key a {@link java.lang.String} object.
257 * @param property a {@link java.lang.Boolean} object.
258 * @param properties a {@link java.util.Properties} object.
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 * <p>setFile.</p>
270 *
271 * @param key a {@link java.lang.String} object.
272 * @param file a {@link java.io.File} object.
273 * @param properties a {@link java.util.Properties} object.
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 }