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                    LayoutFriendlyURL existingLayoutFriendlyURL =
095                            getExistingLayoutFriendlyURL(
096                                    portletDataContext, layoutFriendlyURL, plid);
097    
098                    layoutFriendlyURL = getUniqueLayoutFriendlyURL(
099                            portletDataContext, layoutFriendlyURL, existingLayoutFriendlyURL);
100    
101                    if (existingLayoutFriendlyURL == null) {
102                            serviceContext.setUuid(layoutFriendlyURL.getUuid());
103    
104                            importedLayoutFriendlyURL =
105                                    LayoutFriendlyURLLocalServiceUtil.addLayoutFriendlyURL(
106                                            userId, portletDataContext.getCompanyId(),
107                                            portletDataContext.getScopeGroupId(), plid,
108                                            portletDataContext.isPrivateLayout(),
109                                            layoutFriendlyURL.getFriendlyURL(),
110                                            layoutFriendlyURL.getLanguageId(), serviceContext);
111                    }
112                    else {
113                            importedLayoutFriendlyURL =
114                                    LayoutFriendlyURLLocalServiceUtil.updateLayoutFriendlyURL(
115                                            userId, portletDataContext.getCompanyId(),
116                                            portletDataContext.getScopeGroupId(), plid,
117                                            portletDataContext.isPrivateLayout(),
118                                            layoutFriendlyURL.getFriendlyURL(),
119                                            layoutFriendlyURL.getLanguageId(), serviceContext);
120                    }
121    
122                    portletDataContext.importClassedModel(
123                            layoutFriendlyURL, importedLayoutFriendlyURL);
124            }
125    
126            protected LayoutFriendlyURL getExistingLayoutFriendlyURL(
127                            PortletDataContext portletDataContext,
128                            LayoutFriendlyURL layoutFriendlyURL, long plid)
129                    throws Exception {
130    
131                    LayoutFriendlyURL existingLayoutFriendlyURL =
132                            LayoutFriendlyURLLocalServiceUtil.
133                                    fetchLayoutFriendlyURLByUuidAndGroupId(
134                                            layoutFriendlyURL.getUuid(),
135                                            portletDataContext.getScopeGroupId());
136    
137                    if (existingLayoutFriendlyURL == null) {
138                            existingLayoutFriendlyURL =
139                                    LayoutFriendlyURLLocalServiceUtil.fetchLayoutFriendlyURL(
140                                            plid, layoutFriendlyURL.getLanguageId(), false);
141                    }
142    
143                    return existingLayoutFriendlyURL;
144            }
145    
146            protected LayoutFriendlyURL getUniqueLayoutFriendlyURL(
147                            PortletDataContext portletDataContext,
148                            LayoutFriendlyURL layoutFriendlyURL,
149                            LayoutFriendlyURL existingLayoutFriendlyURL)
150                    throws Exception {
151    
152                    String friendlyURL = layoutFriendlyURL.getFriendlyURL();
153    
154                    for (int i = 1;; i++) {
155                            LayoutFriendlyURL duplicateLayoutFriendlyURL =
156                                    LayoutFriendlyURLLocalServiceUtil.fetchLayoutFriendlyURL(
157                                            portletDataContext.getScopeGroupId(),
158                                            layoutFriendlyURL.isPrivateLayout(),
159                                            layoutFriendlyURL.getFriendlyURL(),
160                                            layoutFriendlyURL.getLanguageId());
161    
162                            if ((duplicateLayoutFriendlyURL == null) ||
163                                    ((existingLayoutFriendlyURL != null) &&
164                                     (existingLayoutFriendlyURL.getLayoutFriendlyURLId() ==
165                                            duplicateLayoutFriendlyURL.getLayoutFriendlyURLId()))) {
166    
167                                    break;
168                            }
169    
170                            layoutFriendlyURL.setFriendlyURL(friendlyURL + i);
171                    }
172    
173                    return layoutFriendlyURL;
174            }
175    
176    }