001    /**
002     * Copyright (c) 2000-present 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.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    /**
033     * @author Brian Wing Shun Chan
034     */
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                    else {
092                            return ContentUtil.get(PropsUtil.get(name));
093                    }
094            }
095    
096            public static String getContent(String name) {
097                    PortletPreferences preferences = getPreferences(true);
098    
099                    return getContent(preferences, 0, name);
100            }
101    
102            public static double getDouble(long companyId, String name) {
103                    PortletPreferences preferences = getPreferences(companyId, true);
104    
105                    return getDouble(preferences, companyId, name);
106            }
107    
108            public static double getDouble(
109                    long companyId, String name, double defaultValue) {
110    
111                    PortletPreferences preferences = getPreferences(companyId, true);
112    
113                    return getDouble(preferences, companyId, name, defaultValue);
114            }
115    
116            public static double getDouble(
117                    PortletPreferences preferences, long companyId, String name) {
118    
119                    return GetterUtil.getDouble(getString(preferences, companyId, name));
120            }
121    
122            public static double getDouble(
123                    PortletPreferences preferences, long companyId, String name,
124                    double defaultValue) {
125    
126                    return GetterUtil.getDouble(
127                            getString(preferences, companyId, name, defaultValue));
128            }
129    
130            public static double getDouble(String name) {
131                    PortletPreferences preferences = getPreferences(true);
132    
133                    return getDouble(preferences, 0, name);
134            }
135    
136            public static double getDouble(String name, double defaultValue) {
137                    PortletPreferences preferences = getPreferences(true);
138    
139                    return getDouble(preferences, 0, name, defaultValue);
140            }
141    
142            public static int getInteger(long companyId, String name) {
143                    PortletPreferences preferences = getPreferences(companyId, true);
144    
145                    return getInteger(preferences, companyId, name);
146            }
147    
148            public static int getInteger(
149                    long companyId, String name, int defaultValue) {
150    
151                    PortletPreferences preferences = getPreferences(companyId, true);
152    
153                    return getInteger(preferences, companyId, name, defaultValue);
154            }
155    
156            public static int getInteger(
157                    PortletPreferences preferences, long companyId, String name) {
158    
159                    return GetterUtil.getInteger(getString(preferences, companyId, name));
160            }
161    
162            public static int getInteger(
163                    PortletPreferences preferences, long companyId, String name,
164                    int defaultValue) {
165    
166                    return GetterUtil.getInteger(
167                            getString(preferences, companyId, name, defaultValue));
168            }
169    
170            public static int getInteger(String name) {
171                    PortletPreferences preferences = getPreferences(true);
172    
173                    return getInteger(preferences, 0, name);
174            }
175    
176            public static int getInteger(String name, int defaultValue) {
177                    PortletPreferences preferences = getPreferences(true);
178    
179                    return getInteger(preferences, 0, name, defaultValue);
180            }
181    
182            public static long getLong(long companyId, String name) {
183                    PortletPreferences preferences = getPreferences(companyId, true);
184    
185                    return getLong(preferences, companyId, name);
186            }
187    
188            public static long getLong(long companyId, String name, long defaultValue) {
189                    PortletPreferences preferences = getPreferences(companyId, true);
190    
191                    return getLong(preferences, companyId, name, defaultValue);
192            }
193    
194            public static long getLong(
195                    PortletPreferences preferences, long companyId, String name) {
196    
197                    return GetterUtil.getLong(getString(preferences, companyId, name));
198            }
199    
200            public static long getLong(
201                    PortletPreferences preferences, long companyId, String name,
202                    long defaultValue) {
203    
204                    return GetterUtil.getLong(
205                            getString(preferences, companyId, name, defaultValue));
206            }
207    
208            public static long getLong(String name) {
209                    PortletPreferences preferences = getPreferences(true);
210    
211                    return getLong(preferences, 0, name);
212            }
213    
214            public static long getLong(String name, long defaultValue) {
215                    PortletPreferences preferences = getPreferences(true);
216    
217                    return getLong(preferences, 0, name, defaultValue);
218            }
219    
220            public static PortletPreferences getPreferences() {
221                    return getPreferences(false);
222            }
223    
224            public static PortletPreferences getPreferences(boolean readOnly) {
225                    PortalPreferencesWrapper portalPreferencesWrapper =
226                            PortalPreferencesWrapperCacheUtil.get(
227                                    PortletKeys.PREFS_OWNER_ID_DEFAULT,
228                                    PortletKeys.PREFS_OWNER_TYPE_COMPANY);
229    
230                    if (portalPreferencesWrapper != null) {
231                            if (!readOnly) {
232                                    portalPreferencesWrapper = portalPreferencesWrapper.clone();
233                            }
234    
235                            return portalPreferencesWrapper;
236                    }
237    
238                    return _portalPreferencesLocalService.getPreferences(
239                            PortletKeys.PREFS_OWNER_ID_DEFAULT,
240                            PortletKeys.PREFS_OWNER_TYPE_COMPANY);
241            }
242    
243            public static PortletPreferences getPreferences(long companyId) {
244                    return getPreferences(companyId, false);
245            }
246    
247            public static PortletPreferences getPreferences(
248                    long companyId, boolean readOnly) {
249    
250                    long ownerId = companyId;
251                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
252    
253                    PortalPreferencesWrapper portalPreferencesWrapper =
254                            PortalPreferencesWrapperCacheUtil.get(ownerId, ownerType);
255    
256                    if (portalPreferencesWrapper != null) {
257                            if (!readOnly) {
258                                    portalPreferencesWrapper = portalPreferencesWrapper.clone();
259                            }
260    
261                            return portalPreferencesWrapper;
262                    }
263    
264                    return _portalPreferencesLocalService.getPreferences(
265                            ownerId, ownerType);
266            }
267    
268            public static Properties getProperties(
269                    PortletPreferences preferences, long companyId, String prefix,
270                    boolean removePrefix) {
271    
272                    Properties newProperties = new Properties();
273    
274                    Enumeration<String> enu = preferences.getNames();
275    
276                    while (enu.hasMoreElements()) {
277                            String key = enu.nextElement();
278    
279                            if (key.startsWith(prefix)) {
280                                    String value = preferences.getValue(key, StringPool.BLANK);
281    
282                                    if (removePrefix) {
283                                            key = key.substring(prefix.length());
284                                    }
285    
286                                    newProperties.setProperty(key, value);
287                            }
288                    }
289    
290                    return newProperties;
291            }
292    
293            public static Properties getProperties(
294                    String prefix, boolean removePrefix) {
295    
296                    PortletPreferences preferences = getPreferences(true);
297    
298                    return getProperties(preferences, 0, prefix, removePrefix);
299            }
300    
301            public static short getShort(long companyId, String name) {
302                    PortletPreferences preferences = getPreferences(companyId, true);
303    
304                    return getShort(preferences, companyId, name);
305            }
306    
307            public static short getShort(
308                    long companyId, String name, short defaultValue) {
309    
310                    PortletPreferences preferences = getPreferences(companyId, true);
311    
312                    return getShort(preferences, companyId, name, defaultValue);
313            }
314    
315            public static short getShort(
316                    PortletPreferences preferences, long companyId, String name) {
317    
318                    return GetterUtil.getShort(getString(preferences, companyId, name));
319            }
320    
321            public static short getShort(
322                    PortletPreferences preferences, long companyId, String name,
323                    short defaultValue) {
324    
325                    return GetterUtil.getShort(
326                            getString(preferences, companyId, name, defaultValue));
327            }
328    
329            public static short getShort(String name) {
330                    PortletPreferences preferences = getPreferences(true);
331    
332                    return getShort(preferences, 0, name);
333            }
334    
335            public static short getShort(String name, short defaultValue) {
336                    PortletPreferences preferences = getPreferences(true);
337    
338                    return getShort(preferences, 0, name, defaultValue);
339            }
340    
341            public static String getString(long companyId, String name) {
342                    PortletPreferences preferences = getPreferences(companyId, true);
343    
344                    return getString(preferences, companyId, name);
345            }
346    
347            public static String getString(
348                    long companyId, String name, String defaultValue) {
349    
350                    PortletPreferences preferences = getPreferences(companyId, true);
351    
352                    return getString(preferences, companyId, name, defaultValue);
353            }
354    
355            public static String getString(
356                    PortletPreferences preferences, long companyId, String name) {
357    
358                    String value = PropsUtil.get(name);
359    
360                    return preferences.getValue(name, value);
361            }
362    
363            public static String getString(
364                    PortletPreferences preferences, long companyId, String name,
365                    boolean defaultValue) {
366    
367                    if (defaultValue) {
368                            return preferences.getValue(name, StringPool.TRUE);
369                    }
370                    else {
371                            return preferences.getValue(name, StringPool.FALSE);
372                    }
373            }
374    
375            public static String getString(
376                    PortletPreferences preferences, long companyId, String name,
377                    double defaultValue) {
378    
379                    String value = getString(preferences, companyId, name);
380    
381                    if (value != null) {
382                            return value;
383                    }
384                    else {
385                            return String.valueOf(defaultValue);
386                    }
387            }
388    
389            public static String getString(
390                    PortletPreferences preferences, long companyId, String name,
391                    int defaultValue) {
392    
393                    String value = getString(preferences, companyId, name);
394    
395                    if (value != null) {
396                            return value;
397                    }
398                    else {
399                            return String.valueOf(defaultValue);
400                    }
401            }
402    
403            public static String getString(
404                    PortletPreferences preferences, long companyId, String name,
405                    long defaultValue) {
406    
407                    String value = getString(preferences, companyId, name);
408    
409                    if (value != null) {
410                            return value;
411                    }
412                    else {
413                            return String.valueOf(defaultValue);
414                    }
415            }
416    
417            public static String getString(
418                    PortletPreferences preferences, long companyId, String name,
419                    short defaultValue) {
420    
421                    String value = getString(preferences, companyId, name);
422    
423                    if (value != null) {
424                            return value;
425                    }
426                    else {
427                            return String.valueOf(defaultValue);
428                    }
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                    else {
441                            return defaultValue;
442                    }
443            }
444    
445            public static String getString(String name) {
446                    PortletPreferences preferences = getPreferences(true);
447    
448                    return getString(preferences, 0, name);
449            }
450    
451            public static String getString(String name, String defaultValue) {
452                    PortletPreferences preferences = getPreferences(true);
453    
454                    return getString(preferences, 0, name, defaultValue);
455            }
456    
457            public static String[] getStringArray(
458                    long companyId, String name, String delimiter) {
459    
460                    PortletPreferences preferences = getPreferences(companyId, true);
461    
462                    return getStringArray(preferences, companyId, name, delimiter);
463            }
464    
465            public static String[] getStringArray(
466                    long companyId, String name, String delimiter, String[] defaultValue) {
467    
468                    PortletPreferences preferences = getPreferences(companyId, true);
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                    PortletPreferences preferences = getPreferences(true);
501    
502                    return getStringArray(preferences, 0, name, delimiter);
503            }
504    
505            public static String[] getStringArray(
506                    String name, String delimiter, String[] defaultValue) {
507    
508                    PortletPreferences preferences = getPreferences(true);
509    
510                    return getStringArray(preferences, 0, name, delimiter, defaultValue);
511            }
512    
513            public static String getStringFromNames(long companyId, String... names) {
514                    for (String name : names) {
515                            String value = getString(companyId, name);
516    
517                            if (Validator.isNotNull(value)) {
518                                    return value;
519                            }
520                    }
521    
522                    return null;
523            }
524    
525            @BeanReference(type = PortalPreferencesLocalService.class)
526            private static PortalPreferencesLocalService _portalPreferencesLocalService;
527    
528    }