001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.journal.lar;
016    
017    import com.liferay.portal.kernel.lar.BasePortletDataHandler;
018    import com.liferay.portal.kernel.lar.PortletDataContext;
019    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
020    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.MapUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.kernel.workflow.WorkflowConstants;
028    import com.liferay.portal.kernel.xml.Document;
029    import com.liferay.portal.kernel.xml.Element;
030    import com.liferay.portal.kernel.xml.SAXReaderUtil;
031    import com.liferay.portal.model.Layout;
032    import com.liferay.portal.service.LayoutLocalServiceUtil;
033    import com.liferay.portlet.journal.NoSuchArticleException;
034    import com.liferay.portlet.journal.model.JournalArticle;
035    import com.liferay.portlet.journal.model.JournalTemplate;
036    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
037    import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
038    
039    import java.util.Map;
040    
041    import javax.portlet.PortletPreferences;
042    
043    /**
044     * <p>
045     * Provides the Journal Content portlet export and import functionality, which
046     * is to clone the article, structure, and template referenced in the Journal
047     * Content portlet if the article is associated with the layout's group. Upon
048     * import, a new instance of the corresponding article, structure, and template
049     * will be created or updated. The author of the newly created objects are
050     * determined by the JournalCreationStrategy class defined in
051     * <i>portal.properties</i>.
052     * </p>
053     *
054     * <p>
055     * This <code>PortletDataHandler</code> differs from from
056     * <code>JournalPortletDataHandlerImpl</code> in that it only exports articles
057     * referenced in Journal Content portlets. Articles not displayed in Journal
058     * Content portlets will not be exported unless
059     * <code>JournalPortletDataHandlerImpl</code> is activated.
060     * </p>
061     *
062     * @author Joel Kozikowski
063     * @author Raymond Augé
064     * @author Bruno Farache
065     * @see    com.liferay.portal.kernel.lar.PortletDataHandler
066     * @see    com.liferay.portlet.journal.lar.JournalCreationStrategy
067     * @see    com.liferay.portlet.journal.lar.JournalPortletDataHandlerImpl
068     */
069    public class JournalContentPortletDataHandlerImpl
070            extends BasePortletDataHandler {
071    
072            @Override
073            public PortletDataHandlerControl[] getExportControls() {
074                    return new PortletDataHandlerControl[] {
075                            _selectedArticles, _embeddedAssets, _images, _comments, _ratings,
076                            _tags
077                    };
078            }
079    
080            @Override
081            public PortletDataHandlerControl[] getImportControls() {
082                    return new PortletDataHandlerControl[] {
083                            _selectedArticles, _images, _comments, _ratings, _tags
084                    };
085            }
086    
087            @Override
088            public boolean isPublishToLiveByDefault() {
089                    return _PUBLISH_TO_LIVE_BY_DEFAULT;
090            }
091    
092            @Override
093            protected PortletPreferences doDeleteData(
094                            PortletDataContext portletDataContext, String portletId,
095                            PortletPreferences portletPreferences)
096                    throws Exception {
097    
098                    portletPreferences.setValue("groupId", StringPool.BLANK);
099                    portletPreferences.setValue("articleId", StringPool.BLANK);
100    
101                    return portletPreferences;
102            }
103    
104            @Override
105            protected String doExportData(
106                            PortletDataContext portletDataContext, String portletId,
107                            PortletPreferences portletPreferences)
108                    throws Exception {
109    
110                    portletDataContext.addPermissions(
111                            "com.liferay.portlet.journal",
112                            portletDataContext.getScopeGroupId());
113    
114                    String articleId = portletPreferences.getValue("articleId", null);
115    
116                    if (articleId == null) {
117                            if (_log.isWarnEnabled()) {
118                                    _log.warn(
119                                            "No article id found in preferences of portlet " +
120                                                    portletId);
121                            }
122    
123                            return StringPool.BLANK;
124                    }
125    
126                    long articleGroupId = GetterUtil.getLong(
127                            portletPreferences.getValue("groupId", StringPool.BLANK));
128    
129                    if (articleGroupId <= 0) {
130                            if (_log.isWarnEnabled()) {
131                                    _log.warn(
132                                            "No group id found in preferences of portlet " + portletId);
133                            }
134    
135                            return StringPool.BLANK;
136                    }
137    
138                    long previousScopeGroupId = portletDataContext.getScopeGroupId();
139    
140                    if (articleGroupId != portletDataContext.getScopeGroupId()) {
141                            portletDataContext.setScopeGroupId(articleGroupId);
142                    }
143    
144                    JournalArticle article = null;
145    
146                    try {
147                            article = JournalArticleLocalServiceUtil.getLatestArticle(
148                                    articleGroupId, articleId, WorkflowConstants.STATUS_APPROVED);
149                    }
150                    catch (NoSuchArticleException nsae) {
151                            if (_log.isWarnEnabled()) {
152                                    _log.warn(
153                                            "No approved article found with group id " +
154                                                    articleGroupId + " and article id " + articleId);
155                            }
156                    }
157    
158                    Document document = SAXReaderUtil.createDocument();
159    
160                    Element rootElement = document.addElement("journal-content-data");
161    
162                    if (article == null) {
163                            portletDataContext.setScopeGroupId(previousScopeGroupId);
164    
165                            return document.formattedString();
166                    }
167    
168                    String path = JournalPortletDataHandlerImpl.getArticlePath(
169                            portletDataContext, article);
170    
171                    Element articleElement = rootElement.addElement("article");
172    
173                    articleElement.addAttribute("path", path);
174    
175                    Element dlFileEntryTypesElement = rootElement.addElement(
176                            "dl-file-entry-types");
177                    Element dlFoldersElement = rootElement.addElement("dl-folders");
178                    Element dlFilesElement = rootElement.addElement("dl-file-entries");
179                    Element dlFileRanksElement = rootElement.addElement("dl-file-ranks");
180                    Element igFoldersElement = rootElement.addElement("ig-folders");
181                    Element igImagesElement = rootElement.addElement("ig-images");
182    
183                    JournalPortletDataHandlerImpl.exportArticle(
184                            portletDataContext, rootElement, rootElement, rootElement,
185                            dlFileEntryTypesElement, dlFoldersElement, dlFilesElement,
186                            dlFileRanksElement, igFoldersElement, igImagesElement, article,
187                            false);
188    
189                    portletDataContext.setScopeGroupId(previousScopeGroupId);
190    
191                    return document.formattedString();
192            }
193    
194            @Override
195            protected PortletPreferences doImportData(
196                            PortletDataContext portletDataContext, String portletId,
197                            PortletPreferences portletPreferences, String data)
198                    throws Exception {
199    
200                    portletDataContext.importPermissions(
201                            "com.liferay.portlet.journal",
202                            portletDataContext.getSourceGroupId(),
203                            portletDataContext.getScopeGroupId());
204    
205                    if (Validator.isNull(data)) {
206                            return null;
207                    }
208    
209                    long previousScopeGroupId = portletDataContext.getScopeGroupId();
210    
211                    long importGroupId = GetterUtil.getLong(
212                            portletPreferences.getValue("groupId", null));
213    
214                    if (importGroupId == portletDataContext.getSourceGroupId()) {
215                            portletDataContext.setScopeGroupId(portletDataContext.getGroupId());
216                    }
217    
218                    Document document = SAXReaderUtil.read(data);
219    
220                    Element rootElement = document.getRootElement();
221    
222                    JournalPortletDataHandlerImpl.importReferencedData(
223                            portletDataContext, rootElement);
224    
225                    Element structureElement = rootElement.element("structure");
226    
227                    if (structureElement != null) {
228                            JournalPortletDataHandlerImpl.importStructure(
229                                    portletDataContext, structureElement);
230                    }
231    
232                    Element templateElement = rootElement.element("template");
233    
234                    if (templateElement != null) {
235                            JournalPortletDataHandlerImpl.importTemplate(
236                                    portletDataContext, templateElement);
237                    }
238    
239                    Element articleElement = rootElement.element("article");
240    
241                    if (articleElement != null) {
242                            JournalPortletDataHandlerImpl.importArticle(
243                                    portletDataContext, articleElement);
244                    }
245    
246                    String articleId = portletPreferences.getValue("articleId", null);
247    
248                    if (Validator.isNotNull(articleId) && (articleElement != null)) {
249                            String importedArticleGroupId = articleElement.attributeValue(
250                                    "imported-article-group-id");
251    
252                            if (Validator.isNull(importedArticleGroupId)) {
253                                    importedArticleGroupId = String.valueOf(
254                                            portletDataContext.getScopeGroupId());
255                            }
256    
257                            portletPreferences.setValue("groupId", importedArticleGroupId);
258    
259                            Map<String, String> articleIds =
260                                    (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
261                                            JournalArticle.class + ".articleId");
262    
263                            articleId = MapUtil.getString(articleIds, articleId, articleId);
264    
265                            portletPreferences.setValue("articleId", articleId);
266    
267                            Layout layout = LayoutLocalServiceUtil.getLayout(
268                                    portletDataContext.getPlid());
269    
270                            JournalContentSearchLocalServiceUtil.updateContentSearch(
271                                    portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
272                                    layout.getLayoutId(), portletId, articleId, true);
273                    }
274                    else {
275                            portletPreferences.setValue("groupId", StringPool.BLANK);
276                            portletPreferences.setValue("articleId", StringPool.BLANK);
277                    }
278    
279                    String templateId = portletPreferences.getValue("templateId", null);
280    
281                    if (Validator.isNotNull(templateId)) {
282                            Map<String, String> templateIds =
283                                    (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
284                                            JournalTemplate.class + ".templateId");
285    
286                            templateId = MapUtil.getString(templateIds, templateId, templateId);
287    
288                            portletPreferences.setValue("templateId", templateId);
289                    }
290                    else {
291                            portletPreferences.setValue("templateId", StringPool.BLANK);
292                    }
293    
294                    portletDataContext.setScopeGroupId(previousScopeGroupId);
295    
296                    return portletPreferences;
297            }
298    
299            private static final String _NAMESPACE = "journal";
300    
301            private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
302    
303            private static PortletDataHandlerBoolean _comments =
304                    new PortletDataHandlerBoolean(_NAMESPACE, "comments");
305    
306            private static PortletDataHandlerBoolean _embeddedAssets =
307                    new PortletDataHandlerBoolean(_NAMESPACE, "embedded-assets");
308    
309            private static PortletDataHandlerBoolean _images =
310                    new PortletDataHandlerBoolean(_NAMESPACE, "images");
311    
312            private static Log _log = LogFactoryUtil.getLog(
313                    JournalContentPortletDataHandlerImpl.class);
314    
315            private static PortletDataHandlerBoolean _ratings =
316                    new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
317    
318            private static PortletDataHandlerBoolean _selectedArticles =
319                    new PortletDataHandlerBoolean(
320                            _NAMESPACE, "selected-web-content", true, true);
321    
322            private static PortletDataHandlerBoolean _tags =
323                    new PortletDataHandlerBoolean(_NAMESPACE, "tags");
324    
325    }