Index: topia2/src/java/org/codelutin/topia/persistence/TopiaDAOAbstract.java diff -u topia2/src/java/org/codelutin/topia/persistence/TopiaDAOAbstract.java:1.9 topia2/src/java/org/codelutin/topia/persistence/TopiaDAOAbstract.java:1.10 --- topia2/src/java/org/codelutin/topia/persistence/TopiaDAOAbstract.java:1.9 Fri Mar 17 15:10:36 2006 +++ topia2/src/java/org/codelutin/topia/persistence/TopiaDAOAbstract.java Wed Jun 7 15:56:01 2006 @@ -23,9 +23,9 @@ * Created: 31 déc. 2005 13:10:34 * * @author poussin - * @version $Revision: 1.9 $ + * @version $Revision: 1.10 $ * - * Last update: $Date: 2006/03/17 15:10:36 $ + * Last update: $Date: 2006/06/07 15:56:01 $ * by : $Author: thimel $ */ @@ -49,6 +49,8 @@ import org.codelutin.topia.framework.TopiaContextImplementor; import org.codelutin.topia.persistence.hibernate.TopiaDAOHibernate; import org.hibernate.HibernateException; +import org.hibernate.criterion.Criterion; +import org.hibernate.criterion.Restrictions; import org.hibernate.metadata.ClassMetadata; @@ -583,6 +585,93 @@ return result; } -} + /* (non-Javadoc) + * @see org.codelutin.topia.persistence.TopiaDAO#like(java.lang.Object) + */ + public Entity like(Object ... values) throws TopiaException { + Criterion criterion = computeCriterions(values); + return query(criterion); + } + + /* (non-Javadoc) + * @see org.codelutin.topia.persistence.TopiaDAO#likeAll(java.lang.Object) + */ + public List likeAll(Object ... values) throws TopiaException { + Criterion criterion = computeCriterions(values); + return queryAll(criterion); + } + + private Criterion computeCriterions(Object ... values) { + if (values == null) { + return null; + } + Criterion criterion = null; + for (Object value : values) { + criterion = or(criterion, computeCriterion(value)); + } + return criterion; + } + + private Criterion computeCriterion(Object value) { + Criterion criterion = null; + SearchFields fields = entityClass.getAnnotation(SearchFields.class); + String textValue = "%" + value + "%"; + //textFields + String[] textFields = fields.txtFields(); + for (String propName : textFields) { + criterion = or(criterion, Restrictions.like(propName, textValue)); + } + //numFields + boolean isNumber = (value instanceof Number); + if (value instanceof String) { + try { + Double.parseDouble((String) value); + isNumber = true; + } catch (NumberFormatException nfe) { + isNumber = false; + } + + } + if (isNumber) { + String[] numFields = fields.numFields(); + for (String propName : numFields) { + criterion = or(criterion, Restrictions.sqlRestriction(propName + " like '" + textValue + "'")); + } + } + //boolFields + boolean isBoolean = (value instanceof Boolean); + if (value instanceof String) { + isBoolean |= ("true".equalsIgnoreCase((String)value) + || "false".equalsIgnoreCase((String)value)); + } + if (isBoolean) { + Boolean booleanValue; + if (value instanceof String) { + booleanValue = Boolean.valueOf((String)value); + } else { + booleanValue = (Boolean)value; + } + String[] boolFields = fields.numFields(); + for (String propName : boolFields) { + criterion = or(criterion, Restrictions.eq(propName, booleanValue)); + } + } + //timeFields + String[] timeFields = fields.dateFields(); + for (String propName : timeFields) { + criterion = or(criterion, Restrictions.sqlRestriction(propName + " like '" + textValue + "'")); + } + return criterion; + } + private Criterion or(Criterion crit1, Criterion crit2) { + if (crit1 == null) { + return crit2; + } + if (crit2 == null) { + return crit1; + } + return Restrictions.or(crit1, crit2); + } +} //TopiaDAOAbstract Index: topia2/src/java/org/codelutin/topia/persistence/TopiaDAO.java diff -u topia2/src/java/org/codelutin/topia/persistence/TopiaDAO.java:1.5 topia2/src/java/org/codelutin/topia/persistence/TopiaDAO.java:1.6 --- topia2/src/java/org/codelutin/topia/persistence/TopiaDAO.java:1.5 Fri Mar 17 15:10:36 2006 +++ topia2/src/java/org/codelutin/topia/persistence/TopiaDAO.java Wed Jun 7 15:56:01 2006 @@ -23,9 +23,9 @@ * Created: 30 déc. 2005 03:00:57 * * @author poussin - * @version $Revision: 1.5 $ + * @version $Revision: 1.6 $ * - * Last update: $Date: 2006/03/17 15:10:36 $ + * Last update: $Date: 2006/06/07 15:56:01 $ * by : $Author: thimel $ */ @@ -38,6 +38,7 @@ import org.codelutin.topia.TopiaException; import org.codelutin.topia.event.TopiaEntityListener; import org.codelutin.topia.framework.TopiaContextImplementor; +import org.hibernate.criterion.Criterion; /** * @author poussin @@ -214,4 +215,30 @@ public abstract List findAllContainsProperties(String propertyName, Collection values, Object... others) throws TopiaException; + /** + * Cherche en renvoie la premiere entite trouvee correspondant au criterion + * donne + */ + public abstract Entity query(Criterion criterion) throws TopiaException; + + /** + * Cherche en renvoie les entites trouvees correspondantes au criterion + * donne + */ + public abstract List queryAll(Criterion criterion) throws TopiaException; + + /** + * Effectue une recherche de type 'like' sur tous les attributs 'likeable' + * de l'entite et renvoi le premier resultat. + * Si plusieurs valeurs sont passees, un or est effectue entre chaque champ + */ + public abstract Entity like(Object ... values) throws TopiaException; + + /** + * Effectue une recherche de type 'like' sur tous les attributs 'likeable' + * de l'entite. + * Si plusieurs valeurs sont passees, un or est effectue entre chaque champ + */ + public abstract List likeAll(Object ... values) throws TopiaException; + } //TopiaDAO Index: topia2/src/java/org/codelutin/topia/persistence/TopiaEntity.java diff -u topia2/src/java/org/codelutin/topia/persistence/TopiaEntity.java:1.6 topia2/src/java/org/codelutin/topia/persistence/TopiaEntity.java:1.7 --- topia2/src/java/org/codelutin/topia/persistence/TopiaEntity.java:1.6 Mon Mar 13 13:29:08 2006 +++ topia2/src/java/org/codelutin/topia/persistence/TopiaEntity.java Wed Jun 7 15:56:01 2006 @@ -23,10 +23,10 @@ * Created: 28 déc. 2005 22:48:10 * * @author poussin - * @version $Revision: 1.6 $ + * @version $Revision: 1.7 $ * - * Last update: $Date: 2006/03/13 13:29:08 $ - * by : $Author: bpoussin $ + * Last update: $Date: 2006/06/07 15:56:01 $ + * by : $Author: thimel $ */ package org.codelutin.topia.persistence; @@ -43,6 +43,7 @@ * * @author poussin */ +@SearchFields public interface TopiaEntity { public String getTopiaId(); Index: topia2/src/java/org/codelutin/topia/persistence/TopiaDAODelegator.java diff -u topia2/src/java/org/codelutin/topia/persistence/TopiaDAODelegator.java:1.5 topia2/src/java/org/codelutin/topia/persistence/TopiaDAODelegator.java:1.6 --- topia2/src/java/org/codelutin/topia/persistence/TopiaDAODelegator.java:1.5 Fri Mar 17 15:10:36 2006 +++ topia2/src/java/org/codelutin/topia/persistence/TopiaDAODelegator.java Wed Jun 7 15:56:01 2006 @@ -23,9 +23,9 @@ * Created: 30 déc. 2005 22:28:48 * * @author poussin - * @version $Revision: 1.5 $ + * @version $Revision: 1.6 $ * - * Last update: $Date: 2006/03/17 15:10:36 $ + * Last update: $Date: 2006/06/07 15:56:01 $ * by : $Author: thimel $ */ @@ -38,6 +38,7 @@ import org.codelutin.topia.TopiaException; import org.codelutin.topia.event.TopiaEntityListener; import org.codelutin.topia.framework.TopiaContextImplementor; +import org.hibernate.criterion.Criterion; /** @@ -236,6 +237,32 @@ return getParentDAO().findAllContainsProperties(propertyName, values, others); } -} + /* (non-Javadoc) + * @see org.codelutin.topia.persistence.TopiaDAO#like(java.lang.Object) + */ + public Entity like(Object ... values) throws TopiaException { + return getParentDAO().like(values); + } + + /* (non-Javadoc) + * @see org.codelutin.topia.persistence.TopiaDAO#likeAll(java.lang.Object) + */ + public List likeAll(Object ... values) throws TopiaException { + return getParentDAO().likeAll(values); + } + /* (non-Javadoc) + * @see org.codelutin.topia.persistence.TopiaDAO#query(org.hibernate.criterion.Criterion) + */ + public Entity query(Criterion criterion) throws TopiaException { + return getParentDAO().query(criterion); + } + + /* (non-Javadoc) + * @see org.codelutin.topia.persistence.TopiaDAO#queryAll(org.hibernate.criterion.Criterion) + */ + public List queryAll(Criterion criterion) throws TopiaException { + return getParentDAO().queryAll(criterion); + } +} //TopiaDAODelegator Index: topia2/src/java/org/codelutin/topia/persistence/SearchFields.java diff -u /dev/null topia2/src/java/org/codelutin/topia/persistence/SearchFields.java:1.1 --- /dev/null Wed Jun 7 15:56:07 2006 +++ topia2/src/java/org/codelutin/topia/persistence/SearchFields.java Wed Jun 7 15:56:01 2006 @@ -0,0 +1,69 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, +* Cédric Pineau, Benjamin Poussin, +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ + +/* * +* SearchFields.java +* +* Created: 7 juin 2006 +* +* @author Arnaud Thimel +* @version $Revision: 1.1 $ +* +* Mise a jour: $Date: 2006/06/07 15:56:01 $ +* par : $Author: thimel $ +*/ + +package org.codelutin.topia.persistence; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import static java.lang.annotation.ElementType.TYPE; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * Ces annotations permettent de savoir quels sont les champs sur lesquels + * la recherche pourra s'effectuer. + */ +@Retention(RUNTIME) +@Target(TYPE) +public @interface SearchFields { + + /** + * @return la liste des champs textes + */ + String [] txtFields() default {}; + + /** + * @return la liste des champs numeriques + */ + String [] numFields() default {}; + + /** + * @return la liste des champs booleens + */ + String [] boolFields() default {}; + + /** + * @return la liste des champs date + */ + String [] dateFields() default {}; + +} //SearchFields