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.service.impl;
022:
023: import com.liferay.portal.NoSuchPortletPreferencesException;
024: import com.liferay.portal.PortalException;
025: import com.liferay.portal.SystemException;
026: import com.liferay.portal.kernel.util.StringMaker;
027: import com.liferay.portal.kernel.util.StringPool;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.portal.model.Portlet;
030: import com.liferay.portal.model.PortletPreferences;
031: import com.liferay.portal.model.PortletPreferencesIds;
032: import com.liferay.portal.model.impl.PortletImpl;
033: import com.liferay.portal.service.base.PortletPreferencesLocalServiceBaseImpl;
034: import com.liferay.portlet.PortletPreferencesImpl;
035: import com.liferay.portlet.PortletPreferencesSerializer;
036:
037: import java.util.List;
038: import java.util.Map;
039:
040: /**
041: * <a href="PortletPreferencesLocalServiceImpl.java.html"><b><i>View Source</i>
042: * </b></a>
043: *
044: * @author Brian Wing Shun Chan
045: *
046: */
047: public class PortletPreferencesLocalServiceImpl extends
048: PortletPreferencesLocalServiceBaseImpl {
049:
050: public void deletePortletPreferences(long portletPreferencesId)
051: throws PortalException, SystemException {
052:
053: PortletPreferences portletPreferences = portletPreferencesPersistence
054: .findByPrimaryKey(portletPreferencesId);
055:
056: long ownerId = portletPreferences.getOwnerId();
057: int ownerType = portletPreferences.getOwnerType();
058:
059: portletPreferencesPersistence.remove(portletPreferences);
060:
061: PortletPreferencesLocalUtil.clearPreferencesPool(ownerId,
062: ownerType);
063: }
064:
065: public void deletePortletPreferences(long ownerId, int ownerType,
066: long plid) throws PortalException, SystemException {
067:
068: portletPreferencesPersistence.removeByO_O_P(ownerId, ownerType,
069: plid);
070:
071: PortletPreferencesLocalUtil.clearPreferencesPool(ownerId,
072: ownerType);
073: }
074:
075: public void deletePortletPreferences(long ownerId, int ownerType,
076: long plid, String portletId) throws PortalException,
077: SystemException {
078:
079: portletPreferencesPersistence.removeByO_O_P_P(ownerId,
080: ownerType, plid, portletId);
081:
082: PortletPreferencesLocalUtil.clearPreferencesPool(ownerId,
083: ownerType);
084: }
085:
086: public javax.portlet.PortletPreferences getDefaultPreferences(
087: long companyId, String portletId) throws PortalException,
088: SystemException {
089:
090: Portlet portlet = portletLocalService.getPortletById(companyId,
091: portletId);
092:
093: return PortletPreferencesSerializer.fromDefaultXML(portlet
094: .getDefaultPreferences());
095: }
096:
097: public List getPortletPreferences() throws SystemException {
098: return portletPreferencesPersistence.findAll();
099: }
100:
101: public List getPortletPreferences(long plid) throws SystemException {
102: return portletPreferencesPersistence.findByPlid(plid);
103: }
104:
105: public List getPortletPreferences(long plid, String portletId)
106: throws SystemException {
107:
108: return portletPreferencesPersistence.findByP_P(plid, portletId);
109: }
110:
111: public List getPortletPreferences(long ownerId, int ownerType,
112: long plid) throws PortalException, SystemException {
113:
114: return portletPreferencesPersistence.findByO_O_P(ownerId,
115: ownerType, plid);
116: }
117:
118: public PortletPreferences getPortletPreferences(long ownerId,
119: int ownerType, long plid, String portletId)
120: throws PortalException, SystemException {
121:
122: return portletPreferencesPersistence.findByO_O_P_P(ownerId,
123: ownerType, plid, portletId);
124: }
125:
126: public javax.portlet.PortletPreferences getPreferences(
127: PortletPreferencesIds portletPreferencesIds)
128: throws PortalException, SystemException {
129:
130: return getPreferences(portletPreferencesIds.getCompanyId(),
131: portletPreferencesIds.getOwnerId(),
132: portletPreferencesIds.getOwnerType(),
133: portletPreferencesIds.getPlid(), portletPreferencesIds
134: .getPortletId());
135: }
136:
137: public javax.portlet.PortletPreferences getPreferences(
138: long companyId, long ownerId, int ownerType, long plid,
139: String portletId) throws PortalException, SystemException {
140:
141: return getPreferences(companyId, ownerId, ownerType, plid,
142: portletId, null);
143: }
144:
145: public javax.portlet.PortletPreferences getPreferences(
146: long companyId, long ownerId, int ownerType, long plid,
147: String portletId, String defaultPreferences)
148: throws PortalException, SystemException {
149:
150: Map prefsPool = PortletPreferencesLocalUtil.getPreferencesPool(
151: ownerId, ownerType);
152:
153: String key = encodeKey(plid, portletId);
154:
155: PortletPreferencesImpl prefs = (PortletPreferencesImpl) prefsPool
156: .get(key);
157:
158: if (prefs == null) {
159: PortletPreferences portletPreferences = null;
160:
161: Portlet portlet = portletLocalService.getPortletById(
162: companyId, portletId);
163:
164: try {
165: portletPreferences = portletPreferencesPersistence
166: .findByO_O_P_P(ownerId, ownerType, plid,
167: portletId);
168: } catch (NoSuchPortletPreferencesException nsppe) {
169: long portletPreferencesId = counterLocalService
170: .increment();
171:
172: portletPreferences = portletPreferencesPersistence
173: .create(portletPreferencesId);
174:
175: portletPreferences.setOwnerId(ownerId);
176: portletPreferences.setOwnerType(ownerType);
177: portletPreferences.setPlid(plid);
178: portletPreferences.setPortletId(portletId);
179:
180: if (Validator.isNull(defaultPreferences)) {
181: if (portlet == null) {
182: defaultPreferences = PortletImpl.DEFAULT_PREFERENCES;
183: } else {
184: defaultPreferences = portlet
185: .getDefaultPreferences();
186: }
187: }
188:
189: portletPreferences.setPreferences(defaultPreferences);
190:
191: portletPreferencesPersistence
192: .update(portletPreferences);
193: }
194:
195: prefs = PortletPreferencesSerializer.fromXML(companyId,
196: ownerId, ownerType, plid, portletId,
197: portletPreferences.getPreferences());
198:
199: prefsPool.put(key, prefs);
200: }
201:
202: return (PortletPreferencesImpl) prefs.clone();
203: }
204:
205: public PortletPreferences updatePreferences(long ownerId,
206: int ownerType, long plid, String portletId,
207: javax.portlet.PortletPreferences prefs)
208: throws PortalException, SystemException {
209:
210: PortletPreferences portletPreferences = null;
211:
212: try {
213: portletPreferences = portletPreferencesPersistence
214: .findByO_O_P_P(ownerId, ownerType, plid, portletId);
215: } catch (NoSuchPortletPreferencesException nsppe) {
216: long portletPreferencesId = counterLocalService.increment();
217:
218: portletPreferences = portletPreferencesPersistence
219: .create(portletPreferencesId);
220:
221: portletPreferences.setOwnerId(ownerId);
222: portletPreferences.setOwnerType(ownerType);
223: portletPreferences.setPlid(plid);
224: portletPreferences.setPortletId(portletId);
225: }
226:
227: PortletPreferencesImpl prefsImpl = (PortletPreferencesImpl) prefs;
228:
229: String xml = PortletPreferencesSerializer.toXML(prefsImpl);
230:
231: portletPreferences.setPreferences(xml);
232:
233: portletPreferencesPersistence.update(portletPreferences);
234:
235: PortletPreferencesLocalUtil.clearPreferencesPool(ownerId,
236: ownerType);
237:
238: return portletPreferences;
239: }
240:
241: protected String encodeKey(long plid, String portletId) {
242: StringMaker sm = new StringMaker();
243:
244: sm.append(plid);
245: sm.append(StringPool.POUND);
246: sm.append(portletId);
247:
248: return sm.toString();
249: }
250:
251: }
|