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.layoutsadmin.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.ExportImportPathUtil;
021    import com.liferay.portal.kernel.lar.PortletDataContext;
022    import com.liferay.portal.kernel.util.MapUtil;
023    import com.liferay.portal.kernel.xml.Element;
024    import com.liferay.portal.model.Layout;
025    import com.liferay.portal.model.LayoutFriendlyURL;
026    import com.liferay.portal.service.LayoutFriendlyURLLocalServiceUtil;
027    import com.liferay.portal.service.ServiceContext;
028    
029    import java.util.Map;
030    
031    /**
032     * @author Sergio Gonz??lez
033     */
034    public class LayoutFriendlyURLStagedModelDataHandler
035            extends BaseStagedModelDataHandler<LayoutFriendlyURL> {
036    
037            public static final String[] CLASS_NAMES =
038                    {LayoutFriendlyURL.class.getName()};
039    
040            @Override
041            public void deleteStagedModel(
042                            String uuid, long groupId, String className, String extraData)
043                    throws PortalException, SystemException {
044    
045                    LayoutFriendlyURL layoutFriendlyURL =
046                            LayoutFriendlyURLLocalServiceUtil.
047                                    getLayoutFriendlyURLByUuidAndGroupId(uuid, groupId);
048    
049                    LayoutFriendlyURLLocalServiceUtil.deleteLayoutFriendlyURL(
050                            layoutFriendlyURL);
051            }
052    
053            @Override
054            public String[] getClassNames() {
055                    return CLASS_NAMES;
056            }
057    
058            @Override
059            protected void doExportStagedModel(
060                            PortletDataContext portletDataContext,
061                            LayoutFriendlyURL layoutFriendlyURL)
062                    throws Exception {
063    
064                    Element layoutFriendlyURLElement =
065                            portletDataContext.getExportDataElement(layoutFriendlyURL);
066    
067                    portletDataContext.addClassedModel(
068                            layoutFriendlyURLElement,
069                            ExportImportPathUtil.getModelPath(layoutFriendlyURL),
070                            layoutFriendlyURL);
071            }
072    
073            @Override
074            protected void doImportStagedModel(
075                            PortletDataContext portletDataContext,
076                            LayoutFriendlyURL layoutFriendlyURL)
077                    throws Exception {
078    
079                    long userId = portletDataContext.getUserId(
080                            layoutFriendlyURL.getUserUuid());
081    
082                    Map<Long, Long> plids =
083                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
084                                    Layout.class);
085    
086                    long plid = MapUtil.getLong(
087                            plids, layoutFriendlyURL.getPlid(), layoutFriendlyURL.getPlid());
088    
089                    ServiceContext serviceContext = portletDataContext.createServiceContext(
090                            layoutFriendlyURL);
091    
092                    LayoutFriendlyURL importedLayoutFriendlyURL = null;
093    
094                    if (portletDataContext.isDataStrategyMirror()) {
095                            LayoutFriendlyURL existingLayoutFriendlyURL =
096                                    getExistingLayoutFriendlyURL(
097                                            portletDataContext, layoutFriendlyURL, plid);
098    
099                            layoutFriendlyURL = getUniqueLayoutFriendlyURL(
100                                    portletDataContext, layoutFriendlyURL,
101                                    existingLayoutFriendlyURL);
102    
103                            if (existingLayoutFriendlyURL == null) {
104                                    serviceContext.setUuid(layoutFriendlyURL.getUuid());
105    
106                                    importedLayoutFriendlyURL =
107                                            LayoutFriendlyURLLocalServiceUtil.addLayoutFriendlyURL(
108                                                    userId, portletDataContext.getCompanyId(),
109                                                    portletDataContext.getScopeGroupId(), plid,
110                                                    portletDataContext.isPrivateLayout(),
111                                                    layoutFriendlyURL.getFriendlyURL(),
112                                                    layoutFriendlyURL.getLanguageId(), serviceContext);
113                            }
114                            else {
115                                    importedLayoutFriendlyURL =
116                                            LayoutFriendlyURLLocalServiceUtil.updateLayoutFriendlyURL(
117                                                    userId, portletDataContext.getCompanyId(),
118                                                    portletDataContext.getScopeGroupId(), plid,
119                                                    portletDataContext.isPrivateLayout(),
120                                                    layoutFriendlyURL.getFriendlyURL(),
121                                                    layoutFriendlyURL.getLanguageId(), serviceContext);
122                            }
123                    }
124                    else {
125                            layoutFriendlyURL = getUniqueLayoutFriendlyURL(
126                                    portletDataContext, layoutFriendlyURL, null);
127    
128                            importedLayoutFriendlyURL =
129                                    LayoutFriendlyURLLocalServiceUtil.addLayoutFriendlyURL(
130                                            userId, portletDataContext.getCompanyId(),
131                                            portletDataContext.getScopeGroupId(), plid,
132                                            portletDataContext.isPrivateLayout(),
133                                            layoutFriendlyURL.getFriendlyURL(),
134                                            layoutFriendlyURL.getLanguageId(), serviceContext);
135                    }
136    
137                    portletDataContext.importClassedModel(
138                            layoutFriendlyURL, importedLayoutFriendlyURL);
139            }
140    
141            protected LayoutFriendlyURL getExistingLayoutFriendlyURL(
142                            PortletDataContext portletDataContext,
143                            LayoutFriendlyURL layoutFriendlyURL, long plid)
144                    throws Exception {
145    
146                    LayoutFriendlyURL existingLayoutFriendlyURL =
147                            LayoutFriendlyURLLocalServiceUtil.
148                                    fetchLayoutFriendlyURLByUuidAndGroupId(
149                                            layoutFriendlyURL.getUuid(),
150                                            portletDataContext.getScopeGroupId());
151    
152                    if (existingLayoutFriendlyURL == null) {
153                            existingLayoutFriendlyURL =
154                                    LayoutFriendlyURLLocalServiceUtil.fetchLayoutFriendlyURL(
155                                            plid, layoutFriendlyURL.getLanguageId(), false);
156                    }
157    
158                    return existingLayoutFriendlyURL;
159            }
160    
161            protected LayoutFriendlyURL getUniqueLayoutFriendlyURL(
162                            PortletDataContext portletDataContext,
163                            LayoutFriendlyURL layoutFriendlyURL,
164                            LayoutFriendlyURL existingLayoutFriendlyURL)
165                    throws Exception {
166    
167                    String friendlyURL = layoutFriendlyURL.getFriendlyURL();
168    
169                    for (int i = 1;; i++) {
170                            LayoutFriendlyURL duplicateLayoutFriendlyURL =
171                                    LayoutFriendlyURLLocalServiceUtil.fetchLayoutFriendlyURL(
172                                            portletDataContext.getScopeGroupId(),
173                                            layoutFriendlyURL.isPrivateLayout(),
174                                            layoutFriendlyURL.getFriendlyURL(),
175                                            layoutFriendlyURL.getLanguageId());
176    
177                            if ((duplicateLayoutFriendlyURL == null) ||
178                                    ((existingLayoutFriendlyURL != null) &&
179                                     (existingLayoutFriendlyURL.getLayoutFriendlyURLId() ==
180                                            duplicateLayoutFriendlyURL.getLayoutFriendlyURLId()))) {
181    
182                                    break;
183                            }
184    
185                            layoutFriendlyURL.setFriendlyURL(friendlyURL + i);
186                    }
187    
188                    return layoutFriendlyURL;
189            }
190    
191    }