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