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