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