001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.kernel.exception.SystemException;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.Validator;
021 import com.liferay.portal.model.PortalPreferences;
022 import com.liferay.portal.model.PortletConstants;
023 import com.liferay.portal.service.base.PortalPreferencesLocalServiceBaseImpl;
024 import com.liferay.portlet.PortalPreferencesImpl;
025 import com.liferay.portlet.PortalPreferencesWrapper;
026 import com.liferay.portlet.PortalPreferencesWrapperCacheUtil;
027 import com.liferay.portlet.PortletPreferencesFactoryUtil;
028
029 import javax.portlet.PortletPreferences;
030
031
034 public class PortalPreferencesLocalServiceImpl
035 extends PortalPreferencesLocalServiceBaseImpl {
036
037 @Override
038 public PortalPreferences addPortalPreferences(
039 long ownerId, int ownerType, String defaultPreferences) {
040
041 PortalPreferencesWrapperCacheUtil.remove(ownerId, ownerType);
042
043 long portalPreferencesId = counterLocalService.increment();
044
045 PortalPreferences portalPreferences =
046 portalPreferencesPersistence.create(portalPreferencesId);
047
048 portalPreferences.setOwnerId(ownerId);
049 portalPreferences.setOwnerType(ownerType);
050
051 if (Validator.isNull(defaultPreferences)) {
052 defaultPreferences = PortletConstants.DEFAULT_PREFERENCES;
053 }
054
055 portalPreferences.setPreferences(defaultPreferences);
056
057 try {
058 portalPreferencesPersistence.update(portalPreferences);
059 }
060 catch (SystemException se) {
061 if (_log.isWarnEnabled()) {
062 _log.warn(
063 "Add failed, fetch {ownerId=" + ownerId + ", ownerType=" +
064 ownerType + "}");
065 }
066
067 portalPreferences = portalPreferencesPersistence.fetchByO_O(
068 ownerId, ownerType, false);
069
070 if (portalPreferences == null) {
071 throw se;
072 }
073 }
074
075 return portalPreferences;
076 }
077
078
082 @Deprecated
083 @Override
084 public PortalPreferences addPortalPreferences(
085 long companyId, long ownerId, int ownerType,
086 String defaultPreferences) {
087
088 return addPortalPreferences(ownerId, ownerType, defaultPreferences);
089 }
090
091 @Override
092 public PortletPreferences getPreferences(long ownerId, int ownerType) {
093 return getPreferences(ownerId, ownerType, null);
094 }
095
096 @Override
097 public PortletPreferences getPreferences(
098 long ownerId, int ownerType, String defaultPreferences) {
099
100 PortalPreferencesWrapper portalPreferencesWrapper =
101 PortalPreferencesWrapperCacheUtil.get(ownerId, ownerType);
102
103 if (portalPreferencesWrapper != null) {
104 return portalPreferencesWrapper.clone();
105 }
106
107 PortalPreferences portalPreferences =
108 portalPreferencesPersistence.fetchByO_O(ownerId, ownerType);
109
110 if (portalPreferences == null) {
111 portalPreferences =
112 portalPreferencesLocalService.addPortalPreferences(
113 ownerId, ownerType, defaultPreferences);
114 }
115
116 PortalPreferencesImpl portalPreferencesImpl = new PortalPreferencesImpl(
117 portalPreferences, false);
118
119 portalPreferencesWrapper = new PortalPreferencesWrapper(
120 portalPreferencesImpl);
121
122 PortalPreferencesWrapperCacheUtil.put(
123 ownerId, ownerType, portalPreferencesWrapper);
124
125 return portalPreferencesWrapper.clone();
126 }
127
128
131 @Deprecated
132 @Override
133 public PortletPreferences getPreferences(
134 long companyId, long ownerId, int ownerType) {
135
136 return getPreferences(ownerId, ownerType);
137 }
138
139
143 @Deprecated
144 @Override
145 public PortletPreferences getPreferences(
146 long companyId, long ownerId, int ownerType,
147 String defaultPreferences) {
148
149 return getPreferences(ownerId, ownerType, defaultPreferences);
150 }
151
152 @Override
153 public PortalPreferences updatePreferences(
154 long ownerId, int ownerType,
155 com.liferay.portlet.PortalPreferences portalPreferences) {
156
157 String xml = PortletPreferencesFactoryUtil.toXML(portalPreferences);
158
159 return updatePreferences(ownerId, ownerType, xml);
160 }
161
162 @Override
163 public PortalPreferences updatePreferences(
164 long ownerId, int ownerType, String xml) {
165
166 PortalPreferencesWrapperCacheUtil.remove(ownerId, ownerType);
167
168 PortalPreferences portalPreferences =
169 portalPreferencesPersistence.fetchByO_O(ownerId, ownerType);
170
171 if (portalPreferences == null) {
172 long portalPreferencesId = counterLocalService.increment();
173
174 portalPreferences = portalPreferencesPersistence.create(
175 portalPreferencesId);
176
177 portalPreferences.setOwnerId(ownerId);
178 portalPreferences.setOwnerType(ownerType);
179 }
180
181 portalPreferences.setPreferences(xml);
182
183 portalPreferencesPersistence.update(portalPreferences);
184
185 return portalPreferences;
186 }
187
188 private static final Log _log = LogFactoryUtil.getLog(
189 PortalPreferencesLocalServiceImpl.class);
190
191 }