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.dynamicdatamapping.registry;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.Validator;
019    import com.liferay.portlet.dynamicdatamapping.model.LocalizedValue;
020    import com.liferay.portlet.dynamicdatamapping.registry.annotations.DDMFormField;
021    
022    import java.lang.reflect.Method;
023    
024    /**
025     * @author Marcellus Tavares
026     */
027    public class DDMFormFactoryHelper {
028    
029            public DDMFormFactoryHelper(Method method) {
030                    _method = method;
031    
032                    _ddmFormField = method.getAnnotation(DDMFormField.class);
033            }
034    
035            public String getDDMFormFieldName() {
036                    if (Validator.isNotNull(_ddmFormField.name())) {
037                            return _ddmFormField.name();
038                    }
039    
040                    return _method.getName();
041            }
042    
043            public String getDDMFormFieldType() {
044                    if (Validator.isNotNull(_ddmFormField.type())) {
045                            return _ddmFormField.type();
046                    }
047    
048                    Class<?> returnType = _method.getReturnType();
049    
050                    if (returnType.isAssignableFrom(boolean.class) ||
051                            returnType.isAssignableFrom(Boolean.class)) {
052    
053                            return "checkbox";
054                    }
055    
056                    return "text";
057            }
058    
059            public String getDDMFormFieldVisibilityExpression() {
060                    if (Validator.isNotNull(_ddmFormField.visibilityExpression())) {
061                            return _ddmFormField.visibilityExpression();
062                    }
063    
064                    return StringPool.TRUE;
065            }
066    
067            public boolean isDDMFormFieldLocalizable(Method method) {
068                    Class<?> returnType = _method.getReturnType();
069    
070                    if (returnType.isAssignableFrom(LocalizedValue.class)) {
071                            return true;
072                    }
073    
074                    return false;
075            }
076    
077            protected String getDDMFormFieldDataType() {
078                    if (Validator.isNotNull(_ddmFormField.dataType())) {
079                            return _ddmFormField.dataType();
080                    }
081    
082                    Class<?> returnType = _method.getReturnType();
083    
084                    if (returnType.isAssignableFrom(boolean.class) ||
085                            returnType.isAssignableFrom(Boolean.class)) {
086    
087                            return "boolean";
088                    }
089    
090                    return "string";
091            }
092    
093            private final DDMFormField _ddmFormField;
094            private final Method _method;
095    
096    }