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