001
014
015 package com.liferay.portlet.journal.lar;
016
017 import com.liferay.portal.kernel.lar.BasePortletDataHandler;
018 import com.liferay.portal.kernel.lar.PortletDataContext;
019 import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
020 import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
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.kernel.xml.Document;
029 import com.liferay.portal.kernel.xml.Element;
030 import com.liferay.portal.kernel.xml.SAXReaderUtil;
031 import com.liferay.portal.model.Layout;
032 import com.liferay.portal.service.LayoutLocalServiceUtil;
033 import com.liferay.portlet.documentlibrary.lar.DLPortletDataHandlerImpl;
034 import com.liferay.portlet.journal.NoSuchArticleException;
035 import com.liferay.portlet.journal.model.JournalArticle;
036 import com.liferay.portlet.journal.model.JournalTemplate;
037 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
038 import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
039 import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
040
041 import java.util.List;
042 import java.util.Map;
043
044 import javax.portlet.PortletPreferences;
045
046
072 public class JournalContentPortletDataHandlerImpl
073 extends BasePortletDataHandler {
074
075 @Override
076 public String[] getDataPortletPreferences() {
077 return new String[] {"groupId", "articleId", "templateId"};
078 }
079
080 @Override
081 public PortletDataHandlerControl[] getExportControls() {
082 return new PortletDataHandlerControl[] {
083 _selectedArticles, _embeddedAssets
084 };
085 }
086
087 @Override
088 public PortletDataHandlerControl[] getExportMetadataControls() {
089 return new PortletDataHandlerControl[] {
090 new PortletDataHandlerBoolean(
091 _NAMESPACE, "web-content", true,
092 JournalPortletDataHandlerImpl.getMetadataControls()),
093 new PortletDataHandlerBoolean(
094 _NAMESPACE, "folders-and-documents", true,
095 DLPortletDataHandlerImpl.getMetadataControls()
096 )
097 };
098 }
099
100 @Override
101 public PortletDataHandlerControl[] getImportControls() {
102 return new PortletDataHandlerControl[] {
103 _selectedArticles
104 };
105 }
106
107 @Override
108 public PortletDataHandlerControl[] getImportMetadataControls() {
109 return new PortletDataHandlerControl[] {
110 new PortletDataHandlerBoolean(
111 _NAMESPACE, "web-content", true,
112 JournalPortletDataHandlerImpl.getMetadataControls()),
113 new PortletDataHandlerBoolean(
114 _NAMESPACE, "folders-and-documents", true,
115 DLPortletDataHandlerImpl.getMetadataControls()
116 )
117 };
118 }
119
120 @Override
121 public boolean isAlwaysExportable() {
122 return _ALWAYS_EXPORTABLE;
123 }
124
125 @Override
126 public boolean isAlwaysStaged() {
127 return _ALWAYS_STAGED;
128 }
129
130 @Override
131 public boolean isDataLocalized() {
132 return _DATA_LOCALIZED;
133 }
134
135 @Override
136 public boolean isPublishToLiveByDefault() {
137 return _PUBLISH_TO_LIVE_BY_DEFAULT;
138 }
139
140 @Override
141 protected PortletPreferences doDeleteData(
142 PortletDataContext portletDataContext, String portletId,
143 PortletPreferences portletPreferences)
144 throws Exception {
145
146 if (portletPreferences == null) {
147 return portletPreferences;
148 }
149
150 portletPreferences.setValue("groupId", StringPool.BLANK);
151 portletPreferences.setValue("articleId", StringPool.BLANK);
152
153 return portletPreferences;
154 }
155
156 @Override
157 protected String doExportData(
158 PortletDataContext portletDataContext, String portletId,
159 PortletPreferences portletPreferences)
160 throws Exception {
161
162 portletDataContext.addPermissions(
163 "com.liferay.portlet.journal",
164 portletDataContext.getScopeGroupId());
165
166 String articleId = portletPreferences.getValue("articleId", null);
167
168 if (articleId == null) {
169 if (_log.isDebugEnabled()) {
170 _log.debug(
171 "No article id found in preferences of portlet " +
172 portletId);
173 }
174
175 return StringPool.BLANK;
176 }
177
178 long articleGroupId = GetterUtil.getLong(
179 portletPreferences.getValue("groupId", StringPool.BLANK));
180
181 if (articleGroupId <= 0) {
182 if (_log.isWarnEnabled()) {
183 _log.warn(
184 "No group id found in preferences of portlet " + portletId);
185 }
186
187 return StringPool.BLANK;
188 }
189
190 long previousScopeGroupId = portletDataContext.getScopeGroupId();
191
192 if (articleGroupId != portletDataContext.getScopeGroupId()) {
193 portletDataContext.setScopeGroupId(articleGroupId);
194 }
195
196 JournalArticle article = null;
197
198 try {
199 article = JournalArticleLocalServiceUtil.getLatestArticle(
200 articleGroupId, articleId, WorkflowConstants.STATUS_APPROVED);
201 }
202 catch (NoSuchArticleException nsae) {
203 }
204
205 if (article == null) {
206 try {
207 article = JournalArticleLocalServiceUtil.getLatestArticle(
208 articleGroupId, articleId,
209 WorkflowConstants.STATUS_EXPIRED);
210 }
211 catch (NoSuchArticleException nsae) {
212 }
213 }
214
215 Document document = SAXReaderUtil.createDocument();
216
217 Element rootElement = document.addElement("journal-content-data");
218
219 if (article == null) {
220 portletDataContext.setScopeGroupId(previousScopeGroupId);
221
222 return document.formattedString();
223 }
224
225 String path = JournalPortletDataHandlerImpl.getArticlePath(
226 portletDataContext, article);
227
228 Element articleElement = rootElement.addElement("article");
229
230 articleElement.addAttribute("path", path);
231
232 Element dlFileEntryTypesElement = rootElement.addElement(
233 "dl-file-entry-types");
234 Element dlFoldersElement = rootElement.addElement("dl-folders");
235 Element dlFilesElement = rootElement.addElement("dl-file-entries");
236 Element dlFileRanksElement = rootElement.addElement("dl-file-ranks");
237 Element dlRepositoriesElement = rootElement.addElement(
238 "dl-repositories");
239 Element dlRepositoryEntriesElement = rootElement.addElement(
240 "dl-repository-entries");
241
242 JournalPortletDataHandlerImpl.exportArticle(
243 portletDataContext, rootElement, rootElement, rootElement,
244 dlFileEntryTypesElement, dlFoldersElement, dlFilesElement,
245 dlFileRanksElement, dlRepositoriesElement,
246 dlRepositoryEntriesElement, article, false);
247
248 String defaultTemplateId = article.getTemplateId();
249 String preferenceTemplateId = portletPreferences.getValue(
250 "templateId", null);
251
252 if (Validator.isNotNull(defaultTemplateId) &&
253 Validator.isNotNull(preferenceTemplateId) &&
254 !defaultTemplateId.equals(preferenceTemplateId)) {
255
256 JournalTemplate template =
257 JournalTemplateLocalServiceUtil.getTemplate(
258 article.getGroupId(), preferenceTemplateId, true);
259
260 JournalPortletDataHandlerImpl.exportTemplate(
261 portletDataContext, rootElement, dlFileEntryTypesElement,
262 dlFoldersElement, dlFilesElement, dlFileRanksElement,
263 dlRepositoriesElement, dlRepositoryEntriesElement, template);
264 }
265
266 portletDataContext.setScopeGroupId(previousScopeGroupId);
267
268 return document.formattedString();
269 }
270
271 @Override
272 protected PortletPreferences doImportData(
273 PortletDataContext portletDataContext, String portletId,
274 PortletPreferences portletPreferences, String data)
275 throws Exception {
276
277 portletDataContext.importPermissions(
278 "com.liferay.portlet.journal",
279 portletDataContext.getSourceGroupId(),
280 portletDataContext.getScopeGroupId());
281
282 if (Validator.isNull(data)) {
283 return null;
284 }
285
286 long previousScopeGroupId = portletDataContext.getScopeGroupId();
287
288 long importGroupId = GetterUtil.getLong(
289 portletPreferences.getValue("groupId", null));
290
291 if (importGroupId == portletDataContext.getSourceGroupId()) {
292 portletDataContext.setScopeGroupId(portletDataContext.getGroupId());
293 }
294 else if (importGroupId ==
295 portletDataContext.getSourceCompanyGroupId()) {
296
297 portletDataContext.setScopeGroupId(
298 portletDataContext.getCompanyGroupId());
299 }
300
301 Document document = SAXReaderUtil.read(data);
302
303 Element rootElement = document.getRootElement();
304
305 JournalPortletDataHandlerImpl.importReferencedData(
306 portletDataContext, rootElement);
307
308 Element structureElement = rootElement.element("structure");
309
310 if (structureElement != null) {
311 JournalPortletDataHandlerImpl.importStructure(
312 portletDataContext, structureElement);
313 }
314
315 List<Element> templateElements = rootElement.elements("template");
316
317 if (templateElements != null) {
318 for (Element templateElement : templateElements) {
319 JournalPortletDataHandlerImpl.importTemplate(
320 portletDataContext, templateElement);
321 }
322 }
323
324 Element articleElement = rootElement.element("article");
325
326 if (articleElement != null) {
327 JournalPortletDataHandlerImpl.importArticle(
328 portletDataContext, articleElement);
329 }
330
331 String articleId = portletPreferences.getValue("articleId", null);
332
333 if (Validator.isNotNull(articleId) && (articleElement != null)) {
334 Map<String, String> articleIds =
335 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
336 JournalArticle.class + ".articleId");
337
338 articleId = MapUtil.getString(articleIds, articleId, articleId);
339
340 portletPreferences.setValue("articleId", articleId);
341
342 String importedArticleGroupId = String.valueOf(
343 portletDataContext.getScopeGroupId());
344
345 portletPreferences.setValue("groupId", importedArticleGroupId);
346
347 Layout layout = LayoutLocalServiceUtil.getLayout(
348 portletDataContext.getPlid());
349
350 JournalContentSearchLocalServiceUtil.updateContentSearch(
351 portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
352 layout.getLayoutId(), portletId, articleId, true);
353 }
354 else {
355 portletPreferences.setValue("groupId", StringPool.BLANK);
356 portletPreferences.setValue("articleId", StringPool.BLANK);
357 }
358
359 String templateId = portletPreferences.getValue("templateId", null);
360
361 if (Validator.isNotNull(templateId)) {
362 Map<String, String> templateIds =
363 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
364 JournalTemplate.class + ".templateId");
365
366 templateId = MapUtil.getString(templateIds, templateId, templateId);
367
368 portletPreferences.setValue("templateId", templateId);
369 }
370 else {
371 portletPreferences.setValue("templateId", StringPool.BLANK);
372 }
373
374 portletDataContext.setScopeGroupId(previousScopeGroupId);
375
376 return portletPreferences;
377 }
378
379 private static final boolean _ALWAYS_EXPORTABLE = true;
380
381 private static final boolean _ALWAYS_STAGED = true;
382
383 private static final boolean _DATA_LOCALIZED = true;
384
385 private static final String _NAMESPACE = "journal";
386
387 private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
388
389 private static Log _log = LogFactoryUtil.getLog(
390 JournalContentPortletDataHandlerImpl.class);
391
392 private static PortletDataHandlerBoolean _embeddedAssets =
393 new PortletDataHandlerBoolean(_NAMESPACE, "embedded-assets");
394 private static PortletDataHandlerBoolean _selectedArticles =
395 new PortletDataHandlerBoolean(
396 _NAMESPACE, "selected-web-content", true, true);
397
398 }