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.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.lar.xstream.XStreamAliasRegistryUtil;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.model.UserGroup;
026    import com.liferay.portal.model.impl.UserGroupImpl;
027    import com.liferay.portal.service.UserGroupLocalServiceUtil;
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                    XStreamAliasRegistryUtil.register(UserGroupImpl.class, "UserGroup");
050            }
051    
052            @Override
053            protected PortletPreferences doDeleteData(
054                            PortletDataContext portletDataContext, String portletId,
055                            PortletPreferences portletPreferences)
056                    throws Exception {
057    
058                    if (portletDataContext.addPrimaryKey(
059                                    UserGroupsAdminPortletDataHandler.class, "deleteData")) {
060    
061                            return portletPreferences;
062                    }
063    
064                    UserGroupLocalServiceUtil.deleteUserGroups(
065                            portletDataContext.getCompanyId());
066    
067                    return portletPreferences;
068            }
069    
070            @Override
071            protected String doExportData(
072                            final PortletDataContext portletDataContext, String portletId,
073                            PortletPreferences portletPreferences)
074                    throws Exception {
075    
076                    portletDataContext.addPortalPermissions();
077    
078                    Element rootElement = addExportDataRootElement(portletDataContext);
079    
080                    rootElement.addAttribute(
081                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
082    
083                    ActionableDynamicQuery actionableDynamicQuery =
084                            UserGroupLocalServiceUtil.getExportActionableDynamicQuery(
085                                    portletDataContext);
086    
087                    actionableDynamicQuery.performActions();
088    
089                    return getExportDataRootElementString(rootElement);
090            }
091    
092            @Override
093            protected PortletPreferences doImportData(
094                            PortletDataContext portletDataContext, String portletId,
095                            PortletPreferences portletPreferences, String data)
096                    throws Exception {
097    
098                    portletDataContext.importPortalPermissions();
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                            UserGroupLocalServiceUtil.getExportActionableDynamicQuery(
121                                    portletDataContext);
122    
123                    actionableDynamicQuery.performCount();
124            }
125    
126    }