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.usergroupsadmin.lar;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.lar.BasePortletDataHandler;
019    import com.liferay.portal.kernel.lar.DataLevel;
020    import com.liferay.portal.kernel.lar.PortletDataContext;
021    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
022    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
023    import com.liferay.portal.kernel.xml.Element;
024    import com.liferay.portal.model.UserGroup;
025    import com.liferay.portal.service.UserGroupLocalServiceUtil;
026    import com.liferay.portal.service.persistence.UserGroupExportActionableDynamicQuery;
027    import com.liferay.portal.util.PortletKeys;
028    
029    import java.util.List;
030    
031    import javax.portlet.PortletPreferences;
032    
033    /**
034     * @author Michael C. Han
035     * @author David Mendez Gonzalez
036     */
037    public class UserGroupsAdminPortletDataHandler extends BasePortletDataHandler {
038    
039            public static final String NAMESPACE = "user_groups_admin";
040    
041            public UserGroupsAdminPortletDataHandler() {
042                    setDataLevel(DataLevel.PORTAL);
043                    setExportControls(
044                            new PortletDataHandlerBoolean(
045                                    NAMESPACE, "user-groups", true, true, null,
046                                    UserGroup.class.getName()));
047                    setSupportsDataStrategyCopyAsNew(false);
048            }
049    
050            @Override
051            protected PortletPreferences doDeleteData(
052                            PortletDataContext portletDataContext, String portletId,
053                            PortletPreferences portletPreferences)
054                    throws Exception {
055    
056                    if (portletDataContext.addPrimaryKey(
057                                    UserGroupsAdminPortletDataHandler.class, "deleteData")) {
058    
059                            return portletPreferences;
060                    }
061    
062                    UserGroupLocalServiceUtil.deleteUserGroups(
063                            portletDataContext.getCompanyId());
064    
065                    return portletPreferences;
066            }
067    
068            @Override
069            protected String doExportData(
070                            final PortletDataContext portletDataContext, String portletId,
071                            PortletPreferences portletPreferences)
072                    throws Exception {
073    
074                    portletDataContext.addPermissions(
075                            PortletKeys.PORTAL, portletDataContext.getCompanyId());
076    
077                    Element rootElement = addExportDataRootElement(portletDataContext);
078    
079                    rootElement.addAttribute(
080                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
081    
082                    ActionableDynamicQuery actionableDynamicQuery =
083                            new UserGroupExportActionableDynamicQuery(portletDataContext);
084    
085                    actionableDynamicQuery.performActions();
086    
087                    return getExportDataRootElementString(rootElement);
088            }
089    
090            @Override
091            protected PortletPreferences doImportData(
092                            PortletDataContext portletDataContext, String portletId,
093                            PortletPreferences portletPreferences, String data)
094                    throws Exception {
095    
096                    portletDataContext.importPermissions(
097                            PortletKeys.PORTAL, portletDataContext.getSourceCompanyId(),
098                            portletDataContext.getCompanyId());
099    
100                    Element userGroupsElement =
101                            portletDataContext.getImportDataGroupElement(UserGroup.class);
102    
103                    List<Element> userGroupElements = userGroupsElement.elements();
104    
105                    for (Element userGroupElement : userGroupElements) {
106                            StagedModelDataHandlerUtil.importStagedModel(
107                                    portletDataContext, userGroupElement);
108                    }
109    
110                    return null;
111            }
112    
113            @Override
114            protected void doPrepareManifestSummary(
115                            PortletDataContext portletDataContext,
116                            PortletPreferences portletPreferences)
117                    throws Exception {
118    
119                    ActionableDynamicQuery actionableDynamicQuery =
120                            new UserGroupExportActionableDynamicQuery(portletDataContext);
121    
122                    actionableDynamicQuery.performCount();
123            }
124    
125    }