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