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