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.portlet.dynamicdatamapping.model.DDMForm;
018    import com.liferay.portlet.dynamicdatamapping.model.DDMFormField;
019    
020    import java.lang.annotation.Annotation;
021    import java.lang.reflect.Method;
022    
023    /**
024     * @author Marcellus Tavares
025     */
026    public class DDMFormFactory {
027    
028            public static DDMForm create(Class<?> clazz) {
029                    if (!clazz.isAnnotationPresent(_DDM_FORM_ANNOTATION)) {
030                            throw new IllegalArgumentException(
031                                    "Unsupported class " + clazz.getName());
032                    }
033    
034                    DDMForm ddmForm = new DDMForm();
035    
036                    addDDMFormFields(clazz, ddmForm);
037    
038                    return ddmForm;
039            }
040    
041            protected static void addDDMFormFields(Class<?> clazz, DDMForm ddmForm) {
042                    for (Class<?> interfaceClass : clazz.getInterfaces()) {
043                            addDDMFormFields(interfaceClass, ddmForm);
044                    }
045    
046                    for (Method method : clazz.getDeclaredMethods()) {
047                            if (!method.isAnnotationPresent(_DDM_FORM_FIELD_ANNOTATION)) {
048                                    continue;
049                            }
050    
051                            DDMFormField ddmFormField = createDDMFormField(method);
052    
053                            ddmForm.addDDMFormField(ddmFormField);
054                    }
055            }
056    
057            protected static DDMFormField createDDMFormField(Method method) {
058                    DDMFormFactoryHelper ddmFormFactoryHelper = new DDMFormFactoryHelper(
059                            method);
060    
061                    String name = ddmFormFactoryHelper.getDDMFormFieldName();
062                    String type = ddmFormFactoryHelper.getDDMFormFieldType();
063    
064                    DDMFormField ddmFormField = new DDMFormField(name, type);
065    
066                    ddmFormField.setDataType(
067                            ddmFormFactoryHelper.getDDMFormFieldDataType());
068                    ddmFormField.setLocalizable(
069                            ddmFormFactoryHelper.isDDMFormFieldLocalizable(method));
070                    ddmFormField.setVisibilityExpression(
071                            ddmFormFactoryHelper.getDDMFormFieldVisibilityExpression());
072    
073                    return ddmFormField;
074            }
075    
076            private static final Class<? extends Annotation> _DDM_FORM_ANNOTATION =
077                    com.liferay.portlet.dynamicdatamapping.registry.annotations.
078                            DDMForm.class;
079    
080            private static final Class<? extends Annotation>
081                    _DDM_FORM_FIELD_ANNOTATION =
082                            com.liferay.portlet.dynamicdatamapping.registry.annotations.
083                                    DDMFormField.class;
084    
085    }