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