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