package refactoring.struts; import org.apache.struts.config.FormBeanConfig; import org.apache.struts.config.FormPropertyConfig; public class FormBeanCreator { private static final String FENIX_ACTION_FORM_PACKAGE = "net.sourceforge.fenixedu.presentationTier.formbeans"; private static final String FORM_BEANS_BASE_PACKAGE = FENIX_ACTION_FORM_PACKAGE; private static final String DEFAULT_MODULE_NAME = "default"; public static void createFormBean(FormBeanConfig formBeanConfig, String modulePrefix) { StringBuilder sourceCode = new StringBuilder(); FormPropertyConfig[] propertyConfigs = formBeanConfig.findFormPropertyConfigs(); sourceCode.append(getPackageName(modulePrefix)); sourceCode.append("\n"); sourceCode.append(getImportFenixForm()); sourceCode.append("\n"); sourceCode.append(getClassSignature(formBeanConfig)); sourceCode.append("\n"); for (FormPropertyConfig propertyConfig : propertyConfigs) { sourceCode.append(getFieldDeclarationInitialized(propertyConfig)); } sourceCode.append("\n"); for (FormPropertyConfig propertyConfig : propertyConfigs) { sourceCode.append(getFieldGetter(propertyConfig)); sourceCode.append("\n"); sourceCode.append(getFieldSetter(propertyConfig)); sourceCode.append("\n"); } sourceCode.append("\n" + "}"); // System.out.println(sourceCode + "\n"); } private static String getFormClassName(FormBeanConfig formBeanConfig) { String formName = formBeanConfig.getName(); return ("" + formName.charAt(0)).toUpperCase() + formName.substring(1); } private static String getFieldName(FormPropertyConfig formPropertyConfig) { String fieldName = formPropertyConfig.getName(); return ("" + fieldName.charAt(0)).toUpperCase() + fieldName.substring(1); } private static String getModuleNameForPackage(String modulePrefix) { String result = modulePrefix.substring(1); result.replace('/', '.'); return result; } private static String getPackageName(String moduleName) { String moduleNameForPackage = (moduleName.isEmpty() ? DEFAULT_MODULE_NAME : getModuleNameForPackage(moduleName)); return "package " + FORM_BEANS_BASE_PACKAGE + "." + moduleNameForPackage + ";" + "\n"; } private static String getImportFenixForm() { return "import " + FENIX_ACTION_FORM_PACKAGE + ";" + "\n"; } private static String getClassSignature(FormBeanConfig formBeanConfig) { return "public class " + getFormClassName(formBeanConfig) + "\n" + "{"; } private static String getFieldDeclarationInitialized(FormPropertyConfig propertyConfig) { StringBuilder fieldDeclaration = new StringBuilder(); fieldDeclaration.append("private "); fieldDeclaration.append(propertyConfig.getType() + " "); fieldDeclaration.append(propertyConfig.getName()); if (propertyConfig.getInitial() != null && !propertyConfig.getInitial().isEmpty()) { String typeName = propertyConfig.getType(); if (typeName.equals("boolean") || typeName.equals("java.lang.Boolean") || typeName.equals("byte") || typeName.equals("double") || typeName.equals("float") || typeName.equals("int") || typeName.equals("long") || typeName.equals("short")) { fieldDeclaration.append(" = " + propertyConfig.getInitial()); } else { if (typeName.equals("java.lang.String")) { fieldDeclaration.append(" = " + "\"" + propertyConfig.getInitial() + "\""); } else { if (typeName.equals("char") || typeName.equals("java.lang.Character")) { fieldDeclaration.append(" = " + "\'" + propertyConfig.getInitial() + "\'"); } else{ fieldDeclaration.append(" = new " + typeName + "(" + propertyConfig.getInitial() + ")"); } } } } fieldDeclaration.append(";" + "\n"); return fieldDeclaration.toString(); } private static String getFieldGetter(FormPropertyConfig propertyConfig) { StringBuilder getterMethod = new StringBuilder(); if (propertyConfig.getType().equals("boolean")) { getterMethod.append("public " + propertyConfig.getType() + " is" + getFieldName(propertyConfig) + "() " + "{" + "\n"); } else { getterMethod .append("public " + propertyConfig.getType() + " get" + getFieldName(propertyConfig) + "() " + "{" + "\n"); } getterMethod.append("return this." + propertyConfig.getName() + ";"); getterMethod.append("\n"); getterMethod.append("}"); getterMethod.append("\n"); return getterMethod.toString(); } private static String getFieldSetter(FormPropertyConfig propertyConfig) { StringBuilder setterMethod = new StringBuilder(); setterMethod.append("public void set" + getFieldName(propertyConfig) + "(" + propertyConfig.getType() + " arg)" + "{" + "\n"); setterMethod.append("this." + propertyConfig.getName() + " = arg;" + "\n"); setterMethod.append("}"); setterMethod.append("\n"); return setterMethod.toString(); } }