001    /**
002     * Copyright (c) 2000-2010 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.Group;
032    import com.liferay.portal.model.Layout;
033    import com.liferay.portal.service.GroupLocalServiceUtil;
034    import com.liferay.portal.service.LayoutLocalServiceUtil;
035    import com.liferay.portlet.documentlibrary.lar.DLPortletDataHandlerImpl;
036    import com.liferay.portlet.imagegallery.lar.IGPortletDataHandlerImpl;
037    import com.liferay.portlet.journal.NoSuchArticleException;
038    import com.liferay.portlet.journal.NoSuchStructureException;
039    import com.liferay.portlet.journal.NoSuchTemplateException;
040    import com.liferay.portlet.journal.model.JournalArticle;
041    import com.liferay.portlet.journal.model.JournalStructure;
042    import com.liferay.portlet.journal.model.JournalTemplate;
043    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
044    import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
045    import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
046    import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
047    
048    import java.util.Collections;
049    import java.util.List;
050    import java.util.Map;
051    
052    import javax.portlet.PortletPreferences;
053    
054    /**
055     * <p>
056     * Provides the Journal Content portlet export and import functionality, which
057     * is to clone the article, structure, and template referenced in the Journal
058     * Content portlet if the article is associated with the layout's group. Upon
059     * import, a new instance of the corresponding article, structure, and template
060     * will be created or updated. The author of the newly created objects are
061     * determined by the JournalCreationStrategy class defined in
062     * <i>portal.properties</i>.
063     * </p>
064     *
065     * <p>
066     * This <code>PortletDataHandler</code> differs from from
067     * <code>JournalPortletDataHandlerImpl</code> in that it only exports articles
068     * referenced in Journal Content portlets. Articles not displayed in Journal
069     * Content portlets will not be exported unless
070     * <code>JournalPortletDataHandlerImpl</code> is activated.
071     * </p>
072     *
073     * @author Joel Kozikowski
074     * @author Raymond Augé
075     * @author Bruno Farache
076     * @see    com.liferay.portal.kernel.lar.PortletDataHandler
077     * @see    com.liferay.portlet.journal.lar.JournalCreationStrategy
078     * @see    com.liferay.portlet.journal.lar.JournalPortletDataHandlerImpl
079     */
080    public class JournalContentPortletDataHandlerImpl
081            extends BasePortletDataHandler {
082    
083            public PortletDataHandlerControl[] getExportControls() {
084                    return new PortletDataHandlerControl[] {
085                            _selectedArticles, _embeddedAssets, _images, _comments, _ratings,
086                            _tags
087                    };
088            }
089    
090            public PortletDataHandlerControl[] getImportControls() {
091                    return new PortletDataHandlerControl[] {
092                            _selectedArticles, _images, _comments, _ratings, _tags
093                    };
094            }
095    
096            public boolean isPublishToLiveByDefault() {
097                    return     _PUBLISH_TO_LIVE_BY_DEFAULT;
098            }
099    
100            protected PortletPreferences doDeleteData(
101                            PortletDataContext context, String portletId,
102                            PortletPreferences preferences)
103                    throws Exception {
104    
105                    preferences.setValue("group-id", StringPool.BLANK);
106                    preferences.setValue("article-id", StringPool.BLANK);
107    
108                    return preferences;
109            }
110    
111            protected String doExportData(
112                            PortletDataContext context, String portletId,
113                            PortletPreferences preferences)
114                    throws Exception {
115    
116                    context.addPermissions(
117                            "com.liferay.portlet.journal", context.getScopeGroupId());
118    
119                    String articleId = preferences.getValue("article-id", null);
120    
121                    if (articleId == null) {
122                            if (_log.isWarnEnabled()) {
123                                    _log.warn(
124                                            "No article id found in preferences of portlet " +
125                                                    portletId);
126                            }
127    
128                            return StringPool.BLANK;
129                    }
130    
131                    long articleGroupId = GetterUtil.getLong(
132                            preferences.getValue("group-id", StringPool.BLANK));
133    
134                    if (articleGroupId <= 0) {
135                            if (_log.isWarnEnabled()) {
136                                    _log.warn(
137                                            "No group id found in preferences of portlet " + portletId);
138                            }
139    
140                            return StringPool.BLANK;
141                    }
142    
143                    JournalArticle article = null;
144    
145                    try {
146                            article = JournalArticleLocalServiceUtil.getLatestArticle(
147                                    articleGroupId, articleId, WorkflowConstants.STATUS_APPROVED);
148                    }
149                    catch (NoSuchArticleException nsae) {
150                            if (_log.isWarnEnabled()) {
151                                    _log.warn(
152                                            "No approved article found with group id " +
153                                                    articleGroupId + " and article id " + articleId);
154                            }
155                    }
156    
157                    if (article == null) {
158                            return StringPool.BLANK;
159                    }
160    
161                    Document document = SAXReaderUtil.createDocument();
162    
163                    Element rootElement = document.addElement("journal-content-data");
164    
165                    String path = JournalPortletDataHandlerImpl.getArticlePath(
166                            context, article);
167    
168                    Element articleElement = rootElement.addElement("article");
169    
170                    articleElement.addAttribute("path", path);
171    
172                    Element dlFoldersElement = rootElement.addElement("dl-folders");
173                    Element dlFilesElement = rootElement.addElement("dl-file-entries");
174                    Element dlFileRanksElement = rootElement.addElement("dl-file-ranks");
175                    Element igFoldersElement = rootElement.addElement("ig-folders");
176                    Element igImagesElement = rootElement.addElement("ig-images");
177    
178                    JournalPortletDataHandlerImpl.exportArticle(
179                            context, rootElement, rootElement, rootElement, dlFoldersElement,
180                            dlFilesElement, dlFileRanksElement, igFoldersElement,
181                            igImagesElement, article, false);
182    
183                    Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
184                            article.getCompanyId());
185    
186                    String structureId = article.getStructureId();
187    
188                    if (Validator.isNotNull(structureId)) {
189                            JournalStructure structure = null;
190    
191                            try {
192                                    structure = JournalStructureLocalServiceUtil.getStructure(
193                                            article.getGroupId(), structureId);
194                            }
195                            catch (NoSuchStructureException nsse) {
196                                    structure = JournalStructureLocalServiceUtil.getStructure(
197                                            companyGroup.getGroupId(), structureId);
198                            }
199    
200                            JournalPortletDataHandlerImpl.exportStructure(
201                                    context, rootElement, structure);
202                    }
203    
204                    String templateId = article.getTemplateId();
205    
206                    if (Validator.isNotNull(templateId)) {
207                            JournalTemplate template = null;
208    
209                            try {
210                                    template = JournalTemplateLocalServiceUtil.getTemplate(
211                                            context.getScopeGroupId(), templateId);
212                            }
213                            catch (NoSuchTemplateException nste) {
214                                    template = JournalTemplateLocalServiceUtil.getTemplate(
215                                            companyGroup.getGroupId(), templateId);
216                            }
217    
218                            JournalPortletDataHandlerImpl.exportTemplate(
219                                    context, rootElement, dlFoldersElement, dlFilesElement,
220                                    dlFileRanksElement, igFoldersElement, igImagesElement,
221                                    template, false);
222                    }
223    
224                    return document.formattedString();
225            }
226    
227            protected PortletPreferences doImportData(
228                            PortletDataContext context, String portletId,
229                            PortletPreferences preferences, String data)
230                    throws Exception {
231    
232                    context.importPermissions(
233                            "com.liferay.portlet.journal", context.getSourceGroupId(),
234                            context.getScopeGroupId());
235    
236                    if (Validator.isNull(data)) {
237                            return null;
238                    }
239    
240                    Document document = SAXReaderUtil.read(data);
241    
242                    Element rootElement = document.getRootElement();
243    
244                    Element dlFoldersElement = rootElement.element("dl-folders");
245    
246                    List<Element> dlFolderElements = Collections.EMPTY_LIST;
247    
248                    if (dlFoldersElement != null) {
249                            dlFolderElements = dlFoldersElement.elements("folder");
250                    }
251    
252                    for (Element folderElement : dlFolderElements) {
253                            DLPortletDataHandlerImpl.importFolder(context, folderElement);
254                    }
255    
256                    Element dlFileEntriesElement = rootElement.element("dl-file-entries");
257    
258                    List<Element> dlFileEntryElements = Collections.EMPTY_LIST;
259    
260                    if (dlFileEntriesElement != null) {
261                            dlFileEntryElements = dlFileEntriesElement.elements("file-entry");
262                    }
263    
264                    for (Element fileEntryElement : dlFileEntryElements) {
265                            DLPortletDataHandlerImpl.importFileEntry(context, fileEntryElement);
266                    }
267    
268                    Element dlFileRanksElement = rootElement.element("dl-file-ranks");
269    
270                    List<Element> dlFileRankElements = Collections.EMPTY_LIST;
271    
272                    if (dlFileRanksElement != null) {
273                            dlFileRankElements = dlFileRanksElement.elements("file-rank");
274                    }
275    
276                    for (Element fileRankElement : dlFileRankElements) {
277                            DLPortletDataHandlerImpl.importFileRank(context, fileRankElement);
278                    }
279    
280                    Element igFoldersElement = rootElement.element("ig-folders");
281    
282                    List<Element> igFolderElements = Collections.EMPTY_LIST;
283    
284                    if (igFoldersElement != null) {
285                            igFolderElements = igFoldersElement.elements("folder");
286                    }
287    
288                    for (Element folderElement : igFolderElements) {
289                            IGPortletDataHandlerImpl.importFolder(context, folderElement);
290                    }
291    
292                    Element igImagesElement = rootElement.element("ig-images");
293    
294                    List<Element> igImageElements = Collections.EMPTY_LIST;
295    
296                    if (igImagesElement != null) {
297                            igImageElements = igImagesElement.elements("image");
298                    }
299    
300                    for (Element imageElement : igImageElements) {
301                            IGPortletDataHandlerImpl.importImage(context, imageElement);
302                    }
303    
304                    Element structureElement = rootElement.element("structure");
305    
306                    if (structureElement != null) {
307                            JournalPortletDataHandlerImpl.importStructure(
308                                    context, structureElement);
309                    }
310    
311                    Element templateElement = rootElement.element("template");
312    
313                    if (templateElement != null) {
314                            JournalPortletDataHandlerImpl.importTemplate(
315                                    context, templateElement);
316                    }
317    
318                    Element articleElement = rootElement.element("article");
319    
320                    if (articleElement != null) {
321                            JournalPortletDataHandlerImpl.importArticle(
322                                    context, articleElement);
323                    }
324    
325                    String articleId = preferences.getValue("article-id", null);
326    
327                    if (Validator.isNotNull(articleId)) {
328                            Map<String, String> articleIds =
329                                    (Map<String, String>)context.getNewPrimaryKeysMap(
330                                            JournalArticle.class);
331    
332                            articleId = MapUtil.getString(articleIds, articleId, articleId);
333    
334                            preferences.setValue(
335                                    "group-id", String.valueOf(context.getScopeGroupId()));
336                            preferences.setValue("article-id", articleId);
337    
338                            Layout layout = LayoutLocalServiceUtil.getLayout(
339                                    context.getPlid());
340    
341                            JournalContentSearchLocalServiceUtil.updateContentSearch(
342                                    context.getScopeGroupId(), layout.isPrivateLayout(),
343                                    layout.getLayoutId(), portletId, articleId, true);
344                    }
345    
346                    String templateId = preferences.getValue("template-id", null);
347    
348                    if (Validator.isNotNull(templateId)) {
349                            Map<String, String> templateIds =
350                                    (Map<String, String>)context.getNewPrimaryKeysMap(
351                                            JournalTemplate.class);
352    
353                            templateId = MapUtil.getString(templateIds, templateId, templateId);
354    
355                            preferences.setValue("template-id", templateId);
356                    }
357    
358                    return preferences;
359            }
360    
361            private static final String _NAMESPACE = "journal";
362    
363            private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
364    
365            private static PortletDataHandlerBoolean _comments =
366                    new PortletDataHandlerBoolean(_NAMESPACE, "comments");
367    
368            private static PortletDataHandlerBoolean _embeddedAssets =
369                    new PortletDataHandlerBoolean(_NAMESPACE, "embedded-assets");
370    
371            private static PortletDataHandlerBoolean _images =
372                    new PortletDataHandlerBoolean(_NAMESPACE, "images");
373    
374            private static Log _log = LogFactoryUtil.getLog(
375                    JournalContentPortletDataHandlerImpl.class);
376    
377            private static PortletDataHandlerBoolean _ratings =
378                    new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
379    
380            private static PortletDataHandlerBoolean _selectedArticles =
381                    new PortletDataHandlerBoolean(
382                            _NAMESPACE, "selected-web-content", true, true);
383    
384            private static PortletDataHandlerBoolean _tags =
385                    new PortletDataHandlerBoolean(_NAMESPACE, "tags");
386    
387    }