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