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