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