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