Author: tchemit Date: 2011-10-03 18:34:57 +0200 (Mon, 03 Oct 2011) New Revision: 2348 Url: http://nuiton.org/repositories/revision/topia/2348 Log: Evolution #1764: Improve the method TopiaDAOImpl.newInstance() Evolution #1765: Introduce the TopiaEntityEnum into the generated dao Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/DAOHelperTransformer.java trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityDAOTransformer.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOLegacy.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/DAOHelperTransformer.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/DAOHelperTransformer.java 2011-09-25 21:42:03 UTC (rev 2347) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/DAOHelperTransformer.java 2011-10-03 16:34:57 UTC (rev 2348) @@ -68,7 +68,7 @@ String packageName = getOutputProperties().getProperty(PROP_DEFAULT_PACKAGE); String modelName = model.getName(); String daoHelperClazzName = modelName + "DAOHelper"; - String entityEnumName = modelName+"EntityEnum"; + String entityEnumName = modelName + "EntityEnum"; List<ObjectModelClass> classes = TopiaGeneratorUtil.getEntityClasses(model, true); Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityDAOTransformer.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityDAOTransformer.java 2011-09-25 21:42:03 UTC (rev 2347) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityDAOTransformer.java 2011-10-03 16:34:57 UTC (rev 2348) @@ -51,7 +51,6 @@ import java.security.Permission; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -94,6 +93,9 @@ */ protected Class<?> daoImplementation; + protected String entityEnumName; + + protected String entityEnumPackage; /** * Map of extra operations for DAO. The key of the map is the qualified * name of the entity relative to the DAO. @@ -104,6 +106,22 @@ @Override public void transformFromModel(ObjectModel model) { + boolean generateStandaloneEnum = + TopiaGeneratorUtil.shouldGenerateStandaloneEnumForDAOHelper(model); + String modelName = model.getName(); + + entityEnumName = modelName + "EntityEnum"; + + String packageName = + getOutputProperties().getProperty(PROP_DEFAULT_PACKAGE); + + if (generateStandaloneEnum) { + entityEnumPackage = packageName+"."+entityEnumName; + } else { + String daoHelperClazzName = modelName + "DAOHelper"; + entityEnumPackage = packageName + "." + daoHelperClazzName + "." + entityEnumName; + } + usages = TopiaGeneratorUtil.searchDirectUsages(model); boolean extendLegacyDAO = Boolean.valueOf(model.getTagValue(TopiaTagValues.TAG_USE_LEGACY_DAO)); @@ -275,7 +293,7 @@ } setSuperClass(daoAbstractClass, extendClass); - addInterface(daoAbstractClass, TopiaDAO.class.getName() + "<E>"); +// addInterface(daoAbstractClass, TopiaDAO.class.getName() + "<E>"); String prefix = getConstantPrefix(clazz, ""); setConstantPrefix(prefix); @@ -292,13 +310,14 @@ addImport(daoAbstractClass, Set.class); } addImport(daoAbstractClass, List.class); - addImport(daoAbstractClass, Arrays.class); +// addImport(daoAbstractClass, Arrays.class); addImport(daoAbstractClass, TopiaException.class); - addImport(daoAbstractClass, TopiaContextImplementor.class); +// addImport(daoAbstractClass, TopiaContextImplementor.class); boolean enableSecurity = TopiaGeneratorUtil.isClassWithSecurity(clazz); if (enableSecurity) { + addImport(daoAbstractClass, TopiaContextImplementor.class); addImport(daoAbstractClass, ArrayList.class); addImport(daoAbstractClass, Permission.class); addImport(daoAbstractClass, "org.nuiton.topia.taas.entities.TaasAuthorizationImpl"); @@ -321,12 +340,26 @@ "getEntityClass", "Class<E>", ObjectModelModifier.PUBLIC); + addAnnotation(daoAbstractClass, op,Override.class.getSimpleName()); setOperationBody(op, "" /*{ return (Class<E>)<%=clazzName%>.class; }*/ ); + // getTopiaEntityEnum + addImport(daoAbstractClass, entityEnumPackage); + op = addOperation(daoAbstractClass, + "getTopiaEntityEnum", + entityEnumName, + ObjectModelModifier.PUBLIC); + addAnnotation(daoAbstractClass, op,Override.class.getSimpleName()); + setOperationBody(op, "" +/*{ + return <%=entityEnumName%>.<%=clazzName%>; + }*/ + ); + generateDAOOperations(daoAbstractClass, DAOoperations); generateDelete(clazz, daoAbstractClass); @@ -458,6 +491,7 @@ ObjectModelClass result) { ObjectModelOperation op; op = addOperation(result, "delete", "void", ObjectModelModifier.PUBLIC); + addAnnotation(result, op,Override.class.getSimpleName()); addException(op, TopiaException.class); addParameter(op, "E", "entity"); StringBuilder body = new StringBuilder(); Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java 2011-09-25 21:42:03 UTC (rev 2347) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java 2011-10-03 16:34:57 UTC (rev 2348) @@ -69,6 +69,8 @@ */ public interface TopiaDAO<E extends TopiaEntity> { + TopiaEntityEnum getTopiaEntityEnum(); + /** * When TopiaContextImpl create the TopiaDAOHibernate, it must call this * method just after. Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2011-09-25 21:42:03 UTC (rev 2347) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2011-10-03 16:34:57 UTC (rev 2348) @@ -81,6 +81,12 @@ protected TopiaContextImplementor context; @Override + public TopiaEntityEnum getTopiaEntityEnum() { + throw new UnsupportedOperationException( + "This method must be overided in generated DAO"); + } + + @Override public Class<E> getEntityClass() { throw new UnsupportedOperationException( "This method must be overided in generated DAO"); @@ -149,80 +155,22 @@ @SuppressWarnings("unchecked") @Override public E newInstance() throws TopiaException { - E result = null; if (log.isDebugEnabled()) { log.debug("entityClass = " + entityClass); } - String classname = entityClass.getName(); - try { - // on commence par essayer d'instancier le Impl - result = ((Class<E>) Class.forName(classname + "Impl")) - .newInstance(); - if (log.isDebugEnabled()) { - log.debug("Utilisation de la classe " + classname + "Impl" - + " pour " + classname); - } - } catch (ClassNotFoundException eee) { - if (log.isWarnEnabled()) { - log.warn("Impossible de trouver la classe " + classname - + "Impl"); - } - if (log.isDebugEnabled()) { - log.debug("StackTrace", eee); - } - } catch (Exception eee) { - if (log.isWarnEnabled()) { - log.warn("Impossible d'instancier " + classname + "Impl"); - } - if (log.isDebugEnabled()) { - log.debug("StackTrace", eee); - } - } + Class<E> implementation = (Class<E>) + getTopiaEntityEnum().getImplementation(); - if (result == null) { - // le impl n'est pas trouvé on essai avec la classe elle meme - try { - result = entityClass.getConstructor().newInstance(); - if (log.isDebugEnabled()) { - log.debug("Utilisation de la classe " + classname - + " pour " + classname); - } - } catch (Exception eee) { - if (log.isWarnEnabled()) { - log.warn("Impossible d'instancier " + classname); - } - if (log.isDebugEnabled()) { - log.debug("StackTrace", eee); - } - } - } - - if (result == null) { + try { + E result = implementation.newInstance(); + return result; + } catch (Exception e) { throw new TopiaException( "Impossible de trouver ou d'instancier la classe " - + classname); + + implementation); } - - return result; } - /** - * private method because this is hibernate specific method and we don't - * want expose it - * - * @return the meta-data of the entity - * @throws TopiaException if any pb - */ - private ClassMetadata getClassMetadata() throws TopiaException { - ClassMetadata meta = getContext().getHibernateFactory() - .getClassMetadata(entityClass); - if (meta == null) { - meta = getContext().getHibernateFactory().getClassMetadata( - entityClass.getName() + "Impl"); - } - return meta; - } - @Override public <U extends TopiaEntity> List<U> findUsages(Class<U> type, E e) throws TopiaException { @@ -263,16 +211,6 @@ getContext().removeTopiaEntityVetoable(entityClass, vetoable); } - /** - * Renvoie la Session contenue dans le contexte - * - * @return hibernate session - * @throws TopiaException if any pb - */ - private Session getSession() throws TopiaException { - return getContext().getHibernate(); - } - @Override public E create(E entity) throws TopiaException { @@ -645,5 +583,31 @@ } + /** + * Renvoie la Session contenue dans le contexte + * + * @return hibernate session + * @throws TopiaException if any pb + */ + Session getSession() throws TopiaException { + return getContext().getHibernate(); + } + /** + * package locale method because this is hibernate specific method and + * we don't want expose it. + * + * @return the meta-data of the entity + * @throws TopiaException if any pb + */ + ClassMetadata getClassMetadata() throws TopiaException { + ClassMetadata meta = getContext().getHibernateFactory() + .getClassMetadata(entityClass); + if (meta == null) { + meta = getContext().getHibernateFactory().getClassMetadata( + getTopiaEntityEnum().getImplementationFQN()); + } + return meta; + } + } //TopiaDAOImpl Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOLegacy.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOLegacy.java 2011-09-25 21:42:03 UTC (rev 2347) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOLegacy.java 2011-10-03 16:34:57 UTC (rev 2348) @@ -49,32 +49,21 @@ import org.hibernate.Criteria; import org.hibernate.FlushMode; import org.hibernate.HibernateException; -import org.hibernate.Session; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions; import org.hibernate.metadata.ClassMetadata; import org.nuiton.topia.TopiaException; -import org.nuiton.topia.event.TopiaEntityListener; -import org.nuiton.topia.event.TopiaEntityVetoable; -import org.nuiton.topia.framework.TopiaContextImplementor; -import java.io.Serializable; import java.lang.reflect.InvocationTargetException; -import java.security.Permission; import java.util.HashMap; import java.util.List; import java.util.Map; /** - * Cette classe permet d'avoir un ensemble de méthode implantée de façon - * standard et plus spécifiquement pour Hibernate. - * - * Certains accès à Hibernate sont tout de même fait ici, car on a pris le - * choix de se baser entièrement sur hibernate pour la persistence, et il - * est ainsi possible d'accèder au meta information hibernate sur les classes - * lorque l'on en a besoin. - * + * Surcharge du {@link TopiaDAOImpl} pour utiliser l'api criteria au lieu du hql + * pour tout ce qui est requétage. + * * Created: 31 déc. 2005 13:10:34 * * @param <E> le type de l'entite @@ -82,148 +71,14 @@ * @author tchemit <chemit@codelutin.com> */ -public class TopiaDAOLegacy<E extends TopiaEntity> extends TopiaDAOImpl<E> { // TopiaDAOImpl +public class TopiaDAOLegacy<E extends TopiaEntity> extends TopiaDAOImpl<E> { // TopiaDAOLegacy /** Logger. */ - private static Log log = LogFactory.getLog(TopiaDAOImpl.class); + private static Log log = LogFactory.getLog(TopiaDAOLegacy.class); - protected Class<E> entityClass; - - protected TopiaContextImplementor context; - - @Override - public Class<E> getEntityClass() { - throw new UnsupportedOperationException( - "This method must be overided in generated DAO"); - } - - /** - * Retourne l'id de l'entity - * @param e l'entity - * @return l'id de l'entity ou null si pas trouvé - * @throws TopiaException Si une erreur survient durant la recherche - */ - protected Serializable getId(E e) throws TopiaException { - ClassMetadata meta = getClassMetadata(); - String idPropName = meta.getIdentifierPropertyName(); - - Serializable result; - try { - result = (Serializable) PropertyUtils.getSimpleProperty(e, - idPropName); - } catch (Exception eee) { - throw new TopiaException("Impossible de récuperer l'identifiant " - + idPropName + " de l'entite: " + e); - } - return result; - } - - /** - * Retourne l'id de l'entity representer comme une map - * @param map l'entity en representation map - * @return l'id de l'entity ou null si pas trouvé - * @throws TopiaException Si une erreur survient durant la recherche - */ - protected Serializable getId(Map map) throws TopiaException { - try { - ClassMetadata meta = getClassMetadata(); - String idPropName = meta.getIdentifierPropertyName(); - - Serializable id = (Serializable) map.get(idPropName); - return id; - } catch (HibernateException eee) { - throw new TopiaException(eee); - } - } - - /** - * When TopiaContextImpl create the TopiaDAOHibernate, it must call this method just - * after - * - * @param entityClass - */ - public void init(TopiaContextImplementor context, Class<E> entityClass) - throws TopiaException { - log.debug("init dao for " + entityClass.getName()); - this.context = context; - this.entityClass = entityClass; - } - - @Override - public TopiaContextImplementor getContext() { - return context; - } - @SuppressWarnings("unchecked") protected E instanciateNew() throws TopiaException { - E result = null; - if (log.isDebugEnabled()) { - log.debug("entityClass = " + entityClass); - } - String classname = entityClass.getName(); - try { - // on commence par essayer d'instancier le Impl - result = ((Class<E>) Class.forName(classname + "Impl")) - .newInstance(); - if (log.isDebugEnabled()) { - log.debug("Utilisation de la classe " + classname + "Impl" - + " pour " + classname); - } - } catch (InstantiationException eee) { - if (log.isWarnEnabled()) { - log.warn("Impossible d'instancier " + classname + "Impl"); - } - if (log.isDebugEnabled()) { - log.debug("StackTrace", eee); - } - } catch (IllegalAccessException eee) { - if (log.isWarnEnabled()) { - log.warn("Impossible d'instancier " + classname + "Impl"); - } - if (log.isDebugEnabled()) { - log.debug("StackTrace", eee); - } - } catch (ClassNotFoundException eee) { - if (log.isWarnEnabled()) { - log.warn("Impossible de trouver la classe " + classname - + "Impl"); - } - if (log.isDebugEnabled()) { - log.debug("StackTrace", eee); - } - } - - if (result == null) { - // le impl n'est pas trouvé on essai avec la classe elle meme - try { - result = entityClass.newInstance(); - if (log.isDebugEnabled()) { - log.debug("Utilisation de la classe " + classname - + " pour " + classname); - } - } catch (InstantiationException eee) { - if (log.isWarnEnabled()) { - log.warn("Impossible d'instancier " + classname); - } - if (log.isDebugEnabled()) { - log.debug("StackTrace", eee); - } - } catch (IllegalAccessException eee) { - if (log.isWarnEnabled()) { - log.warn("Impossible d'instancier " + classname); - } - if (log.isDebugEnabled()) { - log.debug("StackTrace", eee); - } - } - } - - if (result == null) { - throw new TopiaException( - "Impossible de trouver ou d'instancier la classe " - + classname); - } - + E result = newInstance(); return result; } @@ -272,23 +127,6 @@ } - /** - * private method because this is hibernate specific method and we don't - * want expose it - * - * @return the meta-data of the entity - * @throws TopiaException if any pb - */ - private ClassMetadata getClassMetadata() throws TopiaException { - ClassMetadata meta = getContext().getHibernateFactory() - .getClassMetadata(entityClass); - if (meta == null) { - meta = getContext().getHibernateFactory().getClassMetadata( - entityClass.getName() + "Impl"); - } - return meta; - } - @Override public E findByPrimaryKey(Object... k) throws TopiaException { // TODO pour une meilleur gestion des problemes a la compilation @@ -588,26 +426,6 @@ return Restrictions.or(crit1, crit2); } - @Override - public void addTopiaEntityListener(TopiaEntityListener listener) { - getContext().addTopiaEntityListener(entityClass,listener); - } - - @Override - public void addTopiaEntityVetoable(TopiaEntityVetoable vetoable) { - getContext().addTopiaEntityVetoable(entityClass,vetoable); - } - - @Override - public void removeTopiaEntityListener(TopiaEntityListener listener) { - getContext().removeTopiaEntityListener(entityClass, listener); - } - - @Override - public void removeTopiaEntityVetoable(TopiaEntityVetoable vetoable) { - getContext().removeTopiaEntityVetoable(entityClass, vetoable); - } - /** * Cette methode appelle fireVetoableCreate et fireOnCreated * Si vous la surchargé, faites attention a appeler le super @@ -652,27 +470,6 @@ } @Override - public E update(E e) throws TopiaException { - try { - getSession().saveOrUpdate(e); - getContext().getFiresSupport().warnOnUpdateEntity(e); - return e; - } catch (HibernateException eee) { - throw new TopiaException(eee); - } - } - - @Override - public void delete(E e) throws TopiaException { - try { - getSession().delete(e); - getContext().getFiresSupport().warnOnDeleteEntity(e); - } catch (HibernateException eee) { - throw new TopiaException(eee); - } - } - - @Override public E findByTopiaId(String k) throws TopiaException { return query(Restrictions.idEq(k)); } @@ -692,7 +489,8 @@ @Override public List<String> findAllIds() throws TopiaException { - List<String> find = context.find("select src.topiaId from " + entityClass.getSimpleName() + "Impl src"); +// List<String> find = context.find("select src.topiaId from " + entityClass.getSimpleName() + "Impl src"); + List<String> find = context.find("select src.topiaId from " + getEntityClass() + " src"); return find; } @@ -824,20 +622,4 @@ return criteria; } - /** - * Renvoie la Session contenue dans le contexte - * @return hibernate session - * @throws TopiaException if any pb - */ - private Session getSession() throws TopiaException { - return getContext().getHibernate(); - } - - @Override - public List<Permission> getRequestPermission(String topiaId, int actions) - throws TopiaException { - return null; - } - - -} //TopiaDAOImpl +} //TopiaDAOLegacy