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.io.DDMFormXSDDeserializerUtil;
048    import com.liferay.portlet.dynamicdatamapping.model.DDMForm;
049    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
050    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
051    import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
052    
053    import java.util.ArrayList;
054    import java.util.Date;
055    import java.util.HashMap;
056    import java.util.List;
057    import java.util.Locale;
058    import java.util.Map;
059    
060    /**
061     * Provides the local service for accessing, adding, cascading, deleting, and
062     * updating file and folder file entry types.
063     *
064     * @author Alexander Chow
065     * @author Sergio Gonz??lez
066     */
067    public class DLFileEntryTypeLocalServiceImpl
068            extends DLFileEntryTypeLocalServiceBaseImpl {
069    
070            @Override
071            public DLFileEntryType addFileEntryType(
072                            long userId, long groupId, String fileEntryTypeKey,
073                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
074                            long[] ddmStructureIds, ServiceContext serviceContext)
075                    throws PortalException {
076    
077                    User user = userPersistence.findByPrimaryKey(userId);
078    
079                    if (Validator.isNull(fileEntryTypeKey)) {
080                            fileEntryTypeKey = String.valueOf(counterLocalService.increment());
081                    }
082                    else {
083                            fileEntryTypeKey = StringUtil.toUpperCase(fileEntryTypeKey.trim());
084                    }
085    
086                    String fileEntryTypeUuid = serviceContext.getUuid();
087    
088                    if (Validator.isNull(fileEntryTypeUuid)) {
089                            fileEntryTypeUuid = PortalUUIDUtil.generate();
090                    }
091    
092                    long fileEntryTypeId = counterLocalService.increment();
093    
094                    long ddmStructureId = updateDDMStructure(
095                            userId, fileEntryTypeUuid, fileEntryTypeId, groupId, nameMap,
096                            descriptionMap, serviceContext);
097    
098                    if (ddmStructureId > 0) {
099                            ddmStructureIds = ArrayUtil.append(ddmStructureIds, ddmStructureId);
100                    }
101    
102                    Date now = new Date();
103    
104                    validate(fileEntryTypeId, groupId, fileEntryTypeKey, ddmStructureIds);
105    
106                    DLFileEntryType dlFileEntryType = dlFileEntryTypePersistence.create(
107                            fileEntryTypeId);
108    
109                    dlFileEntryType.setUuid(fileEntryTypeUuid);
110                    dlFileEntryType.setGroupId(groupId);
111                    dlFileEntryType.setCompanyId(user.getCompanyId());
112                    dlFileEntryType.setUserId(user.getUserId());
113                    dlFileEntryType.setUserName(user.getFullName());
114                    dlFileEntryType.setCreateDate(serviceContext.getCreateDate(now));
115                    dlFileEntryType.setModifiedDate(serviceContext.getModifiedDate(now));
116                    dlFileEntryType.setFileEntryTypeKey(fileEntryTypeKey);
117                    dlFileEntryType.setNameMap(nameMap);
118                    dlFileEntryType.setDescriptionMap(descriptionMap);
119    
120                    dlFileEntryTypePersistence.update(dlFileEntryType);
121    
122                    dlFileEntryTypePersistence.addDDMStructures(
123                            fileEntryTypeId, ddmStructureIds);
124    
125                    if (serviceContext.isAddGroupPermissions() ||
126                            serviceContext.isAddGuestPermissions()) {
127    
128                            addFileEntryTypeResources(
129                                    dlFileEntryType, serviceContext.isAddGroupPermissions(),
130                                    serviceContext.isAddGuestPermissions());
131                    }
132                    else {
133                            addFileEntryTypeResources(
134                                    dlFileEntryType, serviceContext.getGroupPermissions(),
135                                    serviceContext.getGuestPermissions());
136                    }
137    
138                    return dlFileEntryType;
139            }
140    
141            @Override
142            public DLFileEntryType addFileEntryType(
143                            long userId, long groupId, String name, String description,
144                            long[] ddmStructureIds, ServiceContext serviceContext)
145                    throws PortalException {
146    
147                    Map<Locale, String> nameMap = new HashMap<Locale, String>();
148    
149                    nameMap.put(LocaleUtil.getSiteDefault(), name);
150    
151                    Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
152    
153                    descriptionMap.put(LocaleUtil.getSiteDefault(), description);
154    
155                    return addFileEntryType(
156                            userId, groupId, null, nameMap, descriptionMap, ddmStructureIds,
157                            serviceContext);
158            }
159    
160            @Override
161            public void cascadeFileEntryTypes(long userId, DLFolder dlFolder)
162                    throws PortalException {
163    
164                    long[] groupIds = PortalUtil.getCurrentAndAncestorSiteGroupIds(
165                            dlFolder.getGroupId());
166    
167                    List<DLFileEntryType> dlFileEntryTypes = getFolderFileEntryTypes(
168                            groupIds, dlFolder.getFolderId(), true);
169    
170                    List<Long> fileEntryTypeIds = getFileEntryTypeIds(dlFileEntryTypes);
171    
172                    long defaultFileEntryTypeId = getDefaultFileEntryTypeId(
173                            dlFolder.getFolderId());
174    
175                    ServiceContext serviceContext = new ServiceContext();
176    
177                    serviceContext.setCompanyId(dlFolder.getCompanyId());
178                    serviceContext.setScopeGroupId(dlFolder.getGroupId());
179                    serviceContext.setUserId(userId);
180    
181                    cascadeFileEntryTypes(
182                            userId, dlFolder.getGroupId(), dlFolder.getFolderId(),
183                            defaultFileEntryTypeId, fileEntryTypeIds, serviceContext);
184            }
185    
186            @Override
187            @SystemEvent(
188                    action = SystemEventConstants.ACTION_SKIP,
189                    type = SystemEventConstants.TYPE_DELETE)
190            public void deleteFileEntryType(DLFileEntryType dlFileEntryType)
191                    throws PortalException {
192    
193                    if (dlFileEntryPersistence.countByFileEntryTypeId(
194                                    dlFileEntryType.getFileEntryTypeId()) > 0) {
195    
196                            throw new RequiredStructureException(
197                                    RequiredStructureException.REFERENCED_STRUCTURE);
198                    }
199    
200                    DDMStructure ddmStructure = ddmStructureLocalService.fetchStructure(
201                            dlFileEntryType.getGroupId(),
202                            classNameLocalService.getClassNameId(DLFileEntryMetadata.class),
203                            DLUtil.getDDMStructureKey(dlFileEntryType));
204    
205                    if (ddmStructure == null) {
206                            ddmStructure = ddmStructureLocalService.fetchStructure(
207                                    dlFileEntryType.getGroupId(),
208                                    classNameLocalService.getClassNameId(DLFileEntryMetadata.class),
209                                    DLUtil.getDeprecatedDDMStructureKey(dlFileEntryType));
210                    }
211    
212                    if (ddmStructure != null) {
213                            ddmStructureLocalService.deleteStructure(
214                                    ddmStructure.getStructureId());
215                    }
216    
217                    dlFileEntryTypePersistence.remove(dlFileEntryType);
218            }
219    
220            @Override
221            public void deleteFileEntryType(long fileEntryTypeId)
222                    throws PortalException {
223    
224                    DLFileEntryType dlFileEntryType =
225                            dlFileEntryTypePersistence.findByPrimaryKey(fileEntryTypeId);
226    
227                    dlFileEntryTypeLocalService.deleteFileEntryType(dlFileEntryType);
228            }
229    
230            @Override
231            public void deleteFileEntryTypes(long groupId) throws PortalException {
232                    List<DLFileEntryType> dlFileEntryTypes =
233                            dlFileEntryTypePersistence.findByGroupId(groupId);
234    
235                    for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
236                            dlFileEntryTypeLocalService.deleteFileEntryType(dlFileEntryType);
237                    }
238            }
239    
240            @Override
241            public DLFileEntryType fetchFileEntryType(long fileEntryTypeId) {
242                    return dlFileEntryTypePersistence.fetchByPrimaryKey(fileEntryTypeId);
243            }
244    
245            @Override
246            public DLFileEntryType fetchFileEntryType(
247                    long groupId, String fileEntryTypeKey) {
248    
249                    fileEntryTypeKey = StringUtil.toUpperCase(fileEntryTypeKey.trim());
250    
251                    return dlFileEntryTypePersistence.fetchByG_F(groupId, fileEntryTypeKey);
252            }
253    
254            @Override
255            public long getDefaultFileEntryTypeId(long folderId)
256                    throws PortalException {
257    
258                    folderId = getFileEntryTypesPrimaryFolderId(folderId);
259    
260                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
261                            DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
262    
263                            return dlFolder.getDefaultFileEntryTypeId();
264                    }
265                    else {
266                            return 0;
267                    }
268            }
269    
270            @Override
271            public DLFileEntryType getFileEntryType(long fileEntryTypeId)
272                    throws PortalException {
273    
274                    return dlFileEntryTypePersistence.findByPrimaryKey(fileEntryTypeId);
275            }
276    
277            @Override
278            public DLFileEntryType getFileEntryType(
279                            long groupId, String fileEntryTypeKey)
280                    throws PortalException {
281    
282                    fileEntryTypeKey = StringUtil.toUpperCase(fileEntryTypeKey.trim());
283    
284                    return dlFileEntryTypePersistence.findByG_F(groupId, fileEntryTypeKey);
285            }
286    
287            @Override
288            public List<DLFileEntryType> getFileEntryTypes(long[] groupIds) {
289                    return dlFileEntryTypePersistence.findByGroupId(groupIds);
290            }
291    
292            @Override
293            public List<DLFileEntryType> getFolderFileEntryTypes(
294                            long[] groupIds, long folderId, boolean inherited)
295                    throws PortalException {
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.findByPrimaryKey(
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<DLFileEntryType> orderByComparator) {
328    
329                    return dlFileEntryTypeFinder.findByKeywords(
330                            companyId, groupIds, keywords, includeBasicFileEntryType, start,
331                            end, orderByComparator);
332            }
333    
334            @Override
335            public int searchCount(
336                    long companyId, long[] groupIds, String keywords,
337                    boolean includeBasicFileEntryType) {
338    
339                    return dlFileEntryTypeFinder.countByKeywords(
340                            companyId, groupIds, keywords, includeBasicFileEntryType);
341            }
342    
343            @Override
344            public void unsetFolderFileEntryTypes(long folderId) {
345                    List<DLFileEntryType> dlFileEntryTypes =
346                            dlFolderPersistence.getDLFileEntryTypes(folderId);
347    
348                    for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
349                            dlFolderPersistence.removeDLFileEntryType(
350                                    folderId, dlFileEntryType);
351                    }
352            }
353    
354            @Override
355            public DLFileEntry updateFileEntryFileEntryType(
356                            DLFileEntry dlFileEntry, ServiceContext serviceContext)
357                    throws PortalException {
358    
359                    long groupId = serviceContext.getScopeGroupId();
360                    long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
361    
362                    DLFolder dlFolder = dlFolderPersistence.fetchByPrimaryKey(
363                            dlFileEntry.getFolderId());
364    
365                    if (dlFolder != null) {
366                            groupId = dlFolder.getGroupId();
367                            folderId = dlFolder.getFolderId();
368                    }
369    
370                    List<DLFileEntryType> dlFileEntryTypes = getFolderFileEntryTypes(
371                            PortalUtil.getCurrentAndAncestorSiteGroupIds(groupId), folderId,
372                            true);
373    
374                    List<Long> fileEntryTypeIds = getFileEntryTypeIds(dlFileEntryTypes);
375    
376                    if (fileEntryTypeIds.contains(dlFileEntry.getFileEntryTypeId())) {
377                            return dlFileEntry;
378                    }
379    
380                    long defaultFileEntryTypeId = getDefaultFileEntryTypeId(folderId);
381    
382                    DLFileVersion dlFileVersion =
383                            dlFileVersionLocalService.getLatestFileVersion(
384                                    dlFileEntry.getFileEntryId(), true);
385    
386                    if (dlFileVersion.isPending()) {
387                            workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
388                                    dlFileVersion.getCompanyId(), dlFileEntry.getGroupId(),
389                                    DLFileEntry.class.getName(), dlFileVersion.getFileVersionId());
390                    }
391    
392                    return dlFileEntryLocalService.updateFileEntry(
393                            serviceContext.getUserId(), dlFileEntry.getFileEntryId(), null,
394                            null, null, null, null, false, defaultFileEntryTypeId, null, null,
395                            null, 0, serviceContext);
396            }
397    
398            @Override
399            public void updateFileEntryType(
400                            long userId, long fileEntryTypeId, Map<Locale, String> nameMap,
401                            Map<Locale, String> descriptionMap, long[] ddmStructureIds,
402                            ServiceContext serviceContext)
403                    throws PortalException {
404    
405                    DLFileEntryType dlFileEntryType =
406                            dlFileEntryTypePersistence.findByPrimaryKey(fileEntryTypeId);
407    
408                    long ddmStructureId = updateDDMStructure(
409                            userId, dlFileEntryType.getUuid(), fileEntryTypeId,
410                            dlFileEntryType.getGroupId(), nameMap, descriptionMap,
411                            serviceContext);
412    
413                    if (ddmStructureId > 0) {
414                            ddmStructureIds = ArrayUtil.append(ddmStructureIds, ddmStructureId);
415                    }
416    
417                    validate(
418                            fileEntryTypeId, dlFileEntryType.getGroupId(),
419                            dlFileEntryType.getFileEntryTypeKey(), ddmStructureIds);
420    
421                    dlFileEntryType.setModifiedDate(serviceContext.getModifiedDate(null));
422                    dlFileEntryType.setNameMap(nameMap);
423                    dlFileEntryType.setDescriptionMap(descriptionMap);
424    
425                    dlFileEntryTypePersistence.update(dlFileEntryType);
426    
427                    dlFileEntryTypePersistence.setDDMStructures(
428                            fileEntryTypeId, ddmStructureIds);
429            }
430    
431            @Override
432            public void updateFileEntryType(
433                            long userId, long fileEntryTypeId, String name, String description,
434                            long[] ddmStructureIds, ServiceContext serviceContext)
435                    throws PortalException {
436    
437                    Map<Locale, String> nameMap = new HashMap<Locale, String>();
438    
439                    nameMap.put(LocaleUtil.getSiteDefault(), name);
440    
441                    Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
442    
443                    descriptionMap.put(LocaleUtil.getSiteDefault(), description);
444    
445                    updateFileEntryType(
446                            userId, fileEntryTypeId, nameMap, descriptionMap, ddmStructureIds,
447                            serviceContext);
448            }
449    
450            @Override
451            public void updateFolderFileEntryTypes(
452                    DLFolder dlFolder, List<Long> fileEntryTypeIds,
453                    long defaultFileEntryTypeId, ServiceContext serviceContext) {
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 {
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 {
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 {
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.updateFileEntryType(
534                                    userId, dlFileEntry.getFileEntryId(), defaultFileEntryTypeId,
535                                    serviceContext);
536    
537                            dlAppHelperLocalService.updateAsset(
538                                    userId, new LiferayFileEntry(dlFileEntry),
539                                    new LiferayFileVersion(dlFileVersion),
540                                    serviceContext.getAssetCategoryIds(),
541                                    serviceContext.getAssetTagNames(),
542                                    serviceContext.getAssetLinkEntryIds());
543                    }
544    
545                    List<DLFolder> subFolders = dlFolderPersistence.findByG_M_P_H(
546                            groupId, false, folderId, false);
547    
548                    for (DLFolder subFolder : subFolders) {
549                            long subFolderId = subFolder.getFolderId();
550    
551                            if (subFolder.getRestrictionType() ==
552                                            DLFolderConstants.RESTRICTION_TYPE_INHERIT) {
553    
554                                    continue;
555                            }
556    
557                            cascadeFileEntryTypes(
558                                    userId, groupId, subFolderId, defaultFileEntryTypeId,
559                                    fileEntryTypeIds, serviceContext);
560                    }
561            }
562    
563            protected void fixDDMStructureKey(
564                    String fileEntryTypeUuid, long fileEntryTypeId, long groupId) {
565    
566                    DDMStructure ddmStructure = ddmStructureLocalService.fetchStructure(
567                            groupId,
568                            classNameLocalService.getClassNameId(DLFileEntryMetadata.class),
569                            DLUtil.getDeprecatedDDMStructureKey(fileEntryTypeId));
570    
571                    if (ddmStructure != null) {
572                            ddmStructure.setStructureKey(
573                                    DLUtil.getDDMStructureKey(fileEntryTypeUuid));
574    
575                            ddmStructureLocalService.updateDDMStructure(ddmStructure);
576                    }
577            }
578    
579            protected DDMForm getDDMForm(String definition) throws PortalException {
580                    DDMXMLUtil.validateXML(definition);
581    
582                    return DDMFormXSDDeserializerUtil.deserialize(definition);
583            }
584    
585            protected List<Long> getFileEntryTypeIds(
586                    List<DLFileEntryType> dlFileEntryTypes) {
587    
588                    List<Long> fileEntryTypeIds = new SortedArrayList<Long>();
589    
590                    for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
591                            fileEntryTypeIds.add(dlFileEntryType.getFileEntryTypeId());
592                    }
593    
594                    return fileEntryTypeIds;
595            }
596    
597            protected long getFileEntryTypesPrimaryFolderId(long folderId)
598                    throws NoSuchFolderException {
599    
600                    while (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
601                            DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
602    
603                            if (dlFolder.getRestrictionType() ==
604                                            DLFolderConstants.
605                                                    RESTRICTION_TYPE_FILE_ENTRY_TYPES_AND_WORKFLOW) {
606    
607                                    break;
608                            }
609    
610                            folderId = dlFolder.getParentFolderId();
611                    }
612    
613                    return folderId;
614            }
615    
616            protected long updateDDMStructure(
617                            long userId, String fileEntryTypeUuid, long fileEntryTypeId,
618                            long groupId, Map<Locale, String> nameMap,
619                            Map<Locale, String> descriptionMap, ServiceContext serviceContext)
620                    throws PortalException {
621    
622                    fixDDMStructureKey(fileEntryTypeUuid, fileEntryTypeId, groupId);
623    
624                    String ddmStructureKey = DLUtil.getDDMStructureKey(fileEntryTypeUuid);
625    
626                    String definition = ParamUtil.getString(serviceContext, "definition");
627    
628                    DDMStructure ddmStructure = ddmStructureLocalService.fetchStructure(
629                            groupId,
630                            classNameLocalService.getClassNameId(DLFileEntryMetadata.class),
631                            ddmStructureKey);
632    
633                    if ((ddmStructure != null) && Validator.isNull(definition)) {
634                            definition = ddmStructure.getDefinition();
635                    }
636    
637                    try {
638                            DDMForm ddmForm = getDDMForm(definition);
639    
640                            if (ddmStructure == null) {
641                                    ddmStructure = ddmStructureLocalService.addStructure(
642                                            userId, groupId,
643                                            DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID,
644                                            classNameLocalService.getClassNameId(
645                                                    DLFileEntryMetadata.class),
646                                            ddmStructureKey, nameMap, descriptionMap, ddmForm, "xml",
647                                            DDMStructureConstants.TYPE_AUTO, serviceContext);
648                            }
649                            else {
650                                    ddmStructure = ddmStructureLocalService.updateStructure(
651                                            ddmStructure.getStructureId(),
652                                            ddmStructure.getParentStructureId(), nameMap,
653                                            descriptionMap, ddmForm, serviceContext);
654                            }
655    
656                            return ddmStructure.getStructureId();
657                    }
658                    catch (StructureDefinitionException sde) {
659                            if (ddmStructure != null) {
660                                    ddmStructureLocalService.deleteStructure(
661                                            ddmStructure.getStructureId());
662                            }
663                    }
664    
665                    return 0;
666            }
667    
668            protected void validate(
669                            long fileEntryTypeId, long groupId, String fileEntryTypeKey,
670                            long[] ddmStructureIds)
671                    throws PortalException {
672    
673                    DLFileEntryType dlFileEntryType = dlFileEntryTypePersistence.fetchByG_F(
674                            groupId, fileEntryTypeKey);
675    
676                    if ((dlFileEntryType != null) &&
677                            (dlFileEntryType.getFileEntryTypeId() != fileEntryTypeId)) {
678    
679                            throw new DuplicateFileEntryTypeException(fileEntryTypeKey);
680                    }
681    
682                    if (ddmStructureIds.length == 0) {
683                            throw new NoSuchMetadataSetException();
684                    }
685    
686                    for (long ddmStructureId : ddmStructureIds) {
687                            DDMStructure ddmStructure =
688                                    ddmStructurePersistence.fetchByPrimaryKey(ddmStructureId);
689    
690                            if (ddmStructure == null) {
691                                    throw new NoSuchMetadataSetException(
692                                            "{ddmStructureId=" + ddmStructureId);
693                            }
694                    }
695            }
696    
697    }