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.DataLevel;
018    import com.liferay.portal.kernel.lar.ManifestSummary;
019    import com.liferay.portal.kernel.lar.PortletDataContext;
020    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
021    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
022    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
023    import com.liferay.portal.kernel.lar.StagedModelType;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.util.ArrayUtil;
027    import com.liferay.portal.kernel.util.GetterUtil;
028    import com.liferay.portal.kernel.util.MapUtil;
029    import com.liferay.portal.kernel.util.StringPool;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.kernel.workflow.WorkflowConstants;
032    import com.liferay.portal.kernel.xml.Element;
033    import com.liferay.portal.model.Layout;
034    import com.liferay.portal.service.LayoutLocalServiceUtil;
035    import com.liferay.portal.util.PortalUtil;
036    import com.liferay.portal.util.PropsValues;
037    import com.liferay.portlet.documentlibrary.lar.DLPortletDataHandler;
038    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
039    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
040    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
041    import com.liferay.portlet.journal.NoSuchArticleException;
042    import com.liferay.portlet.journal.model.JournalArticle;
043    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
044    import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
045    import com.liferay.portlet.journal.service.permission.JournalPermission;
046    
047    import java.util.List;
048    import java.util.Map;
049    
050    import javax.portlet.PortletPreferences;
051    
052    /**
053     * <p>
054     * Provides the Journal Content portlet export and import functionality, which
055     * is to clone the article, structure, and template referenced in the Journal
056     * Content portlet if the article is associated with the layout's group. Upon
057     * import, a new instance of the corresponding article, structure, and template
058     * will be created or updated. The author of the newly created objects are
059     * determined by the JournalCreationStrategy class defined in
060     * <i>portal.properties</i>.
061     * </p>
062     *
063     * <p>
064     * This <code>PortletDataHandler</code> differs from from
065     * <code>JournalPortletDataHandlerImpl</code> in that it only exports articles
066     * referenced in Journal Content portlets. Articles not displayed in Journal
067     * Content portlets will not be exported unless
068     * <code>JournalPortletDataHandlerImpl</code> is activated.
069     * </p>
070     *
071     * @author Joel Kozikowski
072     * @author Raymond Aug??
073     * @author Bruno Farache
074     * @author Daniel Kocsis
075     * @see    com.liferay.portal.kernel.lar.PortletDataHandler
076     * @see    com.liferay.portlet.journal.lar.JournalCreationStrategy
077     * @see    com.liferay.portlet.journal.lar.JournalPortletDataHandler
078     */
079    public class JournalContentPortletDataHandler
080            extends JournalPortletDataHandler {
081    
082            public JournalContentPortletDataHandler() {
083                    setDataLevel(DataLevel.PORTLET_INSTANCE);
084                    setDataPortletPreferences("groupId", "articleId", "templateId");
085                    setExportControls(
086                            new PortletDataHandlerBoolean(
087                                    NAMESPACE, "selected-web-content", true, true,
088                                    new PortletDataHandlerControl[] {
089                                            new PortletDataHandlerBoolean(
090                                                    NAMESPACE, "referenced-content")
091                                    },
092                                    JournalArticle.class.getName()));
093    
094                    DLPortletDataHandler dlPortletDataHandler = new DLPortletDataHandler();
095    
096                    setExportMetadataControls(
097                            ArrayUtil.append(
098                                    getExportMetadataControls(),
099                                    dlPortletDataHandler.getExportMetadataControls()));
100    
101                    setImportControls(getExportControls()[0]);
102                    setPublishToLiveByDefault(
103                            PropsValues.JOURNAL_CONTENT_PUBLISH_TO_LIVE_BY_DEFAULT);
104            }
105    
106            @Override
107            protected PortletPreferences doDeleteData(
108                            PortletDataContext portletDataContext, String portletId,
109                            PortletPreferences portletPreferences)
110                    throws Exception {
111    
112                    if (portletPreferences == null) {
113                            return portletPreferences;
114                    }
115    
116                    portletPreferences.setValue("groupId", StringPool.BLANK);
117                    portletPreferences.setValue("articleId", StringPool.BLANK);
118    
119                    return portletPreferences;
120            }
121    
122            @Override
123            protected String doExportData(
124                            PortletDataContext portletDataContext, String portletId,
125                            PortletPreferences portletPreferences)
126                    throws Exception {
127    
128                    portletDataContext.addPermissions(
129                            JournalPermission.RESOURCE_NAME,
130                            portletDataContext.getScopeGroupId());
131    
132                    String articleId = portletPreferences.getValue("articleId", null);
133    
134                    if (articleId == null) {
135                            if (_log.isDebugEnabled()) {
136                                    _log.debug(
137                                            "No article id found in preferences of portlet " +
138                                                    portletId);
139                            }
140    
141                            return StringPool.BLANK;
142                    }
143    
144                    long articleGroupId = GetterUtil.getLong(
145                            portletPreferences.getValue("groupId", StringPool.BLANK));
146    
147                    if (articleGroupId <= 0) {
148                            if (_log.isWarnEnabled()) {
149                                    _log.warn(
150                                            "No group id found in preferences of portlet " + portletId);
151                            }
152    
153                            return StringPool.BLANK;
154                    }
155    
156                    long previousScopeGroupId = portletDataContext.getScopeGroupId();
157    
158                    if (articleGroupId != portletDataContext.getScopeGroupId()) {
159                            portletDataContext.setScopeGroupId(articleGroupId);
160                    }
161    
162                    JournalArticle article = null;
163    
164                    try {
165                            article = JournalArticleLocalServiceUtil.getLatestArticle(
166                                    articleGroupId, articleId, WorkflowConstants.STATUS_APPROVED);
167                    }
168                    catch (NoSuchArticleException nsae) {
169                    }
170    
171                    if (article == null) {
172                            try {
173                                    article = JournalArticleLocalServiceUtil.getLatestArticle(
174                                            articleGroupId, articleId,
175                                            WorkflowConstants.STATUS_EXPIRED);
176                            }
177                            catch (NoSuchArticleException nsae) {
178                            }
179                    }
180    
181                    Element rootElement = addExportDataRootElement(portletDataContext);
182    
183                    if (article == null) {
184                            portletDataContext.setScopeGroupId(previousScopeGroupId);
185    
186                            return getExportDataRootElementString(rootElement);
187                    }
188    
189                    StagedModelDataHandlerUtil.exportStagedModel(
190                            portletDataContext, article);
191    
192                    String defaultTemplateId = article.getTemplateId();
193                    String preferenceTemplateId = portletPreferences.getValue(
194                            "ddmTemplateKey", null);
195    
196                    if (Validator.isNotNull(defaultTemplateId) &&
197                            Validator.isNotNull(preferenceTemplateId) &&
198                            !defaultTemplateId.equals(preferenceTemplateId)) {
199    
200                            DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.getTemplate(
201                                    article.getGroupId(),
202                                    PortalUtil.getClassNameId(DDMStructure.class),
203                                    preferenceTemplateId, true);
204    
205                            StagedModelDataHandlerUtil.exportStagedModel(
206                                    portletDataContext, ddmTemplate);
207    
208                            Element articleElement = portletDataContext.getExportDataElement(
209                                    article);
210    
211                            portletDataContext.addReferenceElement(
212                                    article, articleElement, ddmTemplate,
213                                    PortletDataContext.REFERENCE_TYPE_STRONG, false);
214                    }
215    
216                    portletDataContext.setScopeGroupId(previousScopeGroupId);
217    
218                    return getExportDataRootElementString(rootElement);
219            }
220    
221            @Override
222            protected PortletPreferences doImportData(
223                            PortletDataContext portletDataContext, String portletId,
224                            PortletPreferences portletPreferences, String data)
225                    throws Exception {
226    
227                    portletDataContext.importPermissions(
228                            JournalPermission.RESOURCE_NAME,
229                            portletDataContext.getSourceGroupId(),
230                            portletDataContext.getScopeGroupId());
231    
232                    long previousScopeGroupId = portletDataContext.getScopeGroupId();
233    
234                    long importGroupId = GetterUtil.getLong(
235                            portletPreferences.getValue("groupId", null));
236    
237                    if (importGroupId == portletDataContext.getSourceGroupId()) {
238                            portletDataContext.setScopeGroupId(portletDataContext.getGroupId());
239                    }
240    
241                    Element ddmStructuresElement =
242                            portletDataContext.getImportDataGroupElement(DDMStructure.class);
243    
244                    List<Element> ddmStructureElements = ddmStructuresElement.elements();
245    
246                    for (Element ddmStructureElement : ddmStructureElements) {
247                            StagedModelDataHandlerUtil.importStagedModel(
248                                    portletDataContext, ddmStructureElement);
249                    }
250    
251                    Element ddmTemplatesElement =
252                            portletDataContext.getImportDataGroupElement(DDMTemplate.class);
253    
254                    List<Element> ddmTemplateElements = ddmTemplatesElement.elements();
255    
256                    for (Element ddmTemplateElement : ddmTemplateElements) {
257                            StagedModelDataHandlerUtil.importStagedModel(
258                                    portletDataContext, ddmTemplateElement);
259                    }
260    
261                    Element articlesElement = portletDataContext.getImportDataGroupElement(
262                            JournalArticle.class);
263    
264                    List<Element> articleElements = articlesElement.elements();
265    
266                    if (!articleElements.isEmpty()) {
267                            StagedModelDataHandlerUtil.importStagedModel(
268                                    portletDataContext, articleElements.get(0));
269                    }
270    
271                    String articleId = portletPreferences.getValue("articleId", null);
272    
273                    if (Validator.isNotNull(articleId)) {
274                            Map<String, String> articleIds =
275                                    (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
276                                            JournalArticle.class + ".articleId");
277    
278                            articleId = MapUtil.getString(articleIds, articleId, articleId);
279    
280                            portletPreferences.setValue("articleId", articleId);
281    
282                            String importedArticleGroupId = String.valueOf(
283                                    portletDataContext.getScopeGroupId());
284    
285                            portletPreferences.setValue("groupId", importedArticleGroupId);
286    
287                            Layout layout = LayoutLocalServiceUtil.getLayout(
288                                    portletDataContext.getPlid());
289    
290                            JournalContentSearchLocalServiceUtil.updateContentSearch(
291                                    portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
292                                    layout.getLayoutId(), portletId, articleId, true);
293                    }
294                    else {
295                            portletPreferences.setValue("groupId", StringPool.BLANK);
296                            portletPreferences.setValue("articleId", StringPool.BLANK);
297                    }
298    
299                    String ddmTemplateKey = portletPreferences.getValue(
300                            "ddmTemplateKey", null);
301    
302                    if (Validator.isNotNull(ddmTemplateKey)) {
303                            Map<String, String> ddmTemplateKeys =
304                                    (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
305                                            DDMTemplate.class + ".ddmTemplateKey");
306    
307                            ddmTemplateKey = MapUtil.getString(
308                                    ddmTemplateKeys, ddmTemplateKey, ddmTemplateKey);
309    
310                            portletPreferences.setValue("ddmTemplateKey", ddmTemplateKey);
311                    }
312                    else {
313                            portletPreferences.setValue("ddmTemplateKey", StringPool.BLANK);
314                    }
315    
316                    portletDataContext.setScopeGroupId(previousScopeGroupId);
317    
318                    return portletPreferences;
319            }
320    
321            @Override
322            protected void doPrepareManifestSummary(
323                            PortletDataContext portletDataContext,
324                            PortletPreferences portletPreferences)
325                    throws Exception {
326    
327                    ManifestSummary manifestSummary =
328                            portletDataContext.getManifestSummary();
329    
330                    if ((portletPreferences == null) ||
331                            (manifestSummary.getModelAdditionCount(
332                                    JournalArticle.class) > -1)) {
333    
334                            return;
335                    }
336    
337                    String articleId = portletPreferences.getValue(
338                            "articleId", StringPool.BLANK);
339    
340                    if (Validator.isNotNull(articleId)) {
341                            manifestSummary.addModelAdditionCount(
342                                    new StagedModelType(JournalArticle.class), 1);
343                    }
344            }
345    
346            private static Log _log = LogFactoryUtil.getLog(
347                    JournalContentPortletDataHandler.class);
348    
349    }