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

net.anotheria.asg.generator.model.docs.DocumentGenerator Maven / Gradle / Ivy

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

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

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.MetaEnumerationProperty;
import net.anotheria.asg.generator.meta.MetaGenericListProperty;
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.asg.generator.model.DataFacadeGenerator;
import net.anotheria.util.StringUtils;

/**
 * This generator generates an ano-doc framework based implementation of the data object interface previously generated
 * by the DataFacadeGenerator. It also generates an according factory. 
 * @author another
 */
public class DocumentGenerator extends AbstractDataObjectGenerator implements IGenerator{

	public static final String PROPERTY_DECLARATION = "public static final String ";	
	public static final String GET_CURRENT_LANG = "ContextManager.getCallContext().getCurrentLanguage()";

	public List generate(IGenerateable gdoc){
		MetaDocument doc = (MetaDocument)gdoc;
		
		//System.out.println(ret);
		List _ret = new ArrayList();
		_ret.add(new FileEntry(generateDocument(doc)));
		_ret.add(new FileEntry(generateDocumentFactory(doc)));
		return _ret;
	}
	
	public static String getDocumentName(MetaDocument doc){
		return doc.getName()+"Document";
	}

	public String getDataObjectImplName(MetaDocument doc){
		return getDocumentImplName(doc);
	}

	public static String getDocumentImplName(MetaDocument doc){
		return doc.getName()+"Document";
	}
	

	public static String getClassImplName(MetaDocument doc){
		return doc.getName()+"Document";
	}

	public static String getSortTypeName(MetaDocument doc){
		return doc.getName()+"SortType";
	}
	
	private GeneratedClass generateDocument(MetaDocument doc){
		GeneratedClass clazz = new GeneratedClass();
		startNewJob(clazz);
		
		clazz.setPackageName(getPackageName(doc));
		clazz.addImport("net.anotheria.asg.data.AbstractASGDocument");

		for (MetaProperty p:doc.getProperties()){
			if (p instanceof MetaContainerProperty){
//				appendImport("java.util.List");
				clazz.addImport("java.util.List");
				if (p instanceof MetaTableProperty)
					clazz.addImport("java.util.ArrayList");
//					appendImport("java.util.ArrayList");
				
				if(p instanceof MetaListProperty)
					clazz.addImport("net.anotheria.anodoc.data." + StringUtils.capitalize(((MetaListProperty)p).getContainedProperty().toJavaType()) + "Property");
//				appendImport("net.anotheria.anodoc.data.StringProperty");
//				listImported = true;
								
//				break;
			}
		}
		
		if (doc.isMultilingual() && GeneratorDataRegistry.getInstance().getContext().areLanguagesSupported()){
			clazz.addImport("net.anotheria.anodoc.util.context.ContextManager");
			clazz.addImport("net.anotheria.anodoc.data.NoSuchPropertyException");
			clazz.addImport("net.anotheria.anodoc.data.BooleanProperty");
		}
		
		clazz.addImport("net.anotheria.util.crypt.MD5Util");

		
		
		clazz.addInterface(doc.getName());
		if (doc.isComparable()){
			clazz.addInterface("IComparable");
			clazz.addImport("net.anotheria.util.sorter.IComparable");
			clazz.addImport("net.anotheria.util.BasicComparable");
		}
		
		if (doc.isMultilingual()){
			clazz.addImport("net.anotheria.asg.data.MultilingualObject");
			clazz.addInterface("MultilingualObject");
		}
		
		clazz.setName(getDocumentName(doc));
		clazz.setParent("AbstractASGDocument");
		
		startClassBody();
		
		generateDefaultConstructor(doc);
		emptyline();
		generateCloneConstructor(doc);
		emptyline();
		generateBuilderConstructor(doc);
		emptyline();
		generatePropertyAccessMethods(doc);
		emptyline();
		generateToStringMethod(doc);
		emptyline();
		generateAdditionalMethods(doc);
		
		if (doc.isComparable()){
			emptyline();
			generateCompareMethod(doc);
		}
		
		emptyline();
		generateDefNameMethod(doc);
		emptyline();
		generateDefParentNameMethod(doc);
		emptyline();
		generateGetFootprintMethod(doc);
		
		if (DataFacadeGenerator.hasLanguageCopyMethods(doc)){
			generateLanguageCopyMethods(doc);
			emptyline();
		}

		generateMultilingualSwitchSupport(doc);

		emptyline();
		generateEqualsMethod(doc);
		
		//emptyline();
		//generateCopyMethod(doc);
		
		return clazz;
	}
	
