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.asset.model;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.LocaleUtil;
019    import com.liferay.portal.util.PortalUtil;
020    import com.liferay.portlet.dynamicdatamapping.DDMStructure;
021    import com.liferay.portlet.dynamicdatamapping.DDMStructureManagerUtil;
022    
023    import java.util.ArrayList;
024    import java.util.List;
025    import java.util.Locale;
026    
027    /**
028     * @author Adolfo P??rez
029     */
030    public class BaseDDMStructureClassTypeReader implements ClassTypeReader {
031    
032            public BaseDDMStructureClassTypeReader(String className) {
033                    _className = className;
034            }
035    
036            @Override
037            public List<ClassType> getAvailableClassTypes(
038                    long[] groupIds, Locale locale) {
039    
040                    List<ClassType> classTypes = new ArrayList<>();
041    
042                    List<DDMStructure> ddmStructures =
043                            DDMStructureManagerUtil.getStructures(
044                                    groupIds, PortalUtil.getClassNameId(_className));
045    
046                    for (DDMStructure ddmStructure : ddmStructures) {
047                            classTypes.add(
048                                    new DDMStructureClassType(
049                                            ddmStructure.getStructureId(), ddmStructure.getName(locale),
050                                            LocaleUtil.toLanguageId(locale)));
051                    }
052    
053                    return classTypes;
054            }
055    
056            @Override
057            public ClassType getClassType(long classTypeId, Locale locale)
058                    throws PortalException {
059    
060                    DDMStructure ddmStructure = DDMStructureManagerUtil.getStructure(
061                            classTypeId);
062    
063                    return new DDMStructureClassType(
064                            classTypeId, ddmStructure.getName(locale),
065                            LocaleUtil.toLanguageId(locale));
066            }
067    
068            private final String _className;
069    
070    }