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.rolesadmin.lar;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
019    import com.liferay.portal.kernel.dao.orm.Property;
020    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.lar.BasePortletDataHandler;
023    import com.liferay.portal.kernel.lar.DataLevel;
024    import com.liferay.portal.kernel.lar.PortletDataContext;
025    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
026    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
027    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
028    import com.liferay.portal.kernel.lar.StagedModelType;
029    import com.liferay.portal.kernel.lar.xstream.XStreamAliasRegistryUtil;
030    import com.liferay.portal.kernel.xml.Element;
031    import com.liferay.portal.model.Role;
032    import com.liferay.portal.model.Team;
033    import com.liferay.portal.model.impl.RoleImpl;
034    import com.liferay.portal.service.RoleLocalServiceUtil;
035    import com.liferay.portal.service.UserLocalServiceUtil;
036    import com.liferay.portal.util.PortalUtil;
037    
038    import java.util.List;
039    
040    import javax.portlet.PortletPreferences;
041    
042    /**
043     * @author Michael C. Han
044     * @author David Mendez Gonzalez
045     */
046    public class RolesAdminPortletDataHandler extends BasePortletDataHandler {
047    
048            public static final String NAMESPACE = "roles_admin";
049    
050            public RolesAdminPortletDataHandler() {
051                    setDataLevel(DataLevel.PORTAL);
052                    setDeletionSystemEventStagedModelTypes(new StagedModelType(Role.class));
053                    setExportControls(
054                            new PortletDataHandlerBoolean(
055                                    NAMESPACE, "roles", true, true,
056                                    new PortletDataHandlerControl[] {
057                                            new PortletDataHandlerBoolean(
058                                                    NAMESPACE, "system-roles", true, false)
059                                    },
060                                    Role.class.getName(), StagedModelType.REFERRER_CLASS_NAME_ALL
061                            ));
062                    setSupportsDataStrategyCopyAsNew(false);
063    
064                    XStreamAliasRegistryUtil.register(RoleImpl.class, "Role");
065            }
066    
067            @Override
068            protected PortletPreferences doDeleteData(
069                            PortletDataContext portletDataContext, String portletId,
070                            PortletPreferences portletPreferences)
071                    throws Exception {
072    
073                    if (portletDataContext.addPrimaryKey(
074                                    RolesAdminPortletDataHandler.class, "deleteData")) {
075    
076                            return portletPreferences;
077                    }
078    
079                    List<Role> roles = RoleLocalServiceUtil.getRoles(
080                            portletDataContext.getCompanyId());
081    
082                    for (Role role : roles) {
083                            if (!PortalUtil.isSystemRole(role.getName())) {
084                                    RoleLocalServiceUtil.deleteRole(role);
085                            }
086                    }
087    
088                    return portletPreferences;
089            }
090    
091            @Override
092            protected String doExportData(
093                            final PortletDataContext portletDataContext, String portletId,
094                            PortletPreferences portletPreferences)
095                    throws Exception {
096    
097                    portletDataContext.addPortalPermissions();
098    
099                    Element rootElement = addExportDataRootElement(portletDataContext);
100    
101                    rootElement.addAttribute(
102                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
103    
104                    ActionableDynamicQuery actionableDynamicQuery =
105                            getRoleActionableDynamicQuery(portletDataContext, true);
106    
107                    actionableDynamicQuery.performActions();
108    
109                    return getExportDataRootElementString(rootElement);
110            }
111    
112            @Override
113            protected PortletPreferences doImportData(
114                            PortletDataContext portletDataContext, String portletId,
115                            PortletPreferences portletPreferences, String data)
116                    throws Exception {
117    
118                    portletDataContext.importPortalPermissions();
119    
120                    Element rolesElement = portletDataContext.getImportDataGroupElement(
121                            Role.class);
122    
123                    List<Element> roleElements = rolesElement.elements();
124    
125                    for (Element roleElement : roleElements) {
126                            StagedModelDataHandlerUtil.importStagedModel(
127                                    portletDataContext, roleElement);
128                    }
129    
130                    return null;
131            }
132    
133            @Override
134            protected void doPrepareManifestSummary(
135                            PortletDataContext portletDataContext,
136                            PortletPreferences portletPreferences)
137                    throws Exception {
138    
139                    ActionableDynamicQuery actionableDynamicQuery =
140                            getRoleActionableDynamicQuery(portletDataContext, false);
141    
142                    actionableDynamicQuery.performCount();
143            }
144    
145            protected ActionableDynamicQuery getRoleActionableDynamicQuery(
146                    final PortletDataContext portletDataContext, final boolean export) {
147    
148                    ActionableDynamicQuery actionableDynamicQuery =
149                            RoleLocalServiceUtil.getExportActionableDynamicQuery(
150                                    portletDataContext);
151    
152                    actionableDynamicQuery.setAddCriteriaMethod(
153                            new ActionableDynamicQuery.AddCriteriaMethod() {
154    
155                                    @Override
156                                    public void addCriteria(DynamicQuery dynamicQuery) {
157                                            portletDataContext.addDateRangeCriteria(
158                                                    dynamicQuery, "modifiedDate");
159    
160                                            long classNameId = PortalUtil.getClassNameId(Team.class);
161    
162                                            Property classNameIdProperty = PropertyFactoryUtil.forName(
163                                                    "classNameId");
164    
165                                            dynamicQuery.add(classNameIdProperty.ne(classNameId));
166                                    }
167    
168                            });
169    
170                    final ActionableDynamicQuery.PerformActionMethod performActionMethod =
171                            actionableDynamicQuery.getPerformActionMethod();
172    
173                    actionableDynamicQuery.setPerformActionMethod(
174                            new ActionableDynamicQuery.PerformActionMethod() {
175    
176                                    @Override
177                                    public void performAction(Object object)
178                                            throws PortalException {
179    
180                                            if (!export) {
181                                                    return;
182                                            }
183    
184                                            Role role = (Role)object;
185    
186                                            long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
187                                                    portletDataContext.getCompanyId());
188    
189                                            if (!portletDataContext.getBooleanParameter(
190                                                            NAMESPACE, "system-roles") &&
191                                                    (role.getUserId() == defaultUserId)) {
192    
193                                                    return;
194                                            }
195    
196                                            performActionMethod.performAction(object);
197                                    }
198    
199                            });
200    
201                    return actionableDynamicQuery;
202            }
203    
204    }