net.anotheria.asg.generator.view.action.ModuleBeanGenerator Maven / Gradle / Ivy
package net.anotheria.asg.generator.view.action;
import net.anotheria.asg.generator.*;
import net.anotheria.asg.generator.forms.meta.MetaForm;
import net.anotheria.asg.generator.meta.*;
import net.anotheria.asg.generator.meta.MetaProperty.Type;
import net.anotheria.asg.generator.view.meta.*;
import net.anotheria.util.ExecutionTimer;
import net.anotheria.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
* TODO Please remain lrosenberg to comment BeanGenerator.java
*
* @author lrosenberg
* @since Feb 25, 2005
* @version $Id: $Id
*/
public class ModuleBeanGenerator extends AbstractGenerator implements IGenerator {
/**
* Implementation is moved into ano-web, the constant remains.
*/
public static final String FLAG_FORM_SUBMITTED = "formSubmittedFlag";
/** Constant FIELD_ML_DISABLED="multilingualInstanceDisabled"
*/
public static final String FIELD_ML_DISABLED = "multilingualInstanceDisabled";
/* (non-Javadoc)
* @see net.anotheria.anodoc.generator.IGenerator#generate(net.anotheria.anodoc.generator.IGenerateable, net.anotheria.anodoc.generator.Context)
*/
/** {@inheritDoc} */
public List generate(IGenerateable g) {
List files = new ArrayList();
MetaModuleSection section = (MetaModuleSection)g;
//System.out.println("Generate section: "+section);
ExecutionTimer timer = new ExecutionTimer("MafBeanGenerator");
timer.startExecution("All");
timer.startExecution(section.getModule().getName()+"-"+section.getTitle()+"-ListItem");
files.add(new FileEntry(generateListItemBean(section)));
timer.stopExecution(section.getModule().getName()+"-"+section.getTitle()+"-ListItem");
files.add(new FileEntry(generateListItemSortType(section)));
List dialogs = section.getDialogs();
for (int i=0; i elements = new ArrayList();
elements.add(new MetaProperty("ownerId",MetaProperty.Type.STRING));
elements.add(new MetaProperty("position",MetaProperty.Type.INT));
elements.add(list.getContainedProperty());
elements.add(new MetaProperty("description",MetaProperty.Type.STRING));
clazz.setName(getContainerEntryFormName(list));
clazz.addInterface("FormBean");
startClassBody();
appendGenerationPoint("generateListElementForm");
for (int i=0; i columns = (List)((ArrayList)p.getColumns()).clone();
columns.add(0, new MetaProperty(p.getName()+"_ownerId", MetaProperty.Type.STRING));
columns.add(0, new MetaProperty(p.getName()+"_position", MetaProperty.Type.INT));
for (MetaProperty pr : columns)
appendStatement("private String "+p.extractSubName(pr));
emptyline();
for (int i=0; igenerateDialogForm.
*
* @param dialog a {@link net.anotheria.asg.generator.view.meta.MetaDialog} object.
* @param doc a {@link net.anotheria.asg.generator.meta.MetaDocument} object.
* @return a {@link net.anotheria.asg.generator.GeneratedClass} object.
*/
public GeneratedClass generateDialogForm(MetaDialog dialog, MetaDocument doc){
GeneratedClass clazz = new GeneratedClass();
startNewJob(clazz);
clazz.setPackageName(getPackage(doc));
clazz.addImport("net.anotheria.maf.bean.FormBean");
startClassBody();
appendGenerationPoint("generateDialogForm");
//this is only used if the multilingual support is enabled for the project AND document.
MetaFieldElement multilingualInstanceDisabledElement = new MetaFieldElement(FIELD_ML_DISABLED);
List elements = createMultilingualList(dialog.getElements(), doc);
for (MetaViewElement element : elements){
if (element instanceof MetaFieldElement){
MetaFieldElement field = (MetaFieldElement)element;
MetaProperty p = doc.getField(field.getName());
if (p.isLinked() || p instanceof MetaEnumerationProperty){
clazz.addImport("java.util.List");
clazz.addImport("net.anotheria.webutils.bean.LabelValueBean");
break;
}
}
}
clazz.setName(getDialogBeanName(dialog, doc));
clazz.addInterface("FormBean");
startClassBody();
appendGenerationPoint("generateDialogForm");
for (MetaViewElement element : elements){
if (element instanceof MetaFieldElement){
MetaFieldElement field = (MetaFieldElement)element;
String lang = getElementLanguage(field);
MetaProperty p = doc.getField(field.getName());
MetaProperty tmp = p instanceof MetaListProperty? new MetaProperty(p.getName(),MetaProperty.Type.INT): p;
if (element.isValidated()) { //TODO what about list validation?
for (MetaValidator validator : element.getValidators()){
clazz.addImport(validator.getClassName());
String key = StringUtils.isEmpty(validator.getKey())? "" : validator.getKey();
String message = StringUtils.isEmpty(validator.getDefaultError())? "" : validator.getDefaultError();
if(validator.isCustomValidator()) {
clazz.addImport("net.anotheria.maf.validation.annotations.ValidateCustom");
appendString("@ValidateCustom(validator=" + validator.getClassNameOnly()+".class, key=\""+key+"\", message=\""+message+"\")");
} else if (validator.isNumericValidator()){
boolean fractional = p.getType() == Type.FLOAT || p.getType() == Type.DOUBLE;
appendString("@"+validator.getClassNameOnly()+"(key=\""+key+"\", message=\""+message+"\", fractional="+fractional+")");
} else {
appendString("@"+validator.getClassNameOnly()+"(key=\""+key+"\", message=\""+message+"\")");
}
}
}
appendStatement("private "+tmp.toJavaType()+" "+tmp.getName(lang));
if (p.isLinked()){
MetaProperty collection = new MetaProperty(p.getName()+"Collection"+(lang==null?"":lang),MetaProperty.Type.LIST);
appendStatement("private "+collection.toJavaType()+" "+collection.getName());//hacky
appendStatement("private String "+p.getName()+"CurrentValue"+(lang==null?"":lang));
appendStatement("private String "+p.getName()+"IdOfCurrentValue"+(lang==null?"":lang));
}
if (p instanceof MetaEnumerationProperty){
MetaProperty collection = new MetaProperty(p.getName()+"Collection",MetaProperty.Type.LIST);
appendStatement("private "+collection.toJavaType()+" "+collection.getName());//hacky
appendStatement("private String "+p.getName()+"CurrentValue");
appendStatement("private String "+p.getName()+"IdOfCurrentValue");
}
}
}
emptyline();
for (MetaViewElement element : elements){
if (element instanceof MetaFieldElement)
generateFieldMethodsInDialog((MetaFieldElement)element, doc);
}
if (doc.isMultilingual()){
MetaProperty mlDisProp = doc.getField(multilingualInstanceDisabledElement.getName());
appendStatement("private "+mlDisProp.toJavaType()+" "+mlDisProp.getName());
emptyline();
generateFieldMethodsInDialog(multilingualInstanceDisabledElement, doc);
}
// add fields!!!! Lock!!!
generateAdditionalFields(doc,"locked", MetaProperty.Type.BOOLEAN,"LockableObject \"locked\" property. For object Locking.");
generateAdditionalFields(doc,"lockerId", MetaProperty.Type.STRING,"LockableObject \"lockerId\" property. For userName containing.");
generateAdditionalFields(doc,"lockingTime", MetaProperty.Type.STRING,"LockableObject \"lockingTime\" property.");
// add fields links to elements
for (MetaViewElement element : elements) {
if (element.isShowLink()) {
generateAdditionalFields(doc, element.getName()+"link", Type.STRING, "Link to "+element.getName());
}
}
emptyline();
return clazz;
}
/**
* getPackage.
*
* @param doc a {@link net.anotheria.asg.generator.meta.MetaDocument} object.
* @return a {@link java.lang.String} object.
*/
public static String getPackage(MetaDocument doc){
return getPackage(GeneratorDataRegistry.getInstance().getContext(), doc);
}
/**
* getPackage.
*
* @param module a {@link net.anotheria.asg.generator.meta.MetaModule} object.
* @return a {@link java.lang.String} object.
*/
public static String getPackage(MetaModule module){
return getPackage(GeneratorDataRegistry.getInstance().getContext(), module);
}
/**
* getPackage.
*
* @param context a {@link net.anotheria.asg.generator.Context} object.
* @param module a {@link net.anotheria.asg.generator.meta.MetaModule} object.
* @return a {@link java.lang.String} object.
*/
public static String getPackage(Context context, MetaModule module){
return context.getPackageName(module)+".bean";
}
/**
* getPackage.
*
* @param context a {@link net.anotheria.asg.generator.Context} object.
* @param doc a {@link net.anotheria.asg.generator.meta.MetaDocument} object.
* @return a {@link java.lang.String} object.
*/
public static String getPackage(Context context, MetaDocument doc){
return context.getPackageName(doc)+".bean";
}
/**
* getDialogBeanName.
*
* @param dialog a {@link net.anotheria.asg.generator.view.meta.MetaDialog} object.
* @param document a {@link net.anotheria.asg.generator.meta.MetaDocument} object.
* @return a {@link java.lang.String} object.
*/
public static String getDialogBeanName(MetaDialog dialog, MetaDocument document){
return StringUtils.capitalize(dialog.getName())+StringUtils.capitalize(document.getName())+"FB";
}
private void generateFieldMethodsInDialog(MetaFieldElement element, MetaDocument doc){
MetaProperty p = null;
// String lang = getElementLanguage(element);
p = doc.getField(element.getName());
if (p.isLinked() || p instanceof MetaEnumerationProperty){
MetaFieldElement pColl = new MetaFieldElement(element.getName()+"Collection");
MetaFieldElement pCurr = new MetaFieldElement(element.getName()+"CurrentValue");
MetaFieldElement pIdOfCurr = new MetaFieldElement(element.getName()+"IdOfCurrentValue");
//;
if (p.isMultilingual()){
String l = getElementLanguage(element);
MetaListProperty pCollProperty = new MetaListProperty(element.getName()+"Collection", new MetaProperty("temp", new ObjectType("LabelValueBean")));
pCollProperty.setMultilingual(true);
generateMethods(new MultilingualFieldElement(l, pColl), pCollProperty);
generateMethods(new MultilingualFieldElement(l, pCurr), new MetaProperty(element.getName()+"CurrentValue", MetaProperty.Type.STRING, true));
generateMethods(new MultilingualFieldElement(l, pIdOfCurr), new MetaProperty(element.getName()+"IdOfCurrentValue", MetaProperty.Type.STRING, true));
}else{
generateMethods(pColl, new MetaListProperty(element.getName()+"Collection", new MetaProperty("temp", new ObjectType("LabelValueBean"))));
generateMethods(pCurr, new MetaProperty(element.getName()+"CurrentValue", MetaProperty.Type.STRING));
generateMethods(pIdOfCurr, new MetaProperty(element.getName()+"IdOfCurrentValue", MetaProperty.Type.STRING));
}
}
MetaProperty tmp = p instanceof MetaListProperty? new MetaProperty(p.getName(),MetaProperty.Type.INT): p;
generateMethods(element, tmp);
}
/**
* Actually allow us add fields such Lock - etc.
* @param doc document itself
* @param fieldName name of field
* @param fieldType field type
* @param comment comment for the field
*/
private void generateAdditionalFields(MetaDocument doc, String fieldName, MetaProperty.Type fieldType, String comment) {
if (doc.getParentModule().getStorageType().equals(StorageType.CMS)) {
MetaFieldElement fieldElement = new MetaFieldElement(fieldName);
MetaProperty maField = new MetaProperty(fieldElement.getName(),fieldType);
appendComment(comment);
appendStatement("private " + maField.toJavaType() + " " + maField.getName());
emptyline();
generateMethods(fieldElement,maField);
}
}
private GeneratedClass generateListItemSortType(MetaModuleSection section){
List elements = section.getElements();
boolean containsComparable = false;
for (MetaViewElement element : elements){
if (element.isComparable()){
containsComparable = true;
break;
}
}
if (!containsComparable)
return null;
GeneratedClass clazz = new GeneratedClass();
startNewJob(clazz);
clazz.setPackageName(getPackage(section.getDocument()));
clazz.addImport("net.anotheria.util.sorter.SortType");
clazz.setName(getListItemBeanSortTypeName(section.getDocument()));
clazz.setParent("SortType");
startClassBody();
appendGenerationPoint("generateListItemSortType");
MetaViewElement defaultElem = section.getDefaultSortable();
String defaultElemName = null;
int lastIndex = 1;
elements = createMultilingualList(elements, section.getDocument());
for (int i=0; i origElements = section.getElements();
if (doc.getName().equals("BoxType"))
debugTest= true;
if (doc.getName().equals("Pagex"))
debugTest= true;
if (debugTest){
System.out.println("%%% DEBUG ON %%%");
}
List elements = createMultilingualList(origElements, doc);
//elements.addAll(origElements);
MetaFieldElement plainId = new MetaFieldElement("plainId");
plainId.setComparable(false);
plainId.setReadonly(true);
plainId.setDecorator(null);
elements.add(plainId);
MetaFieldElement versionInfo = new MetaFieldElement("documentLastUpdateTimestamp");
plainId.setComparable(false);
plainId.setReadonly(true);
plainId.setDecorator(null);
elements.add(versionInfo);
clazz.setPackageName(getPackage(section.getDocument()));
boolean containsComparable = false;
for (MetaViewElement element : elements){
if (element.isComparable()){
containsComparable = true;
break;
}
}
for(MetaViewElement element: elements){
if (!(element instanceof MetaFieldElement))
continue;
MetaFieldElement field = (MetaFieldElement)element;
MetaProperty p = doc.getField(field.getName());
if(!(p instanceof MetaListProperty))
continue;
clazz.addImport("java.util.List");
break;
}
clazz.setName(getListItemBeanName(section.getDocument()));
//section.getModule().
startClassBody();
appendGenerationPoint("generateListItemBean");
for (int i=0; i elements){
appendString("public int compareTo(IComparable anotherComparable, int method){");
increaseIdent();
appendStatement(getListItemBeanName(doc)+" anotherBean = ("+getListItemBeanName(doc)+") anotherComparable");
appendString("switch(method){");
increaseIdent();
for (MetaViewElement element: elements){
if (!element.isComparable())
continue;
MetaFieldElement field = (MetaFieldElement)element;
MetaProperty p = doc.getField(field.getName());
String lang = getElementLanguage(element);
String caseDecl = lang != null? getListItemBeanSortTypeName(doc)+".SORT_BY_"+p.getName(lang).toUpperCase():
getListItemBeanSortTypeName(doc)+".SORT_BY_"+p.getName().toUpperCase();
appendString("case "+caseDecl+":");
String type2compare = p instanceof MetaEnumerationProperty? "String": StringUtils.capitalize(p.toJavaErasedType());
String retDecl = "return BasicComparable.compare"+type2compare;
if (element.getName().equals("id")){
retDecl = "return BasicComparable."+element.getSortingType().getCompareCall();
}
retDecl += field.getDecorator()!=null? "("+p.getName("ForSorting", lang)+", anotherBean."+p.getName("ForSorting", lang)+")" : "("+p.getName(lang)+", anotherBean."+p.getName(lang)+")";
appendIncreasedStatement(retDecl);
}
appendString("default:");
appendIncreasedStatement("throw new RuntimeException(\"Sort method \"+method+\" is not supported.\")");
closeBlockNEW();
closeBlockNEW();
}
private void generateFieldMethods(MetaFieldElement element, MetaDocument doc){
MetaProperty p = doc.getField(element.getName());
if (p instanceof MetaEnumerationProperty){
MetaProperty tmp = new MetaProperty(p.getName(), MetaProperty.Type.STRING);
generateMethods(element, tmp);
return;
}
if (debugTest)
System.out.println(element.getName()+" - "+element.getDecorator());
if (element.getDecorator()!=null){
MetaProperty tmpForSorting = (MetaProperty) p.clone();//new MetaProperty(p.getName()+"ForSorting", p.getType());
tmpForSorting.setName(tmpForSorting.getName()+"ForSorting");
generateMethodsForSorting(element, tmpForSorting);
//if this field has a decorator we have to generate string methods instaed of original methods.
p = new MetaProperty(p.getName(), MetaProperty.Type.STRING, p.isMultilingual());
}
generateMethods(element, p);
}
private void generateMethods(MetaViewElement element, MetaProperty p){
if (debugTest)
System.out.println(" GenerateMethods for "+element+" ... "+p);
if (element instanceof MultilingualFieldElement){
generateMethodsMultilinguage((MultilingualFieldElement)element, p);
return;
}
appendString("public void "+p.toBeanSetter()+"("+p.toJavaType()+" "+p.getName()+" ){");
increaseIdent();
appendStatement("this."+p.getName()+" = "+p.getName());
closeBlockNEW();
emptyline();
appendString("public "+p.toJavaType()+" "+p.toBeanGetter()+"(){");
increaseIdent();
appendStatement("return "+p.getName());
closeBlockNEW();
emptyline();
}
private void generateMethodsForSorting(MetaViewElement element, MetaProperty p){
if (element instanceof MultilingualFieldElement){
generateMethodsMultilinguage((MultilingualFieldElement)element, p);
return;
}
appendString("public void "+p.toBeanSetter()+"("+p.toJavaType()+" "+p.getName()+" ){");
increaseIdent();
appendStatement("this."+p.getName()+" = "+element.getSortingType().convertValue(p.getName()));
closeBlockNEW();
emptyline();
if (element.getName().equals("id"))
appendString("public "+element.getSortingType().getJavaType()+" "+p.toBeanGetter()+"(){");
else
appendString("public "+p.toJavaType()+" "+p.toBeanGetter()+"(){");
increaseIdent();
appendStatement("return "+p.getName());
closeBlockNEW();
emptyline();
}
private void generateMethodsMultilinguage(MultilingualFieldElement element, MetaProperty p){
//System.out.println("--- m "+p+", "+p.getType());
if (p.getType() == MetaProperty.Type.LIST)
appendString("@SuppressWarnings(\"unchecked\")");
appendString("public void "+p.toBeanSetter(element.getLanguage())+"("+p.toJavaType()+" "+p.getName()+" ){");
increaseIdent();
appendStatement("this."+p.getName(element.getLanguage())+" = "+p.getName());
closeBlockNEW();
emptyline();
if (p.getType() == MetaProperty.Type.LIST)
appendString("@SuppressWarnings(\"unchecked\")");
appendString("public "+p.toJavaType()+" "+p.toBeanGetter(element.getLanguage())+"(){");
increaseIdent();
appendStatement("return "+p.getName(element.getLanguage()));
closeBlockNEW();
emptyline();
}
/**
* getListItemBeanSortTypeImport.
*
* @param context a {@link net.anotheria.asg.generator.Context} object.
* @param doc a {@link net.anotheria.asg.generator.meta.MetaDocument} object.
* @return a {@link java.lang.String} object.
*/
public static String getListItemBeanSortTypeImport(Context context, MetaDocument doc){
return getPackage(context, doc)+"."+getListItemBeanSortTypeName(doc);
}
/**
* getListItemBeanSortTypeName.
*
* @param doc a {@link net.anotheria.asg.generator.meta.MetaDocument} object.
* @return a {@link java.lang.String} object.
*/
public static String getListItemBeanSortTypeName(MetaDocument doc){
return getListItemBeanName(doc)+"SortType";
}
/**
* getListItemBeanName.
*
* @param doc a {@link net.anotheria.asg.generator.meta.MetaDocument} object.
* @return a {@link java.lang.String} object.
*/
public static String getListItemBeanName(MetaDocument doc){
return doc.getName()+"ListItemBean";
}
/**
* getDialogBeanImport.
*
* @param dialog a {@link net.anotheria.asg.generator.view.meta.MetaDialog} object.
* @param doc a {@link net.anotheria.asg.generator.meta.MetaDocument} object.
* @return a {@link java.lang.String} object.
*/
public static String getDialogBeanImport(MetaDialog dialog, MetaDocument doc){
return getPackage(GeneratorDataRegistry.getInstance().getContext(), doc)+"."+getDialogBeanName(dialog, doc);
}
/**
* getListItemBeanImport.
*
* @param context a {@link net.anotheria.asg.generator.Context} object.
* @param doc a {@link net.anotheria.asg.generator.meta.MetaDocument} object.
* @return a {@link java.lang.String} object.
*/
public static String getListItemBeanImport(Context context, MetaDocument doc){
return getPackage(context, doc)+"."+getListItemBeanName(doc);
}
/**
* getContainerEntryFormImport.
*
* @param doc a {@link net.anotheria.asg.generator.meta.MetaDocument} object.
* @param p a {@link net.anotheria.asg.generator.meta.MetaContainerProperty} object.
* @return a {@link java.lang.String} object.
*/
public static String getContainerEntryFormImport(MetaDocument doc, MetaContainerProperty p){
return GeneratorDataRegistry.getInstance().getContext().getPackageName(doc)+".bean."+getContainerEntryFormName(p);
}
/**
* getContainerEntryFormName.
*
* @param p a {@link net.anotheria.asg.generator.meta.MetaContainerProperty} object.
* @return a {@link java.lang.String} object.
*/
public static String getContainerEntryFormName(MetaContainerProperty p){
return StringUtils.capitalize(p.getName())+p.getContainerEntryName()+"FB";
}
/**
* getContainerQuickAddFormImport.
*
* @param doc a {@link net.anotheria.asg.generator.meta.MetaDocument} object.
* @param p a {@link net.anotheria.asg.generator.meta.MetaContainerProperty} object.
* @return a {@link java.lang.String} object.
*/
public static String getContainerQuickAddFormImport(MetaDocument doc, MetaContainerProperty p){
return GeneratorDataRegistry.getInstance().getContext().getPackageName(doc)+".bean."+getContainerQuickAddFormName(p);
}
/**
* getContainerQuickAddFormName.
*
* @param p a {@link net.anotheria.asg.generator.meta.MetaContainerProperty} object.
* @return a {@link java.lang.String} object.
*/
public static String getContainerQuickAddFormName(MetaContainerProperty p){
return StringUtils.capitalize(p.getName())+"QuickAddFB";
}
/**
* getFormBeanImport.
*
* @param form a {@link net.anotheria.asg.generator.forms.meta.MetaForm} object.
* @return a {@link java.lang.String} object.
*/
public static String getFormBeanImport(MetaForm form){
return getPackage()+"."+getFormBeanName(form);
}
/**
* getPackage.
*
* @return a {@link java.lang.String} object.
*/
@Deprecated
public static String getPackage(){
return GeneratorDataRegistry.getInstance().getContext().getPackageName()+".bean";
}
/**
* getFormBeanName.
*
* @param form a {@link net.anotheria.asg.generator.forms.meta.MetaForm} object.
* @return a {@link java.lang.String} object.
*/
public static String getFormBeanName(MetaForm form){
return StringUtils.capitalize(form.getId())+"AutoForm";
}
}