1
22
23 package com.liferay.portlet.journal.lar;
24
25 import com.liferay.portal.kernel.lar.PortletDataContext;
26 import com.liferay.portal.kernel.lar.PortletDataException;
27 import com.liferay.portal.kernel.lar.PortletDataHandler;
28 import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
29 import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
30 import com.liferay.portal.kernel.util.StringMaker;
31 import com.liferay.portal.kernel.util.StringPool;
32 import com.liferay.portal.util.PortalUtil;
33 import com.liferay.portal.util.PortletKeys;
34 import com.liferay.portlet.journal.model.JournalArticle;
35 import com.liferay.portlet.journal.model.JournalStructure;
36 import com.liferay.portlet.journal.model.JournalTemplate;
37 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
38 import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
39 import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
40 import com.liferay.portlet.journal.service.persistence.JournalArticleUtil;
41 import com.liferay.portlet.journal.service.persistence.JournalStructureUtil;
42 import com.liferay.portlet.journal.service.persistence.JournalTemplateUtil;
43 import com.liferay.util.MapUtil;
44 import com.liferay.util.xml.XMLFormatter;
45
46 import com.thoughtworks.xstream.XStream;
47
48 import java.util.Iterator;
49 import java.util.List;
50 import java.util.Map;
51
52 import javax.portlet.PortletPreferences;
53
54 import org.apache.commons.logging.Log;
55 import org.apache.commons.logging.LogFactory;
56
57 import org.dom4j.Document;
58 import org.dom4j.DocumentHelper;
59 import org.dom4j.Element;
60
61
89 public class JournalPortletDataHandlerImpl implements PortletDataHandler {
90
91 public PortletDataHandlerControl[] getExportControls()
92 throws PortletDataException{
93
94 return new PortletDataHandlerControl[] {_enableExport};
95 }
96
97 public PortletDataHandlerControl[] getImportControls()
98 throws PortletDataException{
99
100 return new PortletDataHandlerControl[] {_enableImport};
101 }
102
103 public String exportData(
104 PortletDataContext context, String portletId,
105 PortletPreferences prefs)
106 throws PortletDataException {
107
108 Map parameterMap = context.getParameterMap();
109
110 boolean exportData = MapUtil.getBoolean(
111 parameterMap, _EXPORT_JOURNAL_DATA,
112 _enableExport.getDefaultState());
113
114 if (_log.isDebugEnabled()) {
115 if (exportData) {
116 _log.debug("Exporting data is enabled");
117 }
118 else {
119 _log.debug("Exporting data is disabled");
120 }
121 }
122
123 if (!exportData) {
124 return null;
125 }
126
127 try {
128 XStream xStream = new XStream();
129
130 Document doc = DocumentHelper.createDocument();
131
132 Element root = doc.addElement("journal-data");
133
134 root.addAttribute("group-id", String.valueOf(context.getGroupId()));
135
136
138 List obj = JournalArticleUtil.findByGroupId(context.getGroupId());
139
140 Iterator itr = obj.iterator();
141
142 while (itr.hasNext()) {
143 JournalArticle article = (JournalArticle)itr.next();
144
145 String articlePrimaryKey = getPrimaryKey(
146 article.getGroupId(), article.getArticleId());
147
148 if (context.addPrimaryKey(
149 JournalArticle.class, articlePrimaryKey)) {
150
151 itr.remove();
152 }
153 }
154
155 String xml = xStream.toXML(obj);
156
157 Element el = root.addElement("articles");
158
159 Document tempDoc = PortalUtil.readDocumentFromXML(xml);
160
161 el.content().add(tempDoc.getRootElement().createCopy());
162
163
165 obj = JournalStructureUtil.findByGroupId(context.getGroupId());
166
167 itr = obj.iterator();
168
169 while (itr.hasNext()) {
170 JournalStructure structure = (JournalStructure)itr.next();
171
172 String structurePrimaryKey = getPrimaryKey(
173 structure.getGroupId(), structure.getStructureId());
174
175 if (context.addPrimaryKey(
176 JournalStructure.class, structurePrimaryKey)) {
177
178 itr.remove();
179 }
180 }
181
182 xml = xStream.toXML(obj);
183
184 tempDoc = PortalUtil.readDocumentFromXML(xml);
185
186 el = root.addElement("structures");
187
188 el.content().add(tempDoc.getRootElement().createCopy());
189
190
192 obj = JournalTemplateUtil.findByGroupId(context.getGroupId());
193
194 itr = obj.iterator();
195
196 while (itr.hasNext()) {
197 JournalTemplate template = (JournalTemplate)itr.next();
198
199 String templatePrimaryKey = getPrimaryKey(
200 template.getGroupId(), template.getTemplateId());
201
202 if (context.addPrimaryKey(
203 JournalTemplate.class, templatePrimaryKey)) {
204
205 itr.remove();
206 }
207 }
208
209 xml = xStream.toXML(obj);
210
211 el = root.addElement("templates");
212
213 tempDoc = PortalUtil.readDocumentFromXML(xml);
214
215 el.content().add(tempDoc.getRootElement().createCopy());
216
217 return XMLFormatter.toString(doc);
218 }
219 catch (Exception e) {
220 throw new PortletDataException(e);
221 }
222 }
223
224 public PortletPreferences importData(
225 PortletDataContext context, String portletId,
226 PortletPreferences prefs, String data)
227 throws PortletDataException {
228
229 Map parameterMap = context.getParameterMap();
230
231 boolean importData = MapUtil.getBoolean(
232 parameterMap, _IMPORT_JOURNAL_DATA,
233 _enableImport.getDefaultState());
234
235 if (_log.isDebugEnabled()) {
236 if (importData) {
237 _log.debug("Importing data is enabled");
238 }
239 else {
240 _log.debug("Importing data is disabled");
241 }
242 }
243
244 if (!importData) {
245 return null;
246 }
247
248 try {
249 JournalCreationStrategy creationStrategy =
250 JournalCreationStrategyFactory.getInstance();
251
252 XStream xStream = new XStream();
253
254 Document doc = PortalUtil.readDocumentFromXML(data);
255
256 Element root = doc.getRootElement();
257
258
260 Element el = root.element("articles").element("list");
261
262 Document tempDoc = DocumentHelper.createDocument();
263
264 tempDoc.content().add(el.createCopy());
265
266 List articles = (List)xStream.fromXML(
267 XMLFormatter.toString(tempDoc));
268
269 Iterator itr = articles.iterator();
270
271 while (itr.hasNext()) {
272 JournalArticle article = (JournalArticle)itr.next();
273
274 article.setGroupId(context.getGroupId());
275
276 if (JournalArticleUtil.fetchByPrimaryKey(
277 article.getPrimaryKey()) == null) {
278
279 article.setNew(true);
280
281 long authorId = creationStrategy.getAuthorUserId(
282 context.getCompanyId(), context.getGroupId(), article);
283
284 if (authorId > 0) {
285 article.setUserId(authorId);
286 article.setUserName(
287 creationStrategy.getAuthorUserName(
288 context.getCompanyId(), context.getGroupId(),
289 article));
290 }
291
292 long approvedById = creationStrategy.getApprovalUserId(
293 context.getCompanyId(), context.getGroupId(), article);
294
295 if (approvedById > 0) {
296 article.setApprovedByUserId(approvedById);
297 article.setApprovedByUserName(
298 creationStrategy.getApprovalUserName(
299 context.getCompanyId(), context.getGroupId(),
300 article));
301 article.setApproved(true);
302 }
303 else {
304 article.setApprovedByUserId(0);
305 article.setApprovedByUserName(null);
306 article.setApproved(false);
307 }
308
309 String newContent = creationStrategy.getTransformedContent(
310 context.getCompanyId(), context.getGroupId(), article);
311
312 if (newContent != null) {
313 article.setContent(newContent);
314 }
315
316 article = JournalArticleUtil.update(article);
317
318 boolean addCommunityPermissions =
319 creationStrategy.addCommunityPermissions(
320 context.getCompanyId(), context.getGroupId(),
321 article);
322 boolean addGuestPermissions =
323 creationStrategy.addGuestPermissions(
324 context.getCompanyId(), context.getGroupId(),
325 article);
326
327 JournalArticleLocalServiceUtil.addArticleResources(
328 article, addCommunityPermissions, addGuestPermissions);
329 }
330 else {
331 JournalArticleUtil.update(article, true);
332 }
333 }
334
335
337 el = root.element("structures").element("list");
338
339 tempDoc = DocumentHelper.createDocument();
340
341 tempDoc.content().add(el.createCopy());
342
343 List structures = (List)xStream.fromXML(
344 XMLFormatter.toString(tempDoc));
345
346 itr = structures.iterator();
347
348 while (itr.hasNext()) {
349 JournalStructure structure = (JournalStructure)itr.next();
350
351 structure.setGroupId(context.getGroupId());
352
353 if (JournalStructureUtil.fetchByPrimaryKey(
354 structure.getPrimaryKey()) == null) {
355
356 structure.setNew(true);
357
358 long authorId = creationStrategy.getAuthorUserId(
359 context.getCompanyId(), context.getGroupId(),
360 structure);
361
362 if (authorId > 0) {
363 structure.setUserId(authorId);
364 structure.setUserName(
365 creationStrategy.getAuthorUserName(
366 context.getCompanyId(), context.getGroupId(),
367 structure));
368 }
369
370 structure = JournalStructureUtil.update(structure);
371
372 boolean addCommunityPermissions =
373 creationStrategy.addCommunityPermissions(
374 context.getCompanyId(), context.getGroupId(),
375 structure);
376 boolean addGuestPermissions =
377 creationStrategy.addGuestPermissions(
378 context.getCompanyId(), context.getGroupId(),
379 structure);
380
381 JournalStructureLocalServiceUtil.addStructureResources(
382 structure, addCommunityPermissions,
383 addGuestPermissions);
384 }
385 else {
386 JournalStructureUtil.update(structure, true);
387 }
388 }
389
390
392 el = root.element("templates").element("list");
393
394 tempDoc = DocumentHelper.createDocument();
395
396 tempDoc.content().add(el.createCopy());
397
398 List templates = (List)xStream.fromXML(
399 XMLFormatter.toString(tempDoc));
400
401 itr = templates.iterator();
402
403 while (itr.hasNext()) {
404 JournalTemplate template = (JournalTemplate)itr.next();
405
406 template.setGroupId(context.getGroupId());
407
408 if (JournalTemplateUtil.fetchByPrimaryKey(
409 template.getPrimaryKey()) == null) {
410
411 template.setNew(true);
412
413 long authorId = creationStrategy.getAuthorUserId(
414 context.getCompanyId(), context.getGroupId(), template);
415
416 if (authorId > 0) {
417 template.setUserId(authorId);
418 template.setUserName(
419 creationStrategy.getAuthorUserName(
420 context.getCompanyId(), context.getGroupId(),
421 template));
422 }
423
424 template = JournalTemplateUtil.update(template);
425
426 boolean addCommunityPermissions =
427 creationStrategy.addCommunityPermissions(
428 context.getCompanyId(), context.getGroupId(),
429 template);
430 boolean addGuestPermissions =
431 creationStrategy.addGuestPermissions(
432 context.getCompanyId(), context.getGroupId(),
433 template);
434
435 JournalTemplateLocalServiceUtil.addTemplateResources(
436 template, addCommunityPermissions, addGuestPermissions);
437 }
438 else {
439 JournalTemplateUtil.update(template, true);
440 }
441 }
442
443
446 return null;
447 }
448 catch (Exception e) {
449 throw new PortletDataException(e);
450 }
451 }
452
453 protected String getPrimaryKey(long groupId, String key) {
454 StringMaker sm = new StringMaker();
455
456 sm.append(groupId);
457 sm.append(StringPool.POUND);
458 sm.append(key);
459
460 return sm.toString();
461 }
462
463 private static final String _EXPORT_JOURNAL_DATA =
464 "export-" + PortletKeys.JOURNAL + "-data";
465
466 private static final String _IMPORT_JOURNAL_DATA =
467 "import-" + PortletKeys.JOURNAL + "-data";
468
469 private static final PortletDataHandlerBoolean _enableExport =
470 new PortletDataHandlerBoolean(_EXPORT_JOURNAL_DATA, true, null);
471
472 private static final PortletDataHandlerBoolean _enableImport =
473 new PortletDataHandlerBoolean(_IMPORT_JOURNAL_DATA, true, null);
474
475 private static Log _log =
476 LogFactory.getLog(JournalPortletDataHandlerImpl.class);
477
478 }