001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journal.lar;
016    
017    import com.liferay.portal.kernel.lar.PortletDataContext;
018    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
019    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.ArrayUtil;
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.Element;
029    import com.liferay.portal.model.Layout;
030    import com.liferay.portal.service.LayoutLocalServiceUtil;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portlet.documentlibrary.lar.DLPortletDataHandler;
033    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
034    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
035    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
036    import com.liferay.portlet.journal.NoSuchArticleException;
037    import com.liferay.portlet.journal.model.JournalArticle;
038    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
039    import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
040    
041    import java.util.List;
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.JournalPortletDataHandler
071     */
072    public class JournalContentPortletDataHandler
073            extends JournalPortletDataHandler {
074    
075            public JournalContentPortletDataHandler() {
076                    setAlwaysStaged(true);
077                    setDataPortletPreferences("groupId", "articleId", "templateId");
078                    setExportControls(
079                            new PortletDataHandlerBoolean(
080                                    NAMESPACE, "selected-web-content", true, true),
081                                    new PortletDataHandlerBoolean(NAMESPACE, "embedded-assets"));
082    
083                    DLPortletDataHandler dlPortletDataHandler = new DLPortletDataHandler();
084    
085                    setExportMetadataControls(
086                            ArrayUtil.append(
087                                    getExportMetadataControls(),
088                                    dlPortletDataHandler.getExportMetadataControls()));
089    
090                    setImportControls(getExportControls()[0]);
091                    setPublishToLiveByDefault(true);
092            }
093    
094            @Override
095            protected PortletPreferences doDeleteData(
096                            PortletDataContext portletDataContext, String portletId,
097                            PortletPreferences portletPreferences)
098                    throws Exception {
099    
100                    if (portletPreferences == null) {
101                            return portletPreferences;
102                    }
103    
104                    portletPreferences.setValue("groupId", StringPool.BLANK);
105                    portletPreferences.setValue("articleId", StringPool.BLANK);
106    
107                    return portletPreferences;
108            }
109    
110            @Override
111            protected String doExportData(
112                            PortletDataContext portletDataContext, String portletId,
113                            PortletPreferences portletPreferences)
114                    throws Exception {
115    
116                    portletDataContext.addPermissions(
117                            "com.liferay.portlet.journal",
118                            portletDataContext.getScopeGroupId());
119    
120                    String articleId = portletPreferences.getValue("articleId", null);
121    
122                    if (articleId == null) {
123                            if (_log.isDebugEnabled()) {
124                                    _log.debug(
125                                            "No article id found in preferences of portlet " +
126                                                    portletId);
127                            }
128    
129                            return StringPool.BLANK;
130                    }
131    
132                    long articleGroupId = GetterUtil.getLong(
133                            portletPreferences.getValue("groupId", StringPool.BLANK));
134    
135                    if (articleGroupId <= 0) {
136                            if (_log.isWarnEnabled()) {
137                                    _log.warn(
138                                            "No group id found in preferences of portlet " + portletId);
139                            }
140    
141                            return StringPool.BLANK;
142                    }
143    
144                    long previousScopeGroupId = portletDataContext.getScopeGroupId();
145    
146                    if (articleGroupId != portletDataContext.getScopeGroupId()) {
147                            portletDataContext.setScopeGroupId(articleGroupId);
148                    }
149                    else if (articleGroupId ==
150                                            portletDataContext.getSourceCompanyGroupId()) {
151    
152                            portletDataContext.setScopeGroupId(
153                                    portletDataContext.getCompanyGroupId());
154                    }
155    
156                    JournalArticle article = null;
157    
158                    try {
159                            article = JournalArticleLocalServiceUtil.getLatestArticle(
160                                    articleGroupId, articleId, WorkflowConstants.STATUS_APPROVED);
161                    }
162                    catch (NoSuchArticleException nsae) {
163                    }
164    
165                    if (article == null) {
166                            try {
167                                    article = JournalArticleLocalServiceUtil.getLatestArticle(
168                                            articleGroupId, articleId,
169                                            WorkflowConstants.STATUS_EXPIRED);
170                            }
171                            catch (NoSuchArticleException nsae) {
172                            }
173                    }
174    
175                    Element rootElement = addExportDataRootElement(portletDataContext);
176    
177                    if (article == null) {
178                            portletDataContext.setScopeGroupId(previousScopeGroupId);
179    
180                            return rootElement.formattedString();
181                    }
182    
183                    String path = JournalPortletDataHandler.getArticlePath(
184                            portletDataContext, article);
185    
186                    Element articleElement = rootElement.addElement("article");
187    
188                    articleElement.addAttribute("path", path);
189    
190                    Element dlFileEntryTypesElement = rootElement.addElement(
191                            "dl-file-entry-types");
192                    Element dlFoldersElement = rootElement.addElement("dl-folders");
193                    Element dlFilesElement = rootElement.addElement("dl-file-entries");
194                    Element dlFileRanksElement = rootElement.addElement("dl-file-ranks");
195                    Element dlRepositoriesElement = rootElement.addElement(
196                            "dl-repositories");
197                    Element dlRepositoryEntriesElement = rootElement.addElement(
198                            "dl-repository-entries");
199    
200                    JournalPortletDataHandler.exportArticle(
201                            portletDataContext, rootElement, rootElement, rootElement,
202                            dlFileEntryTypesElement, dlFoldersElement, dlFilesElement,
203                            dlFileRanksElement, dlRepositoriesElement,
204                            dlRepositoryEntriesElement, article, false);
205    
206                    String defaultTemplateId = article.getTemplateId();
207                    String preferenceTemplateId = portletPreferences.getValue(
208                            "templateId", null);
209    
210                    if (Validator.isNotNull(defaultTemplateId) &&
211                            Validator.isNotNull(preferenceTemplateId) &&
212                            !defaultTemplateId.equals(preferenceTemplateId)) {
213    
214                            DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.getTemplate(
215                                    article.getGroupId(),
216                                    PortalUtil.getClassNameId(DDMStructure.class),
217                                    preferenceTemplateId, true);
218    
219                            StagedModelDataHandlerUtil.exportStagedModel(
220                                    portletDataContext, ddmTemplate);
221                    }
222    
223                    portletDataContext.setScopeGroupId(previousScopeGroupId);
224    
225                    return getExportDataRootElementString(rootElement);
226            }
227    
228            @Override
229            protected PortletPreferences doImportData(
230                            PortletDataContext portletDataContext, String portletId,
231                            PortletPreferences portletPreferences, String data)
232                    throws Exception {
233    
234                    portletDataContext.importPermissions(
235                            "com.liferay.portlet.journal",
236                            portletDataContext.getSourceGroupId(),
237                            portletDataContext.getScopeGroupId());
238    
239                    long previousScopeGroupId = portletDataContext.getScopeGroupId();
240    
241                    long importGroupId = GetterUtil.getLong(
242                            portletPreferences.getValue("groupId", null));
243    
244                    if (importGroupId == portletDataContext.getSourceGroupId()) {
245                            portletDataContext.setScopeGroupId(portletDataContext.getGroupId());
246                    }
247    
248                    Element rootElement = portletDataContext.getImportDataRootElement();
249    
250                    JournalPortletDataHandler.importReferencedData(
251                            portletDataContext, rootElement);
252    
253                    Element ddmStructuresElement =
254                            portletDataContext.getImportDataGroupElement(DDMStructure.class);
255    
256                    List<Element> ddmStructureElements = ddmStructuresElement.elements();
257    
258                    for (Element ddmStructureElement : ddmStructureElements) {
259                            StagedModelDataHandlerUtil.importStagedModel(
260                                    portletDataContext, ddmStructureElement);
261                    }
262    
263                    Element ddmTemplatesElement =
264                            portletDataContext.getImportDataGroupElement(DDMTemplate.class);
265    
266                    List<Element> ddmTemplateElements = ddmTemplatesElement.elements();
267    
268                    for (Element ddmTemplateElement : ddmTemplateElements) {
269                            StagedModelDataHandlerUtil.importStagedModel(
270                                    portletDataContext, ddmTemplateElement);
271                    }
272    
273                    Element articleElement = rootElement.element("article");
274    
275                    if (articleElement != null) {
276                            JournalPortletDataHandler.importArticle(
277                                    portletDataContext, articleElement);
278                    }
279    
280                    String articleId = portletPreferences.getValue("articleId", null);
281    
282                    if (Validator.isNotNull(articleId) && (articleElement != null)) {
283                            Map<String, String> articleIds =
284                                    (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
285                                            JournalArticle.class + ".articleId");
286    
287                            articleId = MapUtil.getString(articleIds, articleId, articleId);
288    
289                            portletPreferences.setValue("articleId", articleId);
290    
291                            String importedArticleGroupId = String.valueOf(
292                                    portletDataContext.getScopeGroupId());
293    
294                            portletPreferences.setValue("groupId", importedArticleGroupId);
295    
296                            Layout layout = LayoutLocalServiceUtil.getLayout(
297                                    portletDataContext.getPlid());
298    
299                            JournalContentSearchLocalServiceUtil.updateContentSearch(
300                                    portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
301                                    layout.getLayoutId(), portletId, articleId, true);
302                    }
303                    else {
304                            portletPreferences.setValue("groupId", StringPool.BLANK);
305                            portletPreferences.setValue("articleId", StringPool.BLANK);
306                    }
307    
308                    String ddmTemplateKey = portletPreferences.getValue(
309                            "ddmTemplateKey", null);
310    
311                    if (Validator.isNotNull(ddmTemplateKey)) {
312                            Map<String, String> ddmTemplateKeys =
313                                    (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
314                                            DDMTemplate.class + ".ddmTemplateKey");
315    
316                            ddmTemplateKey = MapUtil.getString(
317                                    ddmTemplateKeys, ddmTemplateKey, ddmTemplateKey);
318    
319                            portletPreferences.setValue("ddmTemplateKey", ddmTemplateKey);
320                    }
321                    else {
322                            portletPreferences.setValue("ddmTemplateKey", StringPool.BLANK);
323                    }
324    
325                    portletDataContext.setScopeGroupId(previousScopeGroupId);
326    
327                    return portletPreferences;
328            }
329    
330            private static Log _log = LogFactoryUtil.getLog(
331                    JournalContentPortletDataHandler.class);
332    
333    }