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.ManifestSummary;
019 import com.liferay.portal.kernel.lar.PortletDataContext;
020 import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
021 import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
022 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
023 import com.liferay.portal.kernel.lar.StagedModelType;
024 import com.liferay.portal.kernel.log.Log;
025 import com.liferay.portal.kernel.log.LogFactoryUtil;
026 import com.liferay.portal.kernel.util.ArrayUtil;
027 import com.liferay.portal.kernel.util.GetterUtil;
028 import com.liferay.portal.kernel.util.MapUtil;
029 import com.liferay.portal.kernel.util.StringPool;
030 import com.liferay.portal.kernel.util.Validator;
031 import com.liferay.portal.kernel.workflow.WorkflowConstants;
032 import com.liferay.portal.kernel.xml.Element;
033 import com.liferay.portal.model.Layout;
034 import com.liferay.portal.service.LayoutLocalServiceUtil;
035 import com.liferay.portal.util.PortalUtil;
036 import com.liferay.portal.util.PropsValues;
037 import com.liferay.portlet.documentlibrary.lar.DLPortletDataHandler;
038 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
039 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
040 import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
041 import com.liferay.portlet.journal.NoSuchArticleException;
042 import com.liferay.portlet.journal.model.JournalArticle;
043 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
044 import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
045 import com.liferay.portlet.journal.service.permission.JournalPermission;
046
047 import java.util.List;
048 import java.util.Map;
049
050 import javax.portlet.PortletPreferences;
051
052
079 public class JournalContentPortletDataHandler
080 extends JournalPortletDataHandler {
081
082 public JournalContentPortletDataHandler() {
083 setDataLevel(DataLevel.PORTLET_INSTANCE);
084 setDataPortletPreferences("groupId", "articleId", "templateId");
085 setExportControls(
086 new PortletDataHandlerBoolean(
087 NAMESPACE, "selected-web-content", true, true,
088 new PortletDataHandlerControl[] {
089 new PortletDataHandlerBoolean(
090 NAMESPACE, "referenced-content")
091 },
092 JournalArticle.class.getName()));
093
094 DLPortletDataHandler dlPortletDataHandler = new DLPortletDataHandler();
095
096 setExportMetadataControls(
097 ArrayUtil.append(
098 getExportMetadataControls(),
099 dlPortletDataHandler.getExportMetadataControls()));
100
101 setImportControls(getExportControls()[0]);
102 setPublishToLiveByDefault(
103 PropsValues.JOURNAL_CONTENT_PUBLISH_TO_LIVE_BY_DEFAULT);
104 }
105
106 @Override
107 protected PortletPreferences doDeleteData(
108 PortletDataContext portletDataContext, String portletId,
109 PortletPreferences portletPreferences)
110 throws Exception {
111
112 if (portletPreferences == null) {
113 return portletPreferences;
114 }
115
116 portletPreferences.setValue("groupId", StringPool.BLANK);
117 portletPreferences.setValue("articleId", StringPool.BLANK);
118
119 return portletPreferences;
120 }
121
122 @Override
123 protected String doExportData(
124 PortletDataContext portletDataContext, String portletId,
125 PortletPreferences portletPreferences)
126 throws Exception {
127
128 portletDataContext.addPermissions(
129 JournalPermission.RESOURCE_NAME,
130 portletDataContext.getScopeGroupId());
131
132 String articleId = portletPreferences.getValue("articleId", null);
133
134 if (articleId == null) {
135 if (_log.isDebugEnabled()) {
136 _log.debug(
137 "No article id found in preferences of portlet " +
138 portletId);
139 }
140
141 return StringPool.BLANK;
142 }
143
144 long articleGroupId = GetterUtil.getLong(
145 portletPreferences.getValue("groupId", StringPool.BLANK));
146
147 if (articleGroupId <= 0) {
148 if (_log.isWarnEnabled()) {
149 _log.warn(
150 "No group id found in preferences of portlet " + portletId);
151 }
152
153 return StringPool.BLANK;
154 }
155
156 long previousScopeGroupId = portletDataContext.getScopeGroupId();
157
158 if (articleGroupId != portletDataContext.getScopeGroupId()) {
159 portletDataContext.setScopeGroupId(articleGroupId);
160 }
161
162 JournalArticle article = null;
163
164 try {
165 article = JournalArticleLocalServiceUtil.getLatestArticle(
166 articleGroupId, articleId, WorkflowConstants.STATUS_APPROVED);
167 }
168 catch (NoSuchArticleException nsae) {
169 }
170
171 if (article == null) {
172 try {
173 article = JournalArticleLocalServiceUtil.getLatestArticle(
174 articleGroupId, articleId,
175 WorkflowConstants.STATUS_EXPIRED);
176 }
177 catch (NoSuchArticleException nsae) {
178 }
179 }
180
181 Element rootElement = addExportDataRootElement(portletDataContext);
182
183 if (article == null) {
184 portletDataContext.setScopeGroupId(previousScopeGroupId);
185
186 return getExportDataRootElementString(rootElement);
187 }
188
189 StagedModelDataHandlerUtil.exportReferenceStagedModel(
190 portletDataContext, portletId, article);
191
192 String defaultTemplateId = article.getTemplateId();
193 String preferenceTemplateId = portletPreferences.getValue(
194 "ddmTemplateKey", null);
195
196 if (Validator.isNotNull(defaultTemplateId) &&
197 Validator.isNotNull(preferenceTemplateId) &&
198 !defaultTemplateId.equals(preferenceTemplateId)) {
199
200 DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.getTemplate(
201 article.getGroupId(),
202 PortalUtil.getClassNameId(DDMStructure.class),
203 preferenceTemplateId, true);
204
205 StagedModelDataHandlerUtil.exportReferenceStagedModel(
206 portletDataContext, article, ddmTemplate,
207 PortletDataContext.REFERENCE_TYPE_STRONG);
208 }
209
210 portletDataContext.setScopeGroupId(previousScopeGroupId);
211
212 return getExportDataRootElementString(rootElement);
213 }
214
215 @Override
216 protected PortletPreferences doImportData(
217 PortletDataContext portletDataContext, String portletId,
218 PortletPreferences portletPreferences, String data)
219 throws Exception {
220
221 portletDataContext.importPermissions(
222 JournalPermission.RESOURCE_NAME,
223 portletDataContext.getSourceGroupId(),
224 portletDataContext.getScopeGroupId());
225
226 long previousScopeGroupId = portletDataContext.getScopeGroupId();
227
228 long importGroupId = GetterUtil.getLong(
229 portletPreferences.getValue("groupId", null));
230
231 if (importGroupId == portletDataContext.getSourceGroupId()) {
232 portletDataContext.setScopeGroupId(portletDataContext.getGroupId());
233 }
234
235 Element ddmStructuresElement =
236 portletDataContext.getImportDataGroupElement(DDMStructure.class);
237
238 List<Element> ddmStructureElements = ddmStructuresElement.elements();
239
240 for (Element ddmStructureElement : ddmStructureElements) {
241 StagedModelDataHandlerUtil.importStagedModel(
242 portletDataContext, ddmStructureElement);
243 }
244
245 Element ddmTemplatesElement =
246 portletDataContext.getImportDataGroupElement(DDMTemplate.class);
247
248 List<Element> ddmTemplateElements = ddmTemplatesElement.elements();
249
250 for (Element ddmTemplateElement : ddmTemplateElements) {
251 StagedModelDataHandlerUtil.importStagedModel(
252 portletDataContext, ddmTemplateElement);
253 }
254
255 Element articlesElement = portletDataContext.getImportDataGroupElement(
256 JournalArticle.class);
257
258 List<Element> articleElements = articlesElement.elements();
259
260 if (!articleElements.isEmpty()) {
261 StagedModelDataHandlerUtil.importStagedModel(
262 portletDataContext, articleElements.get(0));
263 }
264
265 String articleId = portletPreferences.getValue("articleId", null);
266
267 if (Validator.isNotNull(articleId)) {
268 Map<String, String> articleIds =
269 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
270 JournalArticle.class + ".articleId");
271
272 articleId = MapUtil.getString(articleIds, articleId, articleId);
273
274 portletPreferences.setValue("articleId", articleId);
275
276 String importedArticleGroupId = String.valueOf(
277 portletDataContext.getScopeGroupId());
278
279 portletPreferences.setValue("groupId", importedArticleGroupId);
280
281 Layout layout = LayoutLocalServiceUtil.getLayout(
282 portletDataContext.getPlid());
283
284 JournalContentSearchLocalServiceUtil.updateContentSearch(
285 portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
286 layout.getLayoutId(), portletId, articleId, true);
287 }
288 else {
289 portletPreferences.setValue("groupId", StringPool.BLANK);
290 portletPreferences.setValue("articleId", StringPool.BLANK);
291 }
292
293 String ddmTemplateKey = portletPreferences.getValue(
294 "ddmTemplateKey", null);
295
296 if (Validator.isNotNull(ddmTemplateKey)) {
297 Map<String, String> ddmTemplateKeys =
298 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
299 DDMTemplate.class + ".ddmTemplateKey");
300
301 ddmTemplateKey = MapUtil.getString(
302 ddmTemplateKeys, ddmTemplateKey, ddmTemplateKey);
303
304 portletPreferences.setValue("ddmTemplateKey", ddmTemplateKey);
305 }
306 else {
307 portletPreferences.setValue("ddmTemplateKey", StringPool.BLANK);
308 }
309
310 portletDataContext.setScopeGroupId(previousScopeGroupId);
311
312 return portletPreferences;
313 }
314
315 @Override
316 protected void doPrepareManifestSummary(
317 PortletDataContext portletDataContext,
318 PortletPreferences portletPreferences)
319 throws Exception {
320
321 ManifestSummary manifestSummary =
322 portletDataContext.getManifestSummary();
323
324 if ((portletPreferences == null) ||
325 (manifestSummary.getModelAdditionCount(
326 JournalArticle.class) > -1)) {
327
328 return;
329 }
330
331 String articleId = portletPreferences.getValue(
332 "articleId", StringPool.BLANK);
333
334 if (Validator.isNotNull(articleId)) {
335 manifestSummary.addModelAdditionCount(
336 new StagedModelType(JournalArticle.class), 1);
337 }
338 }
339
340 private static Log _log = LogFactoryUtil.getLog(
341 JournalContentPortletDataHandler.class);
342
343 }