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