All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.anotheria.asg.generator.model.db.VOGenerator Maven / Gradle / Ivy

package net.anotheria.asg.generator.model.db;

import java.util.ArrayList;
import java.util.List;

import net.anotheria.asg.generator.CommentGenerator;
import net.anotheria.asg.generator.Context;
import net.anotheria.asg.generator.FileEntry;
import net.anotheria.asg.generator.GeneratedClass;
import net.anotheria.asg.generator.GeneratorDataRegistry;
import net.anotheria.asg.generator.IGenerateable;
import net.anotheria.asg.generator.IGenerator;
import net.anotheria.asg.generator.meta.MetaContainerProperty;
import net.anotheria.asg.generator.meta.MetaDocument;
import net.anotheria.asg.generator.meta.MetaGenericProperty;
import net.anotheria.asg.generator.meta.MetaListProperty;
import net.anotheria.asg.generator.meta.MetaProperty;
import net.anotheria.asg.generator.meta.MetaTableProperty;
import net.anotheria.asg.generator.model.AbstractDataObjectGenerator;
import net.anotheria.util.StringUtils;

/**
 * Generates value objects for db bound document implementation.
 * @author another
 */
public class VOGenerator extends AbstractDataObjectGenerator implements IGenerator{

	/**
	 * Constant for dao-created column name.
	 */
	public static final String DAO_CREATED = "daoCreated";
	/**
	 * Constant for dao-updaten column name.
	 */
	public static final String DAO_UPDATED = "daoUpdated";
	/**
	 * The id property.
	 */
	MetaProperty id = new MetaProperty("id",MetaProperty.Type.STRING);
	/**
	 * The dao created property (not included in document property set).
	 */
	MetaProperty daoCreated = new MetaProperty(DAO_CREATED, MetaProperty.Type.LONG);
	/**
	 * The dao created property (not included in document property set).
	 */
	MetaProperty daoUpdated = new MetaProperty(DAO_UPDATED, MetaProperty.Type.LONG);
	
	public List generate(IGenerateable gdoc){
		MetaDocument doc = (MetaDocument)gdoc;
		id.setReadonly(true);
		
		
		List _ret = new ArrayList();
		_ret.add(new FileEntry(generateDocument(doc)));
		_ret.add(new FileEntry(generateDocumentFactory(doc)));
		return _ret;
	}
	
	public String getDataObjectImplName(MetaDocument doc){
		return getDocumentImplName(doc);
	}
	
	public static String getDocumentImplName(MetaDocument doc){
		return doc.getName()+"VO";
	}
	

	public static String getClassImplName(MetaDocument doc){
		return doc.getName()+"Document";
	}
	
	private GeneratedClass generateDocument(MetaDocument doc){
		GeneratedClass clazz = new GeneratedClass();
		startNewJob(clazz);
		
		clazz.setPackageName(getPackageName(doc));
		clazz.setTypeComment(CommentGenerator.generateJavaTypeComment("The implementation of the "+(doc.getName())+".", this));

	 
		for (int i=0; i");
		}
		

		appendMark(1);
		
		clazz.setName(getDocumentImplName(doc));
		clazz.setParent("AbstractVO");

		startClassBody();
		appendMark(2);
		generatePropertyFields(doc);
		emptyline();
		appendMark(3);
		generateDefaultConstructor(doc);
		emptyline();
		appendMark(4);
		generateCloneConstructor(doc);
		emptyline();
		generateBuilderConstructor(doc);
		emptyline();
		generatePropertyAccessMethods(doc);
		emptyline();
		generateToStringMethod(doc);
		emptyline();
		generateCloneMethod(doc);
		emptyline();
		generateCopyMethod(doc);
		emptyline();
		generateGetPropertyValueMethod(doc);
		emptyline();
		generateAdditionalMethods(doc);
		
		if (doc.isComparable()){
			emptyline();
			generateCompareMethod(doc);
		}
		
		emptyline();
		generateDefNameMethod(doc);
		
		emptyline();
		generateDefParentNameMethod(doc);

		emptyline();
		generateGetFootprintMethod(doc);
		
		emptyline();
		generateEqualsMethod(doc);

		return clazz;
	}
	
	private void generateDefaultConstructor(MetaDocument doc){
		appendString("public "+getDocumentImplName(doc)+"(String anId){");
		increaseIdent();
		appendStatement("id = anId");
		append(closeBlock());
	}
	
	private void generateCloneConstructor(MetaDocument doc){
		appendString("public "+getDocumentImplName(doc)+"("+getDocumentImplName(doc)+" toClone){");
		increaseIdent();
		//TODO add multilingual support 
		appendStatement("this.id = toClone.id");
		appendStatement("copyAttributesFrom(toClone)");
		append(closeBlock());
	}

	private void generateBuilderConstructor(MetaDocument doc){
		appendString(getDocumentImplName(doc)+"("+getDocumentBuilderName(doc)+" builder){");
		increaseIdent();
		//TODO add multilingual support 
		appendStatement("id = "+quote(""));
		for (MetaProperty p : doc.getProperties()){
			appendStatement(p.getName().toLowerCase()," = ", "builder.", p.getName());
		}
		
		for (MetaProperty p : doc.getLinks()){
			appendStatement(p.getName().toLowerCase()," = ", "builder.", p.getName());
		}

		append(closeBlock());
	}

	private void generatePropertyFields(MetaDocument doc){
		_generatePropertyField(id);
		_generatePropertyFields(doc.getProperties());
		_generatePropertyFields(doc.getLinks());
		
		//support for dao information
		_generatePropertyField(daoCreated);
		_generatePropertyField(daoUpdated);
	}
	
