Topia-commits
Threads by month
- ----- 2026 -----
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2005 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2004 -----
- December
- November
- October
- September
- August
- July
- June
- May
May 2010
- 4 participants
- 68 discussions
r1985 - trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence
by fdesbois@users.nuiton.org 31 May '10
by fdesbois@users.nuiton.org 31 May '10
31 May '10
Author: fdesbois
Date: 2010-05-31 17:54:09 +0200 (Mon, 31 May 2010)
New Revision: 1985
Url: http://nuiton.org/repositories/revision/topia/1985
Log:
Missing getContext method to use DAOLegacy
Modified:
trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java
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 2010-05-30 14:56:55 UTC (rev 1984)
+++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2010-05-31 15:54:09 UTC (rev 1985)
@@ -506,7 +506,7 @@
@Override
public E findByQuery(TopiaQuery query) throws TopiaException {
- E result = query.executeToEntity(context, getEntityClass());
+ E result = query.executeToEntity(getContext(), getEntityClass());
return result;
}
1
0
r1984 - in trunk/topia-persistence/src: main/java/org/nuiton/topia/framework main/java/org/nuiton/topia/generator main/java/org/nuiton/topia/persistence test/xmi
by fdesbois@users.nuiton.org 30 May '10
by fdesbois@users.nuiton.org 30 May '10
30 May '10
Author: fdesbois
Date: 2010-05-30 16:56:55 +0200 (Sun, 30 May 2010)
New Revision: 1984
Url: http://nuiton.org/repositories/revision/topia/1984
Log:
- Add LegacyDAO to compare memory charge between HQL query and Criteria (old usage for findByProperties) : 'model.tagvalue.useLegacyDAO=true' in model properties
- Change log level in TopiaQuery
- Manage null case for findByTopiaId in TopiaDAOImpl
Added:
trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOLegacy.java
Modified:
trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java
trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/DAOAbstractTransformer.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/test/xmi/topiatest.properties
Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java
===================================================================
--- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2010-05-28 13:15:53 UTC (rev 1983)
+++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2010-05-30 14:56:55 UTC (rev 1984)
@@ -634,8 +634,8 @@
*/
public TopiaQuery addWhere(String paramName, Op operator, Object paramValue) {
StringBuilder result = new StringBuilder(paramName).append(' ');
- if (log.isDebugEnabled()) {
- log.debug("paramValue = " + paramValue);
+ if (log.isTraceEnabled()) {
+ log.trace("paramValue = " + paramValue);
}
if (paramValue == null) {
result.append(Op.NULL);
@@ -700,8 +700,8 @@
}
// Only one paramValue
if (length == 1) {
- if (log.isDebugEnabled()) {
- log.debug("Only one value " + Arrays.toString(paramValue));
+ if (log.isTraceEnabled()) {
+ log.trace("Only one value " + Arrays.toString(paramValue));
}
return addWhere(paramName, Op.EQ, paramValue[0]);
}
Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/DAOAbstractTransformer.java
===================================================================
--- trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/DAOAbstractTransformer.java 2010-05-28 13:15:53 UTC (rev 1983)
+++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/DAOAbstractTransformer.java 2010-05-30 14:56:55 UTC (rev 1984)
@@ -44,6 +44,7 @@
import org.nuiton.topia.framework.TopiaContextImplementor;
import org.nuiton.topia.persistence.TopiaDAO;
import org.nuiton.topia.persistence.TopiaDAOImpl;
+import org.nuiton.topia.persistence.TopiaDAOLegacy;
import org.nuiton.topia.persistence.TopiaEntity;
import org.nuiton.util.StringUtil;
@@ -81,6 +82,8 @@
Map<ObjectModelClass, Set<ObjectModelClass>> usages;
+ protected boolean extendLegacyDAO;
+
/**
* Map of extra operations for DAO. The key of the map is the qualified
* name of the entity relative to the DAO.
@@ -92,6 +95,7 @@
public void transformFromModel(ObjectModel model) {
usages = TopiaGeneratorUtil.searchDirectUsages(model);
+ extendLegacyDAO = Boolean.valueOf(model.getTagValue("useLegacyDAO"));
}
@Override
@@ -156,8 +160,11 @@
}
}
if (extendClass.length() == 0) {
- extendClass = TopiaDAOImpl.class.getName() + "<E>";
-
+ if (extendLegacyDAO) {
+ extendClass = TopiaDAOLegacy.class.getName() + "<E>";
+ } else {
+ extendClass = TopiaDAOImpl.class.getName() + "<E>";
+ }
}
if (log.isDebugEnabled()) {
log.debug("super class = " + extendClass);
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 2010-05-28 13:15:53 UTC (rev 1983)
+++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java 2010-05-30 14:56:55 UTC (rev 1984)
@@ -259,7 +259,15 @@
*/
TopiaQuery createQuery(String entityAlias);
- E findByTopiaId(String k) throws TopiaException;
+ /**
+ * Find an entity corresponding to the {@code id}. If the {@code id} is
+ * null, nothing will be search.
+ *
+ * @param id topiaId of the entity to found
+ * @return the entity found or null if not
+ * @throws TopiaException for Topia errors on query
+ */
+ E findByTopiaId(String id) throws TopiaException;
E findByProperty(String propertyName, Object value)
throws TopiaException;
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 2010-05-28 13:15:53 UTC (rev 1983)
+++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2010-05-30 14:56:55 UTC (rev 1984)
@@ -469,8 +469,12 @@
@Override
public E findByTopiaId(String id) throws TopiaException {
- TopiaQuery query = createQuery().addEquals(TopiaEntity.TOPIA_ID, id);
- E result = findByQuery(query);
+ // Nothing to do if id is null, the result will still be null.
+ E result = null;
+ if (id != null) {
+ TopiaQuery query = createQuery().addEquals(TopiaEntity.TOPIA_ID, id);
+ result = findByQuery(query);
+ }
return result;
}
@@ -515,7 +519,7 @@
@Override
public List<String> findAllIds() throws TopiaException {
TopiaQuery query = createQuery().setSelect(TopiaEntity.TOPIA_ID);
- List<String> results = context.findByQuery(query);
+ List<String> results = getContext().findByQuery(query);
return results;
}
@@ -547,7 +551,7 @@
@Override
public List<E> findAllByQuery(TopiaQuery query) throws TopiaException {
- List<E> results = query.executeToEntityList(context, getEntityClass());
+ List<E> results = query.executeToEntityList(getContext(), getEntityClass());
return results;
}
@@ -555,7 +559,7 @@
public Map<String, E> findAllMappedByQuery(TopiaQuery query)
throws TopiaException {
Map<String, E> results =
- query.executeToEntityMap(context, getEntityClass());
+ query.executeToEntityMap(getContext(), getEntityClass());
return results;
}
@@ -564,7 +568,7 @@
Class<K> keyClass)
throws TopiaException {
Map<K, E> results =
- query.executeToEntityMap(context, getEntityClass(),
+ query.executeToEntityMap(getContext(), getEntityClass(),
keyName, keyClass);
return results;
}
@@ -621,7 +625,7 @@
@Override
public int countByQuery(TopiaQuery query) throws TopiaException {
- int result = query.executeCount(context);
+ int result = query.executeCount(getContext());
return result;
}
Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOLegacy.java
===================================================================
--- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOLegacy.java (rev 0)
+++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOLegacy.java 2010-05-30 14:56:55 UTC (rev 1984)
@@ -0,0 +1,871 @@
+/* *##%
+ * ToPIA :: Persistence
+ * Copyright (C) 2004 - 2009 CodeLutin
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 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 Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU General Lesser Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/lgpl-3.0.html>.
+ * ##%*/
+
+/* *
+ * TopiaDAOAbstract.java
+ *
+ * Created: 31 déc. 2005 13:10:34
+ *
+ * @author poussin
+ * @version $Revision$
+ *
+ * Last update: $Date$
+ * by : $Author$
+ */
+
+package org.nuiton.topia.persistence;
+
+import java.io.Serializable;
+import java.lang.reflect.InvocationTargetException;
+import java.security.Permission;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.commons.beanutils.PropertyUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.hibernate.Criteria;
+import org.hibernate.FlushMode;
+import org.nuiton.topia.TopiaException;
+import org.nuiton.topia.event.TopiaEntityListener;
+import org.nuiton.topia.event.TopiaEntityVetoable;
+import org.nuiton.topia.framework.TopiaContextImplementor;
+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.framework.TopiaQuery;
+
+/**
+ * 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.
+ *
+ * @param <E> le type de l'entite
+ * @author poussin
+ *
+ */
+
+public class TopiaDAOLegacy<E extends TopiaEntity> extends TopiaDAOImpl<E> { // TopiaDAOImpl
+ /** to use log facility, just put in your code: log.info(\"...\"); */
+ private static Log log = LogFactory.getLog(TopiaDAOImpl.class);
+
+ protected Class<E> entityClass = null;
+
+ protected TopiaContextImplementor context = null;
+
+ @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 (IllegalAccessException eee) {
+ throw new TopiaException("Impossible de récuperer l'identifiant "
+ + idPropName + " de l'entite: " + e);
+ } catch (InvocationTargetException eee) {
+ throw new TopiaException("Impossible de récuperer l'identifiant "
+ + idPropName + " de l'entite: " + e);
+ } catch (NoSuchMethodException 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);
+ }
+
+ return result;
+ }
+
+ /**
+ * Callback method when context was commit.
+ * By default, the method does nothing
+ * @throws org.nuiton.topia.TopiaException
+ */
+ @Override
+ public void commitTransaction() throws TopiaException {
+ }
+
+ /**
+ * Callback method when context was rollback.
+ * By default, the method does nothing
+ * @throws org.nuiton.topia.TopiaException
+ */
+ @Override
+ public void rollbackTransaction() throws TopiaException {
+ }
+
+ @Override
+ public E create(Object... properties) throws TopiaException {
+ Map<String, Object> map = new HashMap<String, Object>();
+ Object propertyName = null;
+ Object value = null;
+ try {
+ for (int i = 0; i < properties.length;) {
+ propertyName = properties[i++];
+ value = properties[i++];
+ map.put((String) propertyName, value);
+ }
+ } catch (ArrayIndexOutOfBoundsException eee) {
+ throw new IllegalArgumentException("Wrong number of argument "
+ + properties.length
+ + ", you must have even number. Last property name read: "
+ + propertyName);
+ } catch (ClassCastException eee) {
+ throw new IllegalArgumentException(
+ "Wrong argument type, wait property name as String and have "
+ + propertyName.getClass().getName());
+ }
+
+ E result = create(map);
+ return result;
+ }
+
+ @Override
+ public E findByPrimaryKey(Map<String, Object> keys)
+ throws TopiaException {
+ try {
+ // we used hibernate meta information for all persistence type
+ // it's more easy than create different for all persistence
+ ClassMetadata meta = getClassMetadata();
+ if (meta.hasNaturalIdentifier()) {
+ E result = findByProperties(keys);
+ return result;
+ }
+ } catch (HibernateException eee) {
+ throw new TopiaException(eee);
+ }
+ throw new TopiaException("La classe " + entityClass.getName()
+ + " n'a pas de cle primaire naturelle");
+
+ }
+
+ /**
+ * 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
+ // mettre un premier couple (propName, value) en argument ca evitera
+ // de pouvoir appeler cette methode sans argument
+ try {
+ ClassMetadata meta = getClassMetadata();
+ if (meta.hasNaturalIdentifier()) {
+ int[] ikeys = meta.getNaturalIdentifierProperties();
+ String[] pnames = meta.getPropertyNames();
+
+ Map<String, Object> keys = new HashMap<String, Object>();
+ for (int ikey : ikeys) {
+ keys.put(pnames[ikey], k[ikey]);
+ }
+
+ E result = findByProperties(keys);
+ return result;
+ }
+ } catch (HibernateException eee) {
+ throw new TopiaException(eee);
+ }
+ throw new TopiaException("La classe " + entityClass.getName()
+ + " n'a pas de cle primaire naturelle");
+
+ }
+
+ @Override
+ public E findByProperties(String propertyName, Object value,
+ Object... others) throws TopiaException {
+ Map<String, Object> properties = new HashMap<String, Object>();
+ properties.put(propertyName, value);
+ Object name = null;
+ for (int i = 0; i < others.length;) {
+ try {
+ name = others[i++];
+ value = others[i++];
+ properties.put((String) name, value);
+ } catch (ClassCastException eee) {
+ throw new IllegalArgumentException(
+ "Les noms des propriétés doivent être des chaines et non pas "
+ + propertyName.getClass().getName(), eee);
+ } catch (ArrayIndexOutOfBoundsException eee) {
+ throw new IllegalArgumentException(
+ "Le nombre d'argument n'est pas un nombre pair: "
+ + (others.length + 2)
+ + " La dernière propriété était: " + name, eee);
+ }
+ }
+ E result = findByProperties(properties);
+ return result;
+ }
+
+ @Override
+ public List<E> findAllByProperties(String propertyName, Object value,
+ Object... others) throws TopiaException {
+ Map<String, Object> properties = new HashMap<String, Object>();
+ properties.put(propertyName, value);
+ Object name = null;
+ for (int i = 0; i < others.length;) {
+ try {
+ name = others[i++];
+ value = others[i++];
+ properties.put((String) name, value);
+ } catch (ClassCastException eee) {
+ throw new IllegalArgumentException(
+ "Les noms des propriétés doivent être des chaines et non pas "
+ + propertyName.getClass().getName(), eee);
+ } catch (ArrayIndexOutOfBoundsException eee) {
+ throw new IllegalArgumentException(
+ "Le nombre d'argument n'est pas un nombre pair: "
+ + (others.length + 2)
+ + " La dernière propriété était: " + name, eee);
+ }
+ }
+ List<E> result = findAllByProperties(properties);
+ return result;
+ }
+
+ @Override
+ public E findContainsProperties(Map<String, Collection<?>> properties)
+ throws TopiaException {
+ List<E> results = findAllContainsProperties(properties);
+ E result = null;
+ if (results.size() > 0) {
+ result = results.get(0);
+ }
+ return result;
+ }
+
+ @Override
+ public E findContainsProperties(String propertyName,
+ Collection values, Object... others) throws TopiaException {
+ Map<String, Collection<?>> properties = new HashMap<String, Collection<?>>();
+ properties.put(propertyName, values);
+ Object name = null;
+ for (int i = 0; i < others.length;) {
+ try {
+ name = others[i++];
+ values = (Collection) others[i++];
+ properties.put((String) name, values);
+ } catch (ClassCastException eee) {
+ throw new IllegalArgumentException(
+ "Les noms des propriétés doivent être des chaines et non pas "
+ + propertyName.getClass().getName(), eee);
+ } catch (ArrayIndexOutOfBoundsException eee) {
+ throw new IllegalArgumentException(
+ "Le nombre d'argument n'est pas un nombre pair: "
+ + (others.length + 2)
+ + " La dernière propriété était: " + name, eee);
+ }
+ }
+ E result = findContainsProperties(properties);
+ return result;
+ }
+
+ /**
+ * Find all entities with a specific rule :
+ * When the entity have a Collection type property, you want to find all entites where some values are
+ * contained in the collection type property.
+ * Example entity parameter : private Collection<Date> historicalDates;
+ * You want some dates to be contained in historicalDates.
+ * Collection<Date> myDates...
+ * myDates.add(date1) ...
+ * Map<String, Collection> properties = new HashMap<String,Collection>();
+ * properties.put("historicalDates",myDates);
+ * findAllContainsProperties(properties);
+ * @param properties
+ * @return the list of entities corresponding to the request
+ * @throws org.nuiton.topia.TopiaException if any pb
+ */
+ @Override
+ public List<E> findAllContainsProperties(Map<String, Collection<?>> properties) throws TopiaException {
+ List<E> all = findAll();
+ List<E> result = new ArrayList<E>();
+ for (E e : all) {
+ boolean ok = true;
+ try {
+ for (Entry<String, Collection<?>> kv : properties.entrySet()) {
+ Collection entityValues = (Collection) PropertyUtils
+ .getProperty(e, kv.getKey());
+ Collection values = kv.getValue();
+ if (!entityValues.containsAll(values)) {
+ ok = false;
+ break;
+ }
+ }
+ } catch (IllegalAccessException eee) {
+ ok = false;
+ if (log.isWarnEnabled()) {
+ log.warn(
+ "Impossible d'acceder a la methode demandé pour l'obbjet "
+ + e, eee);
+ }
+ } catch (InvocationTargetException eee) {
+ ok = false;
+ if (log.isWarnEnabled()) {
+ log.warn(
+ "Impossible d'acceder a la methode demandé pour l'obbjet "
+ + e, eee);
+ }
+ } catch (NoSuchMethodException eee) {
+ ok = false;
+ if (log.isWarnEnabled()) {
+ log.warn(
+ "Impossible d'acceder a la methode demandé pour l'obbjet "
+ + e, eee);
+ }
+ }
+ if (ok) {
+ result.add(e);
+ }
+ }
+ return result;
+ }
+
+ @Override
+ public List<E> findAllContainsProperties(String propertyName,
+ Collection values, Object... others) throws TopiaException {
+ Map<String, Collection<?>> properties = new HashMap<String, Collection<?>>();
+ properties.put(propertyName, values);
+ Object name = null;
+ for (int i = 0; i < others.length;) {
+ try {
+ name = others[i++];
+ values = (Collection) others[i++];
+ properties.put((String) name, values);
+ } catch (ClassCastException eee) {
+ throw new IllegalArgumentException(
+ "Les noms des propriétés doivent être des chaines et non pas "
+ + propertyName.getClass().getName(), eee);
+ } catch (ArrayIndexOutOfBoundsException eee) {
+ throw new IllegalArgumentException(
+ "Le nombre d'argument n'est pas un nombre pair: "
+ + (others.length + 2)
+ + " La dernière propriété était: " + name, eee);
+ }
+ }
+ List<E> result = findAllContainsProperties(properties);
+ return result;
+ }
+
+ @Override
+ public List<E> findAllByProperty(String propertyName, Object value)
+ throws TopiaException {
+ Map<String, Object> properties = new HashMap<String, Object>();
+ properties.put(propertyName, value);
+ List<E> result = findAllByProperties(properties);
+ return result;
+ }
+
+ @Override
+ public E findByProperty(String propertyName, Object value)
+ throws TopiaException {
+ Map<String, Object> properties = new HashMap<String, Object>();
+ properties.put(propertyName, value);
+ E result = findByProperties(properties);
+ return result;
+ }
+
+ @Deprecated
+ private Criterion computeCriterions(Object... values) {
+ if (values == null) {
+ return null;
+ }
+ Criterion criterion = null;
+ for (Object value : values) {
+ criterion = or(criterion, computeCriterion(value));
+ }
+ return criterion;
+ }
+
+ @Deprecated
+ 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;
+ }
+
+ @Deprecated
+ private Criterion or(Criterion crit1, Criterion crit2) {
+ if (crit1 == null) {
+ return crit2;
+ }
+ if (crit2 == null) {
+ return crit1;
+ }
+ 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
+ * ou a appeler vous aussi ces deux methodes.
+ */
+ @Override
+ public E create(Map<String, Object> properties) throws TopiaException {
+ E result = instanciateNew();
+
+ // TODO reflechir s'il ne faudrait pas creer l'id avant l'event precedent
+ // reflechir toujours dans un context on les E pourrait ne pas
+ // etre des TopiaEntity
+ if (result instanceof TopiaEntity) {
+ String topiaId = TopiaId.create(entityClass);
+ TopiaEntityAbstract entity = (TopiaEntityAbstract) result;
+ entity.setTopiaId(topiaId);
+ entity.setTopiaContext(getContext());
+ }
+ try {
+ for (Map.Entry<String, Object> e : properties.entrySet()) {
+ String propertyName = e.getKey();
+ Object value = e.getValue();
+ PropertyUtils.setProperty(result, propertyName, value);
+ }
+ } catch (IllegalAccessException eee) {
+ throw new IllegalArgumentException(
+ "Can't put properties on new Object", eee);
+ } catch (InvocationTargetException eee) {
+ throw new IllegalArgumentException(
+ "Can't put properties on new Object", eee);
+ } catch (NoSuchMethodException eee) {
+ throw new IllegalArgumentException(
+ "Can't put properties on new Object", eee);
+ }
+ result.postCreate();
+
+ // on fait un save maintenant, car puisqu'on a creer l'entity au
+ // travers du DAO, on s'attend a l'avoir a disposition tout de
+ // suite pour les requetes sans avoir a faire un update dessus
+ getSession().save(result);
+ getContext().getFiresSupport().warnOnCreateEntity(result);
+ return result;
+ }
+
+ @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));
+ }
+
+ @Override
+ public List<E> findAll() throws TopiaException {
+ try {
+ Criteria criteria = createCriteria(FlushMode.AUTO);
+ List<E> result = (List<E>) criteria.list();
+ result = getContext().getFiresSupport().fireEntitiesLoad(context,
+ result);
+ return result;
+ } catch (HibernateException eee) {
+ throw new TopiaException(eee);
+ }
+ }
+
+ @Override
+ public List<String> findAllIds() throws TopiaException {
+ List<String> find = context.find("select src.topiaId from " + entityClass.getSimpleName() + "Impl src");
+ return find;
+ }
+
+
+
+ @Override
+ public List<E> findAllWithOrder(String... propertyNames)
+ throws TopiaException {
+ try {
+ Criteria criteria = createCriteria(FlushMode.AUTO);
+ for (String propertyName : propertyNames) {
+ criteria.addOrder(Order.asc(propertyName));
+ }
+ List<E> result = (List<E>) criteria.list();
+ result = getContext().getFiresSupport().fireEntitiesLoad(context,
+ result);
+ return result;
+ } catch (HibernateException eee) {
+ throw new TopiaException(eee);
+ }
+ }
+
+ /**
+ * Count number of existing entities using {@link org.nuiton.topia.framework.TopiaQuery#executeCount(org.nuiton.topia.TopiaContext) }
+ * FIXME-FD20091224 change type to int like in 2.2.2 version
+ *
+ * @return a long for the number of entities in database
+ */
+ @Override
+ public long size() throws TopiaException {
+ //int result = findAll().size();
+// List result = this.getContext().find("SELECT count(*) FROM " + getEntityClass().getName() + "Impl");
+// return (Long)result.get(0);
+ return new TopiaQuery(getEntityClass()).executeCount(context);
+ }
+
+ @Override
+ public E findByProperties(Map<String, Object> properties)
+ throws TopiaException {
+ return query(Restrictions.allEq(properties));
+ }
+
+ @Override
+ public List<E> findAllByProperties(Map<String, Object> properties)
+ throws TopiaException {
+ return queryAll(Restrictions.allEq(properties));
+ }
+
+ private List<E> queryAll(Criterion criterion) throws TopiaException {
+ try {
+ Criteria criteria = createCriteria(FlushMode.AUTO);
+ criteria.add(criterion);
+ List<E> result = (List<E>) criteria.list();
+ result = getContext().getFiresSupport().fireEntitiesLoad(context,
+ result);
+ return result;
+ } catch (HibernateException eee) {
+ throw new TopiaException(eee);
+ }
+ }
+
+ private E query(Criterion criterion) throws TopiaException {
+ try {
+ Criteria criteria = createCriteria(FlushMode.AUTO);
+ criteria.add(criterion);
+ criteria.setMaxResults(1);
+ List<E> result = (List<E>) criteria.list();
+ int sizeBefore = (result != null ? result.size() : 0);
+ result = getContext().getFiresSupport().fireEntitiesLoad(context,
+ result);
+ int sizeAfter = (result != null ? result.size() : 0);
+ if (sizeAfter < sizeBefore) {
+ if (log.isDebugEnabled()) {
+ log.debug((sizeBefore - sizeAfter)
+ + " element(s) removed. Filter entity: "
+ + entityClass.getName() + " - criterion: "
+ + criterion);
+ }
+ }
+ if (result != null && result.size() > 0) {
+ E elem = result.get(0);
+ return elem;
+ }
+ return null;
+ } catch (HibernateException eee) {
+ throw new TopiaException(eee);
+ }
+ }
+
+ //FIXME : Commenté car impossible de trouver le bon Criterion
+ // ATTENTION ancienne methode du TopiaDAOAbstract deja presente dans le fichier
+
+ // /* (non-Javadoc)
+ // * @see org.nuiton.topia.persistence.TopiaDAO#findContainsProperties(java.util.Map)
+ // */
+ // public E findContainsProperties(Map<String, Collection> properties) throws TopiaException {
+ // try {
+ // Criteria criteria = createCriteria(FlushMode.AUTO);
+ // for (Entry<String, Collection> entry : properties.entrySet()) {
+ // for (Object value : entry.getValue()) {
+ // criteria.add(Restrictions.eq(entry.getKey(), value));
+ // }
+ // }
+ // criteria.setMaxResults(1);
+ // List<E> results = (List<E>)criteria.list();
+ // if (results.size() > 0) {
+ // return (E)results.get(0);
+ // } else {
+ // return null;
+ // }
+ // } catch (HibernateException eee) {
+ // throw new TopiaException(eee);
+ // }
+ // }
+ //
+ // /* (non-Javadoc)
+ // * @see org.nuiton.topia.persistence.TopiaDAO#findAllContainsProperties(java.util.Map)
+ // */
+ // public List<E> findAllContainsProperties(Map<String, Collection> properties) throws TopiaException {
+ // try {
+ // Criteria criteria = createCriteria(FlushMode.AUTO);
+ // for (Entry<String, Collection> entry : properties.entrySet()) {
+ // for (Object value : entry.getValue()) {
+ // criteria.add(Restrictions.eq(entry.getKey(), value));
+ // }
+ // }
+ // List<E> result = (List<E>)criteria.list();
+ // return result;
+ // } catch (HibernateException eee) {
+ // throw new TopiaException(eee);
+ // }
+ // }
+
+ /**
+ * Renvoie un Criteria créé avec l'entityClass
+ * @param mode le FlushMode du Criteria
+ * @return le Criteria nouvellement créé
+ * @throws org.nuiton.topia.TopiaException if any pb
+ */
+ private Criteria createCriteria(FlushMode mode) throws TopiaException {
+ Criteria criteria = getSession().createCriteria(entityClass);
+ criteria.setFlushMode(mode);
+ return criteria;
+ }
+
+ /**
+ * Renvoie la Session contenue dans le contexte
+ * @return hibernate session
+ * @throws org.nuiton.topia.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
Property changes on: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOLegacy.java
___________________________________________________________________
Added: svn:keywords
+ Author Date Id Revision HeadURL
Modified: trunk/topia-persistence/src/test/xmi/topiatest.properties
===================================================================
--- trunk/topia-persistence/src/test/xmi/topiatest.properties 2010-05-28 13:15:53 UTC (rev 1983)
+++ trunk/topia-persistence/src/test/xmi/topiatest.properties 2010-05-30 14:56:55 UTC (rev 1984)
@@ -1,5 +1,6 @@
model.tagvalue.i18n=topia.test.common.
model.tagvalue.generateOperatorForDAOHelper=true
+model.tagvalue.useLegacyDAO=true
#org.nuiton.topiatest.Company.class.tagvalue.naturalIdMutable=false
#org.nuiton.topiatest.Company.attribute.siret.tagvalue.naturalId=true
1
0
r1983 - trunk/topia-persistence/src/main/java/org/nuiton/topia/framework
by fdesbois@users.nuiton.org 28 May '10
by fdesbois@users.nuiton.org 28 May '10
28 May '10
Author: fdesbois
Date: 2010-05-28 15:15:53 +0200 (Fri, 28 May 2010)
New Revision: 1983
Url: http://nuiton.org/repositories/revision/topia/1983
Log:
Change log level for loading properties
Modified:
trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java
Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java
===================================================================
--- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2010-05-28 11:37:47 UTC (rev 1982)
+++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2010-05-28 13:15:53 UTC (rev 1983)
@@ -1400,8 +1400,8 @@
throws TopiaException {
try {
Object res = PropertyUtils.getProperty(entity, property);
- if (log.isTraceEnabled()) {
- log.trace("load property '" + property + "' for '" +
+ if (log.isDebugEnabled()) {
+ log.debug("load property '" + property + "' for '" +
entity.getClass().getSimpleName() + "'");
}
if (res != null && Collection.class.isAssignableFrom(res.getClass())) {
1
0
r1982 - trunk/topia-persistence/src/main/java/org/nuiton/topia/generator
by tchemit@users.nuiton.org 28 May '10
by tchemit@users.nuiton.org 28 May '10
28 May '10
Author: tchemit
Date: 2010-05-28 13:37:47 +0200 (Fri, 28 May 2010)
New Revision: 1982
Url: http://nuiton.org/repositories/revision/topia/1982
Log:
add registerTopiaBinder which returns actually inserted binder
Modified:
trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/BinderHelperTransformer.java
Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/BinderHelperTransformer.java
===================================================================
--- trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/BinderHelperTransformer.java 2010-05-28 08:35:24 UTC (rev 1981)
+++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/BinderHelperTransformer.java 2010-05-28 11:37:47 UTC (rev 1982)
@@ -119,6 +119,21 @@
);
op = addOperation(resultClass,
+ "registerTopiaBinder",
+ "<E extends TopiaEntity> TopiaEntityBinder<E>",
+ ObjectModelModifier.PUBLIC,
+ ObjectModelModifier.STATIC);
+ addParameter(op, "Class<E>", "entityClass");
+ addParameter(op, "BinderBuilder", "builder");
+ addParameter(op, "String", "contextName");
+ setOperationBody(op, ""
+/*{
+ BinderProvider.registerBinder(builder, TopiaEntityBinder.class, contextName);
+ return getTopiaBinder(entityClass, contextName);
+ }*/
+ );
+
+ op = addOperation(resultClass,
"copy",
"<E extends TopiaEntity> void",
ObjectModelModifier.PUBLIC,
1
0
r1981 - in trunk/topia-persistence/src: main/java/org/nuiton/topia/generator test/xmi
by fdesbois@users.nuiton.org 28 May '10
by fdesbois@users.nuiton.org 28 May '10
28 May '10
Author: fdesbois
Date: 2010-05-28 10:35:24 +0200 (Fri, 28 May 2010)
New Revision: 1981
Url: http://nuiton.org/repositories/revision/topia/1981
Log:
Add memory debug in ServiceTransformer
Modified:
trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/ServiceTransformer.java
trunk/topia-persistence/src/test/xmi/topiatest.zargo
Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/ServiceTransformer.java
===================================================================
--- trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/ServiceTransformer.java 2010-05-27 16:44:07 UTC (rev 1980)
+++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/ServiceTransformer.java 2010-05-28 08:35:24 UTC (rev 1981)
@@ -300,17 +300,6 @@
ObjectModelModifier.PROTECTED);
addException(beginTransaction, TopiaException.class);
-// ObjectModelOperation treateError1 =
-// addOperation(serviceAbstract, OP_NAME_TREATE_ERROR, "void",
-// ObjectModelModifier.ABSTRACT,
-// ObjectModelModifier.PROTECTED);
-// addParameter(treateError1, Exception.class, "eee");
-// addParameter(treateError1, String.class, "message");
-// addParameter(treateError1, "Object...", "args");
-// if (exceptionName != null) {
-// addException(treateError1, exceptionName);
-// }
-
ObjectModelOperation treateError1 =
addOperation(serviceAbstract, OP_NAME_TREATE_ERROR, "void",
ObjectModelModifier.ABSTRACT,
@@ -492,11 +481,6 @@
String abstName = abstOp.getName();
String abstParams =
GeneratorUtil.getOperationParametersListName(abstOp);
-// String separator = "";
-// for (ObjectModelParameter param : abstOp.getParameters()) {
-// abstParams += separator + param.getName();
-// separator = ", ";
-// }
// Abstract operation return managment
String abstReturnType = "";
@@ -565,9 +549,11 @@
<%=abstReturnType%><%=abstName%>(<%=abstParams%>);
if (log.isDebugEnabled()) {
long stopTime = System.currentTimeMillis();
+ Runtime runtime = Runtime.getRuntime();
+ long mem = (runtime.totalMemory() - runtime.freeMemory()) / 1048576;
log.debug("<%=first%>:[ end <%=implName%> ] Time = " +
DurationFormatUtils.formatDurationHMS(
- stopTime - startTime));
+ stopTime - startTime) + " _ Memory = " + mem + " Mo");
}
<%=abstReturn%> }*/);
Modified: trunk/topia-persistence/src/test/xmi/topiatest.zargo
===================================================================
(Binary files differ)
1
0
r1980 - in trunk/topia-persistence: . src/main/java/org/nuiton/topia/generator src/test/xmi
by fdesbois@users.nuiton.org 27 May '10
by fdesbois@users.nuiton.org 27 May '10
27 May '10
Author: fdesbois
Date: 2010-05-27 18:44:07 +0200 (Thu, 27 May 2010)
New Revision: 1980
Url: http://nuiton.org/repositories/revision/topia/1980
Log:
- Add EntityTransformer for tests (exclude old transformers)
- Evo #643 : manage getter in EntityTransformer (with 'is' prefix for boolean attributes)
Modified:
trunk/topia-persistence/pom.xml
trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityTransformer.java
trunk/topia-persistence/src/test/xmi/topiatest.zargo
Modified: trunk/topia-persistence/pom.xml
===================================================================
--- trunk/topia-persistence/pom.xml 2010-05-26 14:17:56 UTC (rev 1979)
+++ trunk/topia-persistence/pom.xml 2010-05-27 16:44:07 UTC (rev 1980)
@@ -187,11 +187,18 @@
<phase>generate-test-sources</phase>
<configuration>
<testPhase>true</testPhase>
- <templates>org.nuiton.topia.generator.TopiaMetaTransformer,
+ <templates>
+ org.nuiton.topia.generator.TopiaMetaTransformer,
+ org.nuiton.topia.generator.EntityTransformer,
org.nuiton.topia.generator.InterfaceTransformer,
org.nuiton.topia.generator.BeanTransformer,
org.nuiton.topia.generator.EntityDTOTransformer
</templates>
+ <excludeTemplates>
+ <excludeTemplate>org.nuiton.topia.generator.EntityAbstractTransformer</excludeTemplate>
+ <excludeTemplate>org.nuiton.topia.generator.EntityImplTransformer</excludeTemplate>
+ <excludeTemplate>org.nuiton.topia.generator.EntityInterfaceTransformer</excludeTemplate>
+ </excludeTemplates>
<fullPackagePath>org.nuiton.topia</fullPackagePath>
<defaultPackage>org.nuiton.topia</defaultPackage>
</configuration>
Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityTransformer.java
===================================================================
--- trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityTransformer.java 2010-05-26 14:17:56 UTC (rev 1979)
+++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityTransformer.java 2010-05-27 16:44:07 UTC (rev 1980)
@@ -81,16 +81,28 @@
* <li>There is no abstract method</li>
* <li>There is no already defined such class in class-path</li>
* </ul>
- *
+ *
* @author tchemit <chemit(a)codelutin.com>
* @since 2.4
* @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.topia.generator.EntityTransformer"
*/
public class EntityTransformer extends ObjectModelTransformerToJava {
-
+
/** Logger */
private static final Log log = LogFactory.getLog(EntityTransformer.class);
+ protected ObjectModelInterface outputInterface;
+
+ protected ObjectModelClass outputAbstract;
+
+ protected ObjectModelClass outputImpl;
+
+ protected void clean() {
+ outputInterface = null;
+ outputAbstract = null;
+ outputImpl = null;
+ }
+
@Override
public void transformFromClass(ObjectModelClass input) {
@@ -99,12 +111,12 @@
// not an entity, skip class.
return;
}
-
+
if (log.isDebugEnabled()) {
log.debug("for entity : "+input.getQualifiedName());
log.info("Will use classLoader "+ getClassLoader());
}
-
+
// fix once for all the constant prefix to use
String prefix = getConstantPrefix(input, "");
@@ -126,23 +138,30 @@
Collection<ObjectModelOperation> operations = input.getOperations();
- ObjectModelClassifier output;
+ // Create classifiers : Interface, Abstract
+ outputInterface = createInterface(clazzName, packageName);
+ outputAbstract = createAbstractClass(clazzName + "Abstract", packageName);
- // generate interface
+ generateInterface(input, outputInterface, attributes, operations);
+ generateAbstract(input, outputAbstract, attributes, operations);
- {
- output = createInterface(clazzName, packageName);
- generateInterface(input, (ObjectModelInterface) output, attributes, operations);
- }
-
- // generate abstract
+// ObjectModelClassifier output;
+//
+// // generate interface
+//
+// {
+// outputInterface = createInterface(clazzName, packageName);
+// generateInterface(input, outputInterface, attributes, operations);
+// }
+//
+// // generate abstract
+//
+// {
+// outputAbstract = createAbstractClass( clazzName + "Abstract", packageName);
+// generateAbstract(input, outputAbstract, attributes, operations);
+//
+// }
- {
- output = createAbstractClass( clazzName + "Abstract", packageName);
- generateAbstract(input, (ObjectModelClass) output, attributes, operations);
-
- }
-
boolean generateImpl = isGenerateImpl(input, operations);
if (generateImpl) {
@@ -153,14 +172,17 @@
}
if (isAbstract(input)) {
- output = createAbstractClass(implName, packageName);
+ outputImpl = createAbstractClass(implName, packageName);
} else {
- output = createClass(implName, packageName);
+ outputImpl = createClass(implName, packageName);
}
// generate impl
- generateImpl(input, (ObjectModelClass) output);
- }
+ generateImpl(input, outputImpl);
+ }
+
+ // Clean data output after transformation
+ clean();
}
protected boolean isGenerateImpl(ObjectModelClass input,
@@ -178,7 +200,7 @@
}
return false;
}
-
+
// On ne génère pas le impl si l'entité a des opérations qui ne sont
// pas seulement pour le DAO
if (!operations.isEmpty()) {
@@ -215,7 +237,7 @@
if (log.isDebugEnabled()) {
log.debug("Add constants from dependency : " + constants);
}
-
+
if (TopiaGeneratorUtil.hasDocumentation(input)) {
setDocumentation(output,input.getDocumentation());
}
@@ -262,7 +284,7 @@
}
//Méthodes d'accès aux attributs d'une classe d'associations
-
+
if (input instanceof ObjectModelAssociationClass) {
ObjectModelAssociationClass assoc =
(ObjectModelAssociationClass) input;
@@ -593,13 +615,11 @@
// getXXX
- op = addOperation(output,
- "get" + StringUtils.capitalize(attrName),
- attrType,
- ObjectModelModifier.PACKAGE);
- if (TopiaGeneratorUtil.hasDocumentation(attr)) {
- setDocumentation(op, attr.getDocumentation());
- }
+ // Add getter for simple property.
+ // Only one call for this method, no need for abstract generation
+ // TODO-fdesbois-2010-05-27 : manage all attribute cases in this method
+ addSimpleGetterOperation(attr, null);
+
} else {
String collectionInterface =
TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr);
@@ -738,7 +758,7 @@
attr2 = addParameter(op, assocClassFQN, GeneratorUtil.toLowerCaseFirstLetter(assocClassName));
setDocumentation(attr2, "La valeur de l'attribut " + assocClassName + " à positionner");
- // getXXX
+ // getXXX : getter interface for association attribute with unique multiplicity
addOperation(output,
"get" + StringUtils.capitalize(assocAttrName),
@@ -868,7 +888,7 @@
addException(op2, exception);
}
}
-
+
private void generateInterfaceAssociationAccessors(ObjectModelInterface output,
String attrName,
String attrType) {
@@ -931,7 +951,7 @@
}*/
);
- // getXXX
+ // getXXX : getter for association attribute with unique multiplicity
op = addOperation(result,
"get" + StringUtils.capitalize(assocAttrName),
@@ -960,20 +980,11 @@
);
- // getXXX
+ // getXXX : getter for simple attribute (as bean convention)
- op = addOperation(result,
- "get" + StringUtils.capitalize(attrName),
- attrType,
- ObjectModelModifier.PUBLIC);
- setOperationBody(op, ""
-/*{
- fireOnPreRead(<%=getConstantName(attrName)%>, <%=attrName%>);
- <%=attrType%> result = this.<%=attrName%>;
- fireOnPostRead(<%=getConstantName(attrName)%>, <%=attrName%>);
- return result;
-}*/
- );
+ // Getter is already set in abstract when added in interface
+ // see {@link #addSimpleGetterOperation(ObjectModelAttribute, String)}
+
}
} else { //NMultiplicity
String collectionInterface = TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr);
@@ -1340,7 +1351,7 @@
if (!attr.hasAssociationClass()) {
- // getXXX
+ // getXXX : getter for collection entity attribute (N multiplicity)
op = addOperation(result,
"get" + StringUtils.capitalize(attrName),
@@ -1385,7 +1396,8 @@
String assocClassFQN = attr.getAssociationClass().getQualifiedName();
// String assocClassFQN = TopiaGeneratorUtil.getSimpleName(attr.getAssociationClass().getQualifiedName());
- // getXXX
+ // getXXX : getter for association attribute with no parameter
+ // (return a collection)
op = addOperation(result,
"get" + StringUtils.capitalize(assocAttrName),
@@ -1397,7 +1409,8 @@
}*/
);
- // getXXX
+ // getXXX : getter for association attribute with one parameter
+ // (return a single element)
op = addOperation(result,
"get" + StringUtils.capitalize(assocAttrName),
@@ -1451,6 +1464,63 @@
}
}
+ /**
+ * Add getter for simple property (neither association nor multiple).
+ * Will add two different operations for boolean case ('is' method and
+ * 'get' method). This method add the operation in both {@code
+ * outputAbstract} and {@code outputInterface}.
+ *
+ * @param attribute ObjectModelAttribute for getter operation
+ * @param operationPrefix Operation prefix : 'get' by default, if prefix
+ * is null
+ */
+ protected void addSimpleGetterOperation(ObjectModelAttribute attribute,
+ String operationPrefix) {
+
+ String attrName = attribute.getName();
+ String attrType = attribute.getType();
+
+ if (operationPrefix == null) {
+ operationPrefix = TopiaGeneratorUtil.OPERATION_GETTER_DEFAULT_PREFIX;
+ }
+
+ if (log.isDebugEnabled()) {
+ log.debug("Add getter operation for " + attrName +
+ " prefix = " + operationPrefix);
+ }
+
+ // CONTRACT in Interface
+ ObjectModelOperation contract = addOperation(outputInterface,
+ operationPrefix + StringUtils.capitalize(attrName),
+ attrType,
+ ObjectModelModifier.PACKAGE);
+
+ if (TopiaGeneratorUtil.hasDocumentation(attribute)) {
+ setDocumentation(contract, attribute.getDocumentation());
+ }
+
+ // IMPLEMENTATION in Abstract
+ ObjectModelOperation impl = addOperation(outputAbstract,
+ contract.getName(),
+ contract.getReturnType(),
+ ObjectModelModifier.PUBLIC);
+ setOperationBody(impl, ""
+/*{
+ fireOnPreRead(<%=getConstantName(attrName)%>, <%=attrName%>);
+ <%=attrType%> result = this.<%=attrName%>;
+ fireOnPostRead(<%=getConstantName(attrName)%>, <%=attrName%>);
+ return result;
+}*/
+ );
+
+ // Generate 'is' getter for boolean attributes
+ if (attrType.toLowerCase().contains("boolean") &&
+ !operationPrefix.equals(TopiaGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX)) {
+ addSimpleGetterOperation(attribute,
+ TopiaGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX);
+ }
+ }
+
protected void generateToStringMethod(ObjectModelClass output,
ObjectModelClass clazz) {
if (log.isDebugEnabled()) {
Modified: trunk/topia-persistence/src/test/xmi/topiatest.zargo
===================================================================
(Binary files differ)
1
0
r1979 - in trunk/topia-persistence/src: main/java/org/nuiton/topia/framework main/java/org/nuiton/topia/persistence test/java/org/nuiton/topia/framework
by fdesbois@users.nuiton.org 26 May '10
by fdesbois@users.nuiton.org 26 May '10
26 May '10
Author: fdesbois
Date: 2010-05-26 16:17:56 +0200 (Wed, 26 May 2010)
New Revision: 1979
Url: http://nuiton.org/repositories/revision/topia/1979
Log:
Change some method names in TopiaQuery
Modified:
trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java
trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java
trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaQueryTest.java
Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java
===================================================================
--- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2010-05-26 11:49:20 UTC (rev 1978)
+++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2010-05-26 14:17:56 UTC (rev 1979)
@@ -227,8 +227,8 @@
* @version $Revision$
* @since 2.3.0
* <p/>
- * Mise a jour: $Date$
- * par : $Author$
+ * Mise a jour: $Date$ par
+ * : $Author$
*/
public class TopiaQuery {
@@ -471,6 +471,7 @@
result.append(selectStatement).append(select);
// Set default select if there is more than one table in from
// part and main alias is defined
+ // Note : maybe a problem with using new {@link #addFrom(String, String) with other than ',' separator
} else if (StringUtils.contains(from.toString(), ',') &&
StringUtils.isNotEmpty(mainAlias)) {
result.append(selectStatement).append(mainAlias);
@@ -518,66 +519,6 @@
return this;
}
- /**
- * Method used to add a subquery in an existing query. The params will be
- * automatically checked and copied from the subquery to the current one.
- * This method is used to inject {@code subquery} in WHERE part of the
- * query. The {@code queryPart} is the element in the query to bind with the
- * {@code subquery}. The ? character is used to inject the subquery into the
- * {@code queryPart}. Ex :
- * <pre>
- * // Add a SUB_ELMT = (subquery) into the query
- * query.addSubQuery("SUB_ELMT = (?)", subquery, false);
- * </pre>
- *
- * @param queryPart part of the query where subquery need to be injected
- * @param subquery existing topiaQuery as subquery
- * @return the TopiaQuery
- * @see TopiaQuery#getValueName(String)
- * @since 2.4
- */
- public TopiaQuery addSubQuery(String queryPart,
- TopiaQuery subquery) {
-
- List<Object> subqueryParams = subquery.getParams();
- String subqueryString = subquery.fullQuery();
-
- // If no params is still defined, use those from subquery.
- if (CollectionUtils.isEmpty(params)) {
- addParams(subqueryParams);
- } else {
- for (int i = 0; i < subqueryParams.size(); i += 2) {
-
- String paramName = (String) subqueryParams.get(i);
- Object paramValue = subqueryParams.get(i + 1);
-
- // Check existence of paramName
- int index = params.indexOf(paramName);
-
- // If already defined
- if (index != -1) {
- Object existingValue = params.get(index + 1);
-
- // Only change paramName in not equals case
- if (!ObjectUtils.equals(existingValue, paramValue)) {
- String newParamName = getValueName(paramName);
- // Replace old paramName in subquery
- subqueryString =
- subqueryString.replace(":" + paramName,
- ":" + newParamName);
-
- // Add the param to the current query
- addParam(newParamName, paramValue);
- }
- }
- }
- }
-
- // Replace ? injection by the subquery
- String result = queryPart.replace("?", subqueryString);
- return add(result);
- }
-
public List<Object> getParams() {
if (params == null) {
params = new ArrayList<Object>();
@@ -624,13 +565,24 @@
}
/**
+ * @param where
+ * @return TopiaQuery
+ * @deprecated since 2.4, use {@link #addWhere(String)} instead
+ */
+ @Deprecated
+ public TopiaQuery add(String where) {
+ return addWhere(where);
+ }
+
+ /**
* Add a where element to the Query. Could be anything. Parentheses are
* added automatically (even if there are not needed).
*
* @param where element to add
* @return the TopiaQuery
+ * @since 2.4
*/
- public TopiaQuery add(String where) {
+ public TopiaQuery addWhere(String where) {
if (StringUtils.isEmpty(where)) {
return this;
}
@@ -652,20 +604,35 @@
}
/**
+ * @param paramName
+ * @param constraint
+ * @param paramValue
+ * @return TopiaQuery
+ * @deprecated since 2.4, use {@link #addWhere(String, Op, Object)} instead
+ */
+ @Deprecated
+ public TopiaQuery add(String paramName, Op constraint, Object paramValue) {
+ return addWhere(paramName, constraint, paramValue);
+ }
+
+ /**
* Add an element to the query. The parameter will be automatically added.
- * The constraint is needed to determine what type of operation it is. Ex :
- * add("boat", Op.EQ, boat) means -> boat = :boat. If the paramValue is
- * Null, the paramName will be added to the query with the constraint null
- * (IS NULL).
+ * The {@code operator} is needed to determine what type of operation it is.
+ * Ex : add("boat", Op.EQ, boat) means -> boat = :boat. Also if the paramValue
+ * is Null, the paramName will be added to the query with the constraint
+ * null (IS NULL).
+ * <p/>
+ * TODO-fdesbois-2010-05-26 : maybe manage more than one paramValue with Object... Depends on operator
*
* @param paramName the name of the parameter in the query (attribute of
* the entity)
- * @param constraint the operation concerned
+ * @param operator the operation concerned
* @param paramValue the value of the parameter (an other entity, a String,
* ...)
* @return the TopiaQuery
+ * @since 2.4
*/
- public TopiaQuery add(String paramName, Op constraint, Object paramValue) {
+ public TopiaQuery addWhere(String paramName, Op operator, Object paramValue) {
StringBuilder result = new StringBuilder(paramName).append(' ');
if (log.isDebugEnabled()) {
log.debug("paramValue = " + paramValue);
@@ -674,34 +641,12 @@
result.append(Op.NULL);
} else {
String valueName = getValueName(paramName);
- result.append(constraint).append(" :").append(valueName);
+ result.append(operator).append(" :").append(valueName);
addParam(valueName, paramValue);
}
parentheses = false;
- return add(result.toString());
- }
+ return addWhere(result.toString());
- /**
- * Add an element to the query. The nullity is tested or a constraint is
- * added for that element. Ex : addNullOr("begin", Op.GT, new Date()) means
- * begin IS NULL OR begin > :begin (where :begin = new Date()).
- *
- * @param paramName the name of the parameter in the query (attribute of
- * the entity)
- * @param constraint the operation concerned by the or
- * @param paramValue the value of the parameter (an other entity, a String,
- * ...)
- * @return the TopiaQuery
- */
- public TopiaQuery addNullOr(String paramName, Op constraint,
- Object paramValue) {
- String valueName = getValueName(paramName);
- StringBuilder result =
- new StringBuilder(paramName).append(' ').append(Op.NULL).
- append(" OR ").append(paramName).append(constraint).
- append(" :").append(valueName);
- addParam(valueName, paramValue);
- return add(result.toString());
}
protected String getValueName(String paramName) {
@@ -718,15 +663,14 @@
}
/**
- * Add an element to the query with the constraint Not null.
- *
- * @param paramName name of the parameter in the query
- * @return the TopiaQuery
+ * @param paramName
+ * @param paramValue
+ * @return TopiaQuery
+ * @since 2.3.1
+ * @deprecated since 2.4, use {@link #addEquals(String, Object...)} instead
*/
- public TopiaQuery addNotNull(String paramName) {
- StringBuilder result =
- new StringBuilder(paramName).append(' ').append(Op.NOT_NULL);
- return add(result.toString());
+ public TopiaQuery add(String paramName, Object... paramValue) {
+ return addEquals(paramName, paramValue);
}
/**
@@ -736,16 +680,19 @@
* statement IN will be used. You can also have a null value in the {@code
* paramValue} list (except if it's the only one value, it's ambiguous).
* Note : this method do nothing if the {@code paramValue} is not defined.
- * If you want to add the constraint null on the {@code paramName} use
- * {@link #add(String, Op, Object)} as : add("param", Op.EQ, null);
+ * You can also set {@code paramValue} to null if you want the {@code
+ * paramName} to be null in the query.
*
* @param paramName name of the parameter in the query
* @param paramValue values of the parameter
* @return the TopiaQuery
- * @see TopiaQuery#add(String, Op, Object)
- * @since 2.3.1
+ * @see TopiaQuery#addWhere(String, Op, Object)
+ * @since 2.4
*/
- public TopiaQuery add(String paramName, Object... paramValue) {
+ public TopiaQuery addEquals(String paramName, Object... paramValue) {
+ if (paramValue == null) {
+ return addWhere(paramName, Op.EQ, null);
+ }
int length = paramValue.length;
// Do nothing if there is no value defined
if (length == 0) {
@@ -756,7 +703,7 @@
if (log.isDebugEnabled()) {
log.debug("Only one value " + Arrays.toString(paramValue));
}
- return add(paramName, Op.EQ, paramValue[0]);
+ return addWhere(paramName, Op.EQ, paramValue[0]);
}
// Multiple values is defined
StringBuilder values = new StringBuilder();
@@ -779,7 +726,7 @@
}
// Create buffer for IN statement with values
StringBuilder buffer = new StringBuilder();
- buffer.append(paramName).append(" IN(").append(values).append(")");
+ buffer.append(paramName).append(" IN (").append(values).append(")");
// Add the OR statement for null value if needed
if (addNull) {
buffer.append(" OR ").
@@ -788,10 +735,20 @@
// no parentheses needed in this case (no OR statement)
parentheses = false;
}
- return add(buffer.toString());
+ return addWhere(buffer.toString());
}
/**
+ * @param properties
+ * @return TopiaQuery
+ * @deprecated since 2.4 use {@link #addEquals(Map)}
+ */
+ @Deprecated
+ public TopiaQuery add(Map<String, Object> properties) {
+ return addEquals(properties);
+ }
+
+ /**
* Add a map of properties to the where clause of the query. Each property
* will be added to the query with Op.EQ operation, the key in the map is
* the property name, and the value is the value of the parameter in the
@@ -799,15 +756,51 @@
*
* @param properties to add to the query
* @return the TopiaQuery
+ * @since 2.4
*/
- public TopiaQuery add(Map<String, Object> properties) {
+ public TopiaQuery addEquals(Map<String, Object> properties) {
for (String key : properties.keySet()) {
- add(key, properties.get(key));
+ addEquals(key, properties.get(key));
}
return this;
}
/**
+ * Add an element to the query with the constraint Not null.
+ *
+ * @param paramName name of the parameter in the query
+ * @return the TopiaQuery
+ */
+ public TopiaQuery addNotNull(String paramName) {
+ StringBuilder result =
+ new StringBuilder(paramName).append(' ').append(Op.NOT_NULL);
+ return addWhere(result.toString());
+ }
+
+ /**
+ * Add an element to the query. The nullity is tested or a constraint is
+ * added for that element. Ex : addNullOr("begin", Op.GT, new Date()) means
+ * begin IS NULL OR begin > :begin (where :begin = new Date()).
+ *
+ * @param paramName the name of the parameter in the query (attribute of
+ * the entity)
+ * @param constraint the operation concerned by the or
+ * @param paramValue the value of the parameter (an other entity, a String,
+ * ...)
+ * @return the TopiaQuery
+ */
+ public TopiaQuery addNullOr(String paramName, Op constraint,
+ Object paramValue) {
+ String valueName = getValueName(paramName);
+ StringBuilder result =
+ new StringBuilder(paramName).append(' ').append(Op.NULL).
+ append(" OR ").append(paramName).append(constraint).
+ append(" :").append(valueName);
+ addParam(valueName, paramValue);
+ return addWhere(result.toString());
+ }
+
+ /**
* Add link constraint between two properties. {@code propertyA} is in
* elements of {@code propertyB} which is a collection with same type than
* {@code propertyA}. (HQL : A IN elements (B))
@@ -819,16 +812,74 @@
*/
public TopiaQuery addInElements(String propertyA, String propertyB) {
StringBuilder builder = new StringBuilder(propertyA).
- append(" IN elements (").
- append(propertyB).
- append(')');
+ append(" IN elements(").append(propertyB).append(')');
parentheses = false;
- add(builder.toString());
+ addWhere(builder.toString());
parentheses = true;
return this;
}
/**
+ * Method used to add a subquery in an existing query. The params will be
+ * automatically checked and copied from the subquery to the current one.
+ * This method is used to inject {@code subquery} in WHERE part of the
+ * query. The {@code queryPart} is the element in the query to bind with the
+ * {@code subquery}. The ? character is used to inject the subquery into the
+ * {@code queryPart}. Ex :
+ * <pre>
+ * // Add a SUB_ELMT = (subquery) into the query
+ * query.addSubQuery("SUB_ELMT = (?)", subquery, false);
+ * </pre>
+ *
+ * @param queryPart part of the query where subquery need to be injected
+ * @param subquery existing topiaQuery as subquery
+ * @return the TopiaQuery
+ * @see TopiaQuery#getValueName(String)
+ * @since 2.4
+ */
+ public TopiaQuery addSubQuery(String queryPart,
+ TopiaQuery subquery) {
+
+ List<Object> subqueryParams = subquery.getParams();
+ String subqueryString = subquery.fullQuery();
+
+ // If no params is still defined, use those from subquery.
+ if (CollectionUtils.isEmpty(params)) {
+ addParams(subqueryParams);
+ } else {
+ for (int i = 0; i < subqueryParams.size(); i += 2) {
+
+ String paramName = (String) subqueryParams.get(i);
+ Object paramValue = subqueryParams.get(i + 1);
+
+ // Check existence of paramName
+ int index = params.indexOf(paramName);
+
+ // If already defined
+ if (index != -1) {
+ Object existingValue = params.get(index + 1);
+
+ // Only change paramName in not equals case
+ if (!ObjectUtils.equals(existingValue, paramValue)) {
+ String newParamName = getValueName(paramName);
+ // Replace old paramName in subquery
+ subqueryString =
+ subqueryString.replace(":" + paramName,
+ ":" + newParamName);
+
+ // Add the param to the current query
+ addParam(newParamName, paramValue);
+ }
+ }
+ }
+ }
+
+ // Replace ? injection by the subquery
+ String result = queryPart.replace("?", subqueryString);
+ return addWhere(result);
+ }
+
+ /**
* Add an element to the select in the query. Depends on the result wanted
* in execute methods. The main entity will be automatically added only if
* an alias is initialize from constructor. If you want only this select
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 2010-05-26 11:49:20 UTC (rev 1978)
+++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2010-05-26 14:17:56 UTC (rev 1979)
@@ -469,7 +469,7 @@
@Override
public E findByTopiaId(String id) throws TopiaException {
- TopiaQuery query = createQuery().add(TopiaEntity.TOPIA_ID, id);
+ TopiaQuery query = createQuery().addEquals(TopiaEntity.TOPIA_ID, id);
E result = findByQuery(query);
return result;
}
@@ -495,7 +495,7 @@
@Override
public E findByProperties(Map<String, Object> properties)
throws TopiaException {
- TopiaQuery query = createQuery().add(properties);
+ TopiaQuery query = createQuery().addEquals(properties);
E result = findByQuery(query);
return result;
}
@@ -540,7 +540,7 @@
@Override
public List<E> findAllByProperties(Map<String, Object> properties)
throws TopiaException {
- TopiaQuery query = createQuery().add(properties);
+ TopiaQuery query = createQuery().addEquals(properties);
List<E> results = findAllByQuery(query);
return results;
}
@@ -588,7 +588,7 @@
Object... others) throws TopiaException {
Map<String, Object> properties =
convertPropertiesArrayToMap(propertyName, propertyValue, others);
- TopiaQuery query = createQuery().add(properties);
+ TopiaQuery query = createQuery().addEquals(properties);
boolean result = existByQuery(query);
return result;
}
@@ -789,28 +789,6 @@
"l'obbjet " + e, eee);
}
}
-// } catch (IllegalAccessException eee) {
-// ok = false;
-// if (log.isWarnEnabled()) {
-// log.warn(
-// "Impossible d'acceder a la methode demandé pour l'obbjet "
-// + e, eee);
-// }
-// } catch (InvocationTargetException eee) {
-// ok = false;
-// if (log.isWarnEnabled()) {
-// log.warn(
-// "Impossible d'acceder a la methode demandé pour l'obbjet "
-// + e, eee);
-// }
-// } catch (NoSuchMethodException eee) {
-// ok = false;
-// if (log.isWarnEnabled()) {
-// log.warn(
-// "Impossible d'acceder a la methode demandé pour l'obbjet "
-// + e, eee);
-// }
-// }
if (ok) {
result.add(e);
}
Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaQueryTest.java
===================================================================
--- trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaQueryTest.java 2010-05-26 11:49:20 UTC (rev 1978)
+++ trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaQueryTest.java 2010-05-26 14:17:56 UTC (rev 1979)
@@ -42,16 +42,16 @@
// Test with one paramValue
String value = "topia";
TopiaQuery query = new TopiaQuery(QueriedEntity.class);
- query.add(QueriedEntity.TEST_ADD, value);
+ query.addEquals(QueriedEntity.TEST_ADD, value);
Assert.assertEquals(
"FROM org.nuiton.topiatest.QueriedEntity " +
"WHERE testAdd = :testAdd",
query.fullQuery());
// Test with null paramValue
- String nullValue = null;
+ //String nullValue = null;
query = new TopiaQuery(QueriedEntity.class);
- query.add(QueriedEntity.TEST_ADD, nullValue);
+ query.addEquals(QueriedEntity.TEST_ADD, null);
Assert.assertEquals(
"FROM org.nuiton.topiatest.QueriedEntity " +
"WHERE testAdd IS NULL",
@@ -60,18 +60,18 @@
// Test with two paramValues
String value2 = "eugene";
query = new TopiaQuery(QueriedEntity.class);
- query.add(QueriedEntity.TEST_ADD, value, value2);
+ query.addEquals(QueriedEntity.TEST_ADD, value, value2);
Assert.assertEquals(
"FROM org.nuiton.topiatest.QueriedEntity " +
- "WHERE testAdd IN(:testAdd1, :testAdd2)",
+ "WHERE testAdd IN (:testAdd1, :testAdd2)",
query.fullQuery());
// Test with two paramValues + null
query = new TopiaQuery(QueriedEntity.class);
- query.add(QueriedEntity.TEST_ADD, value, value2, null);
+ query.addEquals(QueriedEntity.TEST_ADD, value, value2, null);
Assert.assertEquals(
"FROM org.nuiton.topiatest.QueriedEntity " +
- "WHERE (testAdd IN(:testAdd1, :testAdd2) OR testAdd IS NULL)",
+ "WHERE (testAdd IN (:testAdd1, :testAdd2) OR testAdd IS NULL)",
query.fullQuery());
}
@@ -80,12 +80,12 @@
// Test 1 : Subquery with two params with different values
TopiaQuery query = new TopiaQuery(QueriedEntity.class).
- add(QueriedEntity.TEST_ADD, "value1");
+ addEquals(QueriedEntity.TEST_ADD, "value1");
// Exist 2 params
Assert.assertEquals(2, query.getParams().size());
TopiaQuery subquery = new TopiaQuery(QueriedEntity.class).
- add(QueriedEntity.TEST_ADD, "value2");
+ addEquals(QueriedEntity.TEST_ADD, "value2");
query.addSubQuery("Q1 = (?)", subquery);
log.debug(query);
@@ -96,12 +96,12 @@
// Test 2 : Subquery with two params with different values
// one of them is null
query = new TopiaQuery(QueriedEntity.class).
- add(QueriedEntity.TEST_ADD, TopiaQuery.Op.EQ, null);
+ addWhere(QueriedEntity.TEST_ADD, TopiaQuery.Op.EQ, null);
// Exist 0 param (null value)
Assert.assertEquals(0, query.getParams().size());
subquery = new TopiaQuery(QueriedEntity.class).
- add(QueriedEntity.TEST_ADD, "value1");
+ addEquals(QueriedEntity.TEST_ADD, "value1");
query.addSubQuery("Q1 = (?)", subquery);
log.debug(query);
@@ -111,12 +111,12 @@
// Test 3 : Subquery with two params with same value
query = new TopiaQuery(QueriedEntity.class, "Q1").
- add(QueriedEntity.TEST_ADD, "value1");
+ addEquals(QueriedEntity.TEST_ADD, "value1");
// Exist 2 params
Assert.assertEquals(2, query.getParams().size());
subquery = new TopiaQuery(QueriedEntity.class, "Q2").
- add(QueriedEntity.TEST_ADD, "value1");
+ addEquals(QueriedEntity.TEST_ADD, "value1");
query.addSubQuery("Q1 = (?)", subquery);
log.debug(query);
1
0
r1978 - trunk/topia-persistence/src/main/java/org/nuiton/topia/framework
by fdesbois@users.nuiton.org 26 May '10
by fdesbois@users.nuiton.org 26 May '10
26 May '10
Author: fdesbois
Date: 2010-05-26 13:49:20 +0200 (Wed, 26 May 2010)
New Revision: 1978
Url: http://nuiton.org/repositories/revision/topia/1978
Log:
change constant name for from separator
Modified:
trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java
Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java
===================================================================
--- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2010-05-26 11:39:47 UTC (rev 1977)
+++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2010-05-26 11:49:20 UTC (rev 1978)
@@ -234,8 +234,10 @@
private static final Log log = LogFactory.getLog(TopiaQuery.class);
- public static final String DEFAULT_FROM_SEPARATOR = ", ";
+ public static final String FROM_SEPARATOR_DEFAULT = ", ";
+ public static final String FROM_SEPARATOR_JOIN = " JOIN ";
+
/** Params for HQL query * */
protected List<Object> params;
@@ -420,13 +422,13 @@
*
* @param str the element to add
* @param separator The separator to use before adding the element (if null
- * the {@link #DEFAULT_FROM_SEPARATOR} will be used).
+ * the {@link #FROM_SEPARATOR_DEFAULT} will be used).
* @return the TopiaQuery
* @since 2.4
*/
public TopiaQuery addFrom(String str, String separator) {
if (separator == null) {
- separator = DEFAULT_FROM_SEPARATOR;
+ separator = FROM_SEPARATOR_DEFAULT;
}
from.append(separator).append(str);
return this;
1
0
r1977 - trunk/topia-persistence/src/main/java/org/nuiton/topia/framework
by fdesbois@users.nuiton.org 26 May '10
by fdesbois@users.nuiton.org 26 May '10
26 May '10
Author: fdesbois
Date: 2010-05-26 13:39:47 +0200 (Wed, 26 May 2010)
New Revision: 1977
Url: http://nuiton.org/repositories/revision/topia/1977
Log:
setFrom with String not necessary, use addFrom with separator argument for join support
Modified:
trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java
Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java
===================================================================
--- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2010-05-26 10:08:58 UTC (rev 1976)
+++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2010-05-26 11:39:47 UTC (rev 1977)
@@ -234,6 +234,8 @@
private static final Log log = LogFactory.getLog(TopiaQuery.class);
+ public static final String DEFAULT_FROM_SEPARATOR = ", ";
+
/** Params for HQL query * */
protected List<Object> params;
@@ -400,13 +402,33 @@
/**
* Add an element to the from in the query. Used to add some other data in
- * the query or for join.
+ * the query. The default separator used is the ", ".
*
* @param str the element to add
* @return the TopiaQuery
+ * @see #addFrom(String, String)
+ * @deprecated since 2.4 use {@link #addFrom(String, String)}
*/
+ @Deprecated
public TopiaQuery addFrom(String str) {
- from.append(", ").append(str);
+ return addFrom(str, null);
+ }
+
+ /**
+ * Add an element to the from in the query. Used to add some other data in
+ * the query or for join as specific {@code separator}.
+ *
+ * @param str the element to add
+ * @param separator The separator to use before adding the element (if null
+ * the {@link #DEFAULT_FROM_SEPARATOR} will be used).
+ * @return the TopiaQuery
+ * @since 2.4
+ */
+ public TopiaQuery addFrom(String str, String separator) {
+ if (separator == null) {
+ separator = DEFAULT_FROM_SEPARATOR;
+ }
+ from.append(separator).append(str);
return this;
}
@@ -417,7 +439,7 @@
* @return the TopiaQuery
*/
public TopiaQuery addFrom(Class<? extends TopiaEntity> entityClass) {
- return addFrom(entityClass.getName());
+ return addFrom(entityClass.getName(), null);
}
/**
@@ -429,7 +451,7 @@
*/
public TopiaQuery addFrom(Class<? extends TopiaEntity> entityClass,
String alias) {
- return addFrom(entityClass.getName() + " " + alias);
+ return addFrom(entityClass.getName() + " " + alias, null);
}
/**
@@ -497,10 +519,10 @@
/**
* Method used to add a subquery in an existing query. The params will be
* automatically checked and copied from the subquery to the current one.
- * This method is used to inject {@code subquery} in WHERE part of the query.
- * The {@code queryPart} is the element in the query to bind with the {@code
- * subquery}. The ? character is used to inject the subquery
- * into the {@code queryPart}. Ex :
+ * This method is used to inject {@code subquery} in WHERE part of the
+ * query. The {@code queryPart} is the element in the query to bind with the
+ * {@code subquery}. The ? character is used to inject the subquery into the
+ * {@code queryPart}. Ex :
* <pre>
* // Add a SUB_ELMT = (subquery) into the query
* query.addSubQuery("SUB_ELMT = (?)", subquery, false);
1
0
Author: fdesbois
Date: 2010-05-26 12:08:58 +0200 (Wed, 26 May 2010)
New Revision: 1976
Url: http://nuiton.org/repositories/revision/topia/1976
Log:
Modified:
trunk/topia-persistence/src/test/xmi/topiatest.zargo
Modified: trunk/topia-persistence/src/test/xmi/topiatest.zargo
===================================================================
(Binary files differ)
1
0