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.addPermissions(
079                            RESOURCE_NAME, portletDataContext.getScopeGroupId());
080    
081                    Element rootElement = addExportDataRootElement(portletDataContext);
082    
083                    rootElement.addAttribute(
084                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
085    
086                    ActionableDynamicQuery actionableDynamicQuery =
087                            getPasswordPolicyActionableDynamicQuery(portletDataContext, true);
088    
089                    actionableDynamicQuery.performActions();
090    
091                    return getExportDataRootElementString(rootElement);
092            }
093    
094            @Override
095            protected PortletPreferences doImportData(
096                            PortletDataContext portletDataContext, String portletId,
097                            PortletPreferences portletPreferences, String data)
098                    throws Exception {
099    
100                    portletDataContext.importPermissions(
101                            RESOURCE_NAME, portletDataContext.getSourceGroupId(),
102                            portletDataContext.getScopeGroupId());
103    
104                    Element passwordPoliciesElement =
105                            portletDataContext.getImportDataGroupElement(PasswordPolicy.class);
106    
107                    List<Element> passwordPolicyElements =
108                            passwordPoliciesElement.elements();
109    
110                    for (Element passwordPolicyElement : passwordPolicyElements) {
111                            StagedModelDataHandlerUtil.importStagedModel(
112                                    portletDataContext, passwordPolicyElement);
113                    }
114    
115                    return null;
116            }
117    
118            @Override
119            protected void doPrepareManifestSummary(
120                            PortletDataContext portletDataContext,
121                            PortletPreferences portletPreferences)
122                    throws Exception {
123    
124                    ActionableDynamicQuery actionableDynamicQuery =
125                            getPasswordPolicyActionableDynamicQuery(portletDataContext, false);
126    
127                    actionableDynamicQuery.performCount();
128            }
129    
130            protected ActionableDynamicQuery getPasswordPolicyActionableDynamicQuery(
131                            final PortletDataContext portletDataContext, final boolean export)
132                    throws SystemException {
133    
134                    return new PasswordPolicyExportActionableDynamicQuery(
135                            portletDataContext) {
136    
137                            @Override
138                            protected void addCriteria(DynamicQuery dynamicQuery) {
139                                    portletDataContext.addDateRangeCriteria(
140                                            dynamicQuery, "modifiedDate");
141                            }
142    
143                            @Override
144                            protected void performAction(Object object) throws PortalException {
145                                    if (!export) {
146                                            return;
147                                    }
148    
149                                    PasswordPolicy passwordPolicy = (PasswordPolicy)object;
150    
151                                    StagedModelDataHandlerUtil.exportStagedModel(
152                                            portletDataContext, passwordPolicy);
153                            }
154    
155                    };
156            }
157    
158            protected static final String RESOURCE_NAME =
159                    "com.liferay.portlet.passwordpoliciesadmin";
160    
161    }