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.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.dao.orm.DynamicQueryFactoryUtil;
020    import com.liferay.portal.kernel.dao.orm.ExportActionableDynamicQuery;
021    import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
022    import com.liferay.portal.kernel.dao.orm.Property;
023    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
024    import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
025    import com.liferay.portal.kernel.exception.PortalException;
026    import com.liferay.portal.kernel.exception.SystemException;
027    import com.liferay.portal.kernel.lar.BasePortletDataHandler;
028    import com.liferay.portal.kernel.lar.PortletDataContext;
029    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
030    import com.liferay.portal.kernel.lar.StagedModelDataHandler;
031    import com.liferay.portal.kernel.lar.StagedModelDataHandlerRegistryUtil;
032    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
033    import com.liferay.portal.kernel.lar.StagedModelType;
034    import com.liferay.portal.kernel.lar.xstream.XStreamAliasRegistryUtil;
035    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
036    import com.liferay.portal.kernel.xml.Element;
037    import com.liferay.portal.util.PortalUtil;
038    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
039    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
040    import com.liferay.portlet.dynamicdatalists.model.DDLRecordVersion;
041    import com.liferay.portlet.dynamicdatalists.model.impl.DDLRecordImpl;
042    import com.liferay.portlet.dynamicdatalists.model.impl.DDLRecordSetImpl;
043    import com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalServiceUtil;
044    import com.liferay.portlet.dynamicdatalists.service.DDLRecordSetLocalServiceUtil;
045    import com.liferay.portlet.dynamicdatalists.service.permission.DDLPermission;
046    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
047    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
048    import com.liferay.portlet.dynamicdatamapping.model.impl.DDMStructureImpl;
049    import com.liferay.portlet.dynamicdatamapping.model.impl.DDMTemplateImpl;
050    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
051    
052    import java.util.ArrayList;
053    import java.util.List;
054    
055    import javax.portlet.PortletPreferences;
056    
057    /**
058     * @author Michael C. Han
059     */
060    public class DDLPortletDataHandler extends BasePortletDataHandler {
061    
062            public static final String NAMESPACE = "dynamic_data_lists";
063    
064            public DDLPortletDataHandler() {
065                    setDataLocalized(true);
066                    setDeletionSystemEventStagedModelTypes(
067                            new StagedModelType(DDLRecord.class),
068                            new StagedModelType(DDLRecordSet.class));
069                    setExportControls(
070                            new PortletDataHandlerBoolean(
071                                    NAMESPACE, "record-sets", true, false, null,
072                                    DDLRecordSet.class.getName()),
073                            new PortletDataHandlerBoolean(
074                                    NAMESPACE, "records", true, false, null,
075                                    DDLRecord.class.getName()),
076                            new PortletDataHandlerBoolean(
077                                    NAMESPACE, "data-definitions", true, false, null,
078                                    DDMStructure.class.getName(), DDLRecordSet.class.getName()));
079    
080                    XStreamAliasRegistryUtil.register(DDLRecordImpl.class, "DDLRecord");
081                    XStreamAliasRegistryUtil.register(
082                            DDLRecordSetImpl.class, "DDLRecordSet");
083                    XStreamAliasRegistryUtil.register(
084                            DDMStructureImpl.class, "DDMStructure");
085                    XStreamAliasRegistryUtil.register(DDMTemplateImpl.class, "DDMTemplate");
086            }
087    
088            @Override
089            protected PortletPreferences doDeleteData(
090                            PortletDataContext portletDataContext, String portletId,
091                            PortletPreferences portletPreferences)
092                    throws Exception {
093    
094                    if (portletDataContext.addPrimaryKey(
095                                    DDLPortletDataHandler.class, "deleteData")) {
096    
097                            return portletPreferences;
098                    }
099    
100                    DDLRecordSetLocalServiceUtil.deleteRecordSets(
101                            portletDataContext.getScopeGroupId());
102    
103                    return portletPreferences;
104            }
105    
106            @Override
107            protected String doExportData(
108                            final PortletDataContext portletDataContext, String portletId,
109                            PortletPreferences portletPreferences)
110                    throws Exception {
111    
112                    portletDataContext.addPortletPermissions(DDLPermission.RESOURCE_NAME);
113    
114                    Element rootElement = addExportDataRootElement(portletDataContext);
115    
116                    if (portletDataContext.getBooleanParameter(
117                                    NAMESPACE, "data-definitions")) {
118    
119                            List<DDMTemplate> ddmTemplates = new ArrayList<DDMTemplate>();
120    
121                            ActionableDynamicQuery ddmStructureActionableDynamicQuery =
122                                    getDDMStructureActionableDynamicQuery(
123                                            portletDataContext, ddmTemplates);
124    
125                            ddmStructureActionableDynamicQuery.performActions();
126    
127                            for (DDMTemplate ddmTemplate : ddmTemplates) {
128                                    StagedModelDataHandlerUtil.exportStagedModel(
129                                            portletDataContext, ddmTemplate);
130                            }
131                    }
132    
133                    if (portletDataContext.getBooleanParameter(NAMESPACE, "record-sets")) {
134                            ActionableDynamicQuery recordSetActionableDynamicQuery =
135                                    DDLRecordSetLocalServiceUtil.getExportActionableDynamicQuery(
136                                            portletDataContext);
137    
138                            recordSetActionableDynamicQuery.performActions();
139                    }
140    
141                    if (portletDataContext.getBooleanParameter(NAMESPACE, "records")) {
142                            ActionableDynamicQuery recordActionableDynamicQuery =
143                                    getRecordActionableDynamicQuery(portletDataContext);
144    
145                            recordActionableDynamicQuery.performActions();
146                    }
147    
148                    return getExportDataRootElementString(rootElement);
149            }
150    
151            @Override
152            protected PortletPreferences doImportData(
153                            PortletDataContext portletDataContext, String portletId,
154                            PortletPreferences portletPreferences, String data)
155                    throws Exception {
156    
157                    portletDataContext.importPortletPermissions(
158                            DDLPermission.RESOURCE_NAME);
159    
160                    if (portletDataContext.getBooleanParameter(
161                                    NAMESPACE, "data-definitions")) {
162    
163                            Element ddmStructuresElement =
164                                    portletDataContext.getImportDataGroupElement(
165                                            DDMStructure.class);
166    
167                            List<Element> ddmStructureElements =
168                                    ddmStructuresElement.elements();
169    
170                            for (Element ddmStructureElement : ddmStructureElements) {
171                                    StagedModelDataHandlerUtil.importStagedModel(
172                                            portletDataContext, ddmStructureElement);
173                            }
174    
175                            Element ddmTemplatesElement =
176                                    portletDataContext.getImportDataGroupElement(DDMTemplate.class);
177    
178                            List<Element> ddmTemplateElements = ddmTemplatesElement.elements();
179    
180                            for (Element ddmTemplateElement : ddmTemplateElements) {
181                                    StagedModelDataHandlerUtil.importStagedModel(
182                                            portletDataContext, ddmTemplateElement);
183                            }
184                    }
185    
186                    if (portletDataContext.getBooleanParameter(NAMESPACE, "record-sets")) {
187                            Element recordSetsElement =
188                                    portletDataContext.getImportDataGroupElement(
189                                            DDLRecordSet.class);
190    
191                            List<Element> recordSetElements = recordSetsElement.elements();
192    
193                            for (Element recordSetElement : recordSetElements) {
194                                    StagedModelDataHandlerUtil.importStagedModel(
195                                            portletDataContext, recordSetElement);
196                            }
197                    }
198    
199                    if (portletDataContext.getBooleanParameter(NAMESPACE, "records")) {
200                            Element recordsElement =
201                                    portletDataContext.getImportDataGroupElement(DDLRecord.class);
202    
203                            List<Element> recordElements = recordsElement.elements();
204    
205                            for (Element recordElement : recordElements) {
206                                    StagedModelDataHandlerUtil.importStagedModel(
207                                            portletDataContext, recordElement);
208                            }
209                    }
210    
211                    return portletPreferences;
212            }
213    
214            @Override
215            protected void doPrepareManifestSummary(
216                            PortletDataContext portletDataContext,
217                            PortletPreferences portletPreferences)
218                    throws Exception {
219    
220                    List<DDMTemplate> ddmTemplates = new ArrayList<DDMTemplate>();
221    
222                    ActionableDynamicQuery ddmStructureActionableDynamicQuery =
223                            getDDMStructureActionableDynamicQuery(
224                                    portletDataContext, ddmTemplates);
225    
226                    ddmStructureActionableDynamicQuery.performCount();
227    
228                    ActionableDynamicQuery recordSetActionableDynamicQuery =
229                            DDLRecordSetLocalServiceUtil.getExportActionableDynamicQuery(
230                                    portletDataContext);
231    
232                    recordSetActionableDynamicQuery.performCount();
233    
234                    ActionableDynamicQuery recordActionableDynamicQuery =
235                            getRecordActionableDynamicQuery(portletDataContext);
236    
237                    recordActionableDynamicQuery.performCount();
238            }
239    
240            protected ActionableDynamicQuery getDDMStructureActionableDynamicQuery(
241                    final PortletDataContext portletDataContext,
242                    final List<DDMTemplate> ddmTemplates) {
243    
244                    ExportActionableDynamicQuery exportActionableDynamicQuery =
245                            DDMStructureLocalServiceUtil.getExportActionableDynamicQuery(
246                                    portletDataContext);
247    
248                    final ActionableDynamicQuery.AddCriteriaMethod addCriteriaMethod =
249                            exportActionableDynamicQuery.getAddCriteriaMethod();
250    
251                    exportActionableDynamicQuery.setAddCriteriaMethod(
252                            new ActionableDynamicQuery.AddCriteriaMethod() {
253    
254                                    @Override
255                                    public void addCriteria(DynamicQuery dynamicQuery) {
256                                            addCriteriaMethod.addCriteria(dynamicQuery);
257    
258                                            Property classNameIdProperty = PropertyFactoryUtil.forName(
259                                                    "classNameId");
260    
261                                            long classNameId = PortalUtil.getClassNameId(
262                                                    DDLRecordSet.class);
263    
264                                            dynamicQuery.add(classNameIdProperty.eq(classNameId));
265                                    }
266    
267                            });
268                    exportActionableDynamicQuery.setPerformActionMethod(
269                            new ActionableDynamicQuery.PerformActionMethod() {
270    
271                                    @Override
272                                    public void performAction(Object object)
273                                            throws PortalException {
274    
275                                            DDMStructure ddmStructure = (DDMStructure)object;
276    
277                                            StagedModelDataHandlerUtil.exportStagedModel(
278                                                    portletDataContext, ddmStructure);
279    
280                                            try {
281                                                    ddmTemplates.addAll(ddmStructure.getTemplates());
282                                            }
283                                            catch (SystemException se) {
284                                            }
285                                    }
286    
287                            });
288                    exportActionableDynamicQuery.setStagedModelType(
289                            new StagedModelType(
290                                    DDMStructure.class.getName(), DDLRecordSet.class.getName()));
291    
292                    return exportActionableDynamicQuery;
293            }
294    
295            protected ActionableDynamicQuery getRecordActionableDynamicQuery(
296                    final PortletDataContext portletDataContext) {
297    
298                    ActionableDynamicQuery actionableDynamicQuery =
299                            DDLRecordLocalServiceUtil.getExportActionableDynamicQuery(
300                                    portletDataContext);
301    
302                    final ActionableDynamicQuery.AddCriteriaMethod addCriteriaMethod =
303                            actionableDynamicQuery.getAddCriteriaMethod();
304    
305                    actionableDynamicQuery.setAddCriteriaMethod(
306                            new ActionableDynamicQuery.AddCriteriaMethod() {
307    
308                                    @Override
309                                    public void addCriteria(DynamicQuery dynamicQuery) {
310                                            addCriteriaMethod.addCriteria(dynamicQuery);
311    
312                                            Property recordIdProperty = PropertyFactoryUtil.forName(
313                                                    "recordId");
314    
315                                            DynamicQuery recordVersionDynamicQuery =
316                                                    DynamicQueryFactoryUtil.forClass(
317                                                            DDLRecordVersion.class, "recordVersion",
318                                                            PortalClassLoaderUtil.getClassLoader());
319    
320                                            recordVersionDynamicQuery.setProjection(
321                                                    ProjectionFactoryUtil.property("recordId"));
322    
323                                            Property statusProperty = PropertyFactoryUtil.forName(
324                                                    "status");
325    
326                                            StagedModelDataHandler<?> stagedModelDataHandler =
327                                                    StagedModelDataHandlerRegistryUtil.
328                                                            getStagedModelDataHandler(
329                                                                    DDLRecord.class.getName());
330    
331                                            recordVersionDynamicQuery.add(
332                                                    statusProperty.in(
333                                                            stagedModelDataHandler.getExportableStatuses()));
334    
335                                            recordVersionDynamicQuery.add(
336                                                    RestrictionsFactoryUtil.eqProperty(
337                                                            "recordVersion.version", "version"));
338    
339                                            recordVersionDynamicQuery.add(
340                                                    RestrictionsFactoryUtil.eqProperty(
341                                                            "recordVersion.recordId", "recordId"));
342    
343                                            dynamicQuery.add(
344                                                    recordIdProperty.in(recordVersionDynamicQuery));
345                                    }
346    
347                            });
348    
349                    return actionableDynamicQuery;
350            }
351    
352    }