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