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