001
014
015 package com.liferay.portlet.journal.lar;
016
017 import com.liferay.portal.kernel.lar.DataLevel;
018 import com.liferay.portal.kernel.lar.PortletDataContext;
019 import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
020 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
021 import com.liferay.portal.kernel.log.Log;
022 import com.liferay.portal.kernel.log.LogFactoryUtil;
023 import com.liferay.portal.kernel.util.GetterUtil;
024 import com.liferay.portal.kernel.util.MapUtil;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.Validator;
027 import com.liferay.portal.kernel.workflow.WorkflowConstants;
028 import com.liferay.portal.model.Layout;
029 import com.liferay.portal.service.LayoutLocalServiceUtil;
030 import com.liferay.portal.util.PortalUtil;
031 import com.liferay.portal.util.PropsValues;
032 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
033 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
034 import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
035 import com.liferay.portlet.journal.model.JournalArticle;
036 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
037 import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
038 import com.liferay.portlet.journal.service.permission.JournalPermission;
039
040 import java.util.Map;
041
042 import javax.portlet.PortletPreferences;
043
044
071 public class JournalContentPortletDataHandler
072 extends JournalPortletDataHandler {
073
074 public JournalContentPortletDataHandler() {
075 setDataLevel(DataLevel.PORTLET_INSTANCE);
076 setDataPortletPreferences("articleId", "ddmTemplateKey", "groupId");
077 setExportControls(
078 new PortletDataHandlerBoolean(
079 null, "selected-web-content", true, true, null,
080 JournalArticle.class.getName()));
081 setPublishToLiveByDefault(
082 PropsValues.JOURNAL_CONTENT_PUBLISH_TO_LIVE_BY_DEFAULT);
083 }
084
085 @Override
086 protected PortletPreferences doDeleteData(
087 PortletDataContext portletDataContext, String portletId,
088 PortletPreferences portletPreferences)
089 throws Exception {
090
091 if (portletPreferences == null) {
092 return portletPreferences;
093 }
094
095 portletPreferences.setValue("articleId", StringPool.BLANK);
096 portletPreferences.setValue("ddmTemplateKey", StringPool.BLANK);
097 portletPreferences.setValue("groupId", StringPool.BLANK);
098
099 return portletPreferences;
100 }
101
102 @Override
103 protected PortletPreferences doProcessExportPortletPreferences(
104 PortletDataContext portletDataContext, String portletId,
105 PortletPreferences portletPreferences)
106 throws Exception {
107
108 portletDataContext.addPortletPermissions(
109 JournalPermission.RESOURCE_NAME);
110
111 String articleId = portletPreferences.getValue("articleId", null);
112
113 if (articleId == null) {
114 if (_log.isDebugEnabled()) {
115 _log.debug(
116 "No article id found in preferences of portlet " +
117 portletId);
118 }
119
120 return portletPreferences;
121 }
122
123 long articleGroupId = GetterUtil.getLong(
124 portletPreferences.getValue("groupId", StringPool.BLANK));
125
126 if (articleGroupId <= 0) {
127 if (_log.isWarnEnabled()) {
128 _log.warn(
129 "No group id found in preferences of portlet " + portletId);
130 }
131
132 return portletPreferences;
133 }
134
135 long previousScopeGroupId = portletDataContext.getScopeGroupId();
136
137 if (articleGroupId != previousScopeGroupId) {
138 portletDataContext.setScopeGroupId(articleGroupId);
139 }
140
141 JournalArticle article = null;
142
143 article = JournalArticleLocalServiceUtil.fetchLatestArticle(
144 articleGroupId, articleId, WorkflowConstants.STATUS_APPROVED);
145
146 if (article == null) {
147 article = JournalArticleLocalServiceUtil.fetchLatestArticle(
148 articleGroupId, articleId, WorkflowConstants.STATUS_EXPIRED);
149 }
150
151 if (article == null) {
152 if (_log.isWarnEnabled()) {
153 _log.warn(
154 "Portlet " + portletId +
155 " refers to an invalid article ID " + articleId);
156 }
157
158 portletDataContext.setScopeGroupId(previousScopeGroupId);
159
160 return portletPreferences;
161 }
162
163 StagedModelDataHandlerUtil.exportReferenceStagedModel(
164 portletDataContext, portletId, article);
165
166 String defaultTemplateId = article.getTemplateId();
167 String preferenceTemplateId = portletPreferences.getValue(
168 "ddmTemplateKey", null);
169
170 if (Validator.isNotNull(defaultTemplateId) &&
171 Validator.isNotNull(preferenceTemplateId) &&
172 !defaultTemplateId.equals(preferenceTemplateId)) {
173
174 DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.getTemplate(
175 article.getGroupId(),
176 PortalUtil.getClassNameId(DDMStructure.class),
177 preferenceTemplateId, true);
178
179 StagedModelDataHandlerUtil.exportReferenceStagedModel(
180 portletDataContext, article, ddmTemplate,
181 PortletDataContext.REFERENCE_TYPE_STRONG);
182 }
183
184 portletDataContext.setScopeGroupId(previousScopeGroupId);
185
186 return portletPreferences;
187 }
188
189 @Override
190 protected PortletPreferences doProcessImportPortletPreferences(
191 PortletDataContext portletDataContext, String portletId,
192 PortletPreferences portletPreferences)
193 throws Exception {
194
195 portletDataContext.importPortletPermissions(
196 JournalPermission.RESOURCE_NAME);
197
198 long previousScopeGroupId = portletDataContext.getScopeGroupId();
199
200 long importGroupId = GetterUtil.getLong(
201 portletPreferences.getValue("groupId", null));
202
203 if (importGroupId == portletDataContext.getSourceGroupId()) {
204 portletDataContext.setScopeGroupId(portletDataContext.getGroupId());
205 }
206
207 if (importGroupId ==
208 portletDataContext.getSourceCompanyGroupId()) {
209
210 portletDataContext.setScopeGroupId(
211 portletDataContext.getCompanyGroupId());
212 }
213
214 StagedModelDataHandlerUtil.importReferenceStagedModels(
215 portletDataContext, DDMStructure.class);
216
217 StagedModelDataHandlerUtil.importReferenceStagedModels(
218 portletDataContext, DDMTemplate.class);
219
220 StagedModelDataHandlerUtil.importReferenceStagedModels(
221 portletDataContext, JournalArticle.class);
222
223 String articleId = portletPreferences.getValue("articleId", null);
224
225 if (Validator.isNotNull(articleId)) {
226 Map<String, String> articleIds =
227 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
228 JournalArticle.class + ".articleId");
229
230 articleId = MapUtil.getString(articleIds, articleId, articleId);
231
232 portletPreferences.setValue("articleId", articleId);
233
234 String importedArticleGroupId = String.valueOf(
235 portletDataContext.getScopeGroupId());
236
237 portletPreferences.setValue("groupId", importedArticleGroupId);
238
239 Layout layout = LayoutLocalServiceUtil.getLayout(
240 portletDataContext.getPlid());
241
242 JournalContentSearchLocalServiceUtil.updateContentSearch(
243 portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
244 layout.getLayoutId(), portletId, articleId, true);
245 }
246 else {
247 portletPreferences.setValue("groupId", StringPool.BLANK);
248 portletPreferences.setValue("articleId", StringPool.BLANK);
249 }
250
251 String ddmTemplateKey = portletPreferences.getValue(
252 "ddmTemplateKey", null);
253
254 if (Validator.isNotNull(ddmTemplateKey)) {
255 Map<String, String> ddmTemplateKeys =
256 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
257 DDMTemplate.class + ".ddmTemplateKey");
258
259 ddmTemplateKey = MapUtil.getString(
260 ddmTemplateKeys, ddmTemplateKey, ddmTemplateKey);
261
262 portletPreferences.setValue("ddmTemplateKey", ddmTemplateKey);
263 }
264 else {
265 portletPreferences.setValue("ddmTemplateKey", StringPool.BLANK);
266 }
267
268 portletDataContext.setScopeGroupId(previousScopeGroupId);
269
270 return portletPreferences;
271 }
272
273 private static Log _log = LogFactoryUtil.getLog(
274 JournalContentPortletDataHandler.class);
275
276 }