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