Author: tchemit Date: 2008-12-14 18:14:26 +0000 (Sun, 14 Dec 2008) New Revision: 1267 Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityInterfaceGenerator.java Log: only generate Entity SearchField fields required Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityInterfaceGenerator.java =================================================================== --- topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityInterfaceGenerator.java 2008-12-11 21:33:45 UTC (rev 1266) +++ topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityInterfaceGenerator.java 2008-12-14 18:14:26 UTC (rev 1267) @@ -29,6 +29,18 @@ package org.codelutin.topia.generator; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.generator.Generator; +import org.codelutin.generator.ObjectModelGenerator; +import org.codelutin.generator.Util; +import org.codelutin.generator.models.object.ObjectModelAssociationClass; +import org.codelutin.generator.models.object.ObjectModelAttribute; +import org.codelutin.generator.models.object.ObjectModelClass; +import org.codelutin.generator.models.object.ObjectModelClassifier; +import org.codelutin.generator.models.object.ObjectModelInterface; +import org.codelutin.generator.models.object.ObjectModelOperation; +import org.codelutin.generator.models.object.ObjectModelParameter; import static org.codelutin.topia.generator.GeneratorUtil.hasUnidirectionalRelationOnAbstractType; import static org.codelutin.topia.generator.GeneratorUtil.isBooleanType; import static org.codelutin.topia.generator.GeneratorUtil.isDateType; @@ -44,24 +56,9 @@ import java.util.List; import java.util.Set; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.codelutin.generator.Generator; -import org.codelutin.generator.ObjectModelGenerator; -import org.codelutin.generator.Util; -import org.codelutin.generator.models.object.ObjectModelAssociationClass; -import org.codelutin.generator.models.object.ObjectModelAttribute; -import org.codelutin.generator.models.object.ObjectModelClass; -import org.codelutin.generator.models.object.ObjectModelClassifier; -import org.codelutin.generator.models.object.ObjectModelInterface; -import org.codelutin.generator.models.object.ObjectModelOperation; -import org.codelutin.generator.models.object.ObjectModelParameter; - public class EntityInterfaceGenerator extends ObjectModelGenerator { - /** - * Logger for this class - */ + /** Logger for this class */ private static final Log log = LogFactory .getLog(EntityInterfaceGenerator.class); @@ -91,12 +88,11 @@ if (!interfacez.hasStereotype(GeneratorUtil.STEREOTYPE_ENTITY)) { return; } - - // log - if(log.isDebugEnabled()) { + + if (log.isDebugEnabled()) { log.debug("Generating interface for : " + interfacez.getName()); } - + generateInterfaceHeader(output, interfacez); generateInterfaceOperations(output, interfacez); @@ -127,27 +123,55 @@ List<String> boolFields = new ArrayList<String>(); List<String> dateFields = new ArrayList<String>(); List<ObjectModelAttribute> allAttrs = new ArrayList<ObjectModelAttribute>(); - allAttrs.addAll((Collection<ObjectModelAttribute>)clazz.getAttributes()); - allAttrs.addAll((Collection<ObjectModelAttribute>)clazz.getAllOtherAttributes()); + allAttrs.addAll(clazz.getAttributes()); + allAttrs.addAll(clazz.getAllOtherAttributes()); + boolean needAnnotation = false; for (ObjectModelAttribute attr : allAttrs) { String name = Util.toLowerCaseFirstLetter(attr.getName()); if (isTextType(attr)) { txtFields.add(name); + needAnnotation = true; } else if (isNumericType(attr)) { numFields.add(name); + needAnnotation = true; } else if (isBooleanType(attr)) { boolFields.add(name); + needAnnotation = true; } else if (isDateType(attr)) { dateFields.add(name); + needAnnotation = true; } } -/*{@SearchFields ( - txtFields={<%=getStringRepresentation(txtFields)%>}, - numFields={<%=getStringRepresentation(numFields)%>}, - boolFields={<%=getStringRepresentation(boolFields)%>}, - dateFields={<%=getStringRepresentation(dateFields)%>} -) -}*/ +/*{@SearchFields (}*/ + if (needAnnotation) { + StringBuilder buffer = new StringBuilder(); + if (!txtFields.isEmpty()) { + buffer.append("\n txtFields={").append(getStringRepresentation(txtFields)).append("}"); + if (!numFields.isEmpty() || !boolFields.isEmpty() || !dateFields.isEmpty()) { + buffer.append(","); + } + } + if (!numFields.isEmpty()) { + buffer.append("\n numFields={").append(getStringRepresentation(numFields)).append("}"); + if (!boolFields.isEmpty() || !dateFields.isEmpty()) { + buffer.append(","); + } + } + if (!boolFields.isEmpty()) { + buffer.append("\n boolFields={").append(getStringRepresentation(boolFields)).append("}"); + if (!dateFields.isEmpty()) { + buffer.append(","); + } + } + if (!dateFields.isEmpty()) { + buffer.append(","); + buffer.append("\n dateFields={").append(getStringRepresentation(dateFields)).append("}"); + } +/*{<%=buffer.toString()%> +}*/ + } +/*{) +}*/ } private void generateInterfaceHeader(Writer output, ObjectModelClassifier classifier) throws IOException { @@ -169,26 +193,24 @@ }*/ } if (classifier instanceof ObjectModelClass) { - generateSearchFields(output, (ObjectModelClass)classifier); + generateSearchFields(output, (ObjectModelClass) classifier); } /*{public interface <%=classifier.getName()%> extends }*/ String extendClass = ""; - for (Iterator i=classifier.getInterfaces().iterator(); i.hasNext();) { - ObjectModelClassifier parent = (ObjectModelClassifier)i.next(); + for (ObjectModelClassifier parent : classifier.getInterfaces()) { extendClass += parent.getQualifiedName(); - extendClass += ", "; + extendClass += ", "; } if (classifier instanceof ObjectModelClass) { - ObjectModelClass clazz = (ObjectModelClass)classifier; - for (Iterator i=clazz.getSuperclasses().iterator(); i.hasNext();) { - ObjectModelClassifier parent = (ObjectModelClassifier)i.next(); + ObjectModelClass clazz = (ObjectModelClass) classifier; + for (ObjectModelClassifier parent : clazz.getSuperclasses()) { if (parent.hasStereotype(GeneratorUtil.STEREOTYPE_ENTITY)) { extendClass += parent.getQualifiedName(); } - extendClass += ", "; + extendClass += ", "; } } - + /*{<%=extendClass%>org.codelutin.topia.persistence.TopiaEntity { }*/ @@ -200,8 +222,7 @@ return; } generateInterfaceHeader(output, clazz); - for (Iterator it = clazz.getAttributes().iterator(); it.hasNext();) { - ObjectModelAttribute attr = (ObjectModelAttribute)it.next(); + for (ObjectModelAttribute attr : clazz.getAttributes()) { ObjectModelAttribute reverse = attr.getReverseAttribute(); if (!attr.isNavigable() && !hasUnidirectionalRelationOnAbstractType(reverse, model)) { @@ -233,7 +254,9 @@ }*/ } else { String assocAttrName = GeneratorUtil.getAssocAttrName(attr); - if (log.isTraceEnabled()) { log.trace("assocAttrName: " + assocAttrName); } + if (log.isTraceEnabled()) { + log.trace("assocAttrName: " + assocAttrName); + } /*{ /** * @param <%=Util.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%> La valeur de l'attribut <%=attr.getAssociationClass().getName()%> à positionner. *) @@ -302,7 +325,9 @@ }*/ } else { String assocAttrName = GeneratorUtil.getAssocAttrName(attr); - if (log.isTraceEnabled()) { log.trace("assocAttrName: " + assocAttrName); } + if (log.isTraceEnabled()) { + log.trace("assocAttrName: " + assocAttrName); + } /*{ /** * @param <%=Util.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%> L'instance de <%=attr.getAssociationClass().getName()%> à ajouter. *) @@ -350,7 +375,9 @@ }*/ } else { String assocAttrName = GeneratorUtil.getAssocAttrName(attr); - if (log.isTraceEnabled()) { log.trace("assocAttrName: " + assocAttrName); } + if (log.isTraceEnabled()) { + log.trace("assocAttrName: " + assocAttrName); + } /*{ /** * @return La liste des attributs <%=attr.getAssociationClass().getName()%>. *) @@ -373,19 +400,18 @@ //Méthodes d'accès aux attributs d'une classe d'associations if (clazz instanceof ObjectModelAssociationClass) { - ObjectModelAssociationClass assoc = (ObjectModelAssociationClass)clazz; - for (Iterator i = assoc.getParticipantsAttributes().iterator(); i.hasNext(); ) { - ObjectModelAttribute attr = (ObjectModelAttribute) i.next(); - if (attr != null) { - String type = attr.getType(); - String name = attr.getName(); - generateAssociationAccessors(output, name, type); - if (attr.getReverseAttribute() == null) { - type = ((ObjectModelClassifier)attr.getDeclaringElement()).getQualifiedName(); - name = attr.getDeclaringElement().getName(); - generateAssociationAccessors(output, name, type); - } - } + ObjectModelAssociationClass assoc = (ObjectModelAssociationClass) clazz; + for (ObjectModelAttribute attr : assoc.getParticipantsAttributes()) { + if (attr != null) { + String type = attr.getType(); + String name = attr.getName(); + generateAssociationAccessors(output, name, type); + if (attr.getReverseAttribute() == null) { + type = ((ObjectModelClassifier) attr.getDeclaringElement()).getQualifiedName(); + name = attr.getDeclaringElement().getName(); + generateAssociationAccessors(output, name, type); + } + } } } @@ -396,33 +422,38 @@ } private void generateInterfaceOperations(Writer output, ObjectModelClassifier classifier) throws IOException { - for (Iterator it = classifier.getOperations().iterator(); it.hasNext();) { - ObjectModelOperation op = (ObjectModelOperation)it.next(); + for (ObjectModelOperation op : classifier.getOperations()) { /*{ /** }*/ if (GeneratorUtil.hasDocumentation(op)) { /*{ * <%=op.getName()%> : <%=op.getDocumentation()%> }*/ } - Collection<ObjectModelParameter> params = (Collection<ObjectModelParameter>)op.getParameters(); - for(ObjectModelParameter param : params) { - if(log.isTraceEnabled()) {log.trace("Param" + param);} + Collection<ObjectModelParameter> params = op.getParameters(); + for (ObjectModelParameter param : params) { + if (log.isTraceEnabled()) { + log.trace("Param" + param); + } /*{ * @param <%=param.getName()%> <%=param.getDocumentation()%> - }*/ + }*/ } /*{ *) <%=op.getVisibility()%> <%=op.getReturnType()%> <%=op.getName()%>(}*/ String vir = ""; - for(ObjectModelParameter param : params){ - if(log.isTraceEnabled()) {log.trace("Param" + param + " vir" + vir);} + for (ObjectModelParameter param : params) { + if (log.isTraceEnabled()) { + log.trace("Param" + param + " vir" + vir); + } /*{<%=vir%><%=param.getType()%> <%=param.getName()%>}*/ vir = ", "; } /*{)}*/ - Set<String> exceptions = (Set<String>)op.getExceptions(); + Set<String> exceptions = op.getExceptions(); vir = " throws "; for (String exception : exceptions) { - if(log.isTraceEnabled()) {log.trace("exception" + exception + " vir" + vir);} + if (log.isTraceEnabled()) { + log.trace("exception" + exception + " vir" + vir); + } /*{<%=vir%><%=exception%>}*/ vir = ", "; }