1
22
23 package com.liferay.portlet.journal.lar;
24
25 import com.liferay.portal.kernel.util.GetterUtil;
26 import com.liferay.portal.kernel.util.StringPool;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.portal.kernel.xml.Document;
29 import com.liferay.portal.kernel.xml.Element;
30 import com.liferay.portal.kernel.xml.SAXReaderUtil;
31 import com.liferay.portal.lar.PortletDataContext;
32 import com.liferay.portal.lar.PortletDataException;
33 import com.liferay.portal.lar.PortletDataHandler;
34 import com.liferay.portal.lar.PortletDataHandlerBoolean;
35 import com.liferay.portal.lar.PortletDataHandlerControl;
36 import com.liferay.portlet.documentlibrary.lar.DLPortletDataHandlerImpl;
37 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
38 import com.liferay.portlet.documentlibrary.model.DLFileRank;
39 import com.liferay.portlet.documentlibrary.model.DLFolder;
40 import com.liferay.portlet.imagegallery.lar.IGPortletDataHandlerImpl;
41 import com.liferay.portlet.imagegallery.model.IGFolder;
42 import com.liferay.portlet.imagegallery.model.IGImage;
43 import com.liferay.portlet.journal.NoSuchArticleException;
44 import com.liferay.portlet.journal.model.JournalArticle;
45 import com.liferay.portlet.journal.model.JournalStructure;
46 import com.liferay.portlet.journal.model.JournalTemplate;
47 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
48 import com.liferay.portlet.journal.service.persistence.JournalStructureUtil;
49 import com.liferay.portlet.journal.service.persistence.JournalTemplateUtil;
50 import com.liferay.util.MapUtil;
51
52 import java.util.Collections;
53 import java.util.List;
54 import java.util.Map;
55
56 import javax.portlet.PortletPreferences;
57
58 import org.apache.commons.logging.Log;
59 import org.apache.commons.logging.LogFactory;
60
61
92 public class JournalContentPortletDataHandlerImpl
93 implements PortletDataHandler {
94
95 public PortletPreferences deleteData(
96 PortletDataContext context, String portletId,
97 PortletPreferences prefs)
98 throws PortletDataException {
99
100 try {
101 prefs.setValue("group-id", StringPool.BLANK);
102 prefs.setValue("article-id", StringPool.BLANK);
103
104 return prefs;
105 }
106 catch (Exception e) {
107 throw new PortletDataException(e);
108 }
109 }
110
111 public String exportData(
112 PortletDataContext context, String portletId,
113 PortletPreferences prefs)
114 throws PortletDataException {
115
116 try {
117 String articleId = prefs.getValue("article-id", null);
118
119 if (articleId == null) {
120 if (_log.isWarnEnabled()) {
121 _log.warn(
122 "No article id found in preferences of portlet " +
123 portletId);
124 }
125
126 return StringPool.BLANK;
127 }
128
129 long articleGroupId = GetterUtil.getLong(
130 prefs.getValue("group-id", StringPool.BLANK));
131
132 if (articleGroupId <= 0) {
133 if (_log.isWarnEnabled()) {
134 _log.warn(
135 "No group id found in preferences of portlet " +
136 portletId);
137 }
138
139 return StringPool.BLANK;
140 }
141
142 JournalArticle article = null;
143
144 try {
145 article = JournalArticleLocalServiceUtil.getLatestArticle(
146 articleGroupId, articleId);
147 }
148 catch (NoSuchArticleException nsae) {
149 if (_log.isWarnEnabled()) {
150 _log.warn(nsae);
151 }
152 }
153
154 if (article == null) {
155 return StringPool.BLANK;
156 }
157
158 Document doc = SAXReaderUtil.createDocument();
159
160 Element root = doc.addElement("journal-content-data");
161
162 Element dlFoldersEl = root.addElement("dl-folders");
163 Element dlFilesEl = root.addElement("dl-file-entries");
164 Element dlFileRanksEl = root.addElement("dl-file-ranks");
165 Element igFoldersEl = root.addElement("ig-folders");
166 Element igImagesEl = root.addElement("ig-images");
167
168 JournalPortletDataHandlerImpl.exportArticle(
169 context, root, dlFoldersEl, dlFilesEl, dlFileRanksEl,
170 igFoldersEl, igImagesEl, article);
171
172 String structureId = article.getStructureId();
173
174 if (Validator.isNotNull(structureId)) {
175 JournalStructure structure = JournalStructureUtil.findByG_S(
176 article.getGroupId(), structureId);
177
178 JournalPortletDataHandlerImpl.exportStructure(
179 context, root, structure);
180 }
181
182 String templateId = article.getTemplateId();
183
184 if (Validator.isNotNull(templateId)) {
185 JournalTemplate template = JournalTemplateUtil.findByG_T(
186 article.getGroupId(), templateId);
187
188 JournalPortletDataHandlerImpl.exportTemplate(
189 context, root, template);
190 }
191
192 return doc.formattedString();
193 }
194 catch (Exception e) {
195 throw new PortletDataException(e);
196 }
197 }
198
199 public PortletDataHandlerControl[] getExportControls() {
200 return new PortletDataHandlerControl[] {
201 _selectedArticles, _embeddedAssets, _images, _comments, _ratings,
202 _tags
203 };
204 }
205
206 public PortletDataHandlerControl[] getImportControls() {
207 return new PortletDataHandlerControl[] {
208 _selectedArticles, _images, _comments, _ratings, _tags
209 };
210 }
211
212 public PortletPreferences importData(
213 PortletDataContext context, String portletId,
214 PortletPreferences prefs, String data)
215 throws PortletDataException {
216
217 try {
218 if (Validator.isNull(data)) {
219 return null;
220 }
221
222 Document doc = SAXReaderUtil.read(data);
223
224 Element root = doc.getRootElement();
225
226 Element structureEl = root.element("structure");
227
228 Map<String, String> structureIds =
229 (Map<String, String>)context.getNewPrimaryKeysMap(
230 JournalStructure.class);
231
232 if (structureEl != null) {
233 JournalPortletDataHandlerImpl.importStructure(
234 context, structureIds, structureEl);
235 }
236
237 Element templateEl = root.element("template");
238
239 Map<String, String> templateIds =
240 (Map<String, String>)context.getNewPrimaryKeysMap(
241 JournalTemplate.class);
242
243 if (templateEl != null) {
244 JournalPortletDataHandlerImpl.importTemplate(
245 context, structureIds, templateIds, templateEl);
246 }
247
248 Element articleEl = root.element("article");
249
250 Map<String, String> articleIds =
251 (Map<String, String>)context.getNewPrimaryKeysMap(
252 JournalArticle.class);
253
254 if (articleEl != null) {
255 JournalPortletDataHandlerImpl.importArticle(
256 context, structureIds, templateIds, articleIds, articleEl);
257 }
258
259 String articleId = prefs.getValue("article-id", StringPool.BLANK);
260
261 if (Validator.isNotNull(articleId)) {
262 articleId = MapUtil.getString(articleIds, articleId, articleId);
263
264 prefs.setValue(
265 "group-id", String.valueOf(context.getGroupId()));
266 prefs.setValue("article-id", articleId);
267 }
268
269 Element dlFoldersEl = root.element("dl-folders");
270
271 List<Element> dlFolderEls = Collections.EMPTY_LIST;
272
273 if (dlFoldersEl != null) {
274 dlFolderEls = dlFoldersEl.elements("folder");
275 }
276
277 Map<Long, Long> dlFolderPKs =
278 (Map<Long, Long>)context.getNewPrimaryKeysMap(DLFolder.class);
279
280 for (Element folderEl : dlFolderEls) {
281 String path = folderEl.attributeValue("path");
282
283 if (!context.isPathNotProcessed(path)) {
284 continue;
285 }
286
287 DLFolder folder = (DLFolder)context.getZipEntryAsObject(path);
288
289 DLPortletDataHandlerImpl.importFolder(
290 context, dlFolderPKs, folder);
291 }
292
293 Element dlFileEntriesEl = root.element("dl-file-entries");
294
295 List<Element> dlFileEntryEls = Collections.EMPTY_LIST;
296
297 if (dlFileEntriesEl != null) {
298 dlFileEntryEls = dlFileEntriesEl.elements("file-entry");
299 }
300
301 Map<String, String> fileEntryNames =
302 (Map<String, String>)context.getNewPrimaryKeysMap(
303 DLFileEntry.class);
304
305 for (Element fileEntryEl : dlFileEntryEls) {
306 String path = fileEntryEl.attributeValue("path");
307
308 if (!context.isPathNotProcessed(path)) {
309 continue;
310 }
311
312 DLFileEntry fileEntry =
313 (DLFileEntry)context.getZipEntryAsObject(path);
314
315 String binPath = fileEntryEl.attributeValue("bin-path");
316
317 DLPortletDataHandlerImpl.importFileEntry(
318 context, dlFolderPKs, fileEntryNames, fileEntry, binPath);
319 }
320
321 Element dlFileRanksEl = root.element("dl-file-ranks");
322
323 List<Element> dlFileRankEls = Collections.EMPTY_LIST;
324
325 if (dlFileRanksEl != null) {
326 dlFileRankEls = dlFileRanksEl.elements("file-rank");
327 }
328
329 for (Element fileRankEl : dlFileRankEls) {
330 String path = fileRankEl.attributeValue("path");
331
332 if (!context.isPathNotProcessed(path)) {
333 continue;
334 }
335
336 DLFileRank fileRank =
337 (DLFileRank)context.getZipEntryAsObject(path);
338
339 DLPortletDataHandlerImpl.importFileRank(
340 context, dlFolderPKs, fileEntryNames, fileRank);
341 }
342
343 Element igFoldersEl = root.element("ig-folders");
344
345 List<Element> igFolderEls = Collections.EMPTY_LIST;
346
347 if (igFoldersEl != null) {
348 igFolderEls = igFoldersEl.elements("folder");
349 }
350
351 Map<Long, Long> igFolderPKs =
352 (Map<Long, Long>)context.getNewPrimaryKeysMap(IGFolder.class);
353
354 for (Element folderEl : igFolderEls) {
355 String path = folderEl.attributeValue("path");
356
357 if (!context.isPathNotProcessed(path)) {
358 continue;
359 }
360
361 IGFolder folder = (IGFolder)context.getZipEntryAsObject(path);
362
363 IGPortletDataHandlerImpl.importFolder(
364 context, igFolderPKs, folder);
365 }
366
367 Element igImagesEl = root.element("ig-images");
368
369 List<Element> igImageEls = Collections.EMPTY_LIST;
370
371 if (igImagesEl != null) {
372 igImageEls = igImagesEl.elements("image");
373 }
374
375 for (Element imageEl : igImageEls) {
376 String path = imageEl.attributeValue("path");
377
378 if (!context.isPathNotProcessed(path)) {
379 continue;
380 }
381
382 IGImage image = (IGImage)context.getZipEntryAsObject(path);
383
384 String binPath = imageEl.attributeValue("bin-path");
385
386 IGPortletDataHandlerImpl.importImage(
387 context, igFolderPKs, image, binPath);
388 }
389
390 return prefs;
391 }
392 catch (Exception e) {
393 throw new PortletDataException(e);
394 }
395 }
396
397 public boolean isPublishToLiveByDefault() {
398 return true;
399 }
400
401 private static final String _NAMESPACE = "journal";
402
403 private static final PortletDataHandlerBoolean _selectedArticles =
404 new PortletDataHandlerBoolean(
405 _NAMESPACE, "selected-articles", true, true);
406
407 private static final PortletDataHandlerBoolean _embeddedAssets =
408 new PortletDataHandlerBoolean(_NAMESPACE, "embedded-assets");
409
410 private static final PortletDataHandlerBoolean _images =
411 new PortletDataHandlerBoolean(_NAMESPACE, "images");
412
413 private static final PortletDataHandlerBoolean _comments =
414 new PortletDataHandlerBoolean(_NAMESPACE, "comments");
415
416 private static final PortletDataHandlerBoolean _ratings =
417 new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
418
419 private static final PortletDataHandlerBoolean _tags =
420 new PortletDataHandlerBoolean(_NAMESPACE, "tags");
421
422 private static Log _log =
423 LogFactory.getLog(JournalContentPortletDataHandlerImpl.class);
424
425 }