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