	private void generateLanguageCopyMethods(MetaDocument doc){
		
		Context context = GeneratorDataRegistry.getInstance().getContext(); 
		
		//first the common method lang2lang
		appendString("public void "+DataFacadeGenerator.getCopyMethodName()+"(String sourceLanguage, String destLanguage){");
		increaseIdent();
		for (String srclang : context.getLanguages()){
			for (String targetlang : context.getLanguages()){
				if (!srclang.equals(targetlang)){
					appendString("if (sourceLanguage.equals("+quote(srclang)+") && destLanguage.equals("+quote(targetlang)+"))");
					appendIncreasedStatement(DataFacadeGenerator.getCopyMethodName(srclang, targetlang)+"()");
				}
			}
		}
		
		closeBlockNEW();
		emptyline();
		
		
		//now the concrete methods
		for (String srclang : context.getLanguages()){
			for (String targetlang : context.getLanguages()){
				if (!srclang.equals(targetlang)){
					appendComment("Copies all multilingual properties from language "+srclang+" to language "+targetlang);
					appendString("public void "+DataFacadeGenerator.getCopyMethodName(srclang, targetlang)+"(){");
					increaseIdent();
					for (MetaProperty p : doc.getProperties()){
						if (p.isMultilingual()){
							String copyCall = p.toSetter(targetlang)+"(";
							copyCall += p.toGetter(srclang)+"()";
							copyCall += ")";
							appendStatement(copyCall);
						}
					}
					closeBlockNEW();
					emptyline();
				}
			}
		}
		
	}
	
	
	private String generateDefaultConstructor(MetaDocument doc){
		String ret = "";
		appendString("public "+getDocumentName(doc)+"(String id){");
		increaseIdent();
		appendStatement("super(id)");
		closeBlockNEW();
		return ret;
	}
	
	private String generateCloneConstructor(MetaDocument doc){
		String ret = "";
		appendString("public "+getDocumentName(doc)+"("+getDocumentName(doc)+" toClone){");
		increaseIdent();
		appendStatement("super(toClone)");
		closeBlockNEW();
		return ret;
	}

	private void generateBuilderConstructor(MetaDocument doc){
		appendString(getDocumentImplName(doc)+"("+getDocumentBuilderName(doc)+" builder){");
		increaseIdent();
		appendStatement("super("+quote("")+")");
		for (MetaProperty p : doc.getProperties()){
			appendStatement("set", p.getAccesserName(), "(builder.", p.getName(), ")");
		}
		
		for (MetaProperty p : doc.getLinks()){
			appendStatement("set", p.getAccesserName(), "(builder.", p.getName(), ")");
		}

		closeBlockNEW();
	}

	private String generatePropertyAccessMethods(MetaDocument doc){
		String ret = "";
		
		ret += _generatePropertyAccessMethods(doc.getProperties());
		ret += _generatePropertyAccessMethods(doc.getLinks());
		return ret;
	}
	
	private String _generatePropertyAccessMethods(List properties){
		String ret = "";
		
		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 = 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){
		
		if (container.isMultilingual()){
			generateContainerMethodsMultilingual(container);
			return;
		}
		
		appendString("public int "+getContainerSizeGetterName(container)+"(){");
		increaseIdent();
		MetaProperty pr = container instanceof MetaTableProperty ? 
			(MetaProperty) ((MetaTableProperty)container).getColumns().get(0) :
			container;
		appendStatement("return getList("+pr.toNameConstant()+").size()"); 
		closeBlockNEW();
		emptyline();
		
		
	}
	
