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.exportStagedModel(
083                                    portletDataContext, ddmTemplate);
084    
085                            portletDataContext.addReferenceElement(
086                                    recordSet, recordSetElement, ddmTemplate,
087                                    PortletDataContext.REFERENCE_TYPE_STRONG, false);
088                    }
089    
090                    portletDataContext.addClassedModel(
091                            recordSetElement, ExportImportPathUtil.getModelPath(recordSet),
092                            recordSet, DDLPortletDataHandler.NAMESPACE);
093            }
094    
095            @Override
096            protected void doImportStagedModel(
097                            PortletDataContext portletDataContext, DDLRecordSet recordSet)
098                    throws Exception {
099    
100                    long userId = portletDataContext.getUserId(recordSet.getUserUuid());
101    
102                    String structurePath = ExportImportPathUtil.getModelPath(
103                            portletDataContext, DDMStructure.class.getName(),
104                            recordSet.getDDMStructureId());
105    
106                    DDMStructure ddmStructure =
107                            (DDMStructure)portletDataContext.getZipEntryAsObject(structurePath);
108    
109                    StagedModelDataHandlerUtil.importStagedModel(
110                            portletDataContext, ddmStructure);
111    
112                    Map<Long, Long> ddmStructureIds =
113                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
114                                    DDMStructure.class);
115    
116                    long ddmStructureId = MapUtil.getLong(
117                            ddmStructureIds, recordSet.getDDMStructureId(),
118                            recordSet.getDDMStructureId());
119    
120                    List<Element> ddmTemplateElements =
121                            portletDataContext.getReferenceDataElements(
122                                    recordSet, DDMTemplate.class);
123    
124                    for (Element ddmTemplateElement : ddmTemplateElements) {
125                            StagedModelDataHandlerUtil.importStagedModel(
126                                    portletDataContext, ddmTemplateElement);
127                    }
128    
129                    ServiceContext serviceContext = portletDataContext.createServiceContext(
130                            recordSet, DDLPortletDataHandler.NAMESPACE);
131    
132                    DDLRecordSet importedRecordSet = null;
133    
134                    if (portletDataContext.isDataStrategyMirror()) {
135                            DDLRecordSet existingRecordSet =
136                                    DDLRecordSetLocalServiceUtil.fetchDDLRecordSetByUuidAndGroupId(
137                                            recordSet.getUuid(), portletDataContext.getScopeGroupId());
138    
139                            if (existingRecordSet == null) {
140                                    serviceContext.setUuid(recordSet.getUuid());
141    
142                                    importedRecordSet = DDLRecordSetLocalServiceUtil.addRecordSet(
143                                            userId, portletDataContext.getScopeGroupId(),
144                                            ddmStructureId, recordSet.getRecordSetKey(),
145                                            recordSet.getNameMap(), recordSet.getDescriptionMap(),
146                                            recordSet.getMinDisplayRows(), recordSet.getScope(),
147                                            serviceContext);
148                            }
149                            else {
150                                    importedRecordSet =
151                                            DDLRecordSetLocalServiceUtil.updateRecordSet(
152                                                    existingRecordSet.getRecordSetId(), ddmStructureId,
153                                                    recordSet.getNameMap(), recordSet.getDescriptionMap(),
154                                                    recordSet.getMinDisplayRows(), serviceContext);
155                            }
156                    }
157                    else {
158                            importedRecordSet = DDLRecordSetLocalServiceUtil.addRecordSet(
159                                    userId, portletDataContext.getScopeGroupId(), ddmStructureId,
160                                    recordSet.getRecordSetKey(), recordSet.getNameMap(),
161                                    recordSet.getDescriptionMap(), recordSet.getMinDisplayRows(),
162                                    recordSet.getScope(), serviceContext);
163                    }
164    
165                    portletDataContext.importClassedModel(
166                            recordSet, importedRecordSet, DDLPortletDataHandler.NAMESPACE);
167            }
168    
169    }