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.addPortletPermissions(
129                            JournalPermission.RESOURCE_NAME);
130    
131                    String articleId = portletPreferences.getValue("articleId", null);
132    
133                    if (articleId == null) {
134                            if (_log.isDebugEnabled()) {
135                                    _log.debug(
136                                            "No article id found in preferences of portlet " +
137                                                    portletId);
138                            }
139    
140                            return StringPool.BLANK;
141                    }
142    
143                    long articleGroupId = GetterUtil.getLong(
144                            portletPreferences.getValue("groupId", StringPool.BLANK));
145    
146                    if (articleGroupId <= 0) {
147                            if (_log.isWarnEnabled()) {
148                                    _log.warn(
149                                            "No group id found in preferences of portlet " + portletId);
150                            }
151    
152                            return StringPool.BLANK;
153                    }
154    
155                    long previousScopeGroupId = portletDataContext.getScopeGroupId();
156    
157                    if (articleGroupId != portletDataContext.getScopeGroupId()) {
158                            portletDataContext.setScopeGroupId(articleGroupId);
159                    }
160    
161                    JournalArticle article = null;
162    
163                    try {
164                            article = JournalArticleLocalServiceUtil.getLatestArticle(
165                                    articleGroupId, articleId, WorkflowConstants.STATUS_APPROVED);
166                    }
167                    catch (NoSuchArticleException nsae) {
168                    }
169    
170                    if (article == null) {
171                            try {
172                                    article = JournalArticleLocalServiceUtil.getLatestArticle(
173                                            articleGroupId, articleId,
174                                            WorkflowConstants.STATUS_EXPIRED);
175                            }
176                            catch (NoSuchArticleException nsae) {
177                            }
178                    }
179    
180                    Element rootElement = addExportDataRootElement(portletDataContext);
181    
182                    if (article == null) {
183                            portletDataContext.setScopeGroupId(previousScopeGroupId);
184    
185                            return getExportDataRootElementString(rootElement);
186                    }
187    
188                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
189                            portletDataContext, portletId, article);
190    
191                    String defaultTemplateId = article.getTemplateId();
192                    String preferenceTemplateId = portletPreferences.getValue(
193                            "ddmTemplateKey", null);
194    
195                    if (Validator.isNotNull(defaultTemplateId) &&
196                            Validator.isNotNull(preferenceTemplateId) &&
197                            !defaultTemplateId.equals(preferenceTemplateId)) {
198    
199                            DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.getTemplate(
200                                    article.getGroupId(),
201                                    PortalUtil.getClassNameId(DDMStructure.class),
202                                    preferenceTemplateId, true);
203    
204                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
205                                    portletDataContext, article, ddmTemplate,
206                                    PortletDataContext.REFERENCE_TYPE_STRONG);
207                    }
208    
209                    portletDataContext.setScopeGroupId(previousScopeGroupId);
210    
211                    return getExportDataRootElementString(rootElement);
212            }
213    
214            @Override
215            protected PortletPreferences doImportData(
216                            PortletDataContext portletDataContext, String portletId,
217                            PortletPreferences portletPreferences, String data)
218                    throws Exception {
219    
220                    portletDataContext.importPortletPermissions(
221                            JournalPermission.RESOURCE_NAME);
222    
223                    long previousScopeGroupId = portletDataContext.getScopeGroupId();
224    
225                    long importGroupId = GetterUtil.getLong(
226                            portletPreferences.getValue("groupId", null));
227    
228                    if (importGroupId == portletDataContext.getSourceGroupId()) {
229                            portletDataContext.setScopeGroupId(portletDataContext.getGroupId());
230                    }
231    
232                    Element ddmStructuresElement =
233                            portletDataContext.getImportDataGroupElement(DDMStructure.class);
234    
235                    List<Element> ddmStructureElements = ddmStructuresElement.elements();
236    
237                    for (Element ddmStructureElement : ddmStructureElements) {
238                            StagedModelDataHandlerUtil.importReferenceStagedModel(
239                                    portletDataContext, ddmStructureElement);
240                    }
241    
242                    Element ddmTemplatesElement =
243                            portletDataContext.getImportDataGroupElement(DDMTemplate.class);
244    
245                    List<Element> ddmTemplateElements = ddmTemplatesElement.elements();
246    
247                    for (Element ddmTemplateElement : ddmTemplateElements) {
248                            StagedModelDataHandlerUtil.importReferenceStagedModel(
249                                    portletDataContext, ddmTemplateElement);
250                    }
251    
252                    Element articlesElement = portletDataContext.getImportDataGroupElement(
253                            JournalArticle.class);
254    
255                    List<Element> articleElements = articlesElement.elements();
256    
257                    if (!articleElements.isEmpty()) {
258                            StagedModelDataHandlerUtil.importReferenceStagedModel(
259                                    portletDataContext, articleElements.get(0));
260                    }
261    
262                    String articleId = portletPreferences.getValue("articleId", null);
263    
264                    if (Validator.isNotNull(articleId)) {
265                            Map<String, String> articleIds =
266                                    (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
267                                            JournalArticle.class + ".articleId");
268    
269                            articleId = MapUtil.getString(articleIds, articleId, articleId);
270    
271                            portletPreferences.setValue("articleId", articleId);
272    
273                            String importedArticleGroupId = String.valueOf(
274                                    portletDataContext.getScopeGroupId());
275    
276                            portletPreferences.setValue("groupId", importedArticleGroupId);
277    
278                            Layout layout = LayoutLocalServiceUtil.getLayout(
279                                    portletDataContext.getPlid());
280    
281                            JournalContentSearchLocalServiceUtil.updateContentSearch(
282                                    portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
283                                    layout.getLayoutId(), portletId, articleId, true);
284                    }
285                    else {
286                            portletPreferences.setValue("groupId", StringPool.BLANK);
287                            portletPreferences.setValue("articleId", StringPool.BLANK);
288                    }
289    
290                    String ddmTemplateKey = portletPreferences.getValue(
291                            "ddmTemplateKey", null);
292    
293                    if (Validator.isNotNull(ddmTemplateKey)) {
294                            Map<String, String> ddmTemplateKeys =
295                                    (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
296                                            DDMTemplate.class + ".ddmTemplateKey");
297    
298                            ddmTemplateKey = MapUtil.getString(
299                                    ddmTemplateKeys, ddmTemplateKey, ddmTemplateKey);
300    
301                            portletPreferences.setValue("ddmTemplateKey", ddmTemplateKey);
302                    }
303                    else {
304                            portletPreferences.setValue("ddmTemplateKey", StringPool.BLANK);
305                    }
306    
307                    portletDataContext.setScopeGroupId(previousScopeGroupId);
308    
309                    return portletPreferences;
310            }
311    
312            @Override
313            protected void doPrepareManifestSummary(
314                            PortletDataContext portletDataContext,
315                            PortletPreferences portletPreferences)
316                    throws Exception {
317    
318                    ManifestSummary manifestSummary =
319                            portletDataContext.getManifestSummary();
320    
321                    if ((portletPreferences == null) ||
322                            (manifestSummary.getModelAdditionCount(
323                                    JournalArticle.class) > -1)) {
324    
325                            return;
326                    }
327    
328                    String articleId = portletPreferences.getValue(
329                            "articleId", StringPool.BLANK);
330    
331                    if (Validator.isNotNull(articleId)) {
332                            manifestSummary.addModelAdditionCount(
333                                    new StagedModelType(JournalArticle.class), 1);
334                    }
335            }
336    
337            private static Log _log = LogFactoryUtil.getLog(
338                    JournalContentPortletDataHandler.class);
339    
340    }