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