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