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.asset;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.language.LanguageUtil;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portlet.asset.model.ClassType;
021    import com.liferay.portlet.asset.model.ClassTypeReader;
022    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
023    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
024    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
025    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeServiceUtil;
026    
027    import java.util.ArrayList;
028    import java.util.List;
029    import java.util.Locale;
030    
031    /**
032     * @author Adolfo P??rez
033     */
034    public class DLFileEntryClassTypeReader implements ClassTypeReader {
035    
036            @Override
037            public List<ClassType> getAvailableClassTypes(
038                    long[] groupIds, Locale locale) {
039    
040                    List<ClassType> classTypes = new ArrayList<>();
041    
042                    classTypes.add(getBasicDocumentClassType(locale));
043    
044                    String languageId = LocaleUtil.toLanguageId(locale);
045    
046                    List<DLFileEntryType> dlFileEntryTypes =
047                            DLFileEntryTypeServiceUtil.getFileEntryTypes(groupIds);
048    
049                    for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
050                            classTypes.add(
051                                    new DLFileEntryClassType(
052                                            dlFileEntryType.getFileEntryTypeId(),
053                                            dlFileEntryType.getName(locale), languageId));
054                    }
055    
056                    return classTypes;
057            }
058    
059            @Override
060            public ClassType getClassType(long classTypeId, Locale locale)
061                    throws PortalException {
062    
063                    if (classTypeId ==
064                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT) {
065    
066                            return getBasicDocumentClassType(locale);
067                    }
068    
069                    DLFileEntryType dlFileEntryType =
070                            DLFileEntryTypeServiceUtil.getFileEntryType(classTypeId);
071    
072                    return new DLFileEntryClassType(
073                            dlFileEntryType.getFileEntryTypeId(),
074                            dlFileEntryType.getName(locale), LocaleUtil.toLanguageId(locale));
075            }
076    
077            protected ClassType getBasicDocumentClassType(Locale locale) {
078                    DLFileEntryType basicDocumentDLFileEntryType =
079                            DLFileEntryTypeLocalServiceUtil.fetchDLFileEntryType(
080                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT);
081    
082                    return new DLFileEntryClassType(
083                            basicDocumentDLFileEntryType.getFileEntryTypeId(),
084                            LanguageUtil.get(
085                                    locale, DLFileEntryTypeConstants.NAME_BASIC_DOCUMENT),
086                            LocaleUtil.toLanguageId(locale));
087            }
088    
089    }