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, 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, name, defaultValue);
050            }
051    
052            /**
053             * @deprecated As of 7.0.0, replaced by {@link
054             *             #getBoolean(PortletPreferences, String)}
055             */
056            @Deprecated
057            public static boolean getBoolean(
058                    PortletPreferences preferences, long companyId, String name) {
059    
060                    return getBoolean(preferences, name);
061            }
062    
063            /**
064             * @deprecated As of 7.0.0, replaced by {@link
065             *             #getBoolean(PortletPreferences, String, boolean)}
066             */
067            @Deprecated
068            public static boolean getBoolean(
069                    PortletPreferences preferences, long companyId, String name,
070                    boolean defaultValue) {
071    
072                    return getBoolean(preferences, name, defaultValue);
073            }
074    
075            public static boolean getBoolean(
076                    PortletPreferences preferences, String name) {
077    
078                    return GetterUtil.getBoolean(getString(preferences, name));
079            }
080    
081            public static boolean getBoolean(
082                    PortletPreferences preferences, String name, boolean defaultValue) {
083    
084                    return GetterUtil.getBoolean(
085                            getString(preferences, name, defaultValue));
086            }
087    
088            public static boolean getBoolean(String name) {
089                    PortletPreferences preferences = getPreferences(true);
090    
091                    return getBoolean(preferences, name);
092            }
093    
094            public static boolean getBoolean(String name, boolean defaultValue) {
095                    PortletPreferences preferences = getPreferences(true);
096    
097                    return getBoolean(preferences, name, defaultValue);
098            }
099    
100            public static String getContent(long companyId, String name) {
101                    PortletPreferences preferences = getPreferences(companyId, true);
102    
103                    return getContent(preferences, name);
104            }
105    
106            /**
107             * @deprecated As of 7.0.0, replaced by {@link
108             *             #getContent(PortletPreferences, String)}
109             */
110            @Deprecated
111            public static String getContent(
112                    PortletPreferences preferences, long companyId, String name) {
113    
114                    return getContent(preferences, name);
115            }
116    
117            public static String getContent(
118                    PortletPreferences preferences, String name) {
119    
120                    String value = preferences.getValue(name, StringPool.BLANK);
121    
122                    if (Validator.isNotNull(value)) {
123                            return value;
124                    }
125    
126                    return ContentUtil.get(PropsUtil.get(name));
127            }
128    
129            public static String getContent(String name) {
130                    PortletPreferences preferences = getPreferences(true);
131    
132                    return getContent(preferences, name);
133            }
134    
135            public static double getDouble(long companyId, String name) {
136                    PortletPreferences preferences = getPreferences(companyId, true);
137    
138                    return getDouble(preferences, name);
139            }
140    
141            public static double getDouble(
142                    long companyId, String name, double defaultValue) {
143    
144                    PortletPreferences preferences = getPreferences(companyId, true);
145    
146                    return getDouble(preferences, name, defaultValue);
147            }
148    
149            /**
150             * @deprecated As of 7.0.0, replaced by {@link
151             *             #getDouble(PortletPreferences, String)}
152             */
153            @Deprecated
154            public static double getDouble(
155                    PortletPreferences preferences, long companyId, String name) {
156    
157                    return getDouble(preferences, name);
158            }
159    
160            /**
161             * @deprecated As of 7.0.0, replaced by {@link
162             *             #getDouble(PortletPreferences, String, double)}
163             */
164            @Deprecated
165            public static double getDouble(
166                    PortletPreferences preferences, long companyId, String name,
167                    double defaultValue) {
168    
169                    return getDouble(preferences, name, defaultValue);
170            }
171    
172            public static double getDouble(
173                    PortletPreferences preferences, String name) {
174    
175                    return GetterUtil.getDouble(getString(preferences, name));
176            }
177    
178            public static double getDouble(
179                    PortletPreferences preferences, String name, double defaultValue) {
180    
181                    return GetterUtil.getDouble(getString(preferences, name, defaultValue));
182            }
183    
184            public static double getDouble(String name) {
185                    PortletPreferences preferences = getPreferences(true);
186    
187                    return getDouble(preferences, name);
188            }
189    
190            public static double getDouble(String name, double defaultValue) {
191                    PortletPreferences preferences = getPreferences(true);
192    
193                    return getDouble(preferences, name, defaultValue);
194            }
195    
196            public static int getInteger(long companyId, String name) {
197                    PortletPreferences preferences = getPreferences(companyId, true);
198    
199                    return getInteger(preferences, name);
200            }
201    
202            public static int getInteger(
203                    long companyId, String name, int defaultValue) {
204    
205                    PortletPreferences preferences = getPreferences(companyId, true);
206    
207                    return getInteger(preferences, name, defaultValue);
208            }
209    
210            /**
211             * @deprecated As of 7.0.0, replaced by {@link
212             *             #getInteger(PortletPreferences, String)}
213             */
214            @Deprecated
215            public static int getInteger(
216                    PortletPreferences preferences, long companyId, String name) {
217    
218                    return getInteger(preferences, name);
219            }
220    
221            /**
222             * @deprecated As of 7.0.0, replaced by {@link
223             *             #getInteger(PortletPreferences, String, int)}
224             */
225            @Deprecated
226            public static int getInteger(
227                    PortletPreferences preferences, long companyId, String name,
228                    int defaultValue) {
229    
230                    return getInteger(preferences, name, defaultValue);
231            }
232    
233            public static int getInteger(PortletPreferences preferences, String name) {
234                    return GetterUtil.getInteger(getString(preferences, name));
235            }
236    
237            public static int getInteger(
238                    PortletPreferences preferences, String name, int defaultValue) {
239    
240                    return GetterUtil.getInteger(
241                            getString(preferences, name, defaultValue));
242            }
243    
244            public static int getInteger(String name) {
245                    PortletPreferences preferences = getPreferences(true);
246    
247                    return getInteger(preferences, name);
248            }
249    
250            public static int getInteger(String name, int defaultValue) {
251                    PortletPreferences preferences = getPreferences(true);
252    
253                    return getInteger(preferences, name, defaultValue);
254            }
255    
256            public static long getLong(long companyId, String name) {
257                    PortletPreferences preferences = getPreferences(companyId, true);
258    
259                    return getLong(preferences, name);
260            }
261    
262            public static long getLong(long companyId, String name, long defaultValue) {
263                    PortletPreferences preferences = getPreferences(companyId, true);
264    
265                    return getLong(preferences, name, defaultValue);
266            }
267    
268            /**
269             * @deprecated As of 7.0.0, replaced by {@link #getLong(PortletPreferences,
270             *             String)}
271             */
272            @Deprecated
273            public static long getLong(
274                    PortletPreferences preferences, long companyId, String name) {
275    
276                    return getLong(preferences, name);
277            }
278    
279            /**
280             * @deprecated As of 7.0.0, replaced by {@link #getLong(PortletPreferences,
281             *             String, long)}
282             */
283            @Deprecated
284            public static long getLong(
285                    PortletPreferences preferences, long companyId, String name,
286                    long defaultValue) {
287    
288                    return getLong(preferences, name, defaultValue);
289            }
290    
291            public static long getLong(PortletPreferences preferences, String name) {
292                    return GetterUtil.getLong(getString(preferences, name));
293            }
294    
295            public static long getLong(
296                    PortletPreferences preferences, String name, long defaultValue) {
297    
298                    return GetterUtil.getLong(getString(preferences, name, defaultValue));
299            }
300    
301            public static long getLong(String name) {
302                    PortletPreferences preferences = getPreferences(true);
303    
304                    return getLong(preferences, name);
305            }
306    
307            public static long getLong(String name, long defaultValue) {
308                    PortletPreferences preferences = getPreferences(true);
309    
310                    return getLong(preferences, name, defaultValue);
311            }
312    
313            public static PortletPreferences getPreferences() {
314                    return getPreferences(false);
315            }
316    
317            public static PortletPreferences getPreferences(boolean readOnly) {
318                    PortalPreferencesWrapper portalPreferencesWrapper =
319                            PortalPreferencesWrapperCacheUtil.get(
320                                    PortletKeys.PREFS_OWNER_ID_DEFAULT,
321                                    PortletKeys.PREFS_OWNER_TYPE_COMPANY);
322    
323                    if (portalPreferencesWrapper != null) {
324                            if (!readOnly) {
325                                    portalPreferencesWrapper = portalPreferencesWrapper.clone();
326                            }
327    
328                            return portalPreferencesWrapper;
329                    }
330    
331                    return _portalPreferencesLocalService.getPreferences(
332                            PortletKeys.PREFS_OWNER_ID_DEFAULT,
333                            PortletKeys.PREFS_OWNER_TYPE_COMPANY);
334            }
335    
336            public static PortletPreferences getPreferences(long companyId) {
337                    return getPreferences(companyId, false);
338            }
339    
340            public static PortletPreferences getPreferences(
341                    long companyId, boolean readOnly) {
342    
343                    long ownerId = companyId;
344                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
345    
346                    PortalPreferencesWrapper portalPreferencesWrapper =
347                            PortalPreferencesWrapperCacheUtil.get(ownerId, ownerType);
348    
349                    if (portalPreferencesWrapper != null) {
350                            if (!readOnly) {
351                                    portalPreferencesWrapper = portalPreferencesWrapper.clone();
352                            }
353    
354                            return portalPreferencesWrapper;
355                    }
356    
357                    return _portalPreferencesLocalService.getPreferences(
358                            ownerId, ownerType);
359            }
360    
361            /**
362             * @deprecated As of 7.0.0, replaced by {@link
363             *             #getProperties(PortletPreferences, String, boolean)}
364             */
365            @Deprecated
366            public static Properties getProperties(
367                    PortletPreferences preferences, long companyId, String prefix,
368                    boolean removePrefix) {
369    
370                    return getProperties(preferences, prefix, removePrefix);
371            }
372    
373            public static Properties getProperties(
374                    PortletPreferences preferences, String prefix, boolean removePrefix) {
375    
376                    Properties newProperties = new Properties();
377    
378                    Enumeration<String> enu = preferences.getNames();
379    
380                    while (enu.hasMoreElements()) {
381                            String key = enu.nextElement();
382    
383                            if (key.startsWith(prefix)) {
384                                    String value = preferences.getValue(key, StringPool.BLANK);
385    
386                                    if (removePrefix) {
387                                            key = key.substring(prefix.length());
388                                    }
389    
390                                    newProperties.setProperty(key, value);
391                            }
392                    }
393    
394                    return newProperties;
395            }
396    
397            public static Properties getProperties(
398                    String prefix, boolean removePrefix) {
399    
400                    PortletPreferences preferences = getPreferences(true);
401    
402                    return getProperties(preferences, prefix, removePrefix);
403            }
404    
405            public static short getShort(long companyId, String name) {
406                    PortletPreferences preferences = getPreferences(companyId, true);
407    
408                    return getShort(preferences, name);
409            }
410    
411            public static short getShort(
412                    long companyId, String name, short defaultValue) {
413    
414                    PortletPreferences preferences = getPreferences(companyId, true);
415    
416                    return getShort(preferences, name, defaultValue);
417            }
418    
419            /**
420             * @deprecated As of 7.0.0, replaced by {@link #getShort(PortletPreferences,
421             *             String)}
422             */
423            @Deprecated
424            public static short getShort(
425                    PortletPreferences preferences, long companyId, String name) {
426    
427                    return getShort(preferences, name);
428            }
429    
430            /**
431             * @deprecated As of 7.0.0, replaced by {@link #getShort(PortletPreferences,
432             *             String, short)}
433             */
434            @Deprecated
435            public static short getShort(
436                    PortletPreferences preferences, long companyId, String name,
437                    short defaultValue) {
438    
439                    return getShort(preferences, name, defaultValue);
440            }
441    
442            public static short getShort(PortletPreferences preferences, String name) {
443                    return GetterUtil.getShort(getString(preferences, name));
444            }
445    
446            public static short getShort(
447                    PortletPreferences preferences, String name, short defaultValue) {
448    
449                    return GetterUtil.getShort(getString(preferences, name, defaultValue));
450            }
451    
452            public static short getShort(String name) {
453                    PortletPreferences preferences = getPreferences(true);
454    
455                    return getShort(preferences, name);
456            }
457    
458            public static short getShort(String name, short defaultValue) {
459                    PortletPreferences preferences = getPreferences(true);
460    
461                    return getShort(preferences, name, defaultValue);
462            }
463    
464            public static String getString(long companyId, String name) {
465                    PortletPreferences preferences = getPreferences(companyId, true);
466    
467                    return getString(preferences, name);
468            }
469    
470            public static String getString(
471                    long companyId, String name, String defaultValue) {
472    
473                    PortletPreferences preferences = getPreferences(companyId, true);
474    
475                    return getString(preferences, name, defaultValue);
476            }
477    
478            /**
479             * @deprecated As of 7.0.0, replaced by {@link
480             *             #getString(PortletPreferences, String)}
481             */
482            @Deprecated
483            public static String getString(
484                    PortletPreferences preferences, long companyId, String name) {
485    
486                    return getString(preferences, name);
487            }
488    
489            /**
490             * @deprecated As of 7.0.0, replaced by {@link
491             *             #getString(PortletPreferences, String, boolean)}
492             */
493            @Deprecated
494            public static String getString(
495                    PortletPreferences preferences, long companyId, String name,
496                    boolean defaultValue) {
497    
498                    return getString(preferences, name, defaultValue);
499            }
500    
501            /**
502             * @deprecated As of 7.0.0, replaced by {@link
503             *             #getString(PortletPreferences, String, double)}
504             */
505            @Deprecated
506            public static String getString(
507                    PortletPreferences preferences, long companyId, String name,
508                    double defaultValue) {
509    
510                    return getString(preferences, name, defaultValue);
511            }
512    
513            /**
514             * @deprecated As of 7.0.0, replaced by {@link
515             *             #getString(PortletPreferences, String, int)}
516             */
517            @Deprecated
518            public static String getString(
519                    PortletPreferences preferences, long companyId, String name,
520                    int defaultValue) {
521    
522                    return getString(preferences, name, defaultValue);
523            }
524    
525            /**
526             * @deprecated As of 7.0.0, replaced by {@link
527             *             #getString(PortletPreferences, String, long)}
528             */
529            @Deprecated
530            public static String getString(
531                    PortletPreferences preferences, long companyId, String name,
532                    long defaultValue) {
533    
534                    return getString(preferences, name, defaultValue);
535            }
536    
537            /**
538             * @deprecated As of 7.0.0, replaced by {@link
539             *             #getString(PortletPreferences, String, short)}
540             */
541            @Deprecated
542            public static String getString(
543                    PortletPreferences preferences, long companyId, String name,
544                    short defaultValue) {
545    
546                    return getString(preferences, name, defaultValue);
547            }
548    
549            /**
550             * @deprecated As of 7.0.0, replaced by {@link
551             *             #getString(PortletPreferences, String, String)}
552             */
553            @Deprecated
554            public static String getString(
555                    PortletPreferences preferences, long companyId, String name,
556                    String defaultValue) {
557    
558                    return getString(preferences, name, defaultValue);
559            }
560    
561            public static String getString(
562                    PortletPreferences preferences, String name) {
563    
564                    String value = PropsUtil.get(name);
565    
566                    return preferences.getValue(name, value);
567            }
568    
569            public static String getString(
570                    PortletPreferences preferences, String name, boolean defaultValue) {
571    
572                    String value = getString(preferences, name);
573    
574                    if (value != null) {
575                            return value;
576                    }
577    
578                    return String.valueOf(defaultValue);
579            }
580    
581            public static String getString(
582                    PortletPreferences preferences, String name, double defaultValue) {
583    
584                    String value = getString(preferences, name);
585    
586                    if (value != null) {
587                            return value;
588                    }
589    
590                    return String.valueOf(defaultValue);
591            }
592    
593            public static String getString(
594                    PortletPreferences preferences, String name, int defaultValue) {
595    
596                    String value = getString(preferences, name);
597    
598                    if (value != null) {
599                            return value;
600                    }
601    
602                    return String.valueOf(defaultValue);
603            }
604    
605            public static String getString(
606                    PortletPreferences preferences, String name, long defaultValue) {
607    
608                    String value = getString(preferences, name);
609    
610                    if (value != null) {
611                            return value;
612                    }
613    
614                    return String.valueOf(defaultValue);
615            }
616    
617            public static String getString(
618                    PortletPreferences preferences, String name, short defaultValue) {
619    
620                    String value = getString(preferences, name);
621    
622                    if (value != null) {
623                            return value;
624                    }
625    
626                    return String.valueOf(defaultValue);
627            }
628    
629            public static String getString(
630                    PortletPreferences preferences, String name, String defaultValue) {
631    
632                    String value = getString(preferences, name);
633    
634                    if (value != null) {
635                            return value;
636                    }
637    
638                    return defaultValue;
639            }
640    
641            public static String getString(String name) {
642                    PortletPreferences preferences = getPreferences(true);
643    
644                    return getString(preferences, name);
645            }
646    
647            public static String getString(String name, String defaultValue) {
648                    PortletPreferences preferences = getPreferences(true);
649    
650                    return getString(preferences, name, defaultValue);
651            }
652    
653            public static String[] getStringArray(
654                    long companyId, String name, String delimiter) {
655    
656                    PortletPreferences preferences = getPreferences(companyId, true);
657    
658                    return getStringArray(preferences, name, delimiter);
659            }
660    
661            public static String[] getStringArray(
662                    long companyId, String name, String delimiter, String[] defaultValue) {
663    
664                    PortletPreferences preferences = getPreferences(companyId, true);
665    
666                    return getStringArray(preferences, name, delimiter, defaultValue);
667            }
668    
669            /**
670             * @deprecated As of 7.0.0, replaced by {@link
671             *             #getStringArray(PortletPreferences, String, String)}
672             */
673            @Deprecated
674            public static String[] getStringArray(
675                    PortletPreferences preferences, long companyId, String name,
676                    String delimiter) {
677    
678                    return getStringArray(preferences, name, delimiter);
679            }
680    
681            /**
682             * @deprecated As of 7.0.0, replaced by {@link
683             *             #getStringArray(PortletPreferences, String, String,
684             *             String[])}
685             */
686            @Deprecated
687            public static String[] getStringArray(
688                    PortletPreferences preferences, long companyId, String name,
689                    String delimiter, String[] defaultValue) {
690    
691                    return getStringArray(preferences, name, delimiter, defaultValue);
692            }
693    
694            public static String[] getStringArray(
695                    PortletPreferences preferences, String name, String delimiter) {
696    
697                    String value = PropsUtil.get(name);
698    
699                    value = preferences.getValue(name, value);
700    
701                    return StringUtil.split(value, delimiter);
702            }
703    
704            public static String[] getStringArray(
705                    PortletPreferences preferences, String name, String delimiter,
706                    String[] defaultValue) {
707    
708                    String value = preferences.getValue(name, null);
709    
710                    if (value == null) {
711                            return defaultValue;
712                    }
713    
714                    return StringUtil.split(value, delimiter);
715            }
716    
717            public static String[] getStringArray(String name, String delimiter) {
718                    PortletPreferences preferences = getPreferences(true);
719    
720                    return getStringArray(preferences, name, delimiter);
721            }
722    
723            public static String[] getStringArray(
724                    String name, String delimiter, String[] defaultValue) {
725    
726                    PortletPreferences preferences = getPreferences(true);
727    
728                    return getStringArray(preferences, name, delimiter, defaultValue);
729            }
730    
731            public static String getStringFromNames(long companyId, String... names) {
732                    for (String name : names) {
733                            String value = getString(companyId, name);
734    
735                            if (Validator.isNotNull(value)) {
736                                    return value;
737                            }
738                    }
739    
740                    return null;
741            }
742    
743            @BeanReference(type = PortalPreferencesLocalService.class)
744            private static PortalPreferencesLocalService _portalPreferencesLocalService;
745    
746    }