1   /**
2    * Copyright (c) 2000-2008 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.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
32  
33  import javax.portlet.PortletPreferences;
34  
35  /**
36   * <a href="PrefsPropsUtil.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class PrefsPropsUtil {
42  
43      public static PortletPreferences getPreferences()
44          throws PortalException, SystemException {
45  
46          return getPreferences(0);
47      }
48  
49      public static PortletPreferences getPreferences(long companyId)
50          throws PortalException, SystemException {
51  
52          long ownerId = companyId;
53          int ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
54          long plid = PortletKeys.PREFS_PLID_SHARED;
55          String portletId = PortletKeys.LIFERAY_PORTAL;
56  
57          return PortletPreferencesLocalServiceUtil.getPreferences(
58              companyId, ownerId, ownerType, plid, portletId);
59      }
60  
61      public static boolean getBoolean(String name)
62          throws PortalException, SystemException {
63  
64          PortletPreferences prefs = getPreferences();
65  
66          return getBoolean(prefs, 0, name);
67      }
68  
69      public static boolean getBoolean(long companyId, String name)
70          throws PortalException, SystemException {
71  
72          PortletPreferences prefs = getPreferences(companyId);
73  
74          return getBoolean(prefs, companyId, name);
75      }
76  
77      public static boolean getBoolean(
78          PortletPreferences prefs, long companyId, String name) {
79  
80          return GetterUtil.getBoolean(getString(prefs, companyId, name));
81      }
82  
83      public static boolean getBoolean(String name, boolean defaultValue)
84          throws PortalException, SystemException {
85  
86          PortletPreferences prefs = getPreferences();
87  
88          return getBoolean(prefs, 0, name, defaultValue);
89      }
90  
91      public static boolean getBoolean(
92              long companyId, String name, boolean defaultValue)
93          throws PortalException, SystemException {
94  
95          PortletPreferences prefs = getPreferences(companyId);
96  
97          return getBoolean(prefs, companyId, name, defaultValue);
98      }
99  
100     public static boolean getBoolean(
101         PortletPreferences prefs, long companyId, String name,
102         boolean defaultValue) {
103 
104         return GetterUtil.getBoolean(
105             getString(prefs, companyId, name, defaultValue));
106     }
107 
108     public static String getContent(String name)
109         throws PortalException, SystemException {
110 
111         PortletPreferences prefs = getPreferences();
112 
113         return getContent(prefs, 0, name);
114     }
115 
116     public static String getContent(long companyId, String name)
117         throws PortalException, SystemException {
118 
119         PortletPreferences prefs = getPreferences(companyId);
120 
121         return getContent(prefs, companyId, name);
122     }
123 
124     public static String getContent(
125         PortletPreferences prefs, long companyId, String name) {
126 
127         String value = prefs.getValue(name, StringPool.BLANK);
128 
129         if (Validator.isNotNull(value)) {
130             return value;
131         }
132         else {
133             return ContentUtil.get(PropsUtil.get(name));
134         }
135     }
136 
137     public static double getDouble(String name)
138         throws PortalException, SystemException {
139 
140         PortletPreferences prefs = getPreferences();
141 
142         return getDouble(prefs, 0, name);
143     }
144 
145     public static double getDouble(long companyId, String name)
146         throws PortalException, SystemException {
147 
148         PortletPreferences prefs = getPreferences(companyId);
149 
150         return getDouble(prefs, companyId, name);
151     }
152 
153     public static double getDouble(
154         PortletPreferences prefs, long companyId, String name) {
155 
156         return GetterUtil.getDouble(getString(prefs, companyId, name));
157     }
158 
159     public static double getDouble(String name, double defaultValue)
160         throws PortalException, SystemException {
161 
162         PortletPreferences prefs = getPreferences();
163 
164         return getDouble(prefs, 0, name, defaultValue);
165     }
166 
167     public static double getDouble(
168             long companyId, String name, double defaultValue)
169         throws PortalException, SystemException {
170 
171         PortletPreferences prefs = getPreferences(companyId);
172 
173         return getDouble(prefs, companyId, name, defaultValue);
174     }
175 
176     public static double getDouble(
177         PortletPreferences prefs, long companyId, String name,
178         double defaultValue) {
179 
180         return GetterUtil.getDouble(
181             getString(prefs, companyId, name, defaultValue));
182     }
183 
184     public static int getInteger(String name)
185         throws PortalException, SystemException {
186 
187         PortletPreferences prefs = getPreferences();
188 
189         return getInteger(prefs, 0, name);
190     }
191 
192     public static int getInteger(long companyId, String name)
193         throws PortalException, SystemException {
194 
195         PortletPreferences prefs = getPreferences(companyId);
196 
197         return getInteger(prefs, companyId, name);
198     }
199 
200     public static int getInteger(
201         PortletPreferences prefs, long companyId, String name) {
202 
203         return GetterUtil.getInteger(getString(prefs, companyId, name));
204     }
205 
206     public static int getInteger(String name, int defaultValue)
207         throws PortalException, SystemException {
208 
209         PortletPreferences prefs = getPreferences();
210 
211         return getInteger(prefs, 0, name, defaultValue);
212     }
213 
214     public static int getInteger(long companyId, String name, int defaultValue)
215         throws PortalException, SystemException {
216 
217         PortletPreferences prefs = getPreferences(companyId);
218 
219         return getInteger(prefs, companyId, name, defaultValue);
220     }
221 
222     public static int getInteger(
223         PortletPreferences prefs, long companyId, String name,
224         int defaultValue) {
225 
226         return GetterUtil.getInteger(
227             getString(prefs, companyId, name, defaultValue));
228     }
229 
230     public static long getLong(String name)
231         throws PortalException, SystemException {
232 
233         PortletPreferences prefs = getPreferences();
234 
235         return getLong(prefs, 0, name);
236     }
237 
238     public static long getLong(long companyId, String name)
239         throws PortalException, SystemException {
240 
241         PortletPreferences prefs = getPreferences(companyId);
242 
243         return getLong(prefs, companyId, name);
244     }
245 
246     public static long getLong(
247         PortletPreferences prefs, long companyId, String name) {
248 
249         return GetterUtil.getLong(getString(prefs, companyId, name));
250     }
251 
252     public static long getLong(String name, long defaultValue)
253         throws PortalException, SystemException {
254 
255         PortletPreferences prefs = getPreferences();
256 
257         return getLong(prefs, 0, name, defaultValue);
258     }
259 
260     public static long getLong(long companyId, String name, long defaultValue)
261         throws PortalException, SystemException {
262 
263         PortletPreferences prefs = getPreferences(companyId);
264 
265         return getLong(prefs, companyId, name, defaultValue);
266     }
267 
268     public static long getLong(
269         PortletPreferences prefs, long companyId, String name,
270         long defaultValue) {
271 
272         return GetterUtil.getLong(
273             getString(prefs, companyId, name, defaultValue));
274     }
275 
276     public static short getShort(String name)
277         throws PortalException, SystemException {
278 
279         PortletPreferences prefs = getPreferences();
280 
281         return getShort(prefs, 0, name);
282     }
283 
284     public static short getShort(long companyId, String name)
285         throws PortalException, SystemException {
286 
287         PortletPreferences prefs = getPreferences(companyId);
288 
289         return getShort(prefs, companyId, name);
290     }
291 
292     public static short getShort(
293         PortletPreferences prefs, long companyId, String name) {
294 
295         return GetterUtil.getShort(getString(prefs, companyId, name));
296     }
297 
298     public static short getShort(String name, short defaultValue)
299         throws PortalException, SystemException {
300 
301         PortletPreferences prefs = getPreferences();
302 
303         return getShort(prefs, 0, name, defaultValue);
304     }
305 
306     public static short getShort(
307             long companyId, String name, short defaultValue)
308         throws PortalException, SystemException {
309 
310         PortletPreferences prefs = getPreferences(companyId);
311 
312         return getShort(prefs, companyId, name, defaultValue);
313     }
314 
315     public static short getShort(
316         PortletPreferences prefs, long companyId, String name,
317         short defaultValue) {
318 
319         return GetterUtil.getShort(
320             getString(prefs, companyId, name, defaultValue));
321     }
322 
323     public static String getString(String name)
324         throws PortalException, SystemException {
325 
326         PortletPreferences prefs = getPreferences();
327 
328         return getString(prefs, 0, name);
329     }
330 
331     public static String getString(long companyId, String name)
332         throws PortalException, SystemException {
333 
334         PortletPreferences prefs = getPreferences(companyId);
335 
336         return getString(prefs, companyId, name);
337     }
338 
339     public static String getString(
340         PortletPreferences prefs, long companyId, String name) {
341 
342         String value = PropsUtil.get(name);
343 
344         return prefs.getValue(name, value);
345     }
346 
347     public static String getString(String name, String defaultValue)
348         throws PortalException, SystemException {
349 
350         PortletPreferences prefs = getPreferences();
351 
352         return getString(prefs, 0, name, defaultValue);
353     }
354 
355     public static String getString(
356             long companyId, String name, String defaultValue)
357         throws PortalException, SystemException {
358 
359         PortletPreferences prefs = getPreferences(companyId);
360 
361         return getString(prefs, companyId, name, defaultValue);
362     }
363 
364     public static String getString(
365         PortletPreferences prefs, long companyId, String name,
366         String defaultValue) {
367 
368         return prefs.getValue(name, defaultValue);
369     }
370 
371     public static String getString(
372         PortletPreferences prefs, long companyId, String name,
373         boolean defaultValue) {
374 
375         if (defaultValue) {
376             return prefs.getValue(name, StringPool.TRUE);
377         }
378         else {
379             return prefs.getValue(name, StringPool.FALSE);
380         }
381     }
382 
383     public static String getString(
384         PortletPreferences prefs, long companyId, String name,
385         double defaultValue) {
386 
387         return prefs.getValue(name, String.valueOf(defaultValue));
388     }
389 
390     public static String getString(
391         PortletPreferences prefs, long companyId, String name,
392         int defaultValue) {
393 
394         return prefs.getValue(name, String.valueOf(defaultValue));
395     }
396 
397     public static String getString(
398         PortletPreferences prefs, long companyId, String name,
399         long defaultValue) {
400 
401         return prefs.getValue(name, String.valueOf(defaultValue));
402     }
403 
404     public static String getString(
405         PortletPreferences prefs, long companyId, String name,
406         short defaultValue) {
407 
408         return prefs.getValue(name, String.valueOf(defaultValue));
409     }
410 
411     public static String[] getStringArray(String name, String delimiter)
412         throws PortalException, SystemException {
413 
414         PortletPreferences prefs = getPreferences();
415 
416         return getStringArray(prefs, 0, name, delimiter);
417     }
418 
419     public static String[] getStringArray(
420             long companyId, String name, String delimiter)
421         throws PortalException, SystemException {
422 
423         PortletPreferences prefs = getPreferences(companyId);
424 
425         return getStringArray(prefs, companyId, name, delimiter);
426     }
427 
428     public static String[] getStringArray(
429             PortletPreferences prefs, long companyId, String name,
430             String delimiter)
431         throws PortalException, SystemException {
432 
433         String value = PropsUtil.get(name);
434 
435         value = prefs.getValue(name, value);
436 
437         return StringUtil.split(value, delimiter);
438     }
439 
440     public static String[] getStringArray(
441             String name, String delimiter, String[] defaultValue)
442         throws PortalException, SystemException {
443 
444         PortletPreferences prefs = getPreferences();
445 
446         return getStringArray(prefs, 0, name, delimiter, defaultValue);
447     }
448 
449     public static String[] getStringArray(
450             long companyId, String name, String delimiter,
451             String[] defaultValue)
452         throws PortalException, SystemException {
453 
454         PortletPreferences prefs = getPreferences(companyId);
455 
456         return getStringArray(prefs, companyId, name, delimiter, defaultValue);
457     }
458 
459     public static String[] getStringArray(
460             PortletPreferences prefs, long companyId, String name,
461             String delimiter, String[] defaultValue)
462         throws PortalException, SystemException {
463 
464         String value = prefs.getValue(name, null);
465 
466         if (value == null) {
467             return defaultValue;
468         }
469         else {
470             return StringUtil.split(value, delimiter);
471         }
472     }
473 
474 }