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.ArrayUtil;
019    import com.liferay.portal.kernel.util.ListUtil;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portlet.asset.NoSuchClassTypeFieldException;
023    import com.liferay.portlet.dynamicdatamapping.DDMFormField;
024    import com.liferay.portlet.dynamicdatamapping.DDMStructure;
025    import com.liferay.portlet.dynamicdatamapping.DDMStructureManagerUtil;
026    import com.liferay.portlet.dynamicdatamapping.LocalizedValue;
027    
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    /**
032     * @author Adolfo P??rez
033     */
034    public class DDMStructureClassType implements ClassType {
035    
036            public DDMStructureClassType(
037                    long classTypeId, String classTypeName, String languageId) {
038    
039                    _classTypeId = classTypeId;
040                    _classTypeName = classTypeName;
041                    _languageId = languageId;
042            }
043    
044            @Override
045            public ClassTypeField getClassTypeField(String fieldName)
046                    throws PortalException {
047    
048                    for (ClassTypeField classTypeField : getClassTypeFields()) {
049                            if (fieldName.equals(classTypeField.getName())) {
050                                    return classTypeField;
051                            }
052                    }
053    
054                    throw new NoSuchClassTypeFieldException();
055            }
056    
057            @Override
058            public List<ClassTypeField> getClassTypeFields() throws PortalException {
059                    List<ClassTypeField> classTypeFields = getClassTypeFields(
060                            getClassTypeId());
061    
062                    return classTypeFields;
063            }
064    
065            @Override
066            public List<ClassTypeField> getClassTypeFields(int start, int end)
067                    throws PortalException {
068    
069                    return ListUtil.subList(getClassTypeFields(), start, end);
070            }
071    
072            @Override
073            public int getClassTypeFieldsCount() throws PortalException {
074                    return getClassTypeFields().size();
075            }
076    
077            @Override
078            public long getClassTypeId() {
079                    return _classTypeId;
080            }
081    
082            @Override
083            public String getName() {
084                    return _classTypeName;
085            }
086    
087            protected List<ClassTypeField> getClassTypeFields(long ddmStructureId)
088                    throws PortalException {
089    
090                    List<ClassTypeField> classTypeFields = new ArrayList<>();
091    
092                    DDMStructure ddmStructure = DDMStructureManagerUtil.getStructure(
093                            ddmStructureId);
094    
095                    List<DDMFormField> ddmFormFields = ddmStructure.getDDMFormFields(false);
096    
097                    for (DDMFormField ddmFormField : ddmFormFields) {
098                            String indexType = ddmFormField.getIndexType();
099                            String name = ddmFormField.getName();
100    
101                            String type = ddmFormField.getType();
102    
103                            if (Validator.isNull(indexType) ||
104                                    !ArrayUtil.contains(_SELECTABLE_DDM_STRUCTURE_FIELDS, type)) {
105    
106                                    continue;
107                            }
108    
109                            LocalizedValue label = ddmFormField.getLabel();
110    
111                            classTypeFields.add(
112                                    new ClassTypeField(
113                                            label.getString(LocaleUtil.fromLanguageId(_languageId)),
114                                            name, type, ddmStructure.getStructureId()));
115                    }
116    
117                    return classTypeFields;
118            }
119    
120            private static final String[] _SELECTABLE_DDM_STRUCTURE_FIELDS = {
121                    "checkbox", "ddm-date", "ddm-decimal", "ddm-integer", "ddm-number",
122                    "radio", "select", "text"
123            };
124    
125            private final long _classTypeId;
126            private final String _classTypeName;
127            private final String _languageId;
128    
129    }