1   /**
2    * Copyright (c) 2000-2008 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.GetterUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.util.DocumentUtil;
34  import com.liferay.portlet.journal.NoSuchArticleException;
35  import com.liferay.portlet.journal.model.JournalArticle;
36  import com.liferay.portlet.journal.model.JournalStructure;
37  import com.liferay.portlet.journal.model.JournalTemplate;
38  import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
39  import com.liferay.portlet.journal.model.impl.JournalStructureImpl;
40  import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
41  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
42  import com.liferay.portlet.journal.service.persistence.JournalStructureUtil;
43  import com.liferay.portlet.journal.service.persistence.JournalTemplateUtil;
44  import com.liferay.util.MapUtil;
45  
46  import com.thoughtworks.xstream.XStream;
47  
48  import java.util.List;
49  import java.util.Map;
50  
51  import javax.portlet.PortletPreferences;
52  
53  import org.apache.commons.logging.Log;
54  import org.apache.commons.logging.LogFactory;
55  
56  import org.dom4j.Document;
57  import org.dom4j.DocumentHelper;
58  import org.dom4j.Element;
59  
60  /**
61   * <a href="JournalContentPortletDataHandlerImpl.java.html"><b><i>View Source
62   * </i></b></a>
63   *
64   * <p>
65   * Provides the Journal Content portlet export and import functionality, which
66   * is to clone the article, structure, and template referenced in the
67   * Journal Content portlet if the article is associated with the layout's group.
68   * Upon import, a new instance of the corresponding article, structure, and
69   * template will be 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 from
76   * <code>JournalPortletDataHandlerImpl</code> in that it only exports articles
77   * referenced in Journal Content portlets. Articles not displayed in Journal
78   * Content portlets will not be exported unless
79   * <code>JournalPortletDataHandlerImpl</code> is activated.
80   * </p>
81   *
82   * @author Joel Kozikowski
83   * @author Raymond Aug�
84   * @author Bruno Farache
85   *
86   * @see com.liferay.portal.kernel.lar.PortletDataHandler
87   * @see com.liferay.portlet.journal.lar.JournalCreationStrategy
88   * @see com.liferay.portlet.journal.lar.JournalPortletDataHandlerImpl
89   *
90   */
91  public class JournalContentPortletDataHandlerImpl
92      implements PortletDataHandler {
93  
94      public PortletPreferences deleteData(
95              PortletDataContext context, String portletId,
96              PortletPreferences prefs)
97          throws PortletDataException {
98  
99          try {
100             prefs.setValue("group-id", StringPool.BLANK);
101             prefs.setValue("article-id", StringPool.BLANK);
102 
103             return prefs;
104         }
105         catch (Exception e) {
106             throw new PortletDataException(e);
107         }
108     }
109 
110     public String exportData(
111             PortletDataContext context, String portletId,
112             PortletPreferences prefs)
113         throws PortletDataException {
114 
115         try {
116             String articleId = prefs.getValue("article-id", null);
117 
118             if (articleId == null) {
119                 if (_log.isWarnEnabled()) {
120                     _log.warn(
121                         "No article id found in preferences of portlet " +
122                             portletId);
123                 }
124 
125                 return StringPool.BLANK;
126             }
127 
128             long articleGroupId = GetterUtil.getLong(
129                 prefs.getValue("group-id", StringPool.BLANK));
130 
131             if (articleGroupId <= 0) {
132                 if (_log.isWarnEnabled()) {
133                     _log.warn(
134                         "No group id found in preferences of portlet " +
135                             portletId);
136                 }
137 
138                 return StringPool.BLANK;
139             }
140 
141             JournalArticle article = null;
142 
143             try {
144                 article = JournalArticleLocalServiceUtil.getLatestArticle(
145                     articleGroupId, articleId);
146             }
147             catch (NoSuchArticleException nsae) {
148                 if (_log.isWarnEnabled()) {
149                     _log.warn(nsae);
150                 }
151             }
152 
153             if (article == null) {
154                 return StringPool.BLANK;
155             }
156 
157             XStream xStream = new XStream();
158 
159             Document doc = DocumentHelper.createDocument();
160 
161             Element root = doc.addElement("journal-content");
162 
163             List<Element> content = root.content();
164 
165             if (!context.addPrimaryKey(
166                     JournalArticle.class, article.getPrimaryKeyObj())) {
167 
168                 JournalPortletDataHandlerImpl.exportArticle(context, article);
169 
170                 String xml = xStream.toXML(article);
171 
172                 Document tempDoc = DocumentUtil.readDocumentFromXML(xml);
173 
174                 content.add(tempDoc.getRootElement().createCopy());
175             }
176 
177             String structureId = article.getStructureId();
178 
179             if (Validator.isNotNull(structureId)) {
180                 JournalStructure structure = JournalStructureUtil.findByG_S(
181                     article.getGroupId(), structureId);
182 
183                 if (!context.addPrimaryKey(
184                         JournalStructure.class, structure.getPrimaryKeyObj())) {
185 
186                     JournalPortletDataHandlerImpl.exportStructure(structure);
187 
188                     String xml = xStream.toXML(structure);
189 
190                     Document tempDoc = DocumentUtil.readDocumentFromXML(xml);
191 
192                     content.add(tempDoc.getRootElement().createCopy());
193                 }
194             }
195 
196             String templateId = article.getTemplateId();
197 
198             if (Validator.isNotNull(templateId)) {
199                 JournalTemplate template = JournalTemplateUtil.findByG_T(
200                     article.getGroupId(), templateId);
201 
202                 if (!context.addPrimaryKey(
203                         JournalTemplate.class, template.getPrimaryKeyObj())) {
204 
205                     JournalPortletDataHandlerImpl.exportTemplate(
206                         context, template);
207 
208                     String xml = xStream.toXML(template);
209 
210                     Document tempDoc = DocumentUtil.readDocumentFromXML(xml);
211 
212                     content.add(tempDoc.getRootElement().createCopy());
213                 }
214             }
215 
216             return doc.asXML();
217 
218         }
219         catch (Exception e) {
220             throw new PortletDataException(e);
221         }
222     }
223 
224     public PortletDataHandlerControl[] getExportControls()
225         throws PortletDataException {
226 
227         return new PortletDataHandlerControl[] {
228             _selectedArticles, _images, _comments, _ratings, _tags
229         };
230     }
231 
232     public PortletDataHandlerControl[] getImportControls()
233         throws PortletDataException {
234 
235         return new PortletDataHandlerControl[] {
236             _selectedArticles, _images, _comments, _ratings, _tags
237         };
238     }
239 
240     public PortletPreferences importData(
241             PortletDataContext context, String portletId,
242             PortletPreferences prefs, String data)
243         throws PortletDataException {
244 
245         try {
246             if (Validator.isNull(data)) {
247                 return null;
248             }
249 
250             XStream xStream = new XStream();
251 
252             Document doc = DocumentUtil.readDocumentFromXML(data);
253 
254             Element root = doc.getRootElement();
255 
256             Element el = root.element(JournalStructureImpl.class.getName());
257 
258             Document tempDoc = DocumentHelper.createDocument();
259 
260             Map<String, String> structureIds = context.getNewPrimaryKeysMap(
261                 JournalStructure.class);
262 
263             if (el != null) {
264                 tempDoc.content().add(el.createCopy());
265 
266                 JournalStructure structure = (JournalStructure)xStream.fromXML(
267                     tempDoc.asXML());
268 
269                 JournalPortletDataHandlerImpl.importStructure(
270                     context, structureIds, structure);
271             }
272 
273             el = root.element(JournalTemplateImpl.class.getName());
274 
275             Map<String, String> templateIds = context.getNewPrimaryKeysMap(
276                 JournalTemplate.class);
277 
278             if (el != null) {
279                 tempDoc = DocumentHelper.createDocument();
280 
281                 tempDoc.content().add(el.createCopy());
282 
283                 JournalTemplate template = (JournalTemplate)xStream.fromXML(
284                     tempDoc.asXML());
285 
286                 JournalPortletDataHandlerImpl.importTemplate(
287                     context, structureIds, templateIds, template);
288             }
289 
290             el = root.element(JournalArticleImpl.class.getName());
291 
292             Map<String, String> articleIds = context.getNewPrimaryKeysMap(
293                 JournalArticle.class);
294 
295             if (el != null) {
296                 tempDoc = DocumentHelper.createDocument();
297 
298                 tempDoc.content().add(el.createCopy());
299 
300                 JournalArticle article = (JournalArticle)xStream.fromXML(
301                     tempDoc.asXML());
302 
303                 JournalPortletDataHandlerImpl.importArticle(
304                     context, structureIds, templateIds, articleIds, article);
305             }
306 
307             String articleId = prefs.getValue("article-id", StringPool.BLANK);
308 
309             if (Validator.isNotNull(articleId)) {
310                 articleId = MapUtil.getString(articleIds, articleId, articleId);
311 
312                 prefs.setValue(
313                     "group-id", String.valueOf(context.getGroupId()));
314                 prefs.setValue("article-id", articleId);
315             }
316 
317             return prefs;
318         }
319         catch (Exception e) {
320             throw new PortletDataException(e);
321         }
322     }
323 
324     public boolean isPublishToLiveByDefault() {
325         return true;
326     }
327 
328     private static final String _NAMESPACE = "journal_content";
329 
330     private static final PortletDataHandlerBoolean _selectedArticles =
331         new PortletDataHandlerBoolean(
332             _NAMESPACE, "selected-articles", true, true);
333 
334     private static final PortletDataHandlerBoolean _images =
335         new PortletDataHandlerBoolean(_NAMESPACE, "images");
336 
337     private static final PortletDataHandlerBoolean _comments =
338         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
339 
340     private static final PortletDataHandlerBoolean _ratings =
341         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
342 
343     private static final PortletDataHandlerBoolean _tags =
344         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
345 
346     private static Log _log =
347         LogFactory.getLog(JournalContentPortletDataHandlerImpl.class);
348 
349 }