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                            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 long getDefaultFileEntryTypeId(long folderId)
244                    throws PortalException, SystemException {
245    
246                    folderId = getFileEntryTypesPrimaryFolderId(folderId);
247    
248                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
249                            DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
250    
251                            return dlFolder.getDefaultFileEntryTypeId();
252                    }
253                    else {
254                            return 0;
255                    }
256            }
257    
258            @Override
259            public DLFileEntryType getFileEntryType(long fileEntryTypeId)
260                    throws PortalException, SystemException {
261    
262                    return dlFileEntryTypePersistence.findByPrimaryKey(fileEntryTypeId);
263            }
264    
265            @Override
266            public DLFileEntryType getFileEntryType(
267                            long groupId, String fileEntryTypeKey)
268                    throws PortalException, SystemException {
269    
270                    fileEntryTypeKey = StringUtil.toUpperCase(fileEntryTypeKey.trim());
271    
272                    return dlFileEntryTypePersistence.findByG_F(groupId, fileEntryTypeKey);
273            }
274    
275            @Override
276            public List<DLFileEntryType> getFileEntryTypes(long[] groupIds)
277                    throws SystemException {
278    
279                    return dlFileEntryTypePersistence.findByGroupId(groupIds);
280            }
281    
282            @Override
283            public List<DLFileEntryType> getFolderFileEntryTypes(
284                            long[] groupIds, long folderId, boolean inherited)
285                    throws PortalException, SystemException {
286    
287                    if (!inherited) {
288                            return dlFolderPersistence.getDLFileEntryTypes(folderId);
289                    }
290    
291                    List<DLFileEntryType> dlFileEntryTypes = null;
292    
293                    folderId = getFileEntryTypesPrimaryFolderId(folderId);
294    
295                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
296                            dlFileEntryTypes = dlFolderPersistence.getDLFileEntryTypes(
297                                    folderId);
298                    }
299                    else {
300                            dlFileEntryTypes = new ArrayList<DLFileEntryType>(
301                                    getFileEntryTypes(groupIds));
302    
303                            DLFileEntryType dlFileEntryType =
304                                    dlFileEntryTypePersistence.fetchByPrimaryKey(
305                                            DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT);
306    
307                            dlFileEntryTypes.add(0, dlFileEntryType);
308                    }
309    
310                    return dlFileEntryTypes;
311            }
312    
313            @Override
314            public List<DLFileEntryType> search(
315                            long companyId, long[] groupIds, String keywords,
316                            boolean includeBasicFileEntryType, int start, int end,
317                            OrderByComparator orderByComparator)
318                    throws SystemException {
319    
320                    return dlFileEntryTypeFinder.findByKeywords(
321                            companyId, groupIds, keywords, includeBasicFileEntryType, start,
322                            end, orderByComparator);
323            }
324    
325            @Override
326            public int searchCount(
327                            long companyId, long[] groupIds, String keywords,
328                            boolean includeBasicFileEntryType)
329                    throws SystemException {
330    
331                    return dlFileEntryTypeFinder.countByKeywords(
332                            companyId, groupIds, keywords, includeBasicFileEntryType);
333            }
334    
335            @Override
336            public void unsetFolderFileEntryTypes(long folderId)
337                    throws SystemException {
338    
339                    List<DLFileEntryType> dlFileEntryTypes =
340                            dlFolderPersistence.getDLFileEntryTypes(folderId);
341    
342                    for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
343                            dlFolderPersistence.removeDLFileEntryType(
344                                    folderId, dlFileEntryType);
345                    }
346            }
347    
348            @Override
349            public DLFileEntry updateFileEntryFileEntryType(
350                            DLFileEntry dlFileEntry, ServiceContext serviceContext)
351                    throws PortalException, SystemException {
352    
353                    long groupId = serviceContext.getScopeGroupId();
354                    long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
355    
356                    DLFolder dlFolder = dlFolderPersistence.fetchByPrimaryKey(
357                            dlFileEntry.getFolderId());
358    
359                    if (dlFolder != null) {
360                            groupId = dlFolder.getGroupId();
361                            folderId = dlFolder.getFolderId();
362                    }
363    
364                    List<DLFileEntryType> dlFileEntryTypes = getFolderFileEntryTypes(
365                            PortalUtil.getSiteAndCompanyGroupIds(groupId), folderId, true);
366    
367                    List<Long> fileEntryTypeIds = getFileEntryTypeIds(dlFileEntryTypes);
368    
369                    if (fileEntryTypeIds.contains(dlFileEntry.getFileEntryTypeId())) {
370                            return dlFileEntry;
371                    }
372    
373                    long defaultFileEntryTypeId = getDefaultFileEntryTypeId(folderId);
374    
375                    DLFileVersion dlFileVersion =
376                            dlFileVersionLocalService.getLatestFileVersion(
377                                    dlFileEntry.getFileEntryId(), true);
378    
379                    if (dlFileVersion.isPending()) {
380                            workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
381                                    dlFileVersion.getCompanyId(), dlFileEntry.getGroupId(),
382                                    DLFileEntry.class.getName(), dlFileVersion.getFileVersionId());
383                    }
384    
385                    return dlFileEntryLocalService.updateFileEntry(
386                            serviceContext.getUserId(), dlFileEntry.getFileEntryId(), null,
387                            null, null, null, null, false, defaultFileEntryTypeId, null, null,
388                            null, 0, serviceContext);
389            }
390    
391            @Override
392            public void updateFileEntryType(
393                            long userId, long fileEntryTypeId, Map<Locale, String> nameMap,
394                            Map<Locale, String> descriptionMap, long[] ddmStructureIds,
395                            ServiceContext serviceContext)
396                    throws PortalException, SystemException {
397    
398                    DLFileEntryType dlFileEntryType =
399                            dlFileEntryTypePersistence.findByPrimaryKey(fileEntryTypeId);
400    
401                    long ddmStructureId = updateDDMStructure(
402                            userId, dlFileEntryType.getUuid(), fileEntryTypeId,
403                            dlFileEntryType.getGroupId(), nameMap, descriptionMap,
404                            serviceContext);
405    
406                    if (ddmStructureId > 0) {
407                            ddmStructureIds = ArrayUtil.append(ddmStructureIds, ddmStructureId);
408                    }
409    
410                    dlFileEntryType.setModifiedDate(serviceContext.getModifiedDate(null));
411                    dlFileEntryType.setNameMap(nameMap);
412                    dlFileEntryType.setDescriptionMap(descriptionMap);
413    
414                    dlFileEntryTypePersistence.update(dlFileEntryType);
415    
416                    dlFileEntryTypePersistence.setDDMStructures(
417                            fileEntryTypeId, ddmStructureIds);
418            }
419    
420            @Override
421            public void updateFileEntryType(
422                            long userId, long fileEntryTypeId, String name, String description,
423                            long[] ddmStructureIds, ServiceContext serviceContext)
424                    throws PortalException, SystemException {
425    
426                    Map<Locale, String> nameMap = new HashMap<Locale, String>();
427    
428                    nameMap.put(LocaleUtil.getSiteDefault(), name);
429    
430                    Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
431    
432                    descriptionMap.put(LocaleUtil.getSiteDefault(), description);
433    
434                    updateFileEntryType(
435                            userId, fileEntryTypeId, nameMap, descriptionMap, ddmStructureIds,
436                            serviceContext);
437            }
438    
439            @Override
440            public void updateFolderFileEntryTypes(
441                            DLFolder dlFolder, List<Long> fileEntryTypeIds,
442                            long defaultFileEntryTypeId, ServiceContext serviceContext)
443                    throws PortalException, SystemException {
444    
445                    List<Long> originalFileEntryTypeIds = getFileEntryTypeIds(
446                            dlFolderPersistence.getDLFileEntryTypes(dlFolder.getFolderId()));
447    
448                    if (fileEntryTypeIds.equals(originalFileEntryTypeIds)) {
449                            return;
450                    }
451    
452                    for (Long fileEntryTypeId : fileEntryTypeIds) {
453                            if (!originalFileEntryTypeIds.contains(fileEntryTypeId)) {
454                                    dlFolderPersistence.addDLFileEntryType(
455                                            dlFolder.getFolderId(), fileEntryTypeId);
456                            }
457                    }
458    
459                    for (Long originalFileEntryTypeId : originalFileEntryTypeIds) {
460                            if (!fileEntryTypeIds.contains(originalFileEntryTypeId)) {
461                                    dlFolderPersistence.removeDLFileEntryType(
462                                            dlFolder.getFolderId(), originalFileEntryTypeId);
463    
464                                    workflowDefinitionLinkLocalService.deleteWorkflowDefinitionLink(
465                                            dlFolder.getCompanyId(), dlFolder.getGroupId(),
466                                            DLFolder.class.getName(), dlFolder.getFolderId(),
467                                            originalFileEntryTypeId);
468                            }
469                    }
470            }
471    
472            protected void addFileEntryTypeResources(
473                            DLFileEntryType dlFileEntryType, boolean addGroupPermissions,
474                            boolean addGuestPermissions)
475                    throws PortalException, SystemException {
476    
477                    resourceLocalService.addResources(
478                            dlFileEntryType.getCompanyId(), dlFileEntryType.getGroupId(),
479                            dlFileEntryType.getUserId(), DLFileEntryType.class.getName(),
480                            dlFileEntryType.getFileEntryTypeId(), false, addGroupPermissions,
481                            addGuestPermissions);
482            }
483    
484            protected void addFileEntryTypeResources(
485                            DLFileEntryType dlFileEntryType, String[] groupPermissions,
486                            String[] guestPermissions)
487                    throws PortalException, SystemException {
488    
489                    resourceLocalService.addModelResources(
490                            dlFileEntryType.getCompanyId(), dlFileEntryType.getGroupId(),
491                            dlFileEntryType.getUserId(), DLFileEntryType.class.getName(),
492                            dlFileEntryType.getFileEntryTypeId(), groupPermissions,
493                            guestPermissions);
494            }
495    
496            protected void cascadeFileEntryTypes(
497                            long groupId, long folderId, long defaultFileEntryTypeId,
498                            List<Long> fileEntryTypeIds, ServiceContext serviceContext)
499                    throws PortalException, SystemException {
500    
501                    List<DLFileEntry> dlFileEntries = dlFileEntryPersistence.findByG_F(
502                            groupId, folderId);
503    
504                    for (DLFileEntry dlFileEntry : dlFileEntries) {
505                            Long fileEntryTypeId = dlFileEntry.getFileEntryTypeId();
506    
507                            if (fileEntryTypeIds.contains(fileEntryTypeId)) {
508                                    continue;
509                            }
510    
511                            DLFileVersion dlFileVersion =
512                                    dlFileVersionLocalService.getLatestFileVersion(
513                                            dlFileEntry.getFileEntryId(), true);
514    
515                            if (dlFileVersion.isPending()) {
516                                    workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
517                                            dlFileVersion.getCompanyId(), groupId,
518                                            DLFileEntry.class.getName(),
519                                            dlFileVersion.getFileVersionId());
520                            }
521    
522                            dlFileEntryService.updateFileEntry(
523                                    dlFileEntry.getFileEntryId(), null, null, null, null, null,
524                                    false, defaultFileEntryTypeId, null, null, null, 0,
525                                    serviceContext);
526                    }
527    
528                    List<DLFolder> subFolders = dlFolderPersistence.findByG_M_P_H(
529                            groupId, false, folderId, false);
530    
531                    for (DLFolder subFolder : subFolders) {
532                            long subFolderId = subFolder.getFolderId();
533    
534                            if (subFolder.isOverrideFileEntryTypes()) {
535                                    continue;
536                            }
537    
538                            cascadeFileEntryTypes(
539                                    groupId, subFolderId, defaultFileEntryTypeId, fileEntryTypeIds,
540                                    serviceContext);
541                    }
542            }
543    
544            protected void fixDDMStructureKey(
545                            String fileEntryTypeUuid, long fileEntryTypeId, long groupId)
546                    throws SystemException {
547    
548                    DDMStructure ddmStructure = ddmStructureLocalService.fetchStructure(
549                            groupId, PortalUtil.getClassNameId(DLFileEntryMetadata.class),
550                            DLUtil.getDeprecatedDDMStructureKey(fileEntryTypeId));
551    
552                    if (ddmStructure != null) {
553                            ddmStructure.setStructureKey(
554                                    DLUtil.getDDMStructureKey(fileEntryTypeUuid));
555    
556                            ddmStructureLocalService.updateDDMStructure(ddmStructure);
557                    }
558            }
559    
560            protected List<Long> getFileEntryTypeIds(
561                    List<DLFileEntryType> dlFileEntryTypes) {
562    
563                    List<Long> fileEntryTypeIds = new SortedArrayList<Long>();
564    
565                    for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
566                            fileEntryTypeIds.add(dlFileEntryType.getFileEntryTypeId());
567                    }
568    
569                    return fileEntryTypeIds;
570            }
571    
572            protected long getFileEntryTypesPrimaryFolderId(long folderId)
573                    throws NoSuchFolderException, SystemException {
574    
575                    while (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
576                            DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
577    
578                            if (dlFolder.isOverrideFileEntryTypes()) {
579                                    break;
580                            }
581    
582                            folderId = dlFolder.getParentFolderId();
583                    }
584    
585                    return folderId;
586            }
587    
588            protected long updateDDMStructure(
589                            long userId, String fileEntryTypeUuid, long fileEntryTypeId,
590                            long groupId, Map<Locale, String> nameMap,
591                            Map<Locale, String> descriptionMap, ServiceContext serviceContext)
592                    throws PortalException, SystemException {
593    
594                    fixDDMStructureKey(fileEntryTypeUuid, fileEntryTypeId, groupId);
595    
596                    String ddmStructureKey = DLUtil.getDDMStructureKey(fileEntryTypeUuid);
597    
598                    String xsd = ParamUtil.getString(serviceContext, "xsd");
599    
600                    DDMStructure ddmStructure = ddmStructureLocalService.fetchStructure(
601                            groupId, PortalUtil.getClassNameId(DLFileEntryMetadata.class),
602                            ddmStructureKey);
603    
604                    if ((ddmStructure != null) && Validator.isNull(xsd)) {
605                            xsd = ddmStructure.getXsd();
606                    }
607    
608                    try {
609                            if (ddmStructure == null) {
610                                    ddmStructure = ddmStructureLocalService.addStructure(
611                                            userId, groupId,
612                                            DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID,
613                                            PortalUtil.getClassNameId(DLFileEntryMetadata.class),
614                                            ddmStructureKey, nameMap, descriptionMap, xsd, "xml",
615                                            DDMStructureConstants.TYPE_AUTO, serviceContext);
616                            }
617                            else {
618                                    ddmStructure = ddmStructureLocalService.updateStructure(
619                                            ddmStructure.getStructureId(),
620                                            ddmStructure.getParentStructureId(), nameMap,
621                                            descriptionMap, xsd, serviceContext);
622                            }
623    
624                            return ddmStructure.getStructureId();
625                    }
626                    catch (StructureXsdException sxe) {
627                            if (ddmStructure != null) {
628                                    ddmStructureLocalService.deleteStructure(
629                                            ddmStructure.getStructureId());
630                            }
631                    }
632    
633                    return 0;
634            }
635    
636            protected void validate(
637                            long fileEntryTypeId, long groupId, String fileEntryTypeKey,
638                            long[] ddmStructureIds)
639                    throws PortalException, SystemException {
640    
641                    DLFileEntryType dlFileEntryType = dlFileEntryTypePersistence.fetchByG_F(
642                            groupId, fileEntryTypeKey);
643    
644                    if ((dlFileEntryType != null) &&
645                            (dlFileEntryType.getFileEntryTypeId() != fileEntryTypeId)) {
646    
647                            throw new DuplicateFileEntryTypeException(fileEntryTypeKey);
648                    }
649    
650                    if (ddmStructureIds.length == 0) {
651                            throw new NoSuchMetadataSetException();
652                    }
653    
654                    for (long ddmStructureId : ddmStructureIds) {
655                            DDMStructure ddmStructure =
656                                    ddmStructurePersistence.fetchByPrimaryKey(ddmStructureId);
657    
658                            if (ddmStructure == null) {
659                                    throw new NoSuchMetadataSetException();
660                            }
661                    }
662            }
663    
664    }