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.sitesadmin.lar;
016    
017    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.xml.Element;
020    import com.liferay.portal.model.Group;
021    import com.liferay.portal.model.adapter.StagedGroup;
022    import com.liferay.portal.service.GroupLocalServiceUtil;
023    import com.liferay.portlet.exportimport.lar.BaseStagedModelDataHandler;
024    import com.liferay.portlet.exportimport.lar.PortletDataContext;
025    
026    import java.util.Collections;
027    import java.util.List;
028    import java.util.Map;
029    
030    /**
031     * @author Daniel Kocsis
032     */
033    @OSGiBeanProperties
034    public class StagedGroupStagedModelDataHandler
035            extends BaseStagedModelDataHandler<StagedGroup> {
036    
037            public static final String[] CLASS_NAMES = {StagedGroup.class.getName()};
038    
039            @Override
040            public void deleteStagedModel(StagedGroup stagedGroup) {
041                    throw new UnsupportedOperationException();
042            }
043    
044            @Override
045            public void deleteStagedModel(
046                    String uuid, long groupId, String className, String extraData) {
047    
048                    throw new UnsupportedOperationException();
049            }
050    
051            @Override
052            public List<StagedGroup> fetchStagedModelsByUuidAndCompanyId(
053                    String uuid, long companyId) {
054    
055                    return Collections.emptyList();
056            }
057    
058            @Override
059            public String[] getClassNames() {
060                    return CLASS_NAMES;
061            }
062    
063            @Override
064            public String getDisplayName(StagedGroup stagedGroup) {
065                    return stagedGroup.getName();
066            }
067    
068            @Override
069            public void importMissingReference(
070                    PortletDataContext portletDataContext, Element referenceElement) {
071    
072                    Map<Long, Long> groupIds =
073                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
074                                    Group.class);
075    
076                    long groupId = GetterUtil.getLong(
077                            referenceElement.attributeValue("group-id"));
078    
079                    if ((groupId == 0) || groupIds.containsKey(groupId)) {
080                            return;
081                    }
082    
083                    Group existingGroup = fetchExistingGroup(
084                            portletDataContext, referenceElement);
085    
086                    groupIds.put(groupId, existingGroup.getGroupId());
087            }
088    
089            @Override
090            public boolean validateReference(
091                    PortletDataContext portletDataContext, Element referenceElement) {
092    
093                    Map<Long, Long> groupIds =
094                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
095                                    Group.class);
096    
097                    long groupId = GetterUtil.getLong(
098                            referenceElement.attributeValue("group-id"));
099    
100                    if ((groupId == 0) || groupIds.containsKey(groupId)) {
101                            return true;
102                    }
103    
104                    Group existingGroup = fetchExistingGroup(
105                            portletDataContext, referenceElement);
106    
107                    if (existingGroup == null) {
108                            return false;
109                    }
110    
111                    groupIds.put(groupId, existingGroup.getGroupId());
112    
113                    return true;
114            }
115    
116            @Override
117            protected void doExportStagedModel(
118                    PortletDataContext portletDataContext, StagedGroup stagedGroup) {
119            }
120    
121            @Override
122            protected void doImportStagedModel(
123                    PortletDataContext portletDataContext, StagedGroup stagedGroup) {
124            }
125    
126            protected Group fetchExistingGroup(
127                    PortletDataContext portletDataContext, Element referenceElement) {
128    
129                    long groupId = GetterUtil.getLong(
130                            referenceElement.attributeValue("group-id"));
131                    long liveGroupId = GetterUtil.getLong(
132                            referenceElement.attributeValue("live-group-id"));
133    
134                    if ((groupId == 0) || (liveGroupId == 0)) {
135                            return null;
136                    }
137    
138                    return fetchExistingGroup(portletDataContext, groupId, liveGroupId);
139            }
140    
141            protected Group fetchExistingGroup(
142                    PortletDataContext portletDataContext, long groupId, long liveGroupId) {
143    
144                    Group liveGroup = GroupLocalServiceUtil.fetchGroup(liveGroupId);
145    
146                    if (liveGroup != null) {
147                            return liveGroup;
148                    }
149    
150                    long existingGroupId = portletDataContext.getScopeGroupId();
151    
152                    if (groupId == portletDataContext.getSourceCompanyGroupId()) {
153                            existingGroupId = portletDataContext.getCompanyGroupId();
154                    }
155                    else if (groupId == portletDataContext.getSourceGroupId()) {
156                            existingGroupId = portletDataContext.getGroupId();
157                    }
158    
159                    // During remote staging, valid mappings are found when the reference's
160                    // group is properly staged. During local staging, valid mappings are
161                    // found when the references do not change between staging and live.
162    
163                    return GroupLocalServiceUtil.fetchGroup(existingGroupId);
164            }
165    
166    }