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.model.DDMFormField;
024    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
025    import com.liferay.portlet.dynamicdatamapping.model.LocalizedValue;
026    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
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                    DDMStructure ddmStructure =
060                            DDMStructureLocalServiceUtil.getDDMStructure(getClassTypeId());
061    
062                    List<ClassTypeField> classTypeFields = getClassTypeFields(ddmStructure);
063    
064                    return classTypeFields;
065            }
066    
067            @Override
068            public List<ClassTypeField> getClassTypeFields(int start, int end)
069                    throws PortalException {
070    
071                    return ListUtil.subList(getClassTypeFields(), start, end);
072            }
073    
074            @Override
075            public int getClassTypeFieldsCount() throws PortalException {
076                    return getClassTypeFields().size();
077            }
078    
079            @Override
080            public long getClassTypeId() {
081                    return _classTypeId;
082            }
083    
084            @Override
085            public String getName() {
086                    return _classTypeName;
087            }
088    
089            protected List<ClassTypeField> getClassTypeFields(
090                    DDMStructure ddmStructure) {
091    
092                    List<ClassTypeField> classTypeFields = new ArrayList<>();
093    
094                    List<DDMFormField> ddmFormFields = ddmStructure.getDDMFormFields(false);
095    
096                    for (DDMFormField ddmFormField : ddmFormFields) {
097                            String indexType = ddmFormField.getIndexType();
098                            String name = ddmFormField.getName();
099    
100                            String type = ddmFormField.getType();
101    
102                            if (Validator.isNull(indexType) ||
103                                    !ArrayUtil.contains(_SELECTABLE_DDM_STRUCTURE_FIELDS, type)) {
104    
105                                    continue;
106                            }
107    
108                            LocalizedValue label = ddmFormField.getLabel();
109    
110                            classTypeFields.add(
111                                    new ClassTypeField(
112                                            label.getString(LocaleUtil.fromLanguageId(_languageId)),
113                                            name, type, ddmStructure.getStructureId()));
114                    }
115    
116                    return classTypeFields;
117            }
118    
119            private static final String[] _SELECTABLE_DDM_STRUCTURE_FIELDS = {
120                    "checkbox", "ddm-date", "ddm-decimal", "ddm-integer", "ddm-number",
121                    "radio", "select", "text"
122            };
123    
124            private final long _classTypeId;
125            private final String _classTypeName;
126            private final String _languageId;
127    
128    }