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