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                            DLPortletDataHandler.NAMESPACE);
092            }
093    
094            @Override
095            protected void doImportCompanyStagedModel(
096                            PortletDataContext portletDataContext,
097                            DLFileEntryType fileEntryType)
098                    throws Exception {
099    
100                    DLFileEntryType existingFileEntryType =
101                            DLFileEntryTypeLocalServiceUtil.
102                                    fetchDLFileEntryTypeByUuidAndGroupId(
103                                            fileEntryType.getUuid(),
104                                            portletDataContext.getCompanyGroupId());
105    
106                    Map<Long, Long> fileEntryTypeIds =
107                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
108                                    DLFileEntryType.class);
109    
110                    fileEntryTypeIds.put(
111                            fileEntryType.getFileEntryTypeId(),
112                            existingFileEntryType.getFileEntryTypeId());
113            }
114    
115            @Override
116            protected void doImportStagedModel(
117                            PortletDataContext portletDataContext,
118                            DLFileEntryType fileEntryType)
119                    throws Exception {
120    
121                    long userId = portletDataContext.getUserId(fileEntryType.getUserUuid());
122    
123                    List<Element> ddmStructureElements =
124                            portletDataContext.getReferenceDataElements(
125                                    fileEntryType, DDMStructure.class);
126    
127                    for (Element ddmStructureElement : ddmStructureElements) {
128                            StagedModelDataHandlerUtil.importReferenceStagedModel(
129                                    portletDataContext, ddmStructureElement);
130                    }
131    
132                    List<Element> ddmStructureReferenceElements =
133                            portletDataContext.getReferenceElements(
134                                    fileEntryType, DDMStructure.class);
135    
136                    long[] ddmStructureIdsArray =
137                            new long[ddmStructureReferenceElements.size()];
138    
139                    Map<Long, Long> ddmStructureIds =
140                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
141                                    DDMStructure.class);
142    
143                    for (int i = 0; i < ddmStructureReferenceElements.size(); i++) {
144                            Element ddmStructureReferenceElement =
145                                    ddmStructureReferenceElements.get(i);
146    
147                            long ddmStructureId = GetterUtil.getLong(
148                                    ddmStructureReferenceElement.attributeValue("class-pk"));
149    
150                            ddmStructureIdsArray[i] = MapUtil.getLong(
151                                    ddmStructureIds, ddmStructureId);
152                    }
153    
154                    ServiceContext serviceContext = portletDataContext.createServiceContext(
155                            fileEntryType, DLPortletDataHandler.NAMESPACE);
156    
157                    DLFileEntryType importedDLFileEntryType = null;
158    
159                    if (portletDataContext.isDataStrategyMirror()) {
160                            DLFileEntryType existingDLFileEntryType =
161                                    DLFileEntryTypeLocalServiceUtil.
162                                            fetchDLFileEntryTypeByUuidAndGroupId(
163                                                    fileEntryType.getUuid(),
164                                                    portletDataContext.getScopeGroupId());
165    
166                            if (existingDLFileEntryType == null) {
167                                    serviceContext.setUuid(fileEntryType.getUuid());
168    
169                                    importedDLFileEntryType =
170                                            DLFileEntryTypeLocalServiceUtil.addFileEntryType(
171                                                    userId, portletDataContext.getScopeGroupId(),
172                                                    fileEntryType.getFileEntryTypeKey(),
173                                                    fileEntryType.getNameMap(),
174                                                    fileEntryType.getDescriptionMap(), ddmStructureIdsArray,
175                                                    serviceContext);
176                            }
177                            else {
178                                    DLFileEntryTypeLocalServiceUtil.updateFileEntryType(
179                                            userId, existingDLFileEntryType.getFileEntryTypeId(),
180                                            fileEntryType.getNameMap(),
181                                            fileEntryType.getDescriptionMap(), ddmStructureIdsArray,
182                                            serviceContext);
183                            }
184                    }
185                    else {
186                            importedDLFileEntryType =
187                                    DLFileEntryTypeLocalServiceUtil.addFileEntryType(
188                                            userId, portletDataContext.getScopeGroupId(),
189                                            fileEntryType.getFileEntryTypeKey(),
190                                            fileEntryType.getNameMap(),
191                                            fileEntryType.getDescriptionMap(), ddmStructureIdsArray,
192                                            serviceContext);
193                    }
194    
195                    portletDataContext.importClassedModel(
196                            fileEntryType, importedDLFileEntryType,
197                            DLPortletDataHandler.NAMESPACE);
198    
199                    String importedDLFileEntryDDMStructureKey = DLUtil.getDDMStructureKey(
200                            importedDLFileEntryType);
201    
202                    List<DDMStructure> importedDDMStructures =
203                            importedDLFileEntryType.getDDMStructures();
204    
205                    for (DDMStructure importedDDMStructure : importedDDMStructures) {
206                            String ddmStructureKey = importedDDMStructure.getStructureKey();
207    
208                            if (!DLUtil.isAutoGeneratedDLFileEntryTypeDDMStructureKey(
209                                            ddmStructureKey)) {
210    
211                                    continue;
212                            }
213    
214                            if (ddmStructureKey.equals(importedDLFileEntryDDMStructureKey)) {
215                                    continue;
216                            }
217    
218                            importedDDMStructure.setStructureKey(
219                                    importedDLFileEntryDDMStructureKey);
220    
221                            DDMStructureLocalServiceUtil.updateDDMStructure(
222                                    importedDDMStructure);
223                    }
224            }
225    
226            @Override
227            protected boolean validateMissingReference(
228                            String uuid, long companyId, long groupId)
229                    throws Exception {
230    
231                    DLFileEntryType dlFileEntryType =
232                            DLFileEntryTypeLocalServiceUtil.
233                                    fetchDLFileEntryTypeByUuidAndGroupId(uuid, groupId);
234    
235                    if (dlFileEntryType == null) {
236                            return false;
237                    }
238    
239                    return true;
240            }
241    
242    }