1   /**
2    * Copyright (c) 2000-2007 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.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
32  
33  import javax.portlet.PortletPreferences;
34  
35  /**
36   * <a href="PrefsPropsUtil.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class PrefsPropsUtil {
42  
43      public static PortletPreferences getPreferences()
44          throws PortalException, SystemException {
45  
46          return getPreferences(0);
47      }
48  
49      public static PortletPreferences getPreferences(long companyId)
50          throws PortalException, SystemException {
51  
52          long ownerId = companyId;
53          int ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
54          long plid = PortletKeys.PREFS_PLID_SHARED;
55          String portletId = PortletKeys.LIFERAY_PORTAL;
56  
57          return PortletPreferencesLocalServiceUtil.getPreferences(
58              companyId, ownerId, ownerType, plid, portletId);
59      }
60  
61      public static boolean getBoolean(String name)
62          throws PortalException, SystemException {
63  
64          PortletPreferences prefs = getPreferences();
65  
66          return getBoolean(prefs, 0, name);
67      }
68  
69      public static boolean getBoolean(long companyId, String name)
70          throws PortalException, SystemException {
71  
72          PortletPreferences prefs = getPreferences(companyId);
73  
74          return getBoolean(prefs, companyId, name);
75      }
76  
77      public static boolean getBoolean(
78          PortletPreferences prefs, long companyId, String name) {
79  
80          return GetterUtil.getBoolean(getString(prefs, companyId, name));
81      }
82  
83      public static String getContent(String name)
84          throws PortalException, SystemException {
85  
86          PortletPreferences prefs = getPreferences();
87  
88          return getContent(prefs, 0, name);
89      }
90  
91      public static String getContent(long companyId, String name)
92          throws PortalException, SystemException {
93  
94          PortletPreferences prefs = getPreferences(companyId);
95  
96          return getContent(prefs, companyId, name);
97      }
98  
99      public static String getContent(
100         PortletPreferences prefs, long companyId, String name) {
101 
102         String value = prefs.getValue(name, StringPool.BLANK);
103 
104         if (Validator.isNotNull(value)) {
105             return value;
106         }
107         else {
108             return ContentUtil.get(PropsUtil.get(name));
109         }
110     }
111 
112     public static double getDouble(String name)
113         throws PortalException, SystemException {
114 
115         PortletPreferences prefs = getPreferences();
116 
117         return getDouble(prefs, 0, name);
118     }
119 
120     public static double getDouble(long companyId, String name)
121         throws PortalException, SystemException {
122 
123         PortletPreferences prefs = getPreferences(companyId);
124 
125         return getDouble(prefs, companyId, name);
126     }
127 
128     public static double getDouble(
129         PortletPreferences prefs, long companyId, String name) {
130 
131         return GetterUtil.getDouble(getString(prefs, companyId, name));
132     }
133 
134     public static int getInteger(String name)
135         throws PortalException, SystemException {
136 
137         PortletPreferences prefs = getPreferences();
138 
139         return getInteger(prefs, 0, name);
140     }
141 
142     public static int getInteger(long companyId, String name)
143         throws PortalException, SystemException {
144 
145         PortletPreferences prefs = getPreferences(companyId);
146 
147         return getInteger(prefs, companyId, name);
148     }
149 
150     public static int getInteger(
151         PortletPreferences prefs, long companyId, String name) {
152 
153         return GetterUtil.getInteger(getString(prefs, companyId, name));
154     }
155 
156     public static long getLong(String name)
157         throws PortalException, SystemException {
158 
159         PortletPreferences prefs = getPreferences();
160 
161         return getLong(prefs, 0, name);
162     }
163 
164     public static long getLong(long companyId, String name)
165         throws PortalException, SystemException {
166 
167         PortletPreferences prefs = getPreferences(companyId);
168 
169         return getLong(prefs, companyId, name);
170     }
171 
172     public static long getLong(
173         PortletPreferences prefs, long companyId, String name) {
174 
175         return GetterUtil.getLong(getString(prefs, companyId, name));
176     }
177 
178     public static short getShort(String name)
179         throws PortalException, SystemException {
180 
181         PortletPreferences prefs = getPreferences();
182 
183         return getShort(prefs, 0, name);
184     }
185 
186     public static short getShort(long companyId, String name)
187         throws PortalException, SystemException {
188 
189         PortletPreferences prefs = getPreferences(companyId);
190 
191         return getShort(prefs, companyId, name);
192     }
193 
194     public static short getShort(
195         PortletPreferences prefs, long companyId, String name) {
196 
197         return GetterUtil.getShort(getString(prefs, companyId, name));
198     }
199 
200     public static String getString(String name)
201         throws PortalException, SystemException {
202 
203         PortletPreferences prefs = getPreferences();
204 
205         return getString(prefs, 0, name);
206     }
207 
208     public static String getString(long companyId, String name)
209         throws PortalException, SystemException {
210 
211         PortletPreferences prefs = getPreferences(companyId);
212 
213         return getString(prefs, companyId, name);
214     }
215 
216     public static String getString(
217         PortletPreferences prefs, long companyId, String name) {
218 
219         String value = PropsUtil.get(name);
220 
221         return prefs.getValue(name, value);
222     }
223 
224     public static String[] getStringArray(String name)
225         throws PortalException, SystemException {
226 
227         PortletPreferences prefs = getPreferences();
228 
229         return getStringArray(prefs, 0, name);
230     }
231 
232     public static String[] getStringArray(long companyId, String name)
233         throws PortalException, SystemException {
234 
235         PortletPreferences prefs = getPreferences(companyId);
236 
237         return getStringArray(prefs, companyId, name);
238     }
239 
240     public static String[] getStringArray(
241             PortletPreferences prefs, long companyId, String name)
242         throws PortalException, SystemException {
243 
244         String value = PropsUtil.get(name);
245 
246         return StringUtil.split(
247             prefs.getValue(name, value), StringPool.NEW_LINE);
248     }
249 
250 }