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.service.JournalArticleLocalServiceUtil;
39  import com.liferay.portlet.journal.service.persistence.JournalStructureUtil;
40  import com.liferay.portlet.journal.service.persistence.JournalTemplateUtil;
41  import com.liferay.util.MapUtil;
42  import com.liferay.util.xml.XMLFormatter;
43  
44  import java.util.Map;
45  
46  import javax.portlet.PortletPreferences;
47  
48  import org.apache.commons.logging.Log;
49  import org.apache.commons.logging.LogFactory;
50  
51  import org.dom4j.Document;
52  import org.dom4j.DocumentHelper;
53  import org.dom4j.Element;
54  
55  /**
56   * <a href="JournalContentPortletDataHandlerImpl.java.html"><b><i>View Source
57   * </i></b></a>
58   *
59   * <p>
60   * Provides the Journal Content portlet export and import functionality, which
61   * is to clone the article, structure, and template referenced in the
62   * Journal Content portlet if the article is associated with the layout's group.
63   * Upon import, a new instance of the corresponding article, structure, and
64   * template will be created or updated. The author of the newly created
65   * objects are determined by the JournalCreationStrategy class defined in
66   * <i>portal.properties</i>.
67   * </p>
68   *
69   * <p>
70   * This <code>PortletDataHandler</code> differs from from
71   * <code>JournalPortletDataHandlerImpl</code> in that it only exports articles
72   * referenced in Journal Content portlets. Articles not displayed in Journal
73   * Content portlets will not be exported unless
74   * <code>JournalPortletDataHandlerImpl</code> is activated.
75   * </p>
76   *
77   * @author Joel Kozikowski
78   * @author Raymond Augé
79   * @author Bruno Farache
80   *
81   * @see com.liferay.portal.kernel.lar.PortletDataHandler
82   * @see com.liferay.portlet.journal.lar.JournalCreationStrategy
83   * @see com.liferay.portlet.journal.lar.JournalPortletDataHandlerImpl
84   *
85   */
86  public class JournalContentPortletDataHandlerImpl
87      implements PortletDataHandler {
88  
89      public PortletPreferences deleteData(
90              PortletDataContext context, String portletId,
91              PortletPreferences prefs)
92          throws PortletDataException {
93  
94          try {
95              prefs.setValue("group-id", StringPool.BLANK);
96              prefs.setValue("article-id", StringPool.BLANK);
97  
98              return prefs;
99          }
100         catch (Exception e) {
101             throw new PortletDataException(e);
102         }
103     }
104 
105     public String exportData(
106             PortletDataContext context, String portletId,
107             PortletPreferences prefs)
108         throws PortletDataException {
109 
110         try {
111             String articleId = prefs.getValue("article-id", null);
112 
113             if (articleId == null) {
114                 if (_log.isWarnEnabled()) {
115                     _log.warn(
116                         "No article id found in preferences of portlet " +
117                             portletId);
118                 }
119 
120                 return StringPool.BLANK;
121             }
122 
123             long articleGroupId = GetterUtil.getLong(
124                 prefs.getValue("group-id", StringPool.BLANK));
125 
126             if (articleGroupId <= 0) {
127                 if (_log.isWarnEnabled()) {
128                     _log.warn(
129                         "No group id found in preferences of portlet " +
130                             portletId);
131                 }
132 
133                 return StringPool.BLANK;
134             }
135 
136             JournalArticle article = null;
137 
138             try {
139                 article = JournalArticleLocalServiceUtil.getLatestArticle(
140                     articleGroupId, articleId);
141             }
142             catch (NoSuchArticleException nsae) {
143                 if (_log.isWarnEnabled()) {
144                     _log.warn(nsae);
145                 }
146             }
147 
148             if (article == null) {
149                 return StringPool.BLANK;
150             }
151 
152             Document doc = DocumentHelper.createDocument();
153 
154             Element root = doc.addElement("journal-content-data");
155 
156             JournalPortletDataHandlerImpl.exportArticle(context, root, article);
157 
158             String structureId = article.getStructureId();
159 
160             if (Validator.isNotNull(structureId)) {
161                 JournalStructure structure = JournalStructureUtil.findByG_S(
162                     article.getGroupId(), structureId);
163 
164                 JournalPortletDataHandlerImpl.exportStructure(
165                     context, root, structure);
166             }
167 
168             String templateId = article.getTemplateId();
169 
170             if (Validator.isNotNull(templateId)) {
171                 JournalTemplate template = JournalTemplateUtil.findByG_T(
172                     article.getGroupId(), templateId);
173 
174                 JournalPortletDataHandlerImpl.exportTemplate(
175                     context, root, template);
176             }
177 
178             return XMLFormatter.toString(doc);
179         }
180         catch (Exception e) {
181             throw new PortletDataException(e);
182         }
183     }
184 
185     public PortletDataHandlerControl[] getExportControls() {
186         return new PortletDataHandlerControl[] {
187             _selectedArticles, _images, _comments, _ratings, _tags
188         };
189     }
190 
191     public PortletDataHandlerControl[] getImportControls() {
192         return new PortletDataHandlerControl[] {
193             _selectedArticles, _images, _comments, _ratings, _tags
194         };
195     }
196 
197     public PortletPreferences importData(
198             PortletDataContext context, String portletId,
199             PortletPreferences prefs, String data)
200         throws PortletDataException {
201 
202         try {
203             if (Validator.isNull(data)) {
204                 return null;
205             }
206 
207             Document doc = DocumentUtil.readDocumentFromXML(data);
208 
209             Element root = doc.getRootElement();
210 
211             Element structureEl = root.element("structure");
212 
213             Map<String, String> structureIds = context.getNewPrimaryKeysMap(
214                 JournalStructure.class);
215 
216             if (structureEl != null) {
217                 JournalPortletDataHandlerImpl.importStructure(
218                     context, structureIds, structureEl);
219             }
220 
221             Element templateEl = root.element("template");
222 
223             Map<String, String> templateIds = context.getNewPrimaryKeysMap(
224                 JournalTemplate.class);
225 
226             if (templateEl != null) {
227                 JournalPortletDataHandlerImpl.importTemplate(
228                     context, structureIds, templateIds, templateEl);
229             }
230 
231             Element articleEl = root.element("article");
232 
233             Map<String, String> articleIds = context.getNewPrimaryKeysMap(
234                 JournalArticle.class);
235 
236             if (articleEl != null) {
237                 JournalPortletDataHandlerImpl.importArticle(
238                     context, structureIds, templateIds, articleIds, articleEl);
239             }
240 
241             String articleId = prefs.getValue("article-id", StringPool.BLANK);
242 
243             if (Validator.isNotNull(articleId)) {
244                 articleId = MapUtil.getString(articleIds, articleId, articleId);
245 
246                 prefs.setValue(
247                     "group-id", String.valueOf(context.getGroupId()));
248                 prefs.setValue("article-id", articleId);
249             }
250 
251             return prefs;
252         }
253         catch (Exception e) {
254             throw new PortletDataException(e);
255         }
256     }
257 
258     public boolean isPublishToLiveByDefault() {
259         return true;
260     }
261 
262     private static final String _NAMESPACE = "journal";
263 
264     private static final PortletDataHandlerBoolean _selectedArticles =
265         new PortletDataHandlerBoolean(
266             _NAMESPACE, "selected-articles", true, true);
267 
268     private static final PortletDataHandlerBoolean _images =
269         new PortletDataHandlerBoolean(_NAMESPACE, "images");
270 
271     private static final PortletDataHandlerBoolean _comments =
272         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
273 
274     private static final PortletDataHandlerBoolean _ratings =
275         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
276 
277     private static final PortletDataHandlerBoolean _tags =
278         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
279 
280     private static Log _log =
281         LogFactory.getLog(JournalContentPortletDataHandlerImpl.class);
282 
283 }