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.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.lar.BaseStagedModelDataHandler;
020    import com.liferay.portal.kernel.lar.ExportImportHelper;
021    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
022    import com.liferay.portal.kernel.lar.PortletDataContext;
023    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.util.MapUtil;
028    import com.liferay.portal.kernel.util.StringBundler;
029    import com.liferay.portal.kernel.util.StringPool;
030    import com.liferay.portal.kernel.util.StringUtil;
031    import com.liferay.portal.kernel.util.Validator;
032    import com.liferay.portal.kernel.xml.Element;
033    import com.liferay.portal.model.Group;
034    import com.liferay.portal.service.GroupLocalServiceUtil;
035    import com.liferay.portal.service.ServiceContext;
036    import com.liferay.portal.util.PortalUtil;
037    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
038    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
039    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
040    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
041    import com.liferay.portlet.journal.FeedTargetLayoutFriendlyUrlException;
042    import com.liferay.portlet.journal.model.JournalArticle;
043    import com.liferay.portlet.journal.model.JournalFeed;
044    import com.liferay.portlet.journal.service.JournalFeedLocalServiceUtil;
045    
046    import java.util.List;
047    import java.util.Map;
048    
049    /**
050     * @author Daniel Kocsis
051     */
052    public class JournalFeedStagedModelDataHandler
053            extends BaseStagedModelDataHandler<JournalFeed> {
054    
055            public static final String[] CLASS_NAMES = {JournalFeed.class.getName()};
056    
057            @Override
058            public void deleteStagedModel(
059                            String uuid, long groupId, String className, String extraData)
060                    throws PortalException, SystemException {
061    
062                    JournalFeed feed =
063                            JournalFeedLocalServiceUtil.fetchJournalFeedByUuidAndGroupId(
064                                    uuid, groupId);
065    
066                    if (feed != null) {
067                            JournalFeedLocalServiceUtil.deleteFeed(feed);
068                    }
069            }
070    
071            @Override
072            public String[] getClassNames() {
073                    return CLASS_NAMES;
074            }
075    
076            @Override
077            protected void doExportStagedModel(
078                            PortletDataContext portletDataContext, JournalFeed feed)
079                    throws Exception {
080    
081                    Element feedElement = portletDataContext.getExportDataElement(feed);
082    
083                    DDMStructure ddmStructure = DDMStructureLocalServiceUtil.fetchStructure(
084                            feed.getGroupId(), PortalUtil.getClassNameId(JournalArticle.class),
085                            feed.getStructureId(), true);
086    
087                    if (ddmStructure != null) {
088                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
089                                    portletDataContext, feed, ddmStructure,
090                                    PortletDataContext.REFERENCE_TYPE_STRONG);
091                    }
092                    else {
093                            if (_log.isWarnEnabled()) {
094                                    _log.warn(
095                                            "Unable to find DDM structure with key " +
096                                                    feed.getStructureId());
097                            }
098                    }
099    
100                    DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.fetchTemplate(
101                            feed.getGroupId(), PortalUtil.getClassNameId(DDMStructure.class),
102                            feed.getTemplateId());
103    
104                    if (ddmTemplate != null) {
105                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
106                                    portletDataContext, feed, ddmTemplate,
107                                    PortletDataContext.REFERENCE_TYPE_STRONG);
108                    }
109                    else {
110                            if (_log.isWarnEnabled()) {
111                                    _log.warn(
112                                            "Unable to find DDM template with key " +
113                                                    feed.getTemplateId());
114                            }
115                    }
116    
117                    DDMTemplate rendererDDMTemplate =
118                            DDMTemplateLocalServiceUtil.fetchTemplate(
119                                    feed.getGroupId(),
120                                    PortalUtil.getClassNameId(DDMStructure.class),
121                                    feed.getRendererTemplateId());
122    
123                    if (rendererDDMTemplate != null) {
124                            Element rendererDDMTemplateElement =
125                                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
126                                            portletDataContext, feed, rendererDDMTemplate,
127                                            PortletDataContext.REFERENCE_TYPE_STRONG);
128    
129                            rendererDDMTemplateElement.addAttribute(
130                                    "rendererDDMTemplate", "true");
131                    }
132                    else {
133                            if (_log.isWarnEnabled()) {
134                                    _log.warn(
135                                            "Unable to find DDM template with key " +
136                                                    feed.getRendererTemplateId());
137                            }
138                    }
139    
140                    Group group = GroupLocalServiceUtil.getGroup(
141                            portletDataContext.getScopeGroupId());
142    
143                    String newGroupFriendlyURL = group.getFriendlyURL().substring(1);
144    
145                    String[] friendlyURLParts = StringUtil.split(
146                            feed.getTargetLayoutFriendlyUrl(), '/');
147    
148                    String oldGroupFriendlyURL = friendlyURLParts[2];
149    
150                    if (newGroupFriendlyURL.equals(oldGroupFriendlyURL)) {
151                            String targetLayoutFriendlyUrl = StringUtil.replaceFirst(
152                                    feed.getTargetLayoutFriendlyUrl(),
153                                    StringPool.SLASH + newGroupFriendlyURL + StringPool.SLASH,
154                                    StringPool.SLASH +
155                                            ExportImportHelper.DATA_HANDLER_GROUP_FRIENDLY_URL +
156                                                    StringPool.SLASH);
157    
158                            feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl);
159                    }
160    
161                    portletDataContext.addClassedModel(
162                            feedElement, ExportImportPathUtil.getModelPath(feed), feed);
163            }
164    
165            @Override
166            protected void doImportStagedModel(
167                            PortletDataContext portletDataContext, JournalFeed feed)
168                    throws Exception {
169    
170                    long userId = portletDataContext.getUserId(feed.getUserUuid());
171    
172                    JournalCreationStrategy creationStrategy =
173                            JournalCreationStrategyFactory.getInstance();
174    
175                    long authorId = creationStrategy.getAuthorUserId(
176                            portletDataContext, feed);
177    
178                    if (authorId != JournalCreationStrategy.USE_DEFAULT_USER_ID_STRATEGY) {
179                            userId = authorId;
180                    }
181    
182                    Group group = GroupLocalServiceUtil.getGroup(
183                            portletDataContext.getScopeGroupId());
184    
185                    String newGroupFriendlyURL = group.getFriendlyURL().substring(1);
186    
187                    String[] friendlyURLParts = StringUtil.split(
188                            feed.getTargetLayoutFriendlyUrl(), '/');
189    
190                    String oldGroupFriendlyURL = friendlyURLParts[2];
191    
192                    if (oldGroupFriendlyURL.equals(
193                                    ExportImportHelper.DATA_HANDLER_GROUP_FRIENDLY_URL)) {
194    
195                            feed.setTargetLayoutFriendlyUrl(
196                                    StringUtil.replace(
197                                            feed.getTargetLayoutFriendlyUrl(),
198                                            ExportImportHelper.DATA_HANDLER_GROUP_FRIENDLY_URL,
199                                            newGroupFriendlyURL));
200                    }
201    
202                    String feedId = feed.getFeedId();
203    
204                    boolean autoFeedId = false;
205    
206                    if (Validator.isNumber(feedId) ||
207                            (JournalFeedLocalServiceUtil.fetchFeed(
208                                    portletDataContext.getScopeGroupId(), feedId) != null)) {
209    
210                            autoFeedId = true;
211                    }
212    
213                    List<Element> ddmStructureElements =
214                            portletDataContext.getReferenceDataElements(
215                                    feed, DDMStructure.class);
216    
217                    String parentDDMStructureKey = StringPool.BLANK;
218    
219                    if (!ddmStructureElements.isEmpty()) {
220                            Element ddmStructureElement = ddmStructureElements.get(0);
221    
222                            String ddmStructurePath = ddmStructureElement.attributeValue(
223                                    "path");
224    
225                            DDMStructure ddmStructure =
226                                    (DDMStructure)portletDataContext.getZipEntryAsObject(
227                                            ddmStructurePath);
228    
229                            StagedModelDataHandlerUtil.importReferenceStagedModel(
230                                    portletDataContext, ddmStructure);
231    
232                            Map<String, String> ddmStructureKeys =
233                                    (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
234                                            DDMStructure.class + ".ddmStructureKey");
235    
236                            parentDDMStructureKey = MapUtil.getString(
237                                    ddmStructureKeys, ddmStructure.getStructureKey(),
238                                    ddmStructure.getStructureKey());
239                    }
240    
241                    List<Element> ddmTemplateElements =
242                            portletDataContext.getReferenceDataElements(
243                                    feed, DDMTemplate.class);
244    
245                    String parentDDMTemplateKey = StringPool.BLANK;
246                    String parentRendererDDMTemplateKey = StringPool.BLANK;
247    
248                    for (Element ddmTemplateElement : ddmTemplateElements) {
249                            String ddmTemplatePath = ddmTemplateElement.attributeValue("path");
250    
251                            DDMTemplate ddmTemplate =
252                                    (DDMTemplate)portletDataContext.getZipEntryAsObject(
253                                            ddmTemplatePath);
254    
255                            StagedModelDataHandlerUtil.importReferenceStagedModel(
256                                    portletDataContext, ddmTemplate);
257    
258                            Map<String, String> ddmTemplateKeys =
259                                    (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
260                                            DDMTemplate.class + ".ddmTemplateKey");
261    
262                            boolean rendererDDMTemplate = GetterUtil.getBoolean(
263                                    ddmTemplateElement.attributeValue("rendererDDMTemplate"));
264    
265                            String ddmTemplateKey = MapUtil.getString(
266                                    ddmTemplateKeys, ddmTemplate.getTemplateKey(),
267                                    ddmTemplate.getTemplateKey());
268    
269                            if (rendererDDMTemplate) {
270                                    parentDDMTemplateKey = ddmTemplateKey;
271                            }
272                            else {
273                                    parentRendererDDMTemplateKey = ddmTemplateKey;
274                            }
275                    }
276    
277                    ServiceContext serviceContext = portletDataContext.createServiceContext(
278                            feed);
279    
280                    boolean addGroupPermissions = creationStrategy.addGroupPermissions(
281                            portletDataContext, feed);
282    
283                    serviceContext.setAddGroupPermissions(addGroupPermissions);
284    
285                    boolean addGuestPermissions = creationStrategy.addGuestPermissions(
286                            portletDataContext, feed);
287    
288                    serviceContext.setAddGuestPermissions(addGuestPermissions);
289    
290                    JournalFeed importedFeed = null;
291    
292                    try {
293                            if (portletDataContext.isDataStrategyMirror()) {
294                                    JournalFeed existingFeed =
295                                            JournalFeedLocalServiceUtil.
296                                                    fetchJournalFeedByUuidAndGroupId(
297                                                            feed.getUuid(),
298                                                            portletDataContext.getScopeGroupId());
299    
300                                    if (existingFeed == null) {
301                                            serviceContext.setUuid(feed.getUuid());
302    
303                                            importedFeed = JournalFeedLocalServiceUtil.addFeed(
304                                                    userId, portletDataContext.getScopeGroupId(), feedId,
305                                                    autoFeedId, feed.getName(), feed.getDescription(),
306                                                    feed.getType(), parentDDMStructureKey,
307                                                    parentDDMTemplateKey, parentRendererDDMTemplateKey,
308                                                    feed.getDelta(), feed.getOrderByCol(),
309                                                    feed.getOrderByType(),
310                                                    feed.getTargetLayoutFriendlyUrl(),
311                                                    feed.getTargetPortletId(), feed.getContentField(),
312                                                    feed.getFeedFormat(), feed.getFeedVersion(),
313                                                    serviceContext);
314                                    }
315                                    else {
316                                            importedFeed = JournalFeedLocalServiceUtil.updateFeed(
317                                                    existingFeed.getGroupId(), existingFeed.getFeedId(),
318                                                    feed.getName(), feed.getDescription(), feed.getType(),
319                                                    parentDDMStructureKey, parentDDMTemplateKey,
320                                                    parentRendererDDMTemplateKey, feed.getDelta(),
321                                                    feed.getOrderByCol(), feed.getOrderByType(),
322                                                    feed.getTargetLayoutFriendlyUrl(),
323                                                    feed.getTargetPortletId(), feed.getContentField(),
324                                                    feed.getFeedFormat(), feed.getFeedVersion(),
325                                                    serviceContext);
326                                    }
327                            }
328                            else {
329                                    importedFeed = JournalFeedLocalServiceUtil.addFeed(
330                                            userId, portletDataContext.getScopeGroupId(), feedId,
331                                            autoFeedId, feed.getName(), feed.getDescription(),
332                                            feed.getType(), parentDDMStructureKey, parentDDMTemplateKey,
333                                            parentRendererDDMTemplateKey, feed.getDelta(),
334                                            feed.getOrderByCol(), feed.getOrderByType(),
335                                            feed.getTargetLayoutFriendlyUrl(),
336                                            feed.getTargetPortletId(), feed.getContentField(),
337                                            feed.getFeedFormat(), feed.getFeedVersion(),
338                                            serviceContext);
339                            }
340    
341                            portletDataContext.importClassedModel(feed, importedFeed);
342    
343                            if (!feedId.equals(importedFeed.getFeedId())) {
344                                    if (_log.isWarnEnabled()) {
345                                            StringBundler sb = new StringBundler(5);
346    
347                                            sb.append("A feed with the ID ");
348                                            sb.append(feedId);
349                                            sb.append(" already exists. The new generated ID is ");
350                                            sb.append(importedFeed.getFeedId());
351                                            sb.append(".");
352    
353                                            _log.warn(sb.toString());
354                                    }
355                            }
356                    }
357                    catch (FeedTargetLayoutFriendlyUrlException ftlfurle) {
358                            if (_log.isWarnEnabled()) {
359                                    StringBundler sb = new StringBundler(6);
360    
361                                    sb.append("A feed with the ID ");
362                                    sb.append(feedId);
363                                    sb.append(" cannot be imported because layout with friendly ");
364                                    sb.append("URL ");
365                                    sb.append(feed.getTargetLayoutFriendlyUrl());
366                                    sb.append(" does not exist");
367    
368                                    _log.warn(sb.toString());
369                            }
370                    }
371            }
372    
373            private static Log _log = LogFactoryUtil.getLog(
374                    JournalFeedStagedModelDataHandler.class);
375    
376    }