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