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