Author: tchemit Date: 2009-02-12 00:03:21 +0000 (Thu, 12 Feb 2009) New Revision: 1361 Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOAbstractGenerator.java topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOHelperGenerator.java Log: the DAOHelper now integrates the EntityEnum Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOAbstractGenerator.java =================================================================== --- topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOAbstractGenerator.java 2009-02-12 00:02:35 UTC (rev 1360) +++ topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOAbstractGenerator.java 2009-02-12 00:03:21 UTC (rev 1361) @@ -139,7 +139,7 @@ public void delete(E entity) throws TopiaException { }*/ String modelName = GeneratorUtil.capitalize(model.getName()); - String providerFQN = getProperty("defaultPackage") + "." + modelName + "EntityEnum.getImplementationClass"; + String providerFQN = getProperty("defaultPackage") + "." + modelName + "DAOHelper.getImplementationClass"; for (ObjectModelAttribute attr : clazz.getAttributes()) { String attrType = attr.getType(); String reverseAttrName = attr.getReverseAttributeName(); Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOHelperGenerator.java =================================================================== --- topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOHelperGenerator.java 2009-02-12 00:02:35 UTC (rev 1360) +++ topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOHelperGenerator.java 2009-02-12 00:03:21 UTC (rev 1361) @@ -46,10 +46,12 @@ /** * Ce generateur permet d'avoir une classe permettant de recuperer les DAO * sans cast. + * + * La classe générée contient aussi une énumération nommée <code>EntityEnum</code> qui permet + * d'avoir facilement les types d'entités gérées par ce dao. + * * @author poussin - * @see Deprecated you should prefer use the {@link EntityEnumGenerator} */ -@Deprecated public class DAOHelperGenerator extends ObjectModelGenerator { private Log log = LogFactory.getLog(DAOHelperGenerator.class); @@ -81,28 +83,179 @@ }*/ } String modelName = GeneratorUtil.capitalize(model.getName()); + String daoClazzName = modelName+"DAOHelper"; + String entityEnumName = modelName+"EntityEnum"; + /*{package <%=getProperty("defaultPackage")%>; import org.codelutin.topia.TopiaContext; +import org.codelutin.topia.TopiaException; import org.codelutin.topia.framework.TopiaContextImplementor; -import org.codelutin.topia.TopiaException; +import org.codelutin.topia.persistence.TopiaDAO; +import org.codelutin.topia.persistence.TopiaEntity; -public class <%=modelName%>DAOHelper { +public class <%=daoClazzName%> { }*/ + List<ObjectModelClass> classes = TopiaGeneratorUtil.getEntityClasses(model,true); +/*{ /** + *) + protected <%=daoClazzName%>() { + } +}*/ + + for (ObjectModelClass clazz : classes) { + String clazzFQN = clazz.getQualifiedName(); + String clazzName = clazz.getName(); +/*{ + public static <%=clazzFQN%>DAO get<%=clazzName%>DAO(TopiaContext context) throws TopiaException { + TopiaContextImplementor ci = (TopiaContextImplementor)context; + <%=clazzFQN%>DAO result = (<%=clazzFQN%>DAO)ci.getDAO(<%=clazzFQN%>.class); + return result; + } +}*/ + } +/*{ + @SuppressWarnings({"unchecked"}) + public static <T extends TopiaEntity, D extends TopiaDAO<? super T>> D getDAO(TopiaContext context, Class<T> klass) throws TopiaException { + TopiaContextImplementor ci = (TopiaContextImplementor)context; + <%=entityEnumName%> constant = <%=entityEnumName%>.valueOf(klass); + D dao = (D) ci.getDAO(constant.getContractClass()); + return dao; + } + + @SuppressWarnings({"unchecked"}) + public static <T extends TopiaEntity, D extends TopiaDAO<? super T>> D getDAO(TopiaContext context, T entity) throws TopiaException { + TopiaContextImplementor ci = (TopiaContextImplementor)context; + <%=entityEnumName%> constant = <%=entityEnumName%>.valueOf(entity); + D dao = (D) ci.getDAO(constant.getContractClass()); + return dao; + } + + @SuppressWarnings({"unchecked"}) + public static <T extends TopiaEntity> Class<T> getContractClass(Class<T> klass) throws TopiaException { + <%=entityEnumName%> constant = <%=entityEnumName%>.valueOf(klass); + return (Class<T>) constant.getContractClass(); + } + + @SuppressWarnings({"unchecked"}) + public static <T extends TopiaEntity> Class<T> getImplementationClass(Class<T> klass) throws TopiaException { + <%=entityEnumName%> constant = <%=entityEnumName%>.valueOf(klass); + return (Class<T>) constant.getImplementationClass(); + } + + @SuppressWarnings({"unchecked"}) + public static Class<? extends TopiaEntity>[] getContractClasses() { + <%=entityEnumName%>[] values = <%=entityEnumName%>.values(); + Class<? extends TopiaEntity>[] result = (Class<? extends TopiaEntity>[]) java.lang.reflect.Array.newInstance(Class.class, values.length); + for (int i = 0; i < values.length; i++) { + result[i] = values[i].getContractClass(); + } + return result; + } + + @SuppressWarnings({"unchecked"}) + public static Class<? extends TopiaEntity>[] getImplementationClasses() { + <%=entityEnumName%>[] values = <%=entityEnumName%>.values(); + Class<? extends TopiaEntity>[] result = (Class<? extends TopiaEntity>[]) java.lang.reflect.Array.newInstance(Class.class, values.length); + for (int i = 0; i < values.length; i++) { + result[i] = values[i].getImplementationClass(); + } + return result; + } + + public static String getImplementationClassesAsString() { + StringBuilder buffer = new StringBuilder(); + for (Class<? extends TopiaEntity> aClass : getImplementationClasses()) { + buffer.append(',').append(aClass.getName()); + } + return buffer.substring(1); + } + + /* + * Enumeration of all types of entitties dealed by this helper. + *) + public enum <%=entityEnumName%> { +}*/ + for (Iterator<ObjectModelClass> i=classes.iterator(); i.hasNext();) { + ObjectModelClass clazz = i.next(); + String clazzFQN = clazz.getQualifiedName(); + String clazzName = clazz.getName(); +/*{ + <%=clazzName%>(<%=clazzFQN%>.class)<%=(i.hasNext() ? "," : ";")%>}*/ + } +/*{ + + /** the contract of the entity *) + private Class<? extends TopiaEntity> contractClass; + + /** the fully qualified name of the implementation of the entity *) + private String implementationFQN; + + /** the implementation class of the entity (will be lazy computed at runtime)*) + private Class<? extends TopiaEntity> implementationClass; + + <%=entityEnumName%>(Class<? extends TopiaEntity > contractClass) { + this.contractClass = contractClass; + this.implementationFQN = contractClass.getName()+"Impl"; + } + + public Class<? extends TopiaEntity> getContractClass() { + return contractClass; + } + + public String getImplementationFQN() { + return implementationFQN; + } + + @SuppressWarnings({"unchecked"}) + public Class<? extends TopiaEntity> getImplementationClass() { + if (implementationClass == null) { + try { + implementationClass = (Class<? extends TopiaEntity>) Class.forName(implementationFQN); + } catch (ClassNotFoundException e) { + throw new RuntimeException("could not find class "+implementationFQN); + } + } + return implementationClass; + } + + public static <%=entityEnumName%> valueOf(TopiaEntity entity) { + return valueOf(entity.getClass()); + } + + public static <%=entityEnumName%> valueOf(Class<?> klass) { + if (klass.isInterface()) { + return <%=entityEnumName%>.valueOf(klass.getSimpleName()); + } + for (<%=entityEnumName%> entityEnum : <%=entityEnumName%>.values()) { + if (entityEnum.getContractClass().isAssignableFrom(klass)) { + //todo check it works for inheritance + return entityEnum; + } + } + throw new IllegalArgumentException("no entity defined for the class " + klass + " in : " + java.util.Arrays.toString(<%=entityEnumName%>.values())); + } + } +}*/ String entitiesList = ""; - List<ObjectModelClass> classes = TopiaGeneratorUtil.getEntityClasses(model,true); if (classes.isEmpty()) { /*{ + /** + * @eprecated (prefer use {@link #getImplementationClassesAsString()} + *) public static final String entitiesList = ""; }*/ } else { /*{ + /** + * @eprecated (prefer use {@link #getImplementationClassesAsString()} + *) public static final String entitiesList = "" + }*/ for (Iterator<ObjectModelClass> i=classes.iterator(); i.hasNext();) { - ObjectModelClass clazz = (ObjectModelClass)i.next(); + ObjectModelClass clazz = i.next(); String doType = TopiaGeneratorUtil.getDOType(clazz, model); entitiesList += doType + (i.hasNext()?",":""); /*{ "<%=doType%><%=(i.hasNext()?",\" +":"\";")%> @@ -112,25 +265,9 @@ if (log.isDebugEnabled()) { log.debug("Full entities list : " + entitiesList); } -/*{ /** - *) - protected <%=modelName%>DAOHelper() { - } -}*/ - for (ObjectModelClass clazz : classes) { - String clazzFQN = clazz.getQualifiedName(); - String clazzName = clazz.getName(); /*{ - static public <%=clazzFQN%>DAO get<%=clazzName%>DAO(TopiaContext context) throws TopiaException { - TopiaContextImplementor ci = (TopiaContextImplementor)context; - <%=clazzFQN%>DAO result = (<%=clazzFQN%>DAO)ci.getDAO(<%=clazzFQN%>.class); - return result; - } +} //<%=daoClazzName%> }*/ - } -/*{ -} //<%=modelName%>DAOHelper -}*/ } } //DAOHelperGenerator