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.service.impl;
016    
017    import com.liferay.document.library.kernel.exception.DuplicateFileEntryTypeException;
018    import com.liferay.document.library.kernel.exception.NoSuchFolderException;
019    import com.liferay.document.library.kernel.exception.NoSuchMetadataSetException;
020    import com.liferay.document.library.kernel.exception.RequiredFileEntryTypeException;
021    import com.liferay.document.library.kernel.model.DLFileEntry;
022    import com.liferay.document.library.kernel.model.DLFileEntryMetadata;
023    import com.liferay.document.library.kernel.model.DLFileEntryType;
024    import com.liferay.document.library.kernel.model.DLFileEntryTypeConstants;
025    import com.liferay.document.library.kernel.model.DLFileVersion;
026    import com.liferay.document.library.kernel.model.DLFolder;
027    import com.liferay.document.library.kernel.model.DLFolderConstants;
028    import com.liferay.document.library.kernel.util.DLUtil;
029    import com.liferay.dynamic.data.mapping.kernel.DDMForm;
030    import com.liferay.dynamic.data.mapping.kernel.DDMStructure;
031    import com.liferay.dynamic.data.mapping.kernel.DDMStructureLink;
032    import com.liferay.dynamic.data.mapping.kernel.DDMStructureLinkManagerUtil;
033    import com.liferay.dynamic.data.mapping.kernel.DDMStructureManager;
034    import com.liferay.dynamic.data.mapping.kernel.DDMStructureManagerUtil;
035    import com.liferay.dynamic.data.mapping.kernel.StorageEngineManager;
036    import com.liferay.dynamic.data.mapping.kernel.StructureDefinitionException;
037    import com.liferay.portal.kernel.exception.PortalException;
038    import com.liferay.portal.kernel.log.Log;
039    import com.liferay.portal.kernel.log.LogFactoryUtil;
040    import com.liferay.portal.kernel.model.SystemEventConstants;
041    import com.liferay.portal.kernel.model.User;
042    import com.liferay.portal.kernel.service.ServiceContext;
043    import com.liferay.portal.kernel.service.permission.ModelPermissions;
044    import com.liferay.portal.kernel.systemevent.SystemEvent;
045    import com.liferay.portal.kernel.util.ArrayUtil;
046    import com.liferay.portal.kernel.util.LocaleUtil;
047    import com.liferay.portal.kernel.util.OrderByComparator;
048    import com.liferay.portal.kernel.util.PortalUtil;
049    import com.liferay.portal.kernel.util.SetUtil;
050    import com.liferay.portal.kernel.util.SortedArrayList;
051    import com.liferay.portal.kernel.util.StringUtil;
052    import com.liferay.portal.kernel.util.Validator;
053    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
054    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
055    import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
056    import com.liferay.portlet.documentlibrary.service.base.DLFileEntryTypeLocalServiceBaseImpl;
057    
058    import java.util.ArrayList;
059    import java.util.HashMap;
060    import java.util.HashSet;
061    import java.util.List;
062    import java.util.Locale;
063    import java.util.Map;
064    import java.util.Set;
065    
066    /**
067     * Provides the local service for accessing, adding, cascading, deleting, and
068     * updating file and folder file entry types.
069     *
070     * @author Alexander Chow
071     * @author Sergio Gonz??lez
072     */
073    public class DLFileEntryTypeLocalServiceImpl
074            extends DLFileEntryTypeLocalServiceBaseImpl {
075    
076            @Override
077            public void addDDMStructureLinks(
078                    long fileEntryTypeId, Set<Long> ddmStructureIds) {
079    
080                    long classNameId = classNameLocalService.getClassNameId(
081                            DLFileEntryType.class);
082    
083                    for (long ddmStructureId : ddmStructureIds) {
084                            DDMStructureLinkManagerUtil.addStructureLink(
085                                    classNameId, fileEntryTypeId, ddmStructureId);
086                    }
087            }
088    
089            @Override
090            public DLFileEntryType addFileEntryType(
091                            long userId, long groupId, String fileEntryTypeKey,
092                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
093                            long[] ddmStructureIds, ServiceContext serviceContext)
094                    throws PortalException {
095    
096                    User user = userPersistence.findByPrimaryKey(userId);
097    
098                    if (Validator.isNull(fileEntryTypeKey)) {
099                            fileEntryTypeKey = String.valueOf(counterLocalService.increment());
100                    }
101                    else {
102                            fileEntryTypeKey = StringUtil.toUpperCase(fileEntryTypeKey.trim());
103                    }
104    
105                    String fileEntryTypeUuid = serviceContext.getUuid();
106    
107                    if (Validator.isNull(fileEntryTypeUuid)) {
108                            fileEntryTypeUuid = PortalUUIDUtil.generate();
109                    }
110    
111                    long fileEntryTypeId = counterLocalService.increment();
112    
113                    long ddmStructureId = updateDDMStructure(
114                            userId, fileEntryTypeUuid, fileEntryTypeId, groupId, nameMap,
115                            descriptionMap, serviceContext);
116    
117                    if (ddmStructureId > 0) {
118                            ddmStructureIds = ArrayUtil.append(ddmStructureIds, ddmStructureId);
119                    }
120    
121                    validate(fileEntryTypeId, groupId, fileEntryTypeKey, ddmStructureIds);
122    
123                    DLFileEntryType dlFileEntryType = dlFileEntryTypePersistence.create(
124                            fileEntryTypeId);
125    
126                    dlFileEntryType.setUuid(fileEntryTypeUuid);
127                    dlFileEntryType.setGroupId(groupId);
128                    dlFileEntryType.setCompanyId(user.getCompanyId());
129                    dlFileEntryType.setUserId(user.getUserId());
130                    dlFileEntryType.setUserName(user.getFullName());
131                    dlFileEntryType.setFileEntryTypeKey(fileEntryTypeKey);
132                    dlFileEntryType.setNameMap(nameMap);
133                    dlFileEntryType.setDescriptionMap(descriptionMap);
134    
135                    dlFileEntryTypePersistence.update(dlFileEntryType);
136    
137                    addDDMStructureLinks(
138                            fileEntryTypeId, SetUtil.fromArray(ddmStructureIds));
139    
140                    if (serviceContext.isAddGroupPermissions() ||
141                            serviceContext.isAddGuestPermissions()) {
142    
143                            addFileEntryTypeResources(
144                                    dlFileEntryType, serviceContext.isAddGroupPermissions(),
145                                    serviceContext.isAddGuestPermissions());
146                    }
147                    else {
148                            addFileEntryTypeResources(
149                                    dlFileEntryType, serviceContext.getModelPermissions());
150                    }
151    
152                    return dlFileEntryType;
153            }
154    
155            @Override
156            public DLFileEntryType addFileEntryType(
157                            long userId, long groupId, String name, String description,
158                            long[] ddmStructureIds, ServiceContext serviceContext)
159                    throws PortalException {
160    
161                    Map<Locale, String> nameMap = new HashMap<>();
162    
163                    nameMap.put(LocaleUtil.getSiteDefault(), name);
164    
165                    Map<Locale, String> descriptionMap = new HashMap<>();
166    
167                    descriptionMap.put(LocaleUtil.getSiteDefault(), description);
168    
169                    return addFileEntryType(
170                            userId, groupId, null, nameMap, descriptionMap, ddmStructureIds,
171                            serviceContext);
172            }
173    
174            @Override
175            public void cascadeFileEntryTypes(long userId, DLFolder dlFolder)
176                    throws PortalException {
177    
178                    long[] groupIds = PortalUtil.getCurrentAndAncestorSiteGroupIds(
179                            dlFolder.getGroupId());
180    
181                    List<DLFileEntryType> dlFileEntryTypes = getFolderFileEntryTypes(
182                            groupIds, dlFolder.getFolderId(), true);
183    
184                    List<Long> fileEntryTypeIds = getFileEntryTypeIds(dlFileEntryTypes);
185    
186                    long defaultFileEntryTypeId = getDefaultFileEntryTypeId(
187                            dlFolder.getFolderId());
188    
189                    ServiceContext serviceContext = new ServiceContext();
190    
191                    serviceContext.setCompanyId(dlFolder.getCompanyId());
192                    serviceContext.setScopeGroupId(dlFolder.getGroupId());
193                    serviceContext.setUserId(userId);
194    
195                    cascadeFileEntryTypes(
196                            userId, dlFolder.getGroupId(), dlFolder.getFolderId(),
197                            defaultFileEntryTypeId, fileEntryTypeIds, serviceContext);
198            }
199    
200            @Override
201            @SystemEvent(
202                    action = SystemEventConstants.ACTION_SKIP,
203                    type = SystemEventConstants.TYPE_DELETE
204            )
205            public void deleteFileEntryType(DLFileEntryType dlFileEntryType)
206                    throws PortalException {
207    
208                    if (dlFileEntryPersistence.countByFileEntryTypeId(
209                                    dlFileEntryType.getFileEntryTypeId()) > 0) {
210    
211                            throw new RequiredFileEntryTypeException(
212                                    "There are file entries of file entry type " +
213                                            dlFileEntryType.getFileEntryTypeId());
214                    }
215    
216                    DDMStructure ddmStructure = DDMStructureManagerUtil.fetchStructure(
217                            dlFileEntryType.getGroupId(),
218                            classNameLocalService.getClassNameId(DLFileEntryMetadata.class),
219                            DLUtil.getDDMStructureKey(dlFileEntryType));
220    
221                    if (ddmStructure == null) {
222                            ddmStructure = DDMStructureManagerUtil.fetchStructure(
223                                    dlFileEntryType.getGroupId(),
224                                    classNameLocalService.getClassNameId(DLFileEntryMetadata.class),
225                                    DLUtil.getDeprecatedDDMStructureKey(dlFileEntryType));
226                    }
227    
228                    if (ddmStructure != null) {
229                            long classNameId = classNameLocalService.getClassNameId(
230                                    DLFileEntryType.class);
231    
232                            DDMStructureLinkManagerUtil.deleteStructureLink(
233                                    classNameId, dlFileEntryType.getFileEntryTypeId(),
234                                    ddmStructure.getStructureId());
235    
236                            DDMStructureManagerUtil.deleteStructure(
237                                    ddmStructure.getStructureId());
238                    }
239    
240                    dlFileEntryTypePersistence.remove(dlFileEntryType);
241            }
242    
243            @Override
244            public void deleteFileEntryType(long fileEntryTypeId)
245                    throws PortalException {
246    
247                    DLFileEntryType dlFileEntryType =
248                            dlFileEntryTypePersistence.findByPrimaryKey(fileEntryTypeId);
249    
250                    dlFileEntryTypeLocalService.deleteFileEntryType(dlFileEntryType);
251            }
252    
253            @Override
254            public void deleteFileEntryTypes(long groupId) throws PortalException {
255                    List<DLFileEntryType> dlFileEntryTypes =
256                            dlFileEntryTypePersistence.findByGroupId(groupId);
257    
258                    for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
259                            dlFileEntryTypeLocalService.deleteFileEntryType(dlFileEntryType);
260                    }
261            }
262    
263            @Override
264            public DLFileEntryType fetchFileEntryType(long fileEntryTypeId) {
265                    return dlFileEntryTypePersistence.fetchByPrimaryKey(fileEntryTypeId);
266            }
267    
268            @Override
269            public DLFileEntryType fetchFileEntryType(
270                    long groupId, String fileEntryTypeKey) {
271    
272                    fileEntryTypeKey = StringUtil.toUpperCase(fileEntryTypeKey.trim());
273    
274                    return dlFileEntryTypePersistence.fetchByG_F(groupId, fileEntryTypeKey);
275            }
276    
277            @Override
278            public long getDefaultFileEntryTypeId(long folderId)
279                    throws PortalException {
280    
281                    folderId = getFileEntryTypesPrimaryFolderId(folderId);
282    
283                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
284                            DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
285    
286                            return dlFolder.getDefaultFileEntryTypeId();
287                    }
288                    else {
289                            return 0;
290                    }
291            }
292    
293            @Override
294            public DLFileEntryType getFileEntryType(long fileEntryTypeId)
295                    throws PortalException {
296    
297                    return dlFileEntryTypePersistence.findByPrimaryKey(fileEntryTypeId);
298            }
299    
300            @Override
301            public DLFileEntryType getFileEntryType(
302                            long groupId, String fileEntryTypeKey)
303                    throws PortalException {
304    
305                    fileEntryTypeKey = StringUtil.toUpperCase(fileEntryTypeKey.trim());
306    
307                    return dlFileEntryTypePersistence.findByG_F(groupId, fileEntryTypeKey);
308            }
309    
310            @Override
311            public List<DLFileEntryType> getFileEntryTypes(long ddmStructureId)
312                    throws PortalException {
313    
314                    List<DLFileEntryType> fileEntryTypes = new ArrayList<>();
315    
316                    long classNameId = classNameLocalService.getClassNameId(
317                            DLFileEntryType.class);
318    
319                    List<DDMStructureLink> ddmStructureLinks =
320                            DDMStructureLinkManagerUtil.getClassNameStructureLinks(classNameId);
321    
322                    for (DDMStructureLink ddmStructureLink : ddmStructureLinks) {
323                            if (ddmStructureId != ddmStructureLink.getStructureId()) {
324                                    continue;
325                            }
326    
327                            DLFileEntryType fileEntryType = getFileEntryType(
328                                    ddmStructureLink.getClassPK());
329    
330                            fileEntryTypes.add(fileEntryType);
331                    }
332    
333                    return fileEntryTypes;
334            }
335    
336            @Override
337            public List<DLFileEntryType> getFileEntryTypes(long[] groupIds) {
338                    return dlFileEntryTypePersistence.findByGroupId(groupIds);
339            }
340    
341            @Override
342            public List<DLFileEntryType> getFolderFileEntryTypes(
343                            long[] groupIds, long folderId, boolean inherited)
344                    throws PortalException {
345    
346                    if (!inherited) {
347                            return dlFolderPersistence.getDLFileEntryTypes(folderId);
348                    }
349    
350                    List<DLFileEntryType> dlFileEntryTypes = null;
351    
352                    folderId = getFileEntryTypesPrimaryFolderId(folderId);
353    
354                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
355                            dlFileEntryTypes = dlFolderPersistence.getDLFileEntryTypes(
356                                    folderId);
357                    }
358                    else {
359                            dlFileEntryTypes = new ArrayList<>(getFileEntryTypes(groupIds));
360    
361                            DLFileEntryType dlFileEntryType =
362                                    dlFileEntryTypePersistence.findByPrimaryKey(
363                                            DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT);
364    
365                            dlFileEntryTypes.add(0, dlFileEntryType);
366                    }
367    
368                    return dlFileEntryTypes;
369            }
370    
371            @Override
372            public List<DLFileEntryType> search(
373                    long companyId, long[] groupIds, String keywords,
374                    boolean includeBasicFileEntryType, int start, int end,
375                    OrderByComparator<DLFileEntryType> orderByComparator) {
376    
377                    return dlFileEntryTypeFinder.findByKeywords(
378                            companyId, groupIds, keywords, includeBasicFileEntryType, start,
379                            end, orderByComparator);
380            }
381    
382            @Override
383            public int searchCount(
384                    long companyId, long[] groupIds, String keywords,
385                    boolean includeBasicFileEntryType) {
386    
387                    return dlFileEntryTypeFinder.countByKeywords(
388                            companyId, groupIds, keywords, includeBasicFileEntryType);
389            }
390    
391            @Override
392            public void unsetFolderFileEntryTypes(long folderId) {
393                    List<DLFileEntryType> dlFileEntryTypes =
394                            dlFolderPersistence.getDLFileEntryTypes(folderId);
395    
396                    for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
397                            dlFolderPersistence.removeDLFileEntryType(
398                                    folderId, dlFileEntryType);
399                    }
400            }
401    
402            @Override
403            public void updateDDMStructureLinks(
404                            long fileEntryTypeId, Set<Long> ddmStructureIds)
405                    throws PortalException {
406    
407                    Set<Long> existingDDMStructureLinkStructureIds =
408                            getExistingDDMStructureLinkStructureIds(fileEntryTypeId);
409    
410                    deleteDDMStructureLinks(
411                            fileEntryTypeId,
412                            getStaleDDMStructureLinkStructureIds(
413                                    ddmStructureIds, existingDDMStructureLinkStructureIds));
414    
415                    addDDMStructureLinks(
416                            fileEntryTypeId,
417                            getMissingDDMStructureLinkStructureIds(
418                                    ddmStructureIds, existingDDMStructureLinkStructureIds));
419            }
420    
421            @Override
422            public DLFileEntry updateFileEntryFileEntryType(
423                            DLFileEntry dlFileEntry, ServiceContext serviceContext)
424                    throws PortalException {
425    
426                    long groupId = serviceContext.getScopeGroupId();
427                    long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
428    
429                    DLFolder dlFolder = dlFolderPersistence.fetchByPrimaryKey(
430                            dlFileEntry.getFolderId());
431    
432                    if (dlFolder != null) {
433                            groupId = dlFolder.getGroupId();
434                            folderId = dlFolder.getFolderId();
435                    }
436    
437                    List<DLFileEntryType> dlFileEntryTypes = getFolderFileEntryTypes(
438                            PortalUtil.getCurrentAndAncestorSiteGroupIds(groupId), folderId,
439                            true);
440    
441                    List<Long> fileEntryTypeIds = getFileEntryTypeIds(dlFileEntryTypes);
442    
443                    if (fileEntryTypeIds.contains(dlFileEntry.getFileEntryTypeId())) {
444                            return dlFileEntry;
445                    }
446    
447                    long defaultFileEntryTypeId = getDefaultFileEntryTypeId(folderId);
448    
449                    DLFileVersion dlFileVersion =
450                            dlFileVersionLocalService.getLatestFileVersion(
451                                    dlFileEntry.getFileEntryId(), true);
452    
453                    if (dlFileVersion.isPending()) {
454                            workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
455                                    dlFileVersion.getCompanyId(), dlFileEntry.getGroupId(),
456                                    DLFileEntry.class.getName(), dlFileVersion.getFileVersionId());
457                    }
458    
459                    return dlFileEntryLocalService.updateFileEntry(
460                            serviceContext.getUserId(), dlFileEntry.getFileEntryId(), null,
461                            null, null, null, null, false, defaultFileEntryTypeId, null, null,
462                            null, 0, serviceContext);
463            }
464    
465            @Override
466            public void updateFileEntryType(
467                            long userId, long fileEntryTypeId, Map<Locale, String> nameMap,
468                            Map<Locale, String> descriptionMap, long[] ddmStructureIds,
469                            ServiceContext serviceContext)
470                    throws PortalException {
471    
472                    DLFileEntryType dlFileEntryType =
473                            dlFileEntryTypePersistence.findByPrimaryKey(fileEntryTypeId);
474    
475                    long ddmStructureId = updateDDMStructure(
476                            userId, dlFileEntryType.getUuid(), fileEntryTypeId,
477                            dlFileEntryType.getGroupId(), nameMap, descriptionMap,
478                            serviceContext);
479    
480                    if (ddmStructureId > 0) {
481                            ddmStructureIds = ArrayUtil.append(ddmStructureIds, ddmStructureId);
482                    }
483    
484                    validate(
485                            fileEntryTypeId, dlFileEntryType.getGroupId(),
486                            dlFileEntryType.getFileEntryTypeKey(), ddmStructureIds);
487    
488                    dlFileEntryType.setNameMap(nameMap);
489                    dlFileEntryType.setDescriptionMap(descriptionMap);
490    
491                    dlFileEntryTypePersistence.update(dlFileEntryType);
492    
493                    updateDDMStructureLinks(
494                            fileEntryTypeId, SetUtil.fromArray(ddmStructureIds));
495            }
496    
497            @Override
498            public void updateFileEntryType(
499                            long userId, long fileEntryTypeId, String name, String description,
500                            long[] ddmStructureIds, ServiceContext serviceContext)
501                    throws PortalException {
502    
503                    Map<Locale, String> nameMap = new HashMap<>();
504    
505                    nameMap.put(LocaleUtil.getSiteDefault(), name);
506    
507                    Map<Locale, String> descriptionMap = new HashMap<>();
508    
509                    descriptionMap.put(LocaleUtil.getSiteDefault(), description);
510    
511                    updateFileEntryType(
512                            userId, fileEntryTypeId, nameMap, descriptionMap, ddmStructureIds,
513                            serviceContext);
514            }
515    
516            @Override
517            public void updateFolderFileEntryTypes(
518                    DLFolder dlFolder, List<Long> fileEntryTypeIds,
519                    long defaultFileEntryTypeId, ServiceContext serviceContext) {
520    
521                    List<Long> originalFileEntryTypeIds = getFileEntryTypeIds(
522                            dlFolderPersistence.getDLFileEntryTypes(dlFolder.getFolderId()));
523    
524                    if (fileEntryTypeIds.equals(originalFileEntryTypeIds)) {
525                            return;
526                    }
527    
528                    for (Long fileEntryTypeId : fileEntryTypeIds) {
529                            if (!originalFileEntryTypeIds.contains(fileEntryTypeId)) {
530                                    dlFolderPersistence.addDLFileEntryType(
531                                            dlFolder.getFolderId(), fileEntryTypeId);
532                            }
533                    }
534    
535                    for (Long originalFileEntryTypeId : originalFileEntryTypeIds) {
536                            if (!fileEntryTypeIds.contains(originalFileEntryTypeId)) {
537                                    dlFolderPersistence.removeDLFileEntryType(
538                                            dlFolder.getFolderId(), originalFileEntryTypeId);
539    
540                                    workflowDefinitionLinkLocalService.deleteWorkflowDefinitionLink(
541                                            dlFolder.getCompanyId(), dlFolder.getGroupId(),
542                                            DLFolder.class.getName(), dlFolder.getFolderId(),
543                                            originalFileEntryTypeId);
544                            }
545                    }
546            }
547    
548            protected void addFileEntryTypeResources(
549                            DLFileEntryType dlFileEntryType, boolean addGroupPermissions,
550                            boolean addGuestPermissions)
551                    throws PortalException {
552    
553                    resourceLocalService.addResources(
554                            dlFileEntryType.getCompanyId(), dlFileEntryType.getGroupId(),
555                            dlFileEntryType.getUserId(), DLFileEntryType.class.getName(),
556                            dlFileEntryType.getFileEntryTypeId(), false, addGroupPermissions,
557                            addGuestPermissions);
558            }
559    
560            protected void addFileEntryTypeResources(
561                            DLFileEntryType dlFileEntryType, ModelPermissions modelPermissions)
562                    throws PortalException {
563    
564                    resourceLocalService.addModelResources(
565                            dlFileEntryType.getCompanyId(), dlFileEntryType.getGroupId(),
566                            dlFileEntryType.getUserId(), DLFileEntryType.class.getName(),
567                            dlFileEntryType.getFileEntryTypeId(), modelPermissions);
568            }
569    
570            protected void cascadeFileEntryTypes(
571                            long userId, long groupId, long folderId,
572                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
573                            ServiceContext serviceContext)
574                    throws PortalException {
575    
576                    List<DLFileEntry> dlFileEntries = dlFileEntryPersistence.findByG_F(
577                            groupId, folderId);
578    
579                    for (DLFileEntry dlFileEntry : dlFileEntries) {
580                            Long fileEntryTypeId = dlFileEntry.getFileEntryTypeId();
581    
582                            if (fileEntryTypeIds.contains(fileEntryTypeId)) {
583                                    continue;
584                            }
585    
586                            DLFileVersion dlFileVersion =
587                                    dlFileVersionLocalService.getLatestFileVersion(
588                                            dlFileEntry.getFileEntryId(), true);
589    
590                            if (dlFileVersion.isPending()) {
591                                    workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
592                                            dlFileVersion.getCompanyId(), groupId,
593                                            DLFileEntry.class.getName(),
594                                            dlFileVersion.getFileVersionId());
595                            }
596    
597                            dlFileEntryLocalService.updateFileEntryType(
598                                    userId, dlFileEntry.getFileEntryId(), defaultFileEntryTypeId,
599                                    serviceContext);
600    
601                            dlAppHelperLocalService.updateAsset(
602                                    userId, new LiferayFileEntry(dlFileEntry),
603                                    new LiferayFileVersion(dlFileVersion),
604                                    serviceContext.getAssetCategoryIds(),
605                                    serviceContext.getAssetTagNames(),
606                                    serviceContext.getAssetLinkEntryIds());
607                    }
608    
609                    List<DLFolder> subFolders = dlFolderPersistence.findByG_M_P_H(
610                            groupId, false, folderId, false);
611    
612                    for (DLFolder subFolder : subFolders) {
613                            long subFolderId = subFolder.getFolderId();
614    
615                            if (subFolder.getRestrictionType() ==
616                                            DLFolderConstants.RESTRICTION_TYPE_INHERIT) {
617    
618                                    continue;
619                            }
620    
621                            cascadeFileEntryTypes(
622                                    userId, groupId, subFolderId, defaultFileEntryTypeId,
623                                    fileEntryTypeIds, serviceContext);
624                    }
625            }
626    
627            protected void deleteDDMStructureLinks(
628                            long fileEntryTypeId, Set<Long> ddmStructureIds)
629                    throws PortalException {
630    
631                    long classNameId = classNameLocalService.getClassNameId(
632                            DLFileEntryType.class);
633    
634                    for (long ddmStructureId : ddmStructureIds) {
635                            DDMStructureLinkManagerUtil.deleteStructureLink(
636                                    classNameId, fileEntryTypeId, ddmStructureId);
637                    }
638            }
639    
640            protected void fixDDMStructureKey(
641                            String fileEntryTypeUuid, long fileEntryTypeId, long groupId)
642                    throws PortalException {
643    
644                    DDMStructure ddmStructure = DDMStructureManagerUtil.fetchStructure(
645                            groupId,
646                            classNameLocalService.getClassNameId(DLFileEntryMetadata.class),
647                            DLUtil.getDeprecatedDDMStructureKey(fileEntryTypeId));
648    
649                    if (ddmStructure != null) {
650                            DDMStructureManagerUtil.updateStructureKey(
651                                    ddmStructure.getStructureId(),
652                                    DLUtil.getDDMStructureKey(fileEntryTypeUuid));
653                    }
654            }
655    
656            protected Set<Long> getExistingDDMStructureLinkStructureIds(
657                    long fileEntryTypeId) {
658    
659                    long classNameId = classNameLocalService.getClassNameId(
660                            DLFileEntryType.class);
661    
662                    Set<Long> existingDDMStructureLinkStructureIds = new HashSet<>();
663    
664                    List<DDMStructureLink> structureLinks =
665                            DDMStructureLinkManagerUtil.getStructureLinks(
666                                    classNameId, fileEntryTypeId);
667    
668                    for (DDMStructureLink structureLink : structureLinks) {
669                            existingDDMStructureLinkStructureIds.add(
670                                    structureLink.getStructureId());
671                    }
672    
673                    return existingDDMStructureLinkStructureIds;
674            }
675    
676            protected List<Long> getFileEntryTypeIds(
677                    List<DLFileEntryType> dlFileEntryTypes) {
678    
679                    List<Long> fileEntryTypeIds = new SortedArrayList<>();
680    
681                    for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
682                            fileEntryTypeIds.add(dlFileEntryType.getFileEntryTypeId());
683                    }
684    
685                    return fileEntryTypeIds;
686            }
687    
688            protected long getFileEntryTypesPrimaryFolderId(long folderId)
689                    throws NoSuchFolderException {
690    
691                    while (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
692                            DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
693    
694                            if (dlFolder.getRestrictionType() ==
695                                            DLFolderConstants.
696                                                    RESTRICTION_TYPE_FILE_ENTRY_TYPES_AND_WORKFLOW) {
697    
698                                    break;
699                            }
700    
701                            folderId = dlFolder.getParentFolderId();
702                    }
703    
704                    return folderId;
705            }
706    
707            protected Set<Long> getMissingDDMStructureLinkStructureIds(
708                    Set<Long> ddmStructureIds, Set<Long> existingDDMStructureIds) {
709    
710                    Set<Long> missingDDMStructureLinkStructureIds = new HashSet<>(
711                            ddmStructureIds);
712    
713                    missingDDMStructureLinkStructureIds.removeAll(existingDDMStructureIds);
714    
715                    return missingDDMStructureLinkStructureIds;
716            }
717    
718            protected Set<Long> getStaleDDMStructureLinkStructureIds(
719                    Set<Long> ddmStructureIds, Set<Long> existingDDMStructureIds) {
720    
721                    Set<Long> staleDDMStructureLinkStructureIds = new HashSet<>(
722                            existingDDMStructureIds);
723    
724                    staleDDMStructureLinkStructureIds.removeAll(ddmStructureIds);
725    
726                    return staleDDMStructureLinkStructureIds;
727            }
728    
729            protected long updateDDMStructure(
730                            long userId, String fileEntryTypeUuid, long fileEntryTypeId,
731                            long groupId, Map<Locale, String> nameMap,
732                            Map<Locale, String> descriptionMap, ServiceContext serviceContext)
733                    throws PortalException {
734    
735                    fixDDMStructureKey(fileEntryTypeUuid, fileEntryTypeId, groupId);
736    
737                    String ddmStructureKey = DLUtil.getDDMStructureKey(fileEntryTypeUuid);
738    
739                    DDMForm ddmForm = (DDMForm)serviceContext.getAttribute("ddmForm");
740    
741                    DDMStructure ddmStructure = DDMStructureManagerUtil.fetchStructure(
742                            groupId,
743                            classNameLocalService.getClassNameId(DLFileEntryMetadata.class),
744                            ddmStructureKey);
745    
746                    if ((ddmStructure != null) && (ddmForm == null)) {
747                            ddmForm = ddmStructure.getDDMForm();
748                    }
749    
750                    if (ddmForm == null) {
751                            return 0;
752                    }
753    
754                    try {
755                            if (ddmStructure == null) {
756                                    ddmStructure = DDMStructureManagerUtil.addStructure(
757                                            userId, groupId, null,
758                                            classNameLocalService.getClassNameId(
759                                                    DLFileEntryMetadata.class),
760                                            ddmStructureKey, nameMap, descriptionMap, ddmForm,
761                                            StorageEngineManager.STORAGE_TYPE_DEFAULT,
762                                            DDMStructureManager.STRUCTURE_TYPE_AUTO, serviceContext);
763                            }
764                            else {
765                                    ddmStructure = DDMStructureManagerUtil.updateStructure(
766                                            userId, ddmStructure.getStructureId(),
767                                            ddmStructure.getParentStructureId(), nameMap,
768                                            descriptionMap, ddmForm, serviceContext);
769                            }
770    
771                            return ddmStructure.getStructureId();
772                    }
773                    catch (StructureDefinitionException sde) {
774                            if (_log.isWarnEnabled()) {
775                                    _log.warn(sde, sde);
776                            }
777    
778                            if (ddmStructure != null) {
779                                    DDMStructureManagerUtil.deleteStructure(
780                                            ddmStructure.getStructureId());
781                            }
782                    }
783    
784                    return 0;
785            }
786    
787            protected void validate(
788                            long fileEntryTypeId, long groupId, String fileEntryTypeKey,
789                            long[] ddmStructureIds)
790                    throws PortalException {
791    
792                    DLFileEntryType dlFileEntryType = dlFileEntryTypePersistence.fetchByG_F(
793                            groupId, fileEntryTypeKey);
794    
795                    if ((dlFileEntryType != null) &&
796                            (dlFileEntryType.getFileEntryTypeId() != fileEntryTypeId)) {
797    
798                            throw new DuplicateFileEntryTypeException(fileEntryTypeKey);
799                    }
800    
801                    if (ddmStructureIds.length == 0) {
802                            throw new NoSuchMetadataSetException("DDM structure IDs is empty");
803                    }
804    
805                    for (long ddmStructureId : ddmStructureIds) {
806                            DDMStructure ddmStructure = DDMStructureManagerUtil.fetchStructure(
807                                    ddmStructureId);
808    
809                            if (ddmStructure == null) {
810                                    throw new NoSuchMetadataSetException(
811                                            "{ddmStructureId=" + ddmStructureId);
812                            }
813                    }
814            }
815    
816            private static final Log _log = LogFactoryUtil.getLog(
817                    DLFileEntryTypeLocalServiceImpl.class);
818    
819    }