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