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.service.PortalPreferencesLocalService;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.PortletKeys;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
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                    PortletPreferences preferences = getPreferences(companyId, true);
040    
041                    return getBoolean(preferences, companyId, name);
042            }
043    
044            public static boolean getBoolean(
045                    long companyId, String name, boolean defaultValue) {
046    
047                    PortletPreferences preferences = getPreferences(companyId, true);
048    
049                    return getBoolean(preferences, companyId, name, defaultValue);
050            }
051    
052            public static boolean getBoolean(
053                    PortletPreferences preferences, long companyId, String name) {
054    
055                    return GetterUtil.getBoolean(getString(preferences, companyId, name));
056            }
057    
058            public static boolean getBoolean(
059                    PortletPreferences preferences, long companyId, String name,
060                    boolean defaultValue) {
061    
062                    return GetterUtil.getBoolean(
063                            getString(preferences, companyId, name, defaultValue));
064            }
065    
066            public static boolean getBoolean(String name) {
067                    PortletPreferences preferences = getPreferences(true);
068    
069                    return getBoolean(preferences, 0, name);
070            }
071    
072            public static boolean getBoolean(String name, boolean defaultValue) {
073                    PortletPreferences preferences = getPreferences(true);
074    
075                    return getBoolean(preferences, 0, name, defaultValue);
076            }
077    
078            public static String getContent(long companyId, String name) {
079                    PortletPreferences preferences = getPreferences(companyId, true);
080    
081                    return getContent(preferences, companyId, name);
082            }
083    
084            public static String getContent(
085                    PortletPreferences preferences, long companyId, String name) {
086    
087                    String value = preferences.getValue(name, StringPool.BLANK);
088    
089                    if (Validator.isNotNull(value)) {
090                            return value;
091                    }
092    
093                    return ContentUtil.get(PropsUtil.get(name));
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                    String value = getString(preferences, companyId, name);
368    
369                    if (value != null) {
370                            return value;
371                    }
372    
373                    if (defaultValue) {
374                            return preferences.getValue(name, StringPool.TRUE);
375                    }
376    
377                    return preferences.getValue(name, StringPool.FALSE);
378            }
379    
380            public static String getString(
381                    PortletPreferences preferences, long companyId, String name,
382                    double defaultValue) {
383    
384                    String value = getString(preferences, companyId, name);
385    
386                    if (value != null) {
387                            return value;
388                    }
389    
390                    return String.valueOf(defaultValue);
391            }
392    
393            public static String getString(
394                    PortletPreferences preferences, long companyId, String name,
395                    int defaultValue) {
396    
397                    String value = getString(preferences, companyId, name);
398    
399                    if (value != null) {
400                            return value;
401                    }
402    
403                    return String.valueOf(defaultValue);
404            }
405    
406            public static String getString(
407                    PortletPreferences preferences, long companyId, String name,
408                    long defaultValue) {
409    
410                    String value = getString(preferences, companyId, name);
411    
412                    if (value != null) {
413                            return value;
414                    }
415    
416                    return String.valueOf(defaultValue);
417            }
418    
419            public static String getString(
420                    PortletPreferences preferences, long companyId, String name,
421                    short defaultValue) {
422    
423                    String value = getString(preferences, companyId, name);
424    
425                    if (value != null) {
426                            return value;
427                    }
428    
429                    return String.valueOf(defaultValue);
430            }
431    
432            public static String getString(
433                    PortletPreferences preferences, long companyId, String name,
434                    String defaultValue) {
435    
436                    String value = getString(preferences, companyId, name);
437    
438                    if (value != null) {
439                            return value;
440                    }
441    
442                    return defaultValue;
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    
495                    return StringUtil.split(value, delimiter);
496            }
497    
498            public static String[] getStringArray(String name, String delimiter) {
499                    PortletPreferences preferences = getPreferences(true);
500    
501                    return getStringArray(preferences, 0, name, delimiter);
502            }
503    
504            public static String[] getStringArray(
505                    String name, String delimiter, String[] defaultValue) {
506    
507                    PortletPreferences preferences = getPreferences(true);
508    
509                    return getStringArray(preferences, 0, name, delimiter, defaultValue);
510            }
511    
512            public static String getStringFromNames(long companyId, String... names) {
513                    for (String name : names) {
514                            String value = getString(companyId, name);
515    
516                            if (Validator.isNotNull(value)) {
517                                    return value;
518                            }
519                    }
520    
521                    return null;
522            }
523    
524            @BeanReference(type = PortalPreferencesLocalService.class)
525            private static PortalPreferencesLocalService _portalPreferencesLocalService;
526    
527    }