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.usersadmin.lar;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.xml.Element;
019    import com.liferay.portal.model.Organization;
020    import com.liferay.portal.model.OrganizationConstants;
021    import com.liferay.portal.model.impl.OrganizationImpl;
022    import com.liferay.portal.service.OrganizationLocalServiceUtil;
023    import com.liferay.portlet.exportimport.lar.BasePortletDataHandler;
024    import com.liferay.portlet.exportimport.lar.DataLevel;
025    import com.liferay.portlet.exportimport.lar.PortletDataContext;
026    import com.liferay.portlet.exportimport.lar.PortletDataHandlerBoolean;
027    import com.liferay.portlet.exportimport.lar.StagedModelDataHandlerUtil;
028    import com.liferay.portlet.exportimport.lar.StagedModelType;
029    import com.liferay.portlet.exportimport.xstream.XStreamAliasRegistryUtil;
030    
031    import java.util.List;
032    
033    import javax.portlet.PortletPreferences;
034    
035    /**
036     * @author Michael C. Han
037     * @author David Gonzalez
038     */
039    public class UsersAdminPortletDataHandler extends BasePortletDataHandler {
040    
041            public static final String NAMESPACE = "users_admin";
042    
043            public UsersAdminPortletDataHandler() {
044                    setDataLevel(DataLevel.PORTAL);
045                    setDeletionSystemEventStagedModelTypes(
046                            new StagedModelType(Organization.class));
047                    setExportControls(
048                            new PortletDataHandlerBoolean(
049                                    NAMESPACE, "organizations", true, true, null,
050                                    Organization.class.getName()));
051                    setSupportsDataStrategyCopyAsNew(false);
052    
053                    XStreamAliasRegistryUtil.register(
054                            OrganizationImpl.class, "Organization");
055            }
056    
057            @Override
058            protected PortletPreferences doDeleteData(
059                            PortletDataContext portletDataContext, String portletId,
060                            PortletPreferences portletPreferences)
061                    throws Exception {
062    
063                    List<Organization> organizations =
064                            OrganizationLocalServiceUtil.getOrganizations(
065                                    portletDataContext.getCompanyId(),
066                                    OrganizationConstants.ANY_PARENT_ORGANIZATION_ID);
067    
068                    for (Organization organization : organizations) {
069                            OrganizationLocalServiceUtil.deleteOrganization(organization);
070                    }
071    
072                    return portletPreferences;
073            }
074    
075            @Override
076            protected String doExportData(
077                            final PortletDataContext portletDataContext, String portletId,
078                            PortletPreferences portletPreferences)
079                    throws Exception {
080    
081                    portletDataContext.addPortalPermissions();
082    
083                    Element rootElement = addExportDataRootElement(portletDataContext);
084    
085                    rootElement.addAttribute(
086                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
087    
088                    ActionableDynamicQuery actionableDynamicQuery =
089                            OrganizationLocalServiceUtil.getExportActionableDynamicQuery(
090                                    portletDataContext);
091    
092                    actionableDynamicQuery.performActions();
093    
094                    return getExportDataRootElementString(rootElement);
095            }
096    
097            @Override
098            protected PortletPreferences doImportData(
099                            PortletDataContext portletDataContext, String portletId,
100                            PortletPreferences portletPreferences, String data)
101                    throws Exception {
102    
103                    portletDataContext.importPortalPermissions();
104    
105                    Element organizationsElement =
106                            portletDataContext.getImportDataGroupElement(Organization.class);
107    
108                    List<Element> organizationElements = organizationsElement.elements();
109    
110                    for (Element organizationElement : organizationElements) {
111                            StagedModelDataHandlerUtil.importStagedModel(
112                                    portletDataContext, organizationElement);
113                    }
114    
115                    return null;
116            }
117    
118            @Override
119            protected void doPrepareManifestSummary(
120                            PortletDataContext portletDataContext,
121                            PortletPreferences portletPreferences)
122                    throws Exception {
123    
124                    ActionableDynamicQuery actionableDynamicQuery =
125                            OrganizationLocalServiceUtil.getExportActionableDynamicQuery(
126                                    portletDataContext);
127    
128                    actionableDynamicQuery.performCount();
129            }
130    
131    }