	private void generateContainerMethodsMultilingual(MetaContainerProperty container){
		for (String l : GeneratorDataRegistry.getInstance().getContext().getLanguages()){
			appendString("public int "+getContainerSizeGetterName(container, l)+"(){");
			increaseIdent();
			MetaProperty pr = container instanceof MetaTableProperty ? 
					(MetaProperty) ((MetaTableProperty)container).getColumns().get(0) :
						container;
					appendStatement("return getList("+pr.toNameConstant(l)+").size()"); 
					closeBlockNEW();
					emptyline();
		}
		
		appendString("public int "+getContainerSizeGetterName(container)+"(){");
		increaseIdent();
		appendStatement("return getList("+quote(container.getName()+"_")+"+"+GET_CURRENT_LANG+").size()"); 
		closeBlockNEW();
		emptyline();
//		appendStatement("return "+p.toPropertyGetter()+"("+quote(p.getName()+"_")+"+"+v+")");

		
	}

	private void generateListMethods(MetaListProperty list){
		
		if (list.isMultilingual()){
			generateListMethodsMultilingual(list);
			return;
		}

		MetaProperty c = list.getContainedProperty();
		String accesserType = StringUtils.capitalize(c.toJavaType()); 

		String decl = "public void "+getContainerEntryAdderName(list)+"(";
		decl += c.toJavaType()+" "+c.getName();
		decl += "){";
		appendString(decl);
		increaseIdent();
		
		
//		appendStatement("getListPropertyAnyCase("+list.toNameConstant()+").add(new "+c.toJavaType()+"Property("+c.getName()+", "+c.getName()+"))");
		if (c instanceof MetaEnumerationProperty) 
			openFun("if (!getListPropertyAnyCase(" + list.toNameConstant() + ").getList().contains(new " + accesserType + "Property(" + quote("") + " + " + c.getName() + ", " + c.getName() + ")))");
		appendStatement("getListPropertyAnyCase("+list.toNameConstant()+").add(new "+accesserType+"Property("+quote("")+" + "+c.getName()+", "+c.getName()+"))");
		if (c instanceof MetaEnumerationProperty)
			closeBlock("if");
		closeBlock("method");
		emptyline();
		
		
		appendString("public void "+getContainerEntryDeleterName(list)+"(int index){");
		increaseIdent();
		appendStatement("getListProperty("+list.toNameConstant()+").remove(index)");
		closeBlock("method");
		emptyline();
		
		appendString("public void "+getContainerEntrySwapperName(list)+"(int index1, int index2){");
		increaseIdent();
		appendStatement(c.toJavaType()+" tmp1, tmp2");
//		appendStatement("tmp1 = (("+c.toJavaType()+"Property"+")getList("+list.toNameConstant()+").get(index1)).get"+c.toJavaType()+"()");
//		appendStatement("tmp2 = (("+c.toJavaType()+"Property"+")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("tmp1 = (("+accesserType+"Property"+")getList("+list.toNameConstant()+").get(index1)).get"+accesserType+"()");
		appendStatement("tmp2 = (("+accesserType+"Property"+")getList("+list.toNameConstant()+").get(index2)).get"+accesserType+"()");
		appendStatement("(("+accesserType+"Property"+")getList("+list.toNameConstant()+").get(index1)).set"+accesserType+"(tmp2)");
		appendStatement("(("+accesserType+"Property"+")getList("+list.toNameConstant()+").get(index2)).set"+accesserType+"(tmp1)");
		closeBlock("method");
		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(accesserType+"Property p = ("+accesserType+"Property"+")getList("+list.toNameConstant()+").get(index)");
		appendStatement("return p.get"+accesserType+"()");
		closeBlock("method");
		emptyline();

	}
	
