r1224 - in topia/trunk: . src/main/java/org/codelutin/topia/generator src/main/java/org/codelutin/topia/persistence src/main/java/org/codelutin/topia/persistence/flatfile src/main/java/org/codelutin/topia/persistence/hibernate
Author: tchemit Date: 2008-11-16 18:29:50 +0000 (Sun, 16 Nov 2008) New Revision: 1224 Modified: topia/trunk/changelog topia/trunk/src/main/java/org/codelutin/topia/generator/EntityEnumGenerator.java topia/trunk/src/main/java/org/codelutin/topia/persistence/TopiaDAO.java topia/trunk/src/main/java/org/codelutin/topia/persistence/TopiaDAOAbstract.java topia/trunk/src/main/java/org/codelutin/topia/persistence/TopiaDAODelegator.java topia/trunk/src/main/java/org/codelutin/topia/persistence/TopiaEntityEnumEntry.java topia/trunk/src/main/java/org/codelutin/topia/persistence/flatfile/TopiaDAOFlatFile.java topia/trunk/src/main/java/org/codelutin/topia/persistence/hibernate/TopiaDAOHibernate.java Log: improve EntityEnum + add findAllWithOrder on DAO Modified: topia/trunk/changelog =================================================================== --- topia/trunk/changelog 2008-11-14 11:16:46 UTC (rev 1223) +++ topia/trunk/changelog 2008-11-16 18:29:50 UTC (rev 1224) @@ -1,4 +1,6 @@ ver-2-0-28 ?? 2008?? + * 20081116 [chemit] improve EntityEnum + * 20081116 [chemit] add findAllWithOrder method on Dao to order by properties * 20081113 [chorlet] add support for lazy loading on attribute with 0..* multiplicity * 20081113 [chorlet] fix bug on bidirectional association by adding inverse attribute in the one side of two hibernate mapping files * 20081107 [chatellier] fix "result" named attribute generation in model Modified: topia/trunk/src/main/java/org/codelutin/topia/generator/EntityEnumGenerator.java =================================================================== --- topia/trunk/src/main/java/org/codelutin/topia/generator/EntityEnumGenerator.java 2008-11-14 11:16:46 UTC (rev 1223) +++ topia/trunk/src/main/java/org/codelutin/topia/generator/EntityEnumGenerator.java 2008-11-16 18:29:50 UTC (rev 1224) @@ -77,6 +77,10 @@ } /*{package <%=getProperty("defaultPackage")%>; +import java.util.Map; +import java.util.List; +import java.util.Collection; + import org.codelutin.topia.TopiaContext; import org.codelutin.topia.TopiaException; import org.codelutin.topia.persistence.TopiaDAO; @@ -111,36 +115,11 @@ <%=className%>(Class<? extends TopiaEntity> contractClass) { entry = new TopiaEntityEnumEntry(contractClass); } -}*/ -/* public Class<? extends TopiaEntity> getContractClass() { - return entry.getContractClass(); - } + public <T extends TopiaEntity> TopiaEntityEnumEntry<T> getEntry() { + return (TopiaEntityEnumEntry<T>)entry; + } - public Class<? extends TopiaEntity> getImplementationClass() { - return entry.getImplementationClass(); - } - - public TopiaDAO getDAO(TopiaContext ctxt) throws TopiaException { - return entry.getDAO(ctxt); - } - - public TopiaEntity create(TopiaContext ctxt) throws TopiaException { - return entry.create(ctxt); - } - - public TopiaEntity update(TopiaEntity entity, TopiaContext - ctxt) throws TopiaException - { - return entry.update(entity, ctxt); - } - - public void delete(TopiaEntity entity, TopiaContext ctxt) throws TopiaException { - entry.delete(entity, ctxt); - } -*/ - -/*{ public static <%=className%> valueOf(TopiaEntity entity) { return valueOf(entity.getClass()); } @@ -159,14 +138,12 @@ public static <T extends TopiaEntity> Class<T> getContractClass(Class<T> klass) throws TopiaException { TopiaEntityEnumEntry<T> entry = getEntry(klass); - <%=className%> e = <%=className%>.valueOf(klass); - return (Class<T>) e.entry.getContractClass(); + return entry.getContractClass(); } public static <T extends TopiaEntity> Class<T> getImplementationClass(Class<T> klass) throws TopiaException { TopiaEntityEnumEntry<T> entry = getEntry(klass); - <%=className%> e = <%=className%>.valueOf(klass); - return (Class<T>) e.entry.getImplementationClass(); + return entry.getImplementationClass(); } public static Class<? extends TopiaEntity>[] getContractClasses() { @@ -195,55 +172,136 @@ return buffer.substring(1); } - public static <T extends TopiaEntity, D extends TopiaDAO<? super T>> D getDAO(Class<T> klass, TopiaContext ctxt) throws TopiaException { - TopiaEntityEnumEntry<T> entry = getEntry(klass); + public static <T extends TopiaEntity> TopiaEntityEnumEntry<T> getEntry(T entity) { + <%=className%> e = <%=className%>.valueOf(entity); + return (TopiaEntityEnumEntry<T>)e.entry; + } + + public static <T extends TopiaEntity> TopiaEntityEnumEntry<T> getEntry(Class<T> klass) { <%=className%> e = <%=className%>.valueOf(klass); - return (D) e.entry.getDAO(ctxt); + return (TopiaEntityEnumEntry<T>)e.entry; } - public static <T extends TopiaEntity, D extends TopiaDAO<? super T>> D getDAO(T entity, TopiaContext ctxt) throws TopiaException { + public static <T extends TopiaEntity> TopiaEntityEnumEntry<T> getEntry(String constantName) { + <%=className%> e = <%=className%>.valueOf(constantName); + return (TopiaEntityEnumEntry<T>)e.entry; + } + + public static <T extends TopiaEntity, D extends TopiaDAO<? super T>> D getDAO(TopiaContext ctxt, Class<T> klass) throws TopiaException { + TopiaEntityEnumEntry<T> entry = getEntry(klass); + return (D) entry.getDAO(ctxt); + } + + public static <T extends TopiaEntity, D extends TopiaDAO<? super T>> D getDAO(TopiaContext ctxt, T entity) throws TopiaException { TopiaEntityEnumEntry<T> entry = getEntry(entity); - <%=className%> e = <%=className%>.valueOf(entity); - return (D) e.entry.getDAO(ctxt); + return (D) entry.getDAO(ctxt); } - public static <T extends TopiaEntity> T create(Class<T> klass, TopiaContext ctxt) throws TopiaException { + public static <T extends TopiaEntity> T create(TopiaContext ctxt, Class<T> klass) throws TopiaException { TopiaEntityEnumEntry<T> entry = getEntry(klass); - <%=className%> e = <%=className%>.valueOf(klass); - return (T) e.entry.create(ctxt); + return entry.create(ctxt); } - public static <T extends TopiaEntity> T update(T entity, TopiaContext ctxt) throws TopiaException { + public static <T extends TopiaEntity> T create(TopiaContext ctxt, Class<T> klass, Object... properties) throws TopiaException { + TopiaEntityEnumEntry<T> entry = getEntry(klass); + return entry.create(ctxt, properties); + } + + public static <T extends TopiaEntity> T create(TopiaContext ctxt, Class<T> klass, Map<String, Object> properties) throws TopiaException { + TopiaEntityEnumEntry<T> entry = getEntry(klass); + return entry.create(ctxt, properties); + } + + public static <T extends TopiaEntity> T update(TopiaContext ctxt, T entity) throws TopiaException { TopiaEntityEnumEntry<T> entry = getEntry(entity); - <%=className%> e = <%=className%>.valueOf(entity); - return (T) e.entry.update(entity, ctxt); + return entry.update(entity, ctxt); } - public static <T extends TopiaEntity> void delete(T entity, TopiaContext ctxt) throws TopiaException { + public static <T extends TopiaEntity> void delete(TopiaContext ctxt, T entity) throws TopiaException { TopiaEntityEnumEntry<T> entry = getEntry(entity); - <%=className%> e = <%=className%>.valueOf(entity); - e.entry.delete(entity, ctxt); + entry.delete(entity, ctxt); } - public static <T extends TopiaEntity> java.util.List<T> findAll(Class<T> klass, TopiaContext ctxt) throws TopiaException { + public static <T extends TopiaEntity> int size(TopiaContext ctxt, Class<T> klass) throws TopiaException { TopiaEntityEnumEntry<T> entry = getEntry(klass); + return entry.size(ctxt); + } + + public static <T extends TopiaEntity> List<T> findAll(TopiaContext ctxt, Class<T> klass) throws TopiaException { + TopiaEntityEnumEntry<T> entry = getEntry(klass); return entry.findAll(ctxt); } - public static <T extends TopiaEntity> T findByTopiaId(Class<T> klass, TopiaContext ctxt,String topiaId) throws TopiaException { + public static <T extends TopiaEntity> List<T> findAllWithOrder(TopiaContext ctxt, Class<T> klass,String... propertyNames) throws TopiaException { TopiaEntityEnumEntry<T> entry = getEntry(klass); + return entry.findAllWithOrder(ctxt, propertyNames); + } + + public static <T extends TopiaEntity> T findByTopiaId(TopiaContext ctxt, Class<T> klass,String topiaId) throws TopiaException { + TopiaEntityEnumEntry<T> entry = getEntry(klass); return entry.findByTopiaId(topiaId,ctxt); } - public static <T extends TopiaEntity> TopiaEntityEnumEntry<T> getEntry(T entity) throws TopiaException { - <%=className%> e = <%=className%>.valueOf(entity); - return (TopiaEntityEnumEntry<T>)e.entry; + public static <T extends TopiaEntity> T findByPrimaryKey(TopiaContext ctxt, Class<T> klass, Map<String, Object> keys) throws TopiaException { + TopiaEntityEnumEntry<T> entry = getEntry(klass); + return entry.findByPrimaryKey(ctxt, keys); } - public static <T extends TopiaEntity> TopiaEntityEnumEntry<T> getEntry(Class<T> klass) throws TopiaException { - <%=className%> e = <%=className%>.valueOf(klass); - return (TopiaEntityEnumEntry<T>)e.entry; + public static <T extends TopiaEntity> T findByPrimaryKey(TopiaContext ctxt, Class<T> klass, Object... k) throws TopiaException { + TopiaEntityEnumEntry<T> entry = getEntry(klass); + return entry.findByPrimaryKey(ctxt, k); } + + public static <T extends TopiaEntity> T findByProperty(TopiaContext ctxt, Class<T> klass, String propertyName, Object value) throws TopiaException { + TopiaEntityEnumEntry<T> entry = getEntry(klass); + return entry.findByProperty(ctxt, propertyName, value); + } + + public static <T extends TopiaEntity> T findByProperties(TopiaContext ctxt, Class<T> klass, String propertyName, Object value, Object... others) throws TopiaException { + TopiaEntityEnumEntry<T> entry = getEntry(klass); + return entry.findByProperties(ctxt, propertyName, value, others); + } + + public static <T extends TopiaEntity> T findByProperties(TopiaContext ctxt, Class<T> klass, Map<String, Object> properties) throws TopiaException { + TopiaEntityEnumEntry<T> entry = getEntry(klass); + return entry.findByProperties(ctxt, properties); + } + + public static <T extends TopiaEntity> List<T> findAllByProperty(TopiaContext ctxt, Class<T> klass, String propertyName, Object value) throws TopiaException { + TopiaEntityEnumEntry<T> entry = getEntry(klass); + return entry.findAllByProperty(ctxt, propertyName, value); + } + + public static <T extends TopiaEntity> List<T> findAllByProperties(TopiaContext ctxt, Class<T> klass, String propertyName, Object value, Object... others) throws TopiaException { + TopiaEntityEnumEntry<T> entry = getEntry(klass); + return entry.findAllByProperties(ctxt, propertyName, value, others); + } + + public static <T extends TopiaEntity> List<T> findAllByProperties(TopiaContext ctxt, Class<T> klass, Map<String, Object> properties) throws TopiaException { + TopiaEntityEnumEntry<T> entry = getEntry(klass); + return entry.findAllByProperties(ctxt, properties); + } + + public static <T extends TopiaEntity> T findContainsProperties(TopiaContext ctxt, Class<T> klass, Map<String, Collection> properties) throws TopiaException { + TopiaEntityEnumEntry<T> entry = getEntry(klass); + return entry.findContainsProperties(ctxt, properties); + } + + public static <T extends TopiaEntity> T findContainsProperties(TopiaContext ctxt, Class<T> klass, String propertyName, Collection values, Object... others) throws TopiaException { + TopiaEntityEnumEntry<T> entry = getEntry(klass); + return entry.findContainsProperties(ctxt, propertyName, values, others); + } + + public static <T extends TopiaEntity> List<T> findAllContainsProperties(TopiaContext ctxt, Class<T> klass, Map<String, Collection> properties) throws TopiaException { + TopiaEntityEnumEntry<T> entry = getEntry(klass); + return entry.findAllContainsProperties(ctxt, properties); + } + + public static <T extends TopiaEntity> List<T> findAllContainsProperties(TopiaContext ctxt, Class<T> klass, String propertyName, Collection values, Object... others) throws TopiaException { + TopiaEntityEnumEntry<T> entry = getEntry(klass); + return entry.findAllContainsProperties(ctxt, propertyName, values, others); + } + } }*/ } Modified: topia/trunk/src/main/java/org/codelutin/topia/persistence/TopiaDAO.java =================================================================== --- topia/trunk/src/main/java/org/codelutin/topia/persistence/TopiaDAO.java 2008-11-14 11:16:46 UTC (rev 1223) +++ topia/trunk/src/main/java/org/codelutin/topia/persistence/TopiaDAO.java 2008-11-16 18:29:50 UTC (rev 1224) @@ -114,6 +114,8 @@ public abstract List<Entity> findAll() throws TopiaException; + public abstract List<Entity> findAllWithOrder(String... propertyNames) throws TopiaException; + /** * TODO modifier l'implantation pour faire un simple count sur la base * Modified: topia/trunk/src/main/java/org/codelutin/topia/persistence/TopiaDAOAbstract.java =================================================================== --- topia/trunk/src/main/java/org/codelutin/topia/persistence/TopiaDAOAbstract.java 2008-11-14 11:16:46 UTC (rev 1223) +++ topia/trunk/src/main/java/org/codelutin/topia/persistence/TopiaDAOAbstract.java 2008-11-16 18:29:50 UTC (rev 1224) @@ -385,7 +385,7 @@ * Recherche une entity contenant certain propriété. Pour l'instant on * charche toute les entités existantes pour faire la recherche * - * @see org.codelutin.topia.TopiaDAO#findAllByProperties(java.util.Map) + * @see org.codelutin.topia.persistence.TopiaDAO#findAllByProperties(java.util.Map) */ public List<Entity> findAllByProperties(Map<String, Object> properties) throws TopiaException { Modified: topia/trunk/src/main/java/org/codelutin/topia/persistence/TopiaDAODelegator.java =================================================================== --- topia/trunk/src/main/java/org/codelutin/topia/persistence/TopiaDAODelegator.java 2008-11-14 11:16:46 UTC (rev 1223) +++ topia/trunk/src/main/java/org/codelutin/topia/persistence/TopiaDAODelegator.java 2008-11-16 18:29:50 UTC (rev 1224) @@ -143,6 +143,10 @@ return getParentDAO().findAll(); } + public List<Entity> findAllWithOrder(String... propertyNames) throws TopiaException { + return getParentDAO().findAllWithOrder(propertyNames); + } + /* (non-Javadoc) * @see org.codelutin.topia.TopiaDAO#size() */ Modified: topia/trunk/src/main/java/org/codelutin/topia/persistence/TopiaEntityEnumEntry.java =================================================================== --- topia/trunk/src/main/java/org/codelutin/topia/persistence/TopiaEntityEnumEntry.java 2008-11-14 11:16:46 UTC (rev 1223) +++ topia/trunk/src/main/java/org/codelutin/topia/persistence/TopiaEntityEnumEntry.java 2008-11-16 18:29:50 UTC (rev 1224) @@ -4,7 +4,9 @@ import org.codelutin.topia.TopiaException; import org.codelutin.topia.framework.TopiaContextImplementor; +import java.util.Collection; import java.util.List; +import java.util.Map; /** @author chemit */ public class TopiaEntityEnumEntry<T extends TopiaEntity> implements java.io.Serializable { @@ -50,6 +52,16 @@ return newInstance; } + public T create(TopiaContext ctxt, Object... properties) throws TopiaException { + TopiaDAO<T> dao = getDAO(ctxt); + return dao.create(properties); + } + + public T create(TopiaContext ctxt, Map<String, Object> properties) throws TopiaException { + TopiaDAO<T> dao = getDAO(ctxt); + return dao.create(properties); + } + public T update(T entity, TopiaContext ctxt) throws TopiaException { TopiaDAO<T> dao = getDAO(ctxt); return dao.update(entity); @@ -60,14 +72,84 @@ dao.delete(entity); } + public int size(TopiaContext ctxt) throws TopiaException { + TopiaDAO<T> dao = getDAO(ctxt); + return dao.size(); + } + public List<T> findAll(TopiaContext ctxt) throws TopiaException { TopiaDAO<T> dao = getDAO(ctxt); return dao.findAll(); } + public List<T> findAllWithOrder(TopiaContext ctxt,String... propertyNames) throws TopiaException { + TopiaDAO<T> dao = getDAO(ctxt); + return dao.findAllWithOrder(propertyNames); + } + public T findByTopiaId(String topiaId, TopiaContext ctxt) throws TopiaException { TopiaDAO<T> dao = getDAO(ctxt); - return dao.findByTopiaId(topiaId); + return dao.findByTopiaId(topiaId); } + public T findByPrimaryKey(TopiaContext ctxt, Map<String, Object> keys) throws TopiaException { + TopiaDAO<T> dao = getDAO(ctxt); + return dao.findByPrimaryKey(keys); + } + + public T findByPrimaryKey(TopiaContext ctxt, Object... k) throws TopiaException { + TopiaDAO<T> dao = getDAO(ctxt); + return dao.findByPrimaryKey(k); + } + + public T findByProperty(TopiaContext ctxt, String propertyName, Object value) throws TopiaException { + TopiaDAO<T> dao = getDAO(ctxt); + return dao.findByProperty(propertyName, value); + } + + public T findByProperties(TopiaContext ctxt, String propertyName, Object value, Object... others) throws TopiaException { + TopiaDAO<T> dao = getDAO(ctxt); + return dao.findByProperties(propertyName, value, others); + } + + public T findByProperties(TopiaContext ctxt, Map<String, Object> properties) throws TopiaException { + TopiaDAO<T> dao = getDAO(ctxt); + return dao.findByProperties(properties); + } + + public List<T> findAllByProperty(TopiaContext ctxt, String propertyName, Object value) throws TopiaException { + TopiaDAO<T> dao = getDAO(ctxt); + return dao.findAllByProperty(propertyName, value); + } + + public List<T> findAllByProperties(TopiaContext ctxt, String propertyName, Object value, Object... others) throws TopiaException { + TopiaDAO<T> dao = getDAO(ctxt); + return dao.findAllByProperties(propertyName, value, others); + } + + public List<T> findAllByProperties(TopiaContext ctxt, Map<String, Object> properties) throws TopiaException { + TopiaDAO<T> dao = getDAO(ctxt); + return dao.findAllByProperties(properties); + } + + public T findContainsProperties(TopiaContext ctxt, Map<String, Collection> properties) throws TopiaException { + TopiaDAO<T> dao = getDAO(ctxt); + return dao.findContainsProperties(properties); + } + + public T findContainsProperties(TopiaContext ctxt, String propertyName, Collection values, Object... others) throws TopiaException { + TopiaDAO<T> dao = getDAO(ctxt); + return dao.findContainsProperties(propertyName, values, others); + } + + public List<T> findAllContainsProperties(TopiaContext ctxt, Map<String, Collection> properties) throws TopiaException { + TopiaDAO<T> dao = getDAO(ctxt); + return dao.findAllContainsProperties(properties); + } + + public List<T> findAllContainsProperties(TopiaContext ctxt, String propertyName, Collection values, Object... others) throws TopiaException { + TopiaDAO<T> dao = getDAO(ctxt); + return dao.findAllContainsProperties(propertyName, values, others); + } + } Modified: topia/trunk/src/main/java/org/codelutin/topia/persistence/flatfile/TopiaDAOFlatFile.java =================================================================== --- topia/trunk/src/main/java/org/codelutin/topia/persistence/flatfile/TopiaDAOFlatFile.java 2008-11-14 11:16:46 UTC (rev 1223) +++ topia/trunk/src/main/java/org/codelutin/topia/persistence/flatfile/TopiaDAOFlatFile.java 2008-11-16 18:29:50 UTC (rev 1224) @@ -250,7 +250,7 @@ /** * Sauve l'entity dans le fichier * - * @see org.codelutin.topia.TopiaDAO#update() + * @see org.codelutin.topia.persistence.TopiaDAO#update(org.codelutin.topia.persistence.TopiaEntity) */ public Entity update(Entity e) throws TopiaException { getContext().getFiresSupport().fireOnPreUpdate(getContext(), e, new Object[]{}); @@ -331,7 +331,7 @@ /** * Supprime le fichier contenant l'entity * - * @see org.codelutin.topia.TopiaDAO#delete() + * @see org.codelutin.topia.persistence.TopiaDAO#delete(org.codelutin.topia.persistence.TopiaEntity) */ public void delete(Entity e) throws TopiaException { getContext().getFiresSupport().fireOnPreDelete(getContext(), e, new Object[]{}); @@ -350,7 +350,7 @@ * Recherche un fichier si dans mapping key est indiqué il est utilisé a la * place du topiaId qui n'existe peut-etre pas * - * @see org.codelutin.topia.TopiaDAO#findByTopiaId() + * @see org.codelutin.topia.persistence.TopiaDAO#findByTopiaId(String) */ @Override public Entity findByTopiaId(String k) throws TopiaException { @@ -460,6 +460,10 @@ return result; } + public List<Entity> findAllWithOrder(String... propertyNames) throws TopiaException { + throw new IllegalStateException("findAllWithOrder method is not implemented by "+getClass()); + } + /** * Permet de filtrer les fichiers si une extension a ete defini dans le * mapping Modified: topia/trunk/src/main/java/org/codelutin/topia/persistence/hibernate/TopiaDAOHibernate.java =================================================================== --- topia/trunk/src/main/java/org/codelutin/topia/persistence/hibernate/TopiaDAOHibernate.java 2008-11-14 11:16:46 UTC (rev 1223) +++ topia/trunk/src/main/java/org/codelutin/topia/persistence/hibernate/TopiaDAOHibernate.java 2008-11-16 18:29:50 UTC (rev 1224) @@ -44,6 +44,7 @@ import org.hibernate.Session; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Restrictions; +import org.hibernate.criterion.Order; /** * Cette persistence accède à l'objet Session hibernate pour exécuter @@ -121,11 +122,25 @@ } } + public List<Entity> findAllWithOrder(String... propertyNames) throws TopiaException { + try { + Criteria criteria = createCriteria(FlushMode.AUTO); + for (String propertyName : propertyNames) { + criteria.addOrder(Order.asc(propertyName)); + } + List<Entity> result = (List<Entity>)criteria.list(); + result = getContext().getFiresSupport().fireEntitiesLoad(context, result); + return result; + } catch (HibernateException eee) { + throw new TopiaException(eee); + } + } + /** * TODO implanter une facon plus rapide de recupere la size (count direct * sur la base) * (non-Javadoc) - * @see org.codelutin.topia.TopiaDAO#size() + * @see org.codelutin.topia.persistence.TopiaDAO#size() */ @Override public int size() throws TopiaException {
participants (1)
-
tchemit@users.labs.libre-entreprise.org