	private MetaProperty getMetaGenericProperty(MetaListProperty p){
		MetaProperty tmp = new MetaGenericProperty(p.getName(), MetaProperty.Type.LIST, p.getContainedProperty());
		if (p.isMultilingual())
			tmp.setMultilingual(true);
		return tmp;
	}
	
	private void _generatePropertyFields(List propertyList){
		for (int i=0; i columns = ((MetaTableProperty)p).getColumns();
				for (int t=0; t properties){
		
		for (int i=0; i columns = p.getColumns();
		for (int t=0; t columns = p.getColumns();
		for (int t=0; t props = doc.getProperties();
		for (int i=0; i columns = ((MetaTableProperty)p).getColumns();
				for (int t=0; tproperties = new ArrayList(doc.getProperties());
		properties.addAll(doc.getLinks());
		// Generate getter for ID property
		appendString("if ("+id.toNameConstant()+".equals(propertyName))");
		appendIncreasedStatement("return get"+id.getAccesserName()+"()");
		// Generate getters for other properties 
		for (MetaProperty p : properties){
			appendString("if ("+p.toNameConstant()+".equals(propertyName))");
			appendIncreasedStatement("return get"+p.getAccesserName()+"()");
					
		}

		appendStatement("throw new RuntimeException("+quote("No property getter for ")+"+propertyName)");
		append(closeBlock());
	}
	


	public static final String getDocumentImport(Context context, MetaDocument doc){
		return context.getPackageName(doc)+".data."+getDocumentImplName(doc);
	}
	
	private void generateAdditionalMethods(MetaDocument doc){
		
		List properties = doc.getProperties();
		for (MetaProperty p : properties){
			if (p instanceof MetaContainerProperty)
				generateContainerMethods((MetaContainerProperty)p);
			if (p instanceof MetaTableProperty)
				generateTableMethods((MetaTableProperty)p);
			if (p instanceof MetaListProperty)
				generateListMethods((MetaListProperty)p);
		}
		
	}
	
	private void generateContainerMethods(MetaContainerProperty container){

		appendString("public int "+getContainerSizeGetterName(container)+"(){");
		increaseIdent();
//		MetaProperty pr = container instanceof MetaTableProperty ? 
//			((MetaTableProperty)container).getColumns().get(0) :
//			container;
//		appendStatement("return getList("+pr.toNameConstant()+").size()"); 
		appendStatement("return get"+container.getAccesserName()+"().size()");
		append(closeBlock());
		emptyline();
	}
	
	private void generateListMethods(MetaListProperty list){
		MetaProperty genericList = getMetaGenericProperty(list);
		MetaProperty c = list.getContainedProperty();

		String decl = "public void "+getContainerEntryAdderName(list)+"(";
		decl += c.toJavaType()+" "+c.getName();
		decl += "){";
		appendString(decl);
		increaseIdent();
		
		appendStatement(c.toJavaObjectType()+" p = new "+c.toJavaObjectType()+"("+c.getName()+")");
		appendStatement(genericList.toJavaType() + " tmp = get"+list.getAccesserName()+"()");
		appendStatement("tmp.add(p)");
//		appendStatement("set"+list.getAccesserName()+"(tmp)");
		append(closeBlock());
		emptyline();
		
		
		appendString("public void "+getContainerEntryDeleterName(list)+"(int index){");
		increaseIdent();
//		appendStatement("getListProperty("+list.toNameConstant()+").remove(index)"); 
		appendStatement("get"+list.getAccesserName()+"().remove(index)");
		append(closeBlock());
		emptyline();
		
		appendString("public void "+getContainerEntrySwapperName(list)+"(int index1, int index2){");
		increaseIdent();
		appendStatement(c.toJavaType()+" tmp1, tmp2");
//		appendStatement("tmp1 = (("+c.toJavaType()+")getList("+list.toNameConstant()+").get(index1)).get"+c.toJavaType()+"()");
//		appendStatement("tmp2 = (("+c.toJavaType()+")getList("+list.toNameConstant()+").get(index2)).get"+c.toJavaType()+"()");
//		appendStatement("(("+c.toJavaType()+"Property"+")getList("+list.toNameConstant()+").get(index1)).set"+c.toJavaType()+"(tmp2)");
//		appendStatement("(("+c.toJavaType()+"Property"+")getList("+list.toNameConstant()+").get(index2)).set"+c.toJavaType()+"(tmp1)");
		appendStatement(genericList.toJavaType() + " tmpList = get"+list.getAccesserName()+"()");
		appendStatement("tmp1 = (("+c.toJavaObjectType()+")tmpList.get(index1))");
		appendStatement("tmp2 = (("+c.toJavaObjectType()+")tmpList.get(index2))");
		appendStatement("tmpList.set(index1, tmp2)");
		appendStatement("tmpList.set(index2, tmp1)");
		append(closeBlock());
		emptyline();

		appendString("public "+c.toJavaType()+ " "+getListElementGetterName(list)+"(int index){");
		increaseIdent();
//		appendStatement(c.toJavaType()+"Property p = ("+c.toJavaType()+"Property"+")getList("+list.toNameConstant()+").get(index)");
//		appendStatement("return p.get"+c.toJavaType()+"()");
		appendStatement(c.toJavaType()+" p = ("+c.toJavaObjectType()+""+")get"+list.getAccesserName()+"().get(index)");
		appendStatement("return p");
		append(closeBlock());
		emptyline();
	}
	
	private void generateTableMethods(MetaTableProperty table){
		List columns = table.getColumns();
		
		String decl = "public void "+getContainerEntryAdderName(table)+"(";
		for (int i =0; i properties = extractSortableProperties(doc);

		for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy