1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.util;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
31  
32  import javax.portlet.PortletPreferences;
33  
34  /**
35   * <a href="PrefsPropsUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class PrefsPropsUtil {
41  
42      public static boolean getBoolean(long companyId, String name)
43          throws SystemException {
44  
45          PortletPreferences preferences = getPreferences(companyId);
46  
47          return getBoolean(preferences, companyId, name);
48      }
49  
50      public static boolean getBoolean(
51              long companyId, String name, boolean defaultValue)
52          throws SystemException {
53  
54          PortletPreferences preferences = getPreferences(companyId);
55  
56          return getBoolean(preferences, companyId, name, defaultValue);
57      }
58  
59      public static boolean getBoolean(
60          PortletPreferences preferences, long companyId, String name) {
61  
62          return GetterUtil.getBoolean(getString(preferences, companyId, name));
63      }
64  
65      public static boolean getBoolean(
66          PortletPreferences preferences, long companyId, String name,
67          boolean defaultValue) {
68  
69          return GetterUtil.getBoolean(
70              getString(preferences, companyId, name, defaultValue));
71      }
72  
73      public static boolean getBoolean(String name) throws SystemException {
74          PortletPreferences preferences = getPreferences();
75  
76          return getBoolean(preferences, 0, name);
77      }
78  
79      public static boolean getBoolean(String name, boolean defaultValue)
80          throws SystemException {
81  
82          PortletPreferences preferences = getPreferences();
83  
84          return getBoolean(preferences, 0, name, defaultValue);
85      }
86  
87      public static String getContent(long companyId, String name)
88          throws SystemException {
89  
90          PortletPreferences preferences = getPreferences(companyId);
91  
92          return getContent(preferences, companyId, name);
93      }
94  
95      public static String getContent(
96          PortletPreferences preferences, long companyId, String name) {
97  
98          String value = preferences.getValue(name, StringPool.BLANK);
99  
100         if (Validator.isNotNull(value)) {
101             return value;
102         }
103         else {
104             return ContentUtil.get(PropsUtil.get(name));
105         }
106     }
107 
108     public static String getContent(String name) throws SystemException {
109         PortletPreferences preferences = getPreferences();
110 
111         return getContent(preferences, 0, name);
112     }
113 
114     public static double getDouble(long companyId, String name)
115         throws SystemException {
116 
117         PortletPreferences preferences = getPreferences(companyId);
118 
119         return getDouble(preferences, companyId, name);
120     }
121 
122     public static double getDouble(
123             long companyId, String name, double defaultValue)
124         throws SystemException {
125 
126         PortletPreferences preferences = getPreferences(companyId);
127 
128         return getDouble(preferences, companyId, name, defaultValue);
129     }
130 
131     public static double getDouble(
132         PortletPreferences preferences, long companyId, String name) {
133 
134         return GetterUtil.getDouble(getString(preferences, companyId, name));
135     }
136 
137     public static double getDouble(
138         PortletPreferences preferences, long companyId, String name,
139         double defaultValue) {
140 
141         return GetterUtil.getDouble(
142             getString(preferences, companyId, name, defaultValue));
143     }
144 
145     public static double getDouble(String name) throws SystemException {
146         PortletPreferences preferences = getPreferences();
147 
148         return getDouble(preferences, 0, name);
149     }
150 
151     public static double getDouble(String name, double defaultValue)
152         throws SystemException {
153 
154         PortletPreferences preferences = getPreferences();
155 
156         return getDouble(preferences, 0, name, defaultValue);
157     }
158 
159     public static int getInteger(long companyId, String name)
160         throws SystemException {
161 
162         PortletPreferences preferences = getPreferences(companyId);
163 
164         return getInteger(preferences, companyId, name);
165     }
166 
167     public static int getInteger(long companyId, String name, int defaultValue)
168         throws SystemException {
169 
170         PortletPreferences preferences = getPreferences(companyId);
171 
172         return getInteger(preferences, companyId, name, defaultValue);
173     }
174 
175     public static int getInteger(
176         PortletPreferences preferences, long companyId, String name) {
177 
178         return GetterUtil.getInteger(getString(preferences, companyId, name));
179     }
180 
181     public static int getInteger(
182         PortletPreferences preferences, long companyId, String name,
183         int defaultValue) {
184 
185         return GetterUtil.getInteger(
186             getString(preferences, companyId, name, defaultValue));
187     }
188 
189     public static int getInteger(String name) throws SystemException {
190         PortletPreferences preferences = getPreferences();
191 
192         return getInteger(preferences, 0, name);
193     }
194 
195     public static int getInteger(String name, int defaultValue)
196         throws SystemException {
197 
198         PortletPreferences preferences = getPreferences();
199 
200         return getInteger(preferences, 0, name, defaultValue);
201     }
202 
203     public static long getLong(long companyId, String name)
204         throws SystemException {
205 
206         PortletPreferences preferences = getPreferences(companyId);
207 
208         return getLong(preferences, companyId, name);
209     }
210 
211     public static long getLong(long companyId, String name, long defaultValue)
212         throws SystemException {
213 
214         PortletPreferences preferences = getPreferences(companyId);
215 
216         return getLong(preferences, companyId, name, defaultValue);
217     }
218 
219     public static long getLong(
220         PortletPreferences preferences, long companyId, String name) {
221 
222         return GetterUtil.getLong(getString(preferences, companyId, name));
223     }
224 
225     public static long getLong(
226         PortletPreferences preferences, long companyId, String name,
227         long defaultValue) {
228 
229         return GetterUtil.getLong(
230             getString(preferences, companyId, name, defaultValue));
231     }
232 
233     public static long getLong(String name) throws SystemException {
234         PortletPreferences preferences = getPreferences();
235 
236         return getLong(preferences, 0, name);
237     }
238 
239     public static long getLong(String name, long defaultValue)
240         throws SystemException {
241 
242         PortletPreferences preferences = getPreferences();
243 
244         return getLong(preferences, 0, name, defaultValue);
245     }
246 
247     public static PortletPreferences getPreferences() throws SystemException {
248         return getPreferences(0);
249     }
250 
251     public static PortletPreferences getPreferences(long companyId)
252         throws SystemException {
253 
254         long ownerId = companyId;
255         int ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
256         long plid = PortletKeys.PREFS_PLID_SHARED;
257         String portletId = PortletKeys.LIFERAY_PORTAL;
258 
259         return PortletPreferencesLocalServiceUtil.getPreferences(
260             companyId, ownerId, ownerType, plid, portletId);
261     }
262 
263     public static short getShort(long companyId, String name)
264         throws SystemException {
265 
266         PortletPreferences preferences = getPreferences(companyId);
267 
268         return getShort(preferences, companyId, name);
269     }
270 
271     public static short getShort(
272             long companyId, String name, short defaultValue)
273         throws SystemException {
274 
275         PortletPreferences preferences = getPreferences(companyId);
276 
277         return getShort(preferences, companyId, name, defaultValue);
278     }
279 
280     public static short getShort(
281         PortletPreferences preferences, long companyId, String name) {
282 
283         return GetterUtil.getShort(getString(preferences, companyId, name));
284     }
285 
286     public static short getShort(
287         PortletPreferences preferences, long companyId, String name,
288         short defaultValue) {
289 
290         return GetterUtil.getShort(
291             getString(preferences, companyId, name, defaultValue));
292     }
293 
294     public static short getShort(String name) throws SystemException {
295         PortletPreferences preferences = getPreferences();
296 
297         return getShort(preferences, 0, name);
298     }
299 
300     public static short getShort(String name, short defaultValue)
301         throws SystemException {
302 
303         PortletPreferences preferences = getPreferences();
304 
305         return getShort(preferences, 0, name, defaultValue);
306     }
307 
308     public static String getString(long companyId, String name)
309         throws SystemException {
310 
311         PortletPreferences preferences = getPreferences(companyId);
312 
313         return getString(preferences, companyId, name);
314     }
315 
316     public static String getString(
317             long companyId, String name, String defaultValue)
318         throws SystemException {
319 
320         PortletPreferences preferences = getPreferences(companyId);
321 
322         return getString(preferences, companyId, name, defaultValue);
323     }
324 
325     public static String getString(
326         PortletPreferences preferences, long companyId, String name) {
327 
328         String value = PropsUtil.get(name);
329 
330         return preferences.getValue(name, value);
331     }
332 
333     public static String getString(
334         PortletPreferences preferences, long companyId, String name,
335         boolean defaultValue) {
336 
337         if (defaultValue) {
338             return preferences.getValue(name, StringPool.TRUE);
339         }
340         else {
341             return preferences.getValue(name, StringPool.FALSE);
342         }
343     }
344 
345     public static String getString(
346         PortletPreferences preferences, long companyId, String name,
347         double defaultValue) {
348 
349         return preferences.getValue(name, String.valueOf(defaultValue));
350     }
351 
352     public static String getString(
353         PortletPreferences preferences, long companyId, String name,
354         int defaultValue) {
355 
356         return preferences.getValue(name, String.valueOf(defaultValue));
357     }
358 
359     public static String getString(
360         PortletPreferences preferences, long companyId, String name,
361         long defaultValue) {
362 
363         return preferences.getValue(name, String.valueOf(defaultValue));
364     }
365 
366     public static String getString(
367         PortletPreferences preferences, long companyId, String name,
368         short defaultValue) {
369 
370         return preferences.getValue(name, String.valueOf(defaultValue));
371     }
372 
373     public static String getString(
374         PortletPreferences preferences, long companyId, String name,
375         String defaultValue) {
376 
377         return preferences.getValue(name, defaultValue);
378     }
379 
380     public static String getString(String name) throws SystemException {
381         PortletPreferences preferences = getPreferences();
382 
383         return getString(preferences, 0, name);
384     }
385 
386     public static String getString(String name, String defaultValue)
387         throws SystemException {
388 
389         PortletPreferences preferences = getPreferences();
390 
391         return getString(preferences, 0, name, defaultValue);
392     }
393 
394     public static String[] getStringArray(
395             long companyId, String name, String delimiter)
396         throws SystemException {
397 
398         PortletPreferences preferences = getPreferences(companyId);
399 
400         return getStringArray(preferences, companyId, name, delimiter);
401     }
402 
403     public static String[] getStringArray(
404             long companyId, String name, String delimiter,
405             String[] defaultValue)
406         throws SystemException {
407 
408         PortletPreferences preferences = getPreferences(companyId);
409 
410         return getStringArray(
411             preferences, companyId, name, delimiter, defaultValue);
412     }
413 
414     public static String[] getStringArray(
415         PortletPreferences preferences, long companyId, String name,
416         String delimiter) {
417 
418         String value = PropsUtil.get(name);
419 
420         value = preferences.getValue(name, value);
421 
422         return StringUtil.split(value, delimiter);
423     }
424 
425     public static String[] getStringArray(
426         PortletPreferences preferences, long companyId, String name,
427         String delimiter, String[] defaultValue) {
428 
429         String value = preferences.getValue(name, null);
430 
431         if (value == null) {
432             return defaultValue;
433         }
434         else {
435             return StringUtil.split(value, delimiter);
436         }
437     }
438 
439     public static String[] getStringArray(String name, String delimiter)
440         throws SystemException {
441 
442         PortletPreferences preferences = getPreferences();
443 
444         return getStringArray(preferences, 0, name, delimiter);
445     }
446 
447     public static String[] getStringArray(
448             String name, String delimiter, String[] defaultValue)
449         throws SystemException {
450 
451         PortletPreferences preferences = getPreferences();
452 
453         return getStringArray(preferences, 0, name, delimiter, defaultValue);
454     }
455 
456 }