	private void generateListMethodsMultilingual(MetaListProperty list){

		MetaProperty c = list.getContainedProperty();
		String accesserType = StringUtils.capitalize(c.toJavaType());

		for (String l : GeneratorDataRegistry.getInstance().getContext().getLanguages()){
			String decl = "public void "+getContainerEntryAdderName(list, l)+"(";
			decl += c.toJavaType()+" "+c.getName();
			decl += "){";
			appendString(decl);
			increaseIdent();
			
//			appendStatement("getListPropertyAnyCase("+list.toNameConstant(l)+").add(new "+c.toJavaType()+"Property("+c.getName()+", "+c.getName()+"))");
			appendStatement("getListPropertyAnyCase("+list.toNameConstant(l)+").add(new "+accesserType+"Property("+quote("")+" + "+c.getName()+", "+c.getName()+"))");
			closeBlockNEW();
			emptyline();
			
			
			appendString("public void "+getContainerEntryDeleterName(list, l)+"(int index){");
			increaseIdent();
			appendStatement("getListProperty("+list.toNameConstant(l)+").remove(index)"); 
			closeBlockNEW();
			emptyline();
			
			appendString("public void "+getContainerEntrySwapperName(list, l)+"(int index1, int index2){");
			increaseIdent();
			appendStatement(c.toJavaType()+" tmp1, tmp2");
//			appendStatement("tmp1 = (("+c.toJavaType()+"Property"+")getList("+list.toNameConstant(l)+").get(index1)).get"+c.toJavaType()+"()");
//			appendStatement("tmp2 = (("+c.toJavaType()+"Property"+")getList("+list.toNameConstant(l)+").get(index2)).get"+c.toJavaType()+"()");
//			appendStatement("(("+c.toJavaType()+"Property"+")getList("+list.toNameConstant(l)+").get(index1)).set"+c.toJavaType()+"(tmp2)");
//			appendStatement("(("+c.toJavaType()+"Property"+")getList("+list.toNameConstant(l)+").get(index2)).set"+c.toJavaType()+"(tmp1)");
			appendStatement("tmp1 = (("+accesserType+"Property"+")getList("+list.toNameConstant(l)+").get(index1)).get"+accesserType+"()");
			appendStatement("tmp2 = (("+accesserType+"Property"+")getList("+list.toNameConstant(l)+").get(index2)).get"+accesserType+"()");
			appendStatement("(("+accesserType+"Property"+")getList("+list.toNameConstant(l)+").get(index1)).set"+accesserType+"(tmp2)");
			appendStatement("(("+accesserType+"Property"+")getList("+list.toNameConstant(l)+").get(index2)).set"+accesserType+"(tmp1)");
			closeBlockNEW();
			emptyline();
	
			appendString("public "+c.toJavaType()+ " "+getListElementGetterName(list, l)+"(int index){");
			increaseIdent();
//			appendStatement(c.toJavaType()+"Property p = ("+c.toJavaType()+"Property"+")getList("+list.toNameConstant(l)+").get(index)");
//			appendStatement("return p.get"+c.toJavaType()+"()");
			appendStatement(accesserType+"Property p = ("+accesserType+"Property"+")getList("+list.toNameConstant(l)+").get(index)");
			appendStatement("return p.get"+accesserType+"()");
			closeBlockNEW();
			emptyline();
		}
		
//		quote(container.getName()+"_")+"+"+GET_CURRENT_LANG+
		String decl = "public void "+getContainerEntryAdderName(list )+"(";
		decl += c.toJavaType()+" "+c.getName();
		decl += "){";
		appendString(decl);
		increaseIdent();
		
//		appendStatement("getListPropertyAnyCase("+quote(list.getName()+"_")+"+"+GET_CURRENT_LANG+").add(new "+c.toJavaType()+"Property("+c.getName()+", "+c.getName()+"))");
		appendStatement("getListPropertyAnyCase("+quote(list.getName()+"_")+"+"+GET_CURRENT_LANG+").add(new "+accesserType+"Property("+quote("")+" + "+c.getName()+", "+c.getName()+"))");
		closeBlockNEW();
		emptyline();
		
		
		appendString("public void "+getContainerEntryDeleterName(list)+"(int index){");
		increaseIdent();
		appendStatement("getListProperty("+quote(list.getName()+"_")+"+"+GET_CURRENT_LANG+").remove(index)"); 
		closeBlockNEW();
		emptyline();
		
		appendString("public void "+getContainerEntrySwapperName(list)+"(int index1, int index2){");
		increaseIdent();
		appendStatement(c.toJavaType()+" tmp1, tmp2");
//		appendStatement("tmp1 = (("+c.toJavaType()+"Property"+")getList("+quote(list.getName()+"_")+"+"+GET_CURRENT_LANG+").get(index1)).get"+c.toJavaType()+"()");
//		appendStatement("tmp2 = (("+c.toJavaType()+"Property"+")getList("+quote(list.getName()+"_")+"+"+GET_CURRENT_LANG+").get(index2)).get"+c.toJavaType()+"()");
//		appendStatement("(("+c.toJavaType()+"Property"+")getList("+quote(list.getName()+"_")+"+"+GET_CURRENT_LANG+").get(index1)).set"+c.toJavaType()+"(tmp2)");
//		appendStatement("(("+c.toJavaType()+"Property"+")getList("+quote(list.getName()+"_")+"+"+GET_CURRENT_LANG+").get(index2)).set"+c.toJavaType()+"(tmp1)");
		appendStatement("tmp1 = (("+accesserType+"Property"+")getList("+quote(list.getName()+"_")+"+"+GET_CURRENT_LANG+").get(index1)).get"+accesserType+"()");
		appendStatement("tmp2 = (("+accesserType+"Property"+")getList("+quote(list.getName()+"_")+"+"+GET_CURRENT_LANG+").get(index2)).get"+accesserType+"()");
		appendStatement("(("+accesserType+"Property"+")getList("+quote(list.getName()+"_")+"+"+GET_CURRENT_LANG+").get(index1)).set"+accesserType+"(tmp2)");
		appendStatement("(("+accesserType+"Property"+")getList("+quote(list.getName()+"_")+"+"+GET_CURRENT_LANG+").get(index2)).set"+accesserType+"(tmp1)");

		closeBlockNEW();
		emptyline();

		appendString("public "+c.toJavaType()+ " "+getListElementGetterName(list)+"(int index){");
		increaseIdent();
//		appendStatement(c.toJavaType()+"Property p = ("+c.toJavaType()+"Property"+")getList("+quote(list.getName()+"_")+"+"+GET_CURRENT_LANG+").get(index)");
//		appendStatement("return p.get"+c.toJavaType()+"()");
		appendStatement(accesserType+"Property p = ("+accesserType+"Property"+")getList("+quote(list.getName()+"_")+"+"+GET_CURRENT_LANG+").get(index)");
		appendStatement("return p.get"+accesserType+"()");
		closeBlockNEW();
		emptyline();

	}

	private void generateTableMethods(MetaTableProperty table){
		List columns = table.getColumns();
		
		String decl = "public void "+getContainerEntryAdderName(table)+"(";
		for (int i =0; i get"+StringUtils.capitalize(table.getName())+"Row(int index){");
		increaseIdent();
		appendStatement("List ret = new ArrayList(1)");
		for (int i=0; i> "+getTableGetterName(table)+"(){");
		increaseIdent();
		appendStatement("int size = "+getContainerSizeGetterName(table)+"();");
		appendStatement("List> ret = new java.util.ArrayList>(size)");
		appendString("for (int i=0; i properties = extractSortableProperties(doc);

		for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy