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