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