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.lar.BaseStagedModelDataHandler;
018    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
019    import com.liferay.portal.kernel.lar.PortletDataContext;
020    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
021    import com.liferay.portal.kernel.util.MapUtil;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
025    import com.liferay.portlet.dynamicdatalists.service.DDLRecordSetLocalServiceUtil;
026    import com.liferay.portlet.dynamicdatalists.service.persistence.DDLRecordSetUtil;
027    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
028    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
029    
030    import java.util.List;
031    import java.util.Map;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class DDLRecordSetStagedModelDataHandler
037            extends BaseStagedModelDataHandler<DDLRecordSet> {
038    
039            public static final String[] CLASS_NAMES = {DDLRecordSet.class.getName()};
040    
041            @Override
042            public String[] getClassNames() {
043                    return CLASS_NAMES;
044            }
045    
046            @Override
047            protected void doExportStagedModel(
048                            PortletDataContext portletDataContext, DDLRecordSet recordSet)
049                    throws Exception {
050    
051                    DDMStructure ddmStructure = recordSet.getDDMStructure();
052    
053                    StagedModelDataHandlerUtil.exportStagedModel(
054                            portletDataContext, ddmStructure);
055    
056                    List<DDMTemplate> ddmTemplates = ddmStructure.getTemplates();
057    
058                    Element recordSetElement =
059                            portletDataContext.getExportDataStagedModelElement(recordSet);
060    
061                    for (DDMTemplate ddmTemplate : ddmTemplates) {
062                            StagedModelDataHandlerUtil.exportStagedModel(
063                                    portletDataContext, ddmTemplate);
064    
065                            portletDataContext.addReferenceElement(
066                                    recordSetElement, ddmTemplate);
067                    }
068    
069                    portletDataContext.addClassedModel(
070                            recordSetElement, ExportImportPathUtil.getModelPath(recordSet),
071                            recordSet, DDLPortletDataHandler.NAMESPACE);
072            }
073    
074            @Override
075            protected void doImportStagedModel(
076                            PortletDataContext portletDataContext, DDLRecordSet recordSet)
077                    throws Exception {
078    
079                    long userId = portletDataContext.getUserId(recordSet.getUserUuid());
080    
081                    Map<Long, Long> ddmStructureIds =
082                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
083                                    DDMStructure.class);
084    
085                    String structurePath = ExportImportPathUtil.getModelPath(
086                            portletDataContext, DDMStructure.class.getName(),
087                            recordSet.getDDMStructureId());
088    
089                    DDMStructure ddmStructure =
090                            (DDMStructure)portletDataContext.getZipEntryAsObject(structurePath);
091    
092                    StagedModelDataHandlerUtil.importStagedModel(
093                            portletDataContext, ddmStructure);
094    
095                    long ddmStructureId = MapUtil.getLong(
096                            ddmStructureIds, recordSet.getDDMStructureId(),
097                            recordSet.getDDMStructureId());
098    
099                    List<Element> ddmTemplateElements =
100                            portletDataContext.getReferencedDataElements(
101                                    recordSet, DDMTemplate.class);
102    
103                    for (Element ddmTemplateElement : ddmTemplateElements) {
104                            StagedModelDataHandlerUtil.importStagedModel(
105                                    portletDataContext, ddmTemplateElement);
106                    }
107    
108                    ServiceContext serviceContext = portletDataContext.createServiceContext(
109                            recordSet, DDLPortletDataHandler.NAMESPACE);
110    
111                    DDLRecordSet importedRecordSet = null;
112    
113                    if (portletDataContext.isDataStrategyMirror()) {
114                            DDLRecordSet existingRecordSet = DDLRecordSetUtil.fetchByUUID_G(
115                                    recordSet.getUuid(), portletDataContext.getScopeGroupId());
116    
117                            if (existingRecordSet == null) {
118                                    serviceContext.setUuid(recordSet.getUuid());
119    
120                                    importedRecordSet = DDLRecordSetLocalServiceUtil.addRecordSet(
121                                            userId, portletDataContext.getScopeGroupId(),
122                                            ddmStructureId, recordSet.getRecordSetKey(),
123                                            recordSet.getNameMap(), recordSet.getDescriptionMap(),
124                                            recordSet.getMinDisplayRows(), recordSet.getScope(),
125                                            serviceContext);
126                            }
127                            else {
128                                    importedRecordSet =
129                                            DDLRecordSetLocalServiceUtil.updateRecordSet(
130                                                    existingRecordSet.getRecordSetId(), ddmStructureId,
131                                                    recordSet.getNameMap(), recordSet.getDescriptionMap(),
132                                                    recordSet.getMinDisplayRows(), serviceContext);
133                            }
134                    }
135                    else {
136                            importedRecordSet = DDLRecordSetLocalServiceUtil.addRecordSet(
137                                    userId, portletDataContext.getScopeGroupId(), ddmStructureId,
138                                    recordSet.getRecordSetKey(), recordSet.getNameMap(),
139                                    recordSet.getDescriptionMap(), recordSet.getMinDisplayRows(),
140                                    recordSet.getScope(), serviceContext);
141                    }
142    
143                    portletDataContext.importClassedModel(
144                            recordSet, importedRecordSet, DDLPortletDataHandler.NAMESPACE);
145            }
146    
147    }