001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.util;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.GetterUtil;
026: import com.liferay.portal.kernel.util.StringPool;
027: import com.liferay.portal.kernel.util.StringUtil;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
030:
031: import javax.portlet.PortletPreferences;
032:
033: /**
034: * <a href="PrefsPropsUtil.java.html"><b><i>View Source</i></b></a>
035: *
036: * @author Brian Wing Shun Chan
037: *
038: */
039: public class PrefsPropsUtil {
040:
041: public static PortletPreferences getPreferences()
042: throws PortalException, SystemException {
043:
044: return getPreferences(0);
045: }
046:
047: public static PortletPreferences getPreferences(long companyId)
048: throws PortalException, SystemException {
049:
050: long ownerId = companyId;
051: int ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
052: long plid = PortletKeys.PREFS_PLID_SHARED;
053: String portletId = PortletKeys.LIFERAY_PORTAL;
054:
055: return PortletPreferencesLocalServiceUtil.getPreferences(
056: companyId, ownerId, ownerType, plid, portletId);
057: }
058:
059: public static boolean getBoolean(String name)
060: throws PortalException, SystemException {
061:
062: PortletPreferences prefs = getPreferences();
063:
064: return getBoolean(prefs, 0, name);
065: }
066:
067: public static boolean getBoolean(long companyId, String name)
068: throws PortalException, SystemException {
069:
070: PortletPreferences prefs = getPreferences(companyId);
071:
072: return getBoolean(prefs, companyId, name);
073: }
074:
075: public static boolean getBoolean(PortletPreferences prefs,
076: long companyId, String name) {
077:
078: return GetterUtil.getBoolean(getString(prefs, companyId, name));
079: }
080:
081: public static boolean getBoolean(String name, boolean defaultValue)
082: throws PortalException, SystemException {
083:
084: PortletPreferences prefs = getPreferences();
085:
086: return getBoolean(prefs, 0, name, defaultValue);
087: }
088:
089: public static boolean getBoolean(long companyId, String name,
090: boolean defaultValue) throws PortalException,
091: SystemException {
092:
093: PortletPreferences prefs = getPreferences(companyId);
094:
095: return getBoolean(prefs, companyId, name, defaultValue);
096: }
097:
098: public static boolean getBoolean(PortletPreferences prefs,
099: long companyId, String name, boolean defaultValue) {
100:
101: return GetterUtil.getBoolean(getString(prefs, companyId, name,
102: defaultValue));
103: }
104:
105: public static String getContent(String name)
106: throws PortalException, SystemException {
107:
108: PortletPreferences prefs = getPreferences();
109:
110: return getContent(prefs, 0, name);
111: }
112:
113: public static String getContent(long companyId, String name)
114: throws PortalException, SystemException {
115:
116: PortletPreferences prefs = getPreferences(companyId);
117:
118: return getContent(prefs, companyId, name);
119: }
120:
121: public static String getContent(PortletPreferences prefs,
122: long companyId, String name) {
123:
124: String value = prefs.getValue(name, StringPool.BLANK);
125:
126: if (Validator.isNotNull(value)) {
127: return value;
128: } else {
129: return ContentUtil.get(PropsUtil.get(name));
130: }
131: }
132:
133: public static double getDouble(String name) throws PortalException,
134: SystemException {
135:
136: PortletPreferences prefs = getPreferences();
137:
138: return getDouble(prefs, 0, name);
139: }
140:
141: public static double getDouble(long companyId, String name)
142: throws PortalException, SystemException {
143:
144: PortletPreferences prefs = getPreferences(companyId);
145:
146: return getDouble(prefs, companyId, name);
147: }
148:
149: public static double getDouble(PortletPreferences prefs,
150: long companyId, String name) {
151:
152: return GetterUtil.getDouble(getString(prefs, companyId, name));
153: }
154:
155: public static double getDouble(String name, double defaultValue)
156: throws PortalException, SystemException {
157:
158: PortletPreferences prefs = getPreferences();
159:
160: return getDouble(prefs, 0, name, defaultValue);
161: }
162:
163: public static double getDouble(long companyId, String name,
164: double defaultValue) throws PortalException,
165: SystemException {
166:
167: PortletPreferences prefs = getPreferences(companyId);
168:
169: return getDouble(prefs, companyId, name, defaultValue);
170: }
171:
172: public static double getDouble(PortletPreferences prefs,
173: long companyId, String name, double defaultValue) {
174:
175: return GetterUtil.getDouble(getString(prefs, companyId, name,
176: defaultValue));
177: }
178:
179: public static int getInteger(String name) throws PortalException,
180: SystemException {
181:
182: PortletPreferences prefs = getPreferences();
183:
184: return getInteger(prefs, 0, name);
185: }
186:
187: public static int getInteger(long companyId, String name)
188: throws PortalException, SystemException {
189:
190: PortletPreferences prefs = getPreferences(companyId);
191:
192: return getInteger(prefs, companyId, name);
193: }
194:
195: public static int getInteger(PortletPreferences prefs,
196: long companyId, String name) {
197:
198: return GetterUtil.getInteger(getString(prefs, companyId, name));
199: }
200:
201: public static int getInteger(String name, int defaultValue)
202: throws PortalException, SystemException {
203:
204: PortletPreferences prefs = getPreferences();
205:
206: return getInteger(prefs, 0, name, defaultValue);
207: }
208:
209: public static int getInteger(long companyId, String name,
210: int defaultValue) throws PortalException, SystemException {
211:
212: PortletPreferences prefs = getPreferences(companyId);
213:
214: return getInteger(prefs, companyId, name, defaultValue);
215: }
216:
217: public static int getInteger(PortletPreferences prefs,
218: long companyId, String name, int defaultValue) {
219:
220: return GetterUtil.getInteger(getString(prefs, companyId, name,
221: defaultValue));
222: }
223:
224: public static long getLong(String name) throws PortalException,
225: SystemException {
226:
227: PortletPreferences prefs = getPreferences();
228:
229: return getLong(prefs, 0, name);
230: }
231:
232: public static long getLong(long companyId, String name)
233: throws PortalException, SystemException {
234:
235: PortletPreferences prefs = getPreferences(companyId);
236:
237: return getLong(prefs, companyId, name);
238: }
239:
240: public static long getLong(PortletPreferences prefs,
241: long companyId, String name) {
242:
243: return GetterUtil.getLong(getString(prefs, companyId, name));
244: }
245:
246: public static long getLong(String name, long defaultValue)
247: throws PortalException, SystemException {
248:
249: PortletPreferences prefs = getPreferences();
250:
251: return getLong(prefs, 0, name, defaultValue);
252: }
253:
254: public static long getLong(long companyId, String name,
255: long defaultValue) throws PortalException, SystemException {
256:
257: PortletPreferences prefs = getPreferences(companyId);
258:
259: return getLong(prefs, companyId, name, defaultValue);
260: }
261:
262: public static long getLong(PortletPreferences prefs,
263: long companyId, String name, long defaultValue) {
264:
265: return GetterUtil.getLong(getString(prefs, companyId, name,
266: defaultValue));
267: }
268:
269: public static short getShort(String name) throws PortalException,
270: SystemException {
271:
272: PortletPreferences prefs = getPreferences();
273:
274: return getShort(prefs, 0, name);
275: }
276:
277: public static short getShort(long companyId, String name)
278: throws PortalException, SystemException {
279:
280: PortletPreferences prefs = getPreferences(companyId);
281:
282: return getShort(prefs, companyId, name);
283: }
284:
285: public static short getShort(PortletPreferences prefs,
286: long companyId, String name) {
287:
288: return GetterUtil.getShort(getString(prefs, companyId, name));
289: }
290:
291: public static short getShort(String name, short defaultValue)
292: throws PortalException, SystemException {
293:
294: PortletPreferences prefs = getPreferences();
295:
296: return getShort(prefs, 0, name, defaultValue);
297: }
298:
299: public static short getShort(long companyId, String name,
300: short defaultValue) throws PortalException, SystemException {
301:
302: PortletPreferences prefs = getPreferences(companyId);
303:
304: return getShort(prefs, companyId, name, defaultValue);
305: }
306:
307: public static short getShort(PortletPreferences prefs,
308: long companyId, String name, short defaultValue) {
309:
310: return GetterUtil.getShort(getString(prefs, companyId, name,
311: defaultValue));
312: }
313:
314: public static String getString(String name) throws PortalException,
315: SystemException {
316:
317: PortletPreferences prefs = getPreferences();
318:
319: return getString(prefs, 0, name);
320: }
321:
322: public static String getString(long companyId, String name)
323: throws PortalException, SystemException {
324:
325: PortletPreferences prefs = getPreferences(companyId);
326:
327: return getString(prefs, companyId, name);
328: }
329:
330: public static String getString(PortletPreferences prefs,
331: long companyId, String name) {
332:
333: String value = PropsUtil.get(name);
334:
335: return prefs.getValue(name, value);
336: }
337:
338: public static String getString(String name, String defaultValue)
339: throws PortalException, SystemException {
340:
341: PortletPreferences prefs = getPreferences();
342:
343: return getString(prefs, 0, name, defaultValue);
344: }
345:
346: public static String getString(long companyId, String name,
347: String defaultValue) throws PortalException,
348: SystemException {
349:
350: PortletPreferences prefs = getPreferences(companyId);
351:
352: return getString(prefs, companyId, name, defaultValue);
353: }
354:
355: public static String getString(PortletPreferences prefs,
356: long companyId, String name, String defaultValue) {
357:
358: return prefs.getValue(name, defaultValue);
359: }
360:
361: public static String getString(PortletPreferences prefs,
362: long companyId, String name, boolean defaultValue) {
363:
364: if (defaultValue) {
365: return prefs.getValue(name, StringPool.TRUE);
366: } else {
367: return prefs.getValue(name, StringPool.FALSE);
368: }
369: }
370:
371: public static String getString(PortletPreferences prefs,
372: long companyId, String name, double defaultValue) {
373:
374: return prefs.getValue(name, String.valueOf(defaultValue));
375: }
376:
377: public static String getString(PortletPreferences prefs,
378: long companyId, String name, int defaultValue) {
379:
380: return prefs.getValue(name, String.valueOf(defaultValue));
381: }
382:
383: public static String getString(PortletPreferences prefs,
384: long companyId, String name, long defaultValue) {
385:
386: return prefs.getValue(name, String.valueOf(defaultValue));
387: }
388:
389: public static String getString(PortletPreferences prefs,
390: long companyId, String name, short defaultValue) {
391:
392: return prefs.getValue(name, String.valueOf(defaultValue));
393: }
394:
395: public static String[] getStringArray(String name, String delimiter)
396: throws PortalException, SystemException {
397:
398: PortletPreferences prefs = getPreferences();
399:
400: return getStringArray(prefs, 0, name, delimiter);
401: }
402:
403: public static String[] getStringArray(long companyId, String name,
404: String delimiter) throws PortalException, SystemException {
405:
406: PortletPreferences prefs = getPreferences(companyId);
407:
408: return getStringArray(prefs, companyId, name, delimiter);
409: }
410:
411: public static String[] getStringArray(PortletPreferences prefs,
412: long companyId, String name, String delimiter)
413: throws PortalException, SystemException {
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(String name,
423: String delimiter, String[] defaultValue)
424: throws PortalException, SystemException {
425:
426: PortletPreferences prefs = getPreferences();
427:
428: return getStringArray(prefs, 0, name, delimiter, defaultValue);
429: }
430:
431: public static String[] getStringArray(long companyId, String name,
432: String delimiter, String[] defaultValue)
433: throws PortalException, SystemException {
434:
435: PortletPreferences prefs = getPreferences(companyId);
436:
437: return getStringArray(prefs, companyId, name, delimiter,
438: defaultValue);
439: }
440:
441: public static String[] getStringArray(PortletPreferences prefs,
442: long companyId, String name, String delimiter,
443: String[] defaultValue) throws PortalException,
444: SystemException {
445:
446: String value = prefs.getValue(name, null);
447:
448: if (value == null) {
449: return defaultValue;
450: } else {
451: return StringUtil.split(value, delimiter);
452: }
453: }
454:
455: }
|