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.documentlibrary.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.util.GetterUtil;
020    import com.liferay.portal.kernel.util.MapUtil;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.model.Group;
024    import com.liferay.portal.model.UserConstants;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.UserLocalServiceUtil;
027    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
028    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
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    import com.liferay.portlet.exportimport.lar.BaseStagedModelDataHandler;
034    import com.liferay.portlet.exportimport.lar.ExportImportPathUtil;
035    import com.liferay.portlet.exportimport.lar.PortletDataContext;
036    import com.liferay.portlet.exportimport.lar.PortletDataException;
037    import com.liferay.portlet.exportimport.lar.StagedModelDataHandlerUtil;
038    import com.liferay.portlet.exportimport.lar.StagedModelModifiedDateComparator;
039    
040    import java.util.HashMap;
041    import java.util.List;
042    import java.util.Map;
043    
044    /**
045     * @author Mate Thurzo
046     */
047    public class DLFileEntryTypeStagedModelDataHandler
048            extends BaseStagedModelDataHandler<DLFileEntryType> {
049    
050            public static final String[] CLASS_NAMES =
051                    {DLFileEntryType.class.getName()};
052    
053            @Override
054            public void deleteStagedModel(DLFileEntryType fileEntryType)
055                    throws PortalException {
056    
057                    DLFileEntryTypeLocalServiceUtil.deleteFileEntryType(fileEntryType);
058            }
059    
060            @Override
061            public void deleteStagedModel(
062                            String uuid, long groupId, String className, String extraData)
063                    throws PortalException {
064    
065                    DLFileEntryType dlFileEntryType = fetchStagedModelByUuidAndGroupId(
066                            uuid, groupId);
067    
068                    if (dlFileEntryType != null) {
069                            deleteStagedModel(dlFileEntryType);
070                    }
071            }
072    
073            @Override
074            public DLFileEntryType fetchStagedModelByUuidAndGroupId(
075                    String uuid, long groupId) {
076    
077                    return DLFileEntryTypeLocalServiceUtil.
078                            fetchDLFileEntryTypeByUuidAndGroupId(uuid, groupId);
079            }
080    
081            @Override
082            public List<DLFileEntryType> fetchStagedModelsByUuidAndCompanyId(
083                    String uuid, long companyId) {
084    
085                    return DLFileEntryTypeLocalServiceUtil.
086                            getDLFileEntryTypesByUuidAndCompanyId(
087                                    uuid, companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
088                                    new StagedModelModifiedDateComparator<DLFileEntryType>());
089            }
090    
091            @Override
092            public String[] getClassNames() {
093                    return CLASS_NAMES;
094            }
095    
096            @Override
097            public Map<String, String> getReferenceAttributes(
098                    PortletDataContext portletDataContext, DLFileEntryType fileEntryType) {
099    
100                    Map<String, String> referenceAttributes = new HashMap<>();
101    
102                    referenceAttributes.put(
103                            "file-entry-type-key", fileEntryType.getFileEntryTypeKey());
104    
105                    long defaultUserId = UserConstants.USER_ID_DEFAULT;
106    
107                    try {
108                            defaultUserId = UserLocalServiceUtil.getDefaultUserId(
109                                    fileEntryType.getCompanyId());
110                    }
111                    catch (Exception e) {
112                    }
113    
114                    boolean preloaded = false;
115    
116                    if ((fileEntryType.getFileEntryTypeId() ==
117                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT) ||
118                            (defaultUserId == fileEntryType.getUserId())) {
119    
120                            preloaded = true;
121                    }
122    
123                    referenceAttributes.put("preloaded", String.valueOf(preloaded));
124    
125                    return referenceAttributes;
126            }
127    
128            @Override
129            public void importMissingReference(
130                            PortletDataContext portletDataContext, Element referenceElement)
131                    throws PortletDataException {
132    
133                    importMissingGroupReference(portletDataContext, referenceElement);
134    
135                    String uuid = referenceElement.attributeValue("uuid");
136    
137                    Map<Long, Long> groupIds =
138                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
139                                    Group.class);
140    
141                    long groupId = GetterUtil.getLong(
142                            referenceElement.attributeValue("group-id"));
143    
144                    groupId = MapUtil.getLong(groupIds, groupId);
145    
146                    String fileEntryTypeKey = referenceElement.attributeValue(
147                            "file-entry-type-key");
148                    boolean preloaded = GetterUtil.getBoolean(
149                            referenceElement.attributeValue("preloaded"));
150    
151                    DLFileEntryType existingFileEntryType = null;
152    
153                    existingFileEntryType = fetchExistingFileEntryType(
154                            uuid, groupId, fileEntryTypeKey, preloaded);
155    
156                    Map<Long, Long> fileEntryTypeIds =
157                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
158                                    DLFileEntryType.class);
159    
160                    long fileEntryTypeId = GetterUtil.getLong(
161                            referenceElement.attributeValue("class-pk"));
162    
163                    fileEntryTypeIds.put(
164                            fileEntryTypeId, existingFileEntryType.getFileEntryTypeId());
165            }
166    
167            @Override
168            public boolean validateReference(
169                    PortletDataContext portletDataContext, Element referenceElement) {
170    
171                    validateMissingGroupReference(portletDataContext, referenceElement);
172    
173                    String uuid = referenceElement.attributeValue("uuid");
174    
175                    Map<Long, Long> groupIds =
176                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
177                                    Group.class);
178    
179                    long groupId = GetterUtil.getLong(
180                            referenceElement.attributeValue("group-id"));
181    
182                    groupId = MapUtil.getLong(groupIds, groupId);
183    
184                    String fileEntryTypeKey = referenceElement.attributeValue(
185                            "file-entry-type-key");
186                    boolean preloaded = GetterUtil.getBoolean(
187                            referenceElement.attributeValue("preloaded"));
188    
189                    DLFileEntryType existingFileEntryType = fetchExistingFileEntryType(
190                            uuid, groupId, fileEntryTypeKey, preloaded);
191    
192                    if (existingFileEntryType == null) {
193                            return false;
194                    }
195    
196                    return true;
197            }
198    
199            @Override
200            protected void doExportStagedModel(
201                            PortletDataContext portletDataContext,
202                            DLFileEntryType fileEntryType)
203                    throws Exception {
204    
205                    Element fileEntryTypeElement = portletDataContext.getExportDataElement(
206                            fileEntryType);
207    
208                    List<DDMStructure> ddmStructures = fileEntryType.getDDMStructures();
209    
210                    for (DDMStructure ddmStructure : ddmStructures) {
211                            Element referenceElement =
212                                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
213                                            portletDataContext, fileEntryType, ddmStructure,
214                                            PortletDataContext.REFERENCE_TYPE_STRONG);
215    
216                            referenceElement.addAttribute(
217                                    "structure-id",
218                                    StringUtil.valueOf(ddmStructure.getStructureId()));
219                    }
220    
221                    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
222                            fileEntryType.getCompanyId());
223    
224                    if (defaultUserId == fileEntryType.getUserId()) {
225                            fileEntryTypeElement.addAttribute("preloaded", "true");
226                    }
227    
228                    portletDataContext.addClassedModel(
229                            fileEntryTypeElement,
230                            ExportImportPathUtil.getModelPath(fileEntryType), fileEntryType);
231            }
232    
233            @Override
234            protected void doImportStagedModel(
235                            PortletDataContext portletDataContext,
236                            DLFileEntryType fileEntryType)
237                    throws Exception {
238    
239                    long userId = portletDataContext.getUserId(fileEntryType.getUserUuid());
240    
241                    List<Element> ddmStructureReferenceElements =
242                            portletDataContext.getReferenceElements(
243                                    fileEntryType, DDMStructure.class);
244    
245                    long[] ddmStructureIdsArray =
246                            new long[ddmStructureReferenceElements.size()];
247    
248                    Map<Long, Long> ddmStructureIds =
249                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
250                                    DDMStructure.class);
251    
252                    for (int i = 0; i < ddmStructureReferenceElements.size(); i++) {
253                            Element ddmStructureReferenceElement =
254                                    ddmStructureReferenceElements.get(i);
255    
256                            long ddmStructureId = GetterUtil.getLong(
257                                    ddmStructureReferenceElement.attributeValue("class-pk"));
258    
259                            ddmStructureIdsArray[i] = MapUtil.getLong(
260                                    ddmStructureIds, ddmStructureId);
261                    }
262    
263                    ServiceContext serviceContext = portletDataContext.createServiceContext(
264                            fileEntryType);
265    
266                    DLFileEntryType importedDLFileEntryType = null;
267    
268                    Element element = portletDataContext.getImportDataStagedModelElement(
269                            fileEntryType);
270    
271                    boolean preloaded = GetterUtil.getBoolean(
272                            element.attributeValue("preloaded"));
273    
274                    if (portletDataContext.isDataStrategyMirror()) {
275                            DLFileEntryType existingDLFileEntryType =
276                                    fetchExistingFileEntryType(
277                                            fileEntryType.getUuid(),
278                                            portletDataContext.getScopeGroupId(),
279                                            fileEntryType.getFileEntryTypeKey(), preloaded);
280    
281                            if (existingDLFileEntryType == null) {
282                                    serviceContext.setUuid(fileEntryType.getUuid());
283    
284                                    importedDLFileEntryType =
285                                            DLFileEntryTypeLocalServiceUtil.addFileEntryType(
286                                                    userId, portletDataContext.getScopeGroupId(),
287                                                    fileEntryType.getFileEntryTypeKey(),
288                                                    fileEntryType.getNameMap(),
289                                                    fileEntryType.getDescriptionMap(), ddmStructureIdsArray,
290                                                    serviceContext);
291                            }
292                            else {
293                                    DLFileEntryTypeLocalServiceUtil.updateFileEntryType(
294                                            userId, existingDLFileEntryType.getFileEntryTypeId(),
295                                            fileEntryType.getNameMap(),
296                                            fileEntryType.getDescriptionMap(), ddmStructureIdsArray,
297                                            serviceContext);
298    
299                                    importedDLFileEntryType =
300                                            DLFileEntryTypeLocalServiceUtil.fetchDLFileEntryType(
301                                                    existingDLFileEntryType.getFileEntryTypeId());
302                            }
303                    }
304                    else {
305                            importedDLFileEntryType =
306                                    DLFileEntryTypeLocalServiceUtil.addFileEntryType(
307                                            userId, portletDataContext.getScopeGroupId(),
308                                            fileEntryType.getFileEntryTypeKey(),
309                                            fileEntryType.getNameMap(),
310                                            fileEntryType.getDescriptionMap(), ddmStructureIdsArray,
311                                            serviceContext);
312                    }
313    
314                    portletDataContext.importClassedModel(
315                            fileEntryType, importedDLFileEntryType);
316    
317                    if (preloaded) {
318                            return;
319                    }
320    
321                    String importedDLFileEntryDDMStructureKey = DLUtil.getDDMStructureKey(
322                            importedDLFileEntryType);
323    
324                    List<DDMStructure> importedDDMStructures =
325                            importedDLFileEntryType.getDDMStructures();
326    
327                    for (DDMStructure importedDDMStructure : importedDDMStructures) {
328                            String ddmStructureKey = importedDDMStructure.getStructureKey();
329    
330                            if (!DLUtil.isAutoGeneratedDLFileEntryTypeDDMStructureKey(
331                                            ddmStructureKey)) {
332    
333                                    continue;
334                            }
335    
336                            if (ddmStructureKey.equals(importedDLFileEntryDDMStructureKey)) {
337                                    continue;
338                            }
339    
340                            importedDDMStructure.setStructureKey(
341                                    importedDLFileEntryDDMStructureKey);
342    
343                            DDMStructureLocalServiceUtil.updateDDMStructure(
344                                    importedDDMStructure);
345                    }
346            }
347    
348            protected DLFileEntryType fetchExistingFileEntryType(
349                    String uuid, long groupId, String fileEntryTypeKey, boolean preloaded) {
350    
351                    DLFileEntryType existingDLFileEntryType = null;
352    
353                    if (!preloaded) {
354                            existingDLFileEntryType = fetchStagedModelByUuidAndGroupId(
355                                    uuid, groupId);
356                    }
357                    else {
358                            existingDLFileEntryType =
359                                    DLFileEntryTypeLocalServiceUtil.fetchFileEntryType(
360                                            groupId, fileEntryTypeKey);
361                    }
362    
363                    return existingDLFileEntryType;
364            }
365    
366    }