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.dynamicdatalists.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.lar.BasePortletDataHandler;
021    import com.liferay.portal.kernel.lar.PortletDataContext;
022    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
023    import com.liferay.portal.kernel.xml.Element;
024    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
025    import com.liferay.portlet.dynamicdatalists.service.DDLRecordSetLocalServiceUtil;
026    import com.liferay.portlet.dynamicdatalists.service.persistence.DDLRecordSetActionableDynamicQuery;
027    
028    import java.util.List;
029    
030    import javax.portlet.PortletPreferences;
031    
032    /**
033     * @author Michael C. Han
034     */
035    public class DDLPortletDataHandler extends BasePortletDataHandler {
036    
037            public static final String NAMESPACE = "dynamic_data_lists";
038    
039            public DDLPortletDataHandler() {
040                    setAlwaysExportable(true);
041                    setDataLocalized(true);
042            }
043    
044            @Override
045            protected PortletPreferences doDeleteData(
046                            PortletDataContext portletDataContext, String portletId,
047                            PortletPreferences portletPreferences)
048                    throws Exception {
049    
050                    if (portletDataContext.addPrimaryKey(
051                                    DDLPortletDataHandler.class, "deleteData")) {
052    
053                            return portletPreferences;
054                    }
055    
056                    DDLRecordSetLocalServiceUtil.deleteRecordSets(
057                            portletDataContext.getScopeGroupId());
058    
059                    return portletPreferences;
060            }
061    
062            @Override
063            protected String doExportData(
064                            final PortletDataContext portletDataContext, String portletId,
065                            PortletPreferences portletPreferences)
066                    throws Exception {
067    
068                    portletDataContext.addPermissions(
069                            "com.liferay.portlet.dynamicdatalist",
070                            portletDataContext.getScopeGroupId());
071    
072                    Element rootElement = addExportDataRootElement(portletDataContext);
073    
074                    ActionableDynamicQuery actionableDynamicQuery =
075                            new DDLRecordSetActionableDynamicQuery() {
076    
077                            @Override
078                            protected void addCriteria(DynamicQuery dynamicQuery) {
079                                    portletDataContext.addDateRangeCriteria(
080                                            dynamicQuery, "modifiedDate");
081                            }
082    
083                            @Override
084                            protected void performAction(Object object) throws PortalException {
085                                    DDLRecordSet recordSet = (DDLRecordSet)object;
086    
087                                    StagedModelDataHandlerUtil.exportStagedModel(
088                                            portletDataContext, recordSet);
089                            }
090    
091                    };
092    
093                    actionableDynamicQuery.setGroupId(portletDataContext.getScopeGroupId());
094    
095                    actionableDynamicQuery.performActions();
096    
097                    return getExportDataRootElementString(rootElement);
098            }
099    
100            @Override
101            protected PortletPreferences doImportData(
102                            PortletDataContext portletDataContext, String portletId,
103                            PortletPreferences portletPreferences, String data)
104                    throws Exception {
105    
106                    portletDataContext.importPermissions(
107                            "com.liferay.portlet.dynamicdatalist",
108                            portletDataContext.getSourceGroupId(),
109                            portletDataContext.getScopeGroupId());
110    
111                    Element recordSetsElement =
112                            portletDataContext.getImportDataGroupElement(DDLRecordSet.class);
113    
114                    List<Element> recordSetElements = recordSetsElement.elements();
115    
116                    for (Element recordSetElement : recordSetElements) {
117                            StagedModelDataHandlerUtil.importStagedModel(
118                                    portletDataContext, recordSetElement);
119                    }
120    
121                    return portletPreferences;
122            }
123    
124    }