1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
62   * <a href="JournalPortletDataHandlerImpl.java.html"><b><i>View Source</i></b>
63   * </a>
64   *
65   * <p>
66   * Provides the Journal portlet export and import functionality, which is to
67   * clone all articles, structures, and templates associated with the layout's
68   * group. Upon import, new instances of the corresponding articles, structures,
69   * and templates are created or updated. The author of the newly created
70   * objects are determined by the JournalCreationStrategy class defined in
71   * <i>portal.properties</i>.
72   * </p>
73   *
74   * <p>
75   * This <code>PortletDataHandler</code> differs from
76   * <code>JournalContentPortletDataHandlerImpl</code> in that it exports all
77   * articles owned by the group whether or not they are actually displayed in a
78   * portlet in the layout set.
79   * </p>
80   *
81   * @author Raymond Augé
82   * @author Joel Kozikowski
83   * @author Brian Wing Shun Chan
84   *
85   * @see com.liferay.portlet.journal.lar.JournalContentPortletDataHandlerImpl
86   * @see com.liferay.portlet.journal.lar.JournalCreationStrategy
87   *
88   */
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             // Articles
137 
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             // Structures
164 
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             // Templates
191 
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             // Articles
259 
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             // Structures
336 
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             // Templates
391 
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             // No special modification to the incoming portlet preferences
444             // needed
445 
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 }