001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
034     * @author Brian Wing Shun Chan
035     */
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                    else {
100                            return ContentUtil.get(PropsUtil.get(name));
101                    }
102            }
103    
104            public static String getContent(String name) throws SystemException {
105                    PortletPreferences preferences = getPreferences(true);
106    
107                    return getContent(preferences, 0, name);
108            }
109    
110            public static double getDouble(long companyId, String name)
111                    throws SystemException {
112    
113                    PortletPreferences preferences = getPreferences(companyId, true);
114    
115                    return getDouble(preferences, companyId, name);
116            }
117    
118            public static double getDouble(
119                            long companyId, String name, double defaultValue)
120                    throws SystemException {
121    
122                    PortletPreferences preferences = getPreferences(companyId, true);
123    
124                    return getDouble(preferences, companyId, name, defaultValue);
125            }
126    
127            public static double getDouble(
128                    PortletPreferences preferences, long companyId, String name) {
129    
130                    return GetterUtil.getDouble(getString(preferences, companyId, name));
131            }
132    
133            public static double getDouble(
134                    PortletPreferences preferences, long companyId, String name,
135                    double defaultValue) {
136    
137                    return GetterUtil.getDouble(
138                            getString(preferences, companyId, name, defaultValue));
139            }
140    
141            public static double getDouble(String name) throws SystemException {
142                    PortletPreferences preferences = getPreferences(true);
143    
144                    return getDouble(preferences, 0, name);
145            }
146    
147            public static double getDouble(String name, double defaultValue)
148                    throws SystemException {
149    
150                    PortletPreferences preferences = getPreferences(true);
151    
152                    return getDouble(preferences, 0, name, defaultValue);
153            }
154    
155            public static int getInteger(long companyId, String name)
156                    throws SystemException {
157    
158                    PortletPreferences preferences = getPreferences(companyId, true);
159    
160                    return getInteger(preferences, companyId, name);
161            }
162    
163            public static int getInteger(long companyId, String name, int defaultValue)
164                    throws SystemException {
165    
166                    PortletPreferences preferences = getPreferences(companyId, true);
167    
168                    return getInteger(preferences, companyId, name, defaultValue);
169            }
170    
171            public static int getInteger(
172                    PortletPreferences preferences, long companyId, String name) {
173    
174                    return GetterUtil.getInteger(getString(preferences, companyId, name));
175            }
176    
177            public static int getInteger(
178                    PortletPreferences preferences, long companyId, String name,
179                    int defaultValue) {
180    
181                    return GetterUtil.getInteger(
182                            getString(preferences, companyId, name, defaultValue));
183            }
184    
185            public static int getInteger(String name) throws SystemException {
186                    PortletPreferences preferences = getPreferences(true);
187    
188                    return getInteger(preferences, 0, name);
189            }
190    
191            public static int getInteger(String name, int defaultValue)
192                    throws SystemException {
193    
194                    PortletPreferences preferences = getPreferences(true);
195    
196                    return getInteger(preferences, 0, name, defaultValue);
197            }
198    
199            public static long getLong(long companyId, String name)
200                    throws SystemException {
201    
202                    PortletPreferences preferences = getPreferences(companyId, true);
203    
204                    return getLong(preferences, companyId, name);
205            }
206    
207            public static long getLong(long companyId, String name, long defaultValue)
208                    throws SystemException {
209    
210                    PortletPreferences preferences = getPreferences(companyId, true);
211    
212                    return getLong(preferences, companyId, name, defaultValue);
213            }
214    
215            public static long getLong(
216                    PortletPreferences preferences, long companyId, String name) {
217    
218                    return GetterUtil.getLong(getString(preferences, companyId, name));
219            }
220    
221            public static long getLong(
222                    PortletPreferences preferences, long companyId, String name,
223                    long defaultValue) {
224    
225                    return GetterUtil.getLong(
226                            getString(preferences, companyId, name, defaultValue));
227            }
228    
229            public static long getLong(String name) throws SystemException {
230                    PortletPreferences preferences = getPreferences(true);
231    
232                    return getLong(preferences, 0, name);
233            }
234    
235            public static long getLong(String name, long defaultValue)
236                    throws SystemException {
237    
238                    PortletPreferences preferences = getPreferences(true);
239    
240                    return getLong(preferences, 0, name, defaultValue);
241            }
242    
243            public static PortletPreferences getPreferences() throws SystemException {
244                    return getPreferences(false);
245            }
246    
247            public static PortletPreferences getPreferences(boolean readOnly)
248                    throws SystemException {
249    
250                    PortalPreferencesWrapper portalPreferencesWrapper =
251                            PortalPreferencesWrapperCacheUtil.get(
252                                    PortletKeys.PREFS_OWNER_ID_DEFAULT,
253                                    PortletKeys.PREFS_OWNER_TYPE_COMPANY);
254    
255                    if (portalPreferencesWrapper != null) {
256                            if (!readOnly) {
257                                    portalPreferencesWrapper = portalPreferencesWrapper.clone();
258                            }
259    
260                            return portalPreferencesWrapper;
261                    }
262    
263                    return _portalPreferencesLocalService.getPreferences(
264                            PortletKeys.PREFS_OWNER_ID_DEFAULT,
265                            PortletKeys.PREFS_OWNER_TYPE_COMPANY);
266            }
267    
268            public static PortletPreferences getPreferences(long companyId)
269                    throws SystemException {
270    
271                    return getPreferences(companyId, false);
272            }
273    
274            public static PortletPreferences getPreferences(
275                            long companyId, boolean readOnly)
276                    throws SystemException {
277    
278                    long ownerId = companyId;
279                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
280    
281                    PortalPreferencesWrapper portalPreferencesWrapper =
282                            PortalPreferencesWrapperCacheUtil.get(ownerId, ownerType);
283    
284                    if (portalPreferencesWrapper != null) {
285                            if (!readOnly) {
286                                    portalPreferencesWrapper = portalPreferencesWrapper.clone();
287                            }
288    
289                            return portalPreferencesWrapper;
290                    }
291    
292                    return _portalPreferencesLocalService.getPreferences(
293                            ownerId, ownerType);
294            }
295    
296            public static Properties getProperties(
297                    PortletPreferences preferences, long companyId, String prefix,
298                    boolean removePrefix) {
299    
300                    Properties newProperties = new Properties();
301    
302                    Enumeration<String> enu = preferences.getNames();
303    
304                    while (enu.hasMoreElements()) {
305                            String key = enu.nextElement();
306    
307                            if (key.startsWith(prefix)) {
308                                    String value = preferences.getValue(key, StringPool.BLANK);
309    
310                                    if (removePrefix) {
311                                            key = key.substring(prefix.length());
312                                    }
313    
314                                    newProperties.setProperty(key, value);
315                            }
316                    }
317    
318                    return newProperties;
319            }
320    
321            public static Properties getProperties(String prefix, boolean removePrefix)
322                    throws SystemException {
323    
324                    PortletPreferences preferences = getPreferences(true);
325    
326                    return getProperties(preferences, 0, prefix, removePrefix);
327            }
328    
329            public static short getShort(long companyId, String name)
330                    throws SystemException {
331    
332                    PortletPreferences preferences = getPreferences(companyId, true);
333    
334                    return getShort(preferences, companyId, name);
335            }
336    
337            public static short getShort(
338                            long companyId, String name, short defaultValue)
339                    throws SystemException {
340    
341                    PortletPreferences preferences = getPreferences(companyId, true);
342    
343                    return getShort(preferences, companyId, name, defaultValue);
344            }
345    
346            public static short getShort(
347                    PortletPreferences preferences, long companyId, String name) {
348    
349                    return GetterUtil.getShort(getString(preferences, companyId, name));
350            }
351    
352            public static short getShort(
353                    PortletPreferences preferences, long companyId, String name,
354                    short defaultValue) {
355    
356                    return GetterUtil.getShort(
357                            getString(preferences, companyId, name, defaultValue));
358            }
359    
360            public static short getShort(String name) throws SystemException {
361                    PortletPreferences preferences = getPreferences(true);
362    
363                    return getShort(preferences, 0, name);
364            }
365    
366            public static short getShort(String name, short defaultValue)
367                    throws SystemException {
368    
369                    PortletPreferences preferences = getPreferences(true);
370    
371                    return getShort(preferences, 0, name, defaultValue);
372            }
373    
374            public static String getString(long companyId, String name)
375                    throws SystemException {
376    
377                    PortletPreferences preferences = getPreferences(companyId, true);
378    
379                    return getString(preferences, companyId, name);
380            }
381    
382            public static String getString(
383                            long companyId, String name, String defaultValue)
384                    throws SystemException {
385    
386                    PortletPreferences preferences = getPreferences(companyId, true);
387    
388                    return getString(preferences, companyId, name, defaultValue);
389            }
390    
391            public static String getString(
392                    PortletPreferences preferences, long companyId, String name) {
393    
394                    String value = PropsUtil.get(name);
395    
396                    return preferences.getValue(name, value);
397            }
398    
399            public static String getString(
400                    PortletPreferences preferences, long companyId, String name,
401                    boolean defaultValue) {
402    
403                    if (defaultValue) {
404                            return preferences.getValue(name, StringPool.TRUE);
405                    }
406                    else {
407                            return preferences.getValue(name, StringPool.FALSE);
408                    }
409            }
410    
411            public static String getString(
412                    PortletPreferences preferences, long companyId, String name,
413                    double defaultValue) {
414    
415                    String value = getString(preferences, companyId, name);
416    
417                    if (value != null) {
418                            return value;
419                    }
420                    else {
421                            return String.valueOf(defaultValue);
422                    }
423            }
424    
425            public static String getString(
426                    PortletPreferences preferences, long companyId, String name,
427                    int defaultValue) {
428    
429                    String value = getString(preferences, companyId, name);
430    
431                    if (value != null) {
432                            return value;
433                    }
434                    else {
435                            return String.valueOf(defaultValue);
436                    }
437            }
438    
439            public static String getString(
440                    PortletPreferences preferences, long companyId, String name,
441                    long defaultValue) {
442    
443                    String value = getString(preferences, companyId, name);
444    
445                    if (value != null) {
446                            return value;
447                    }
448                    else {
449                            return String.valueOf(defaultValue);
450                    }
451            }
452    
453            public static String getString(
454                    PortletPreferences preferences, long companyId, String name,
455                    short defaultValue) {
456    
457                    String value = getString(preferences, companyId, name);
458    
459                    if (value != null) {
460                            return value;
461                    }
462                    else {
463                            return String.valueOf(defaultValue);
464                    }
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                    else {
477                            return defaultValue;
478                    }
479            }
480    
481            public static String getString(String name) throws SystemException {
482                    PortletPreferences preferences = getPreferences(true);
483    
484                    return getString(preferences, 0, name);
485            }
486    
487            public static String getString(String name, String defaultValue)
488                    throws SystemException {
489    
490                    PortletPreferences preferences = getPreferences(true);
491    
492                    return getString(preferences, 0, name, defaultValue);
493            }
494    
495            public static String[] getStringArray(
496                            long companyId, String name, String delimiter)
497                    throws SystemException {
498    
499                    PortletPreferences preferences = getPreferences(companyId, true);
500    
501                    return getStringArray(preferences, companyId, name, delimiter);
502            }
503    
504            public static String[] getStringArray(
505                            long companyId, String name, String delimiter,
506                            String[] defaultValue)
507                    throws SystemException {
508    
509                    PortletPreferences preferences = getPreferences(companyId, true);
510    
511                    return getStringArray(
512                            preferences, companyId, name, delimiter, defaultValue);
513            }
514    
515            public static String[] getStringArray(
516                    PortletPreferences preferences, long companyId, String name,
517                    String delimiter) {
518    
519                    String value = PropsUtil.get(name);
520    
521                    value = preferences.getValue(name, value);
522    
523                    return StringUtil.split(value, delimiter);
524            }
525    
526            public static String[] getStringArray(
527                    PortletPreferences preferences, long companyId, String name,
528                    String delimiter, String[] defaultValue) {
529    
530                    String value = preferences.getValue(name, null);
531    
532                    if (value == null) {
533                            return defaultValue;
534                    }
535                    else {
536                            return StringUtil.split(value, delimiter);
537                    }
538            }
539    
540            public static String[] getStringArray(String name, String delimiter)
541                    throws SystemException {
542    
543                    PortletPreferences preferences = getPreferences(true);
544    
545                    return getStringArray(preferences, 0, name, delimiter);
546            }
547    
548            public static String[] getStringArray(
549                            String name, String delimiter, String[] defaultValue)
550                    throws SystemException {
551    
552                    PortletPreferences preferences = getPreferences(true);
553    
554                    return getStringArray(preferences, 0, name, delimiter, defaultValue);
555            }
556    
557            public static String getStringFromNames(long companyId, String... names)
558                    throws SystemException {
559    
560                    for (String name : names) {
561                            String value = getString(companyId, name);
562    
563                            if (Validator.isNotNull(value)) {
564                                    return value;
565                            }
566                    }
567    
568                    return null;
569            }
570    
571            @BeanReference(type = PortalPreferencesLocalService.class)
572            private static PortalPreferencesLocalService _portalPreferencesLocalService;
573    
574    }