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