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