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