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.passwordpoliciesadmin.lar;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.lar.BasePortletDataHandler;
020    import com.liferay.portal.kernel.lar.DataLevel;
021    import com.liferay.portal.kernel.lar.PortletDataContext;
022    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
023    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
024    import com.liferay.portal.kernel.lar.StagedModelType;
025    import com.liferay.portal.kernel.lar.xstream.XStreamAliasRegistryUtil;
026    import com.liferay.portal.kernel.xml.Element;
027    import com.liferay.portal.model.PasswordPolicy;
028    import com.liferay.portal.model.impl.PasswordPolicyImpl;
029    import com.liferay.portal.service.PasswordPolicyLocalServiceUtil;
030    
031    import java.util.List;
032    
033    import javax.portlet.PortletPreferences;
034    
035    /**
036     * @author Daniela Zapata Riesco
037     */
038    public class PasswordPolicyPortletDataHandler extends BasePortletDataHandler {
039    
040            public static final String NAMESPACE = "password_policies_admin";
041    
042            public PasswordPolicyPortletDataHandler() {
043                    setDataLevel(DataLevel.PORTAL);
044                    setDeletionSystemEventStagedModelTypes(
045                            new StagedModelType(PasswordPolicy.class));
046                    setExportControls(
047                            new PortletDataHandlerBoolean(
048                                    NAMESPACE, "password-policies", true, true, null,
049                                    PasswordPolicy.class.getName()));
050                    setSupportsDataStrategyCopyAsNew(false);
051    
052                    XStreamAliasRegistryUtil.register(
053                            PasswordPolicyImpl.class, "PasswordPolicy");
054            }
055    
056            @Override
057            protected PortletPreferences doDeleteData(
058                            PortletDataContext portletDataContext, String portletId,
059                            PortletPreferences portletPreferences)
060                    throws Exception {
061    
062                    if (portletDataContext.addPrimaryKey(
063                                    PasswordPolicyPortletDataHandler.class, "deleteData")) {
064    
065                            return portletPreferences;
066                    }
067    
068                    PasswordPolicyLocalServiceUtil.deleteNondefaultPasswordPolicies(
069                            portletDataContext.getCompanyId());
070    
071                    return portletPreferences;
072            }
073    
074            @Override
075            protected String doExportData(
076                            final PortletDataContext portletDataContext, String portletId,
077                            PortletPreferences portletPreferences)
078                    throws Exception {
079    
080                    portletDataContext.addPortletPermissions(RESOURCE_NAME);
081    
082                    Element rootElement = addExportDataRootElement(portletDataContext);
083    
084                    rootElement.addAttribute(
085                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
086    
087                    ActionableDynamicQuery actionableDynamicQuery =
088                            getPasswordPolicyActionableDynamicQuery(portletDataContext, true);
089    
090                    actionableDynamicQuery.performActions();
091    
092                    return getExportDataRootElementString(rootElement);
093            }
094    
095            @Override
096            protected PortletPreferences doImportData(
097                            PortletDataContext portletDataContext, String portletId,
098                            PortletPreferences portletPreferences, String data)
099                    throws Exception {
100    
101                    portletDataContext.importPortletPermissions(RESOURCE_NAME);
102    
103                    Element passwordPoliciesElement =
104                            portletDataContext.getImportDataGroupElement(PasswordPolicy.class);
105    
106                    List<Element> passwordPolicyElements =
107                            passwordPoliciesElement.elements();
108    
109                    for (Element passwordPolicyElement : passwordPolicyElements) {
110                            StagedModelDataHandlerUtil.importStagedModel(
111                                    portletDataContext, passwordPolicyElement);
112                    }
113    
114                    return null;
115            }
116    
117            @Override
118            protected void doPrepareManifestSummary(
119                            PortletDataContext portletDataContext,
120                            PortletPreferences portletPreferences)
121                    throws Exception {
122    
123                    ActionableDynamicQuery actionableDynamicQuery =
124                            getPasswordPolicyActionableDynamicQuery(portletDataContext, false);
125    
126                    actionableDynamicQuery.performCount();
127            }
128    
129            protected ActionableDynamicQuery getPasswordPolicyActionableDynamicQuery(
130                    final PortletDataContext portletDataContext, final boolean export) {
131    
132                    ActionableDynamicQuery actionableDynamicQuery =
133                            PasswordPolicyLocalServiceUtil.getExportActionableDynamicQuery(
134                                    portletDataContext);
135    
136                    actionableDynamicQuery.setPerformActionMethod(
137                            new ActionableDynamicQuery.PerformActionMethod() {
138    
139                                    @Override
140                                    public void performAction(Object object)
141                                            throws PortalException {
142    
143                                            if (!export) {
144                                                    return;
145                                            }
146    
147                                            PasswordPolicy passwordPolicy = (PasswordPolicy)object;
148    
149                                            StagedModelDataHandlerUtil.exportStagedModel(
150                                                    portletDataContext, passwordPolicy);
151                                    }
152    
153                            });
154    
155                    return actionableDynamicQuery;
156            }
157    
158            protected static final String RESOURCE_NAME =
159                    "com.liferay.portlet.passwordpoliciesadmin";
160    
161    }