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.documentlibrary.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.GetterUtil;
024    import com.liferay.portal.kernel.util.MapUtil;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.xml.Element;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
029    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
030    import com.liferay.portlet.documentlibrary.util.DLUtil;
031    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
032    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
033    
034    import java.util.List;
035    import java.util.Map;
036    
037    /**
038     * @author Mate Thurzo
039     */
040    public class DLFileEntryTypeStagedModelDataHandler
041            extends BaseStagedModelDataHandler<DLFileEntryType> {
042    
043            public static final String[] CLASS_NAMES =
044                    {DLFileEntryType.class.getName()};
045    
046            @Override
047            public void deleteStagedModel(
048                            String uuid, long groupId, String className, String extraData)
049                    throws PortalException, SystemException {
050    
051                    DLFileEntryType dlFileEntryType =
052                            DLFileEntryTypeLocalServiceUtil.
053                                    fetchDLFileEntryTypeByUuidAndGroupId(uuid, groupId);
054    
055                    if (dlFileEntryType != null) {
056                            DLFileEntryTypeLocalServiceUtil.deleteFileEntryType(
057                                    dlFileEntryType);
058                    }
059            }
060    
061            @Override
062            public String[] getClassNames() {
063                    return CLASS_NAMES;
064            }
065    
066            @Override
067            protected void doExportStagedModel(
068                            PortletDataContext portletDataContext,
069                            DLFileEntryType fileEntryType)
070                    throws Exception {
071    
072                    Element fileEntryTypeElement = portletDataContext.getExportDataElement(
073                            fileEntryType);
074    
075                    List<DDMStructure> ddmStructures = fileEntryType.getDDMStructures();
076    
077                    for (DDMStructure ddmStructure : ddmStructures) {
078                            Element referenceElement =
079                                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
080                                            portletDataContext, fileEntryType, ddmStructure,
081                                            PortletDataContext.REFERENCE_TYPE_STRONG);
082    
083                            referenceElement.addAttribute(
084                                    "structure-id",
085                                    StringUtil.valueOf(ddmStructure.getStructureId()));
086                    }
087    
088                    portletDataContext.addClassedModel(
089                            fileEntryTypeElement,
090                            ExportImportPathUtil.getModelPath(fileEntryType), fileEntryType);
091            }
092    
093            @Override
094            protected void doImportCompanyStagedModel(
095                            PortletDataContext portletDataContext,
096                            DLFileEntryType fileEntryType)
097                    throws Exception {
098    
099                    DLFileEntryType existingFileEntryType =
100                            DLFileEntryTypeLocalServiceUtil.
101                                    fetchDLFileEntryTypeByUuidAndGroupId(
102                                            fileEntryType.getUuid(),
103                                            portletDataContext.getCompanyGroupId());
104    
105                    Map<Long, Long> fileEntryTypeIds =
106                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
107                                    DLFileEntryType.class);
108    
109                    fileEntryTypeIds.put(
110                            fileEntryType.getFileEntryTypeId(),
111                            existingFileEntryType.getFileEntryTypeId());
112            }
113    
114            @Override
115            protected void doImportStagedModel(
116                            PortletDataContext portletDataContext,
117                            DLFileEntryType fileEntryType)
118                    throws Exception {
119    
120                    long userId = portletDataContext.getUserId(fileEntryType.getUserUuid());
121    
122                    List<Element> ddmStructureElements =
123                            portletDataContext.getReferenceDataElements(
124                                    fileEntryType, DDMStructure.class);
125    
126                    for (Element ddmStructureElement : ddmStructureElements) {
127                            StagedModelDataHandlerUtil.importReferenceStagedModel(
128                                    portletDataContext, ddmStructureElement);
129                    }
130    
131                    List<Element> ddmStructureReferenceElements =
132                            portletDataContext.getReferenceElements(
133                                    fileEntryType, DDMStructure.class);
134    
135                    long[] ddmStructureIdsArray =
136                            new long[ddmStructureReferenceElements.size()];
137    
138                    Map<Long, Long> ddmStructureIds =
139                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
140                                    DDMStructure.class);
141    
142                    for (int i = 0; i < ddmStructureReferenceElements.size(); i++) {
143                            Element ddmStructureReferenceElement =
144                                    ddmStructureReferenceElements.get(i);
145    
146                            long ddmStructureId = GetterUtil.getLong(
147                                    ddmStructureReferenceElement.attributeValue("class-pk"));
148    
149                            ddmStructureIdsArray[i] = MapUtil.getLong(
150                                    ddmStructureIds, ddmStructureId);
151                    }
152    
153                    ServiceContext serviceContext = portletDataContext.createServiceContext(
154                            fileEntryType);
155    
156                    DLFileEntryType importedDLFileEntryType = null;
157    
158                    if (portletDataContext.isDataStrategyMirror()) {
159                            DLFileEntryType existingDLFileEntryType =
160                                    DLFileEntryTypeLocalServiceUtil.
161                                            fetchDLFileEntryTypeByUuidAndGroupId(
162                                                    fileEntryType.getUuid(),
163                                                    portletDataContext.getScopeGroupId());
164    
165                            if (existingDLFileEntryType == null) {
166                                    serviceContext.setUuid(fileEntryType.getUuid());
167    
168                                    importedDLFileEntryType =
169                                            DLFileEntryTypeLocalServiceUtil.addFileEntryType(
170                                                    userId, portletDataContext.getScopeGroupId(),
171                                                    fileEntryType.getFileEntryTypeKey(),
172                                                    fileEntryType.getNameMap(),
173                                                    fileEntryType.getDescriptionMap(), ddmStructureIdsArray,
174                                                    serviceContext);
175                            }
176                            else {
177                                    DLFileEntryTypeLocalServiceUtil.updateFileEntryType(
178                                            userId, existingDLFileEntryType.getFileEntryTypeId(),
179                                            fileEntryType.getNameMap(),
180                                            fileEntryType.getDescriptionMap(), ddmStructureIdsArray,
181                                            serviceContext);
182    
183                                    importedDLFileEntryType =
184                                            DLFileEntryTypeLocalServiceUtil.fetchDLFileEntryType(
185                                                    existingDLFileEntryType.getFileEntryTypeId());
186                            }
187                    }
188                    else {
189                            importedDLFileEntryType =
190                                    DLFileEntryTypeLocalServiceUtil.addFileEntryType(
191                                            userId, portletDataContext.getScopeGroupId(),
192                                            fileEntryType.getFileEntryTypeKey(),
193                                            fileEntryType.getNameMap(),
194                                            fileEntryType.getDescriptionMap(), ddmStructureIdsArray,
195                                            serviceContext);
196                    }
197    
198                    portletDataContext.importClassedModel(
199                            fileEntryType, importedDLFileEntryType);
200    
201                    String importedDLFileEntryDDMStructureKey = DLUtil.getDDMStructureKey(
202                            importedDLFileEntryType);
203    
204                    List<DDMStructure> importedDDMStructures =
205                            importedDLFileEntryType.getDDMStructures();
206    
207                    for (DDMStructure importedDDMStructure : importedDDMStructures) {
208                            String ddmStructureKey = importedDDMStructure.getStructureKey();
209    
210                            if (!DLUtil.isAutoGeneratedDLFileEntryTypeDDMStructureKey(
211                                            ddmStructureKey)) {
212    
213                                    continue;
214                            }
215    
216                            if (ddmStructureKey.equals(importedDLFileEntryDDMStructureKey)) {
217                                    continue;
218                            }
219    
220                            importedDDMStructure.setStructureKey(
221                                    importedDLFileEntryDDMStructureKey);
222    
223                            DDMStructureLocalServiceUtil.updateDDMStructure(
224                                    importedDDMStructure);
225                    }
226            }
227    
228            @Override
229            protected boolean validateMissingReference(
230                            String uuid, long companyId, long groupId)
231                    throws Exception {
232    
233                    DLFileEntryType dlFileEntryType =
234                            DLFileEntryTypeLocalServiceUtil.
235                                    fetchDLFileEntryTypeByUuidAndGroupId(uuid, groupId);
236    
237                    if (dlFileEntryType == null) {
238                            return false;
239                    }
240    
241                    return true;
242            }
243    
244    }