001
014
015 package com.liferay.portal.util;
016
017 import com.liferay.portal.kernel.bean.BeanReference;
018 import com.liferay.portal.kernel.util.GetterUtil;
019 import com.liferay.portal.kernel.util.StringPool;
020 import com.liferay.portal.kernel.util.StringUtil;
021 import com.liferay.portal.kernel.util.Validator;
022 import com.liferay.portal.service.PortalPreferencesLocalService;
023 import com.liferay.portlet.PortalPreferencesWrapper;
024 import com.liferay.portlet.PortalPreferencesWrapperCacheUtil;
025 import com.liferay.util.ContentUtil;
026
027 import java.util.Enumeration;
028 import java.util.Properties;
029
030 import javax.portlet.PortletPreferences;
031
032
035 public class PrefsPropsUtil {
036
037 public static boolean getBoolean(long companyId, String name) {
038 PortletPreferences preferences = getPreferences(companyId, true);
039
040 return getBoolean(preferences, companyId, name);
041 }
042
043 public static boolean getBoolean(
044 long companyId, String name, boolean defaultValue) {
045
046 PortletPreferences preferences = getPreferences(companyId, true);
047
048 return getBoolean(preferences, companyId, name, defaultValue);
049 }
050
051 public static boolean getBoolean(
052 PortletPreferences preferences, long companyId, String name) {
053
054 return GetterUtil.getBoolean(getString(preferences, companyId, name));
055 }
056
057 public static boolean getBoolean(
058 PortletPreferences preferences, long companyId, String name,
059 boolean defaultValue) {
060
061 return GetterUtil.getBoolean(
062 getString(preferences, companyId, name, defaultValue));
063 }
064
065 public static boolean getBoolean(String name) {
066 PortletPreferences preferences = getPreferences(true);
067
068 return getBoolean(preferences, 0, name);
069 }
070
071 public static boolean getBoolean(String name, boolean defaultValue) {
072 PortletPreferences preferences = getPreferences(true);
073
074 return getBoolean(preferences, 0, name, defaultValue);
075 }
076
077 public static String getContent(long companyId, String name) {
078 PortletPreferences preferences = getPreferences(companyId, true);
079
080 return getContent(preferences, companyId, name);
081 }
082
083 public static String getContent(
084 PortletPreferences preferences, long companyId, String name) {
085
086 String value = preferences.getValue(name, StringPool.BLANK);
087
088 if (Validator.isNotNull(value)) {
089 return value;
090 }
091
092 return ContentUtil.get(PropsUtil.get(name));
093 }
094
095 public static String getContent(String name) {
096 PortletPreferences preferences = getPreferences(true);
097
098 return getContent(preferences, 0, name);
099 }
100
101 public static double getDouble(long companyId, String name) {
102 PortletPreferences preferences = getPreferences(companyId, true);
103
104 return getDouble(preferences, companyId, name);
105 }
106
107 public static double getDouble(
108 long companyId, String name, double defaultValue) {
109
110 PortletPreferences preferences = getPreferences(companyId, true);
111
112 return getDouble(preferences, companyId, name, defaultValue);
113 }
114
115 public static double getDouble(
116 PortletPreferences preferences, long companyId, String name) {
117
118 return GetterUtil.getDouble(getString(preferences, companyId, name));
119 }
120
121 public static double getDouble(
122 PortletPreferences preferences, long companyId, String name,
123 double defaultValue) {
124
125 return GetterUtil.getDouble(
126 getString(preferences, companyId, name, defaultValue));
127 }
128
129 public static double getDouble(String name) {
130 PortletPreferences preferences = getPreferences(true);
131
132 return getDouble(preferences, 0, name);
133 }
134
135 public static double getDouble(String name, double defaultValue) {
136 PortletPreferences preferences = getPreferences(true);
137
138 return getDouble(preferences, 0, name, defaultValue);
139 }
140
141 public static int getInteger(long companyId, String name) {
142 PortletPreferences preferences = getPreferences(companyId, true);
143
144 return getInteger(preferences, companyId, name);
145 }
146
147 public static int getInteger(
148 long companyId, String name, int defaultValue) {
149
150 PortletPreferences preferences = getPreferences(companyId, true);
151
152 return getInteger(preferences, companyId, name, defaultValue);
153 }
154
155 public static int getInteger(
156 PortletPreferences preferences, long companyId, String name) {
157
158 return GetterUtil.getInteger(getString(preferences, companyId, name));
159 }
160
161 public static int getInteger(
162 PortletPreferences preferences, long companyId, String name,
163 int defaultValue) {
164
165 return GetterUtil.getInteger(
166 getString(preferences, companyId, name, defaultValue));
167 }
168
169 public static int getInteger(String name) {
170 PortletPreferences preferences = getPreferences(true);
171
172 return getInteger(preferences, 0, name);
173 }
174
175 public static int getInteger(String name, int defaultValue) {
176 PortletPreferences preferences = getPreferences(true);
177
178 return getInteger(preferences, 0, name, defaultValue);
179 }
180
181 public static long getLong(long companyId, String name) {
182 PortletPreferences preferences = getPreferences(companyId, true);
183
184 return getLong(preferences, companyId, name);
185 }
186
187 public static long getLong(long companyId, String name, long defaultValue) {
188 PortletPreferences preferences = getPreferences(companyId, true);
189
190 return getLong(preferences, companyId, name, defaultValue);
191 }
192
193 public static long getLong(
194 PortletPreferences preferences, long companyId, String name) {
195
196 return GetterUtil.getLong(getString(preferences, companyId, name));
197 }
198
199 public static long getLong(
200 PortletPreferences preferences, long companyId, String name,
201 long defaultValue) {
202
203 return GetterUtil.getLong(
204 getString(preferences, companyId, name, defaultValue));
205 }
206
207 public static long getLong(String name) {
208 PortletPreferences preferences = getPreferences(true);
209
210 return getLong(preferences, 0, name);
211 }
212
213 public static long getLong(String name, long defaultValue) {
214 PortletPreferences preferences = getPreferences(true);
215
216 return getLong(preferences, 0, name, defaultValue);
217 }
218
219 public static PortletPreferences getPreferences() {
220 return getPreferences(false);
221 }
222
223 public static PortletPreferences getPreferences(boolean readOnly) {
224 PortalPreferencesWrapper portalPreferencesWrapper =
225 PortalPreferencesWrapperCacheUtil.get(
226 PortletKeys.PREFS_OWNER_ID_DEFAULT,
227 PortletKeys.PREFS_OWNER_TYPE_COMPANY);
228
229 if (portalPreferencesWrapper != null) {
230 if (!readOnly) {
231 portalPreferencesWrapper = portalPreferencesWrapper.clone();
232 }
233
234 return portalPreferencesWrapper;
235 }
236
237 return _portalPreferencesLocalService.getPreferences(
238 PortletKeys.PREFS_OWNER_ID_DEFAULT,
239 PortletKeys.PREFS_OWNER_TYPE_COMPANY);
240 }
241
242 public static PortletPreferences getPreferences(long companyId) {
243 return getPreferences(companyId, false);
244 }
245
246 public static PortletPreferences getPreferences(
247 long companyId, boolean readOnly) {
248
249 long ownerId = companyId;
250 int ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
251
252 PortalPreferencesWrapper portalPreferencesWrapper =
253 PortalPreferencesWrapperCacheUtil.get(ownerId, ownerType);
254
255 if (portalPreferencesWrapper != null) {
256 if (!readOnly) {
257 portalPreferencesWrapper = portalPreferencesWrapper.clone();
258 }
259
260 return portalPreferencesWrapper;
261 }
262
263 return _portalPreferencesLocalService.getPreferences(
264 ownerId, ownerType);
265 }
266
267 public static Properties getProperties(
268 PortletPreferences preferences, long companyId, String prefix,
269 boolean removePrefix) {
270
271 Properties newProperties = new Properties();
272
273 Enumeration<String> enu = preferences.getNames();
274
275 while (enu.hasMoreElements()) {
276 String key = enu.nextElement();
277
278 if (key.startsWith(prefix)) {
279 String value = preferences.getValue(key, StringPool.BLANK);
280
281 if (removePrefix) {
282 key = key.substring(prefix.length());
283 }
284
285 newProperties.setProperty(key, value);
286 }
287 }
288
289 return newProperties;
290 }
291
292 public static Properties getProperties(
293 String prefix, boolean removePrefix) {
294
295 PortletPreferences preferences = getPreferences(true);
296
297 return getProperties(preferences, 0, prefix, removePrefix);
298 }
299
300 public static short getShort(long companyId, String name) {
301 PortletPreferences preferences = getPreferences(companyId, true);
302
303 return getShort(preferences, companyId, name);
304 }
305
306 public static short getShort(
307 long companyId, String name, short defaultValue) {
308
309 PortletPreferences preferences = getPreferences(companyId, true);
310
311 return getShort(preferences, companyId, name, defaultValue);
312 }
313
314 public static short getShort(
315 PortletPreferences preferences, long companyId, String name) {
316
317 return GetterUtil.getShort(getString(preferences, companyId, name));
318 }
319
320 public static short getShort(
321 PortletPreferences preferences, long companyId, String name,
322 short defaultValue) {
323
324 return GetterUtil.getShort(
325 getString(preferences, companyId, name, defaultValue));
326 }
327
328 public static short getShort(String name) {
329 PortletPreferences preferences = getPreferences(true);
330
331 return getShort(preferences, 0, name);
332 }
333
334 public static short getShort(String name, short defaultValue) {
335 PortletPreferences preferences = getPreferences(true);
336
337 return getShort(preferences, 0, name, defaultValue);
338 }
339
340 public static String getString(long companyId, String name) {
341 PortletPreferences preferences = getPreferences(companyId, true);
342
343 return getString(preferences, companyId, name);
344 }
345
346 public static String getString(
347 long companyId, String name, String defaultValue) {
348
349 PortletPreferences preferences = getPreferences(companyId, true);
350
351 return getString(preferences, companyId, name, defaultValue);
352 }
353
354 public static String getString(
355 PortletPreferences preferences, long companyId, String name) {
356
357 String value = PropsUtil.get(name);
358
359 return preferences.getValue(name, value);
360 }
361
362 public static String getString(
363 PortletPreferences preferences, long companyId, String name,
364 boolean defaultValue) {
365
366 String value = getString(preferences, companyId, name);
367
368 if (value != null) {
369 return value;
370 }
371
372 if (defaultValue) {
373 return preferences.getValue(name, StringPool.TRUE);
374 }
375
376 return preferences.getValue(name, StringPool.FALSE);
377 }
378
379 public static String getString(
380 PortletPreferences preferences, long companyId, String name,
381 double defaultValue) {
382
383 String value = getString(preferences, companyId, name);
384
385 if (value != null) {
386 return value;
387 }
388
389 return String.valueOf(defaultValue);
390 }
391
392 public static String getString(
393 PortletPreferences preferences, long companyId, String name,
394 int defaultValue) {
395
396 String value = getString(preferences, companyId, name);
397
398 if (value != null) {
399 return value;
400 }
401
402 return String.valueOf(defaultValue);
403 }
404
405 public static String getString(
406 PortletPreferences preferences, long companyId, String name,
407 long defaultValue) {
408
409 String value = getString(preferences, companyId, name);
410
411 if (value != null) {
412 return value;
413 }
414
415 return String.valueOf(defaultValue);
416 }
417
418 public static String getString(
419 PortletPreferences preferences, long companyId, String name,
420 short defaultValue) {
421
422 String value = getString(preferences, companyId, name);
423
424 if (value != null) {
425 return value;
426 }
427
428 return String.valueOf(defaultValue);
429 }
430
431 public static String getString(
432 PortletPreferences preferences, long companyId, String name,
433 String defaultValue) {
434
435 String value = getString(preferences, companyId, name);
436
437 if (value != null) {
438 return value;
439 }
440
441 return defaultValue;
442 }
443
444 public static String getString(String name) {
445 PortletPreferences preferences = getPreferences(true);
446
447 return getString(preferences, 0, name);
448 }
449
450 public static String getString(String name, String defaultValue) {
451 PortletPreferences preferences = getPreferences(true);
452
453 return getString(preferences, 0, name, defaultValue);
454 }
455
456 public static String[] getStringArray(
457 long companyId, String name, String delimiter) {
458
459 PortletPreferences preferences = getPreferences(companyId, true);
460
461 return getStringArray(preferences, companyId, name, delimiter);
462 }
463
464 public static String[] getStringArray(
465 long companyId, String name, String delimiter, String[] defaultValue) {
466
467 PortletPreferences preferences = getPreferences(companyId, true);
468
469 return getStringArray(
470 preferences, companyId, name, delimiter, defaultValue);
471 }
472
473 public static String[] getStringArray(
474 PortletPreferences preferences, long companyId, String name,
475 String delimiter) {
476
477 String value = PropsUtil.get(name);
478
479 value = preferences.getValue(name, value);
480
481 return StringUtil.split(value, delimiter);
482 }
483
484 public static String[] getStringArray(
485 PortletPreferences preferences, long companyId, String name,
486 String delimiter, String[] defaultValue) {
487
488 String value = preferences.getValue(name, null);
489
490 if (value == null) {
491 return defaultValue;
492 }
493
494 return StringUtil.split(value, delimiter);
495 }
496
497 public static String[] getStringArray(String name, String delimiter) {
498 PortletPreferences preferences = getPreferences(true);
499
500 return getStringArray(preferences, 0, name, delimiter);
501 }
502
503 public static String[] getStringArray(
504 String name, String delimiter, String[] defaultValue) {
505
506 PortletPreferences preferences = getPreferences(true);
507
508 return getStringArray(preferences, 0, name, delimiter, defaultValue);
509 }
510
511 public static String getStringFromNames(long companyId, String... names) {
512 for (String name : names) {
513 String value = getString(companyId, name);
514
515 if (Validator.isNotNull(value)) {
516 return value;
517 }
518 }
519
520 return null;
521 }
522
523 @BeanReference(type = PortalPreferencesLocalService.class)
524 private static PortalPreferencesLocalService _portalPreferencesLocalService;
525
526 }