r1753 - trunk/topia-persistence/src/main/java/org/nuiton/topia/framework
Author: fdesbois Date: 2010-01-07 11:37:20 +0100 (Thu, 07 Jan 2010) New Revision: 1753 Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java Log: Add managment for select with more than one 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-01-07 03:29:06 UTC (rev 1752) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2010-01-07 10:37:20 UTC (rev 1753) @@ -262,62 +262,72 @@ } } - /** - * Constructor of TopiaQuery with entityClass initialization. - * - * @param entityClass Class for an entity Query - */ protected TopiaQuery(Class<E> entityClass) { parentheses = true; mainEntityClass = entityClass; this.from = " FROM " + mainEntityClass.getName(); } - /** - * Constructor of TopiaQuery from a DAO - * - * @param dao DAO linked to the entity to threat - */ protected TopiaQuery(TopiaDAO<E> dao) { this(dao.getEntityClass()); this.dao = dao; } - /** - * Constructor of TopiaQuery with String form initialization. - * - * @param entityClass - * @param alias of the main entity in the query - */ protected TopiaQuery(Class<E> entityClass, String alias) { this(entityClass); this.mainAlias = alias; this.from += " " + alias; } - /** - * Constructor of TopiaQuery from a DAO with an Alias - * - * @param dao DAO linked to the entity to threat - * @param alias of the main entity in the query - */ protected TopiaQuery(TopiaDAO<E> dao, String alias) { this(dao.getEntityClass(), alias); this.dao = dao; } + /** + * Create a TopiaQuery with entityClass initialization. + * + * @param <T> entity type extends TopiaEntity + * @param entityClass Class for an entity Query + * @return the new TopiaQuery + */ public static <T extends TopiaEntity> TopiaQuery<T> createQuery(Class<T> entityClass) { return new TopiaQuery<T>(entityClass); } + /** + * Create a TopiaQuery from a DAO. + * + * @param <T> entity type in the dao extends TopiaEntity + * @param dao DAO linked to the entity to threat + * @return the new TopiaQuery + */ public static <T extends TopiaEntity> TopiaQuery<T> createQuery(TopiaDAO<T> dao) { return new TopiaQuery<T>(dao); } + /** + * Create a TopiaQuery with entityClass initialization and its Alias. The main entity will be automatically added to the select part of + * the query if it is needed. + * + * @param <T> entity type in the dao extends TopiaEntity + * @param entityClass Class for an entity Query + * @param alias of the main entity in the query + * @return the new TopiaQuery + */ public static <T extends TopiaEntity> TopiaQuery<T> createQuery(Class<T> entityClass, String alias) { return new TopiaQuery<T>(entityClass, alias); } + /** + * Create a TopiaQuery from a DAO with an Alias. The main entity will be automatically added to the select part of + * the query if it is needed. + * + * @param <T> entity type in the dao extends TopiaEntity + * @param dao DAO linked to the entity to threat + * @param alias of the main entity in the query + * @return the new TopiaQuery + */ public static <T extends TopiaEntity> TopiaQuery<T> createQuery(TopiaDAO<T> dao, String alias) { return new TopiaQuery<T>(dao, alias); } @@ -554,18 +564,38 @@ } /** - * Set the select in the query. Depends on the result wanted in execute methods. + * 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 element, use {@link #setSelect(java.lang.String) } + * method instead. * * @param select element to add * @return the TopiaQuery */ + public TopiaQuery<E> addSelect(String select) { + // if select is the mainAlias, do nothing + if (mainAlias != null && select.equals(mainAlias)) { + return this; + } + // if select is not null, add the new element to the select + if (this.select != null) { + this.select += ", "; + // if mainAlias is not null, add it before adding the select in argument + } else if (mainAlias != null) { + this.select = mainAlias + ", "; + } else { + this.select = ""; + } + this.select += select; + return this; + } + + /** + * Set the select in the query. Depends on the result wanted in execute methods. + * + * @param select element to set + * @return the TopiaQuery + */ public TopiaQuery<E> setSelect(String select) { -// if (this.select == null) { -// this.select = "SELECT "; -// } else { -// this.select += ", "; -// } -// this.select += select; this.select = select; return this; } @@ -687,14 +717,29 @@ } List<E> results = new ArrayList<E>(); for (Object o : res) { - if (o != null && !mainEntityClass.isAssignableFrom(o.getClass())) { - throw new ClassCastException("Invalid cast for " + mainEntityClass.getName()); + // FIXME-FD20090106 How to change this try/catch by a better solution to test if the object is an array +// try { +// Object[] tab = (Object[])o; +// // If it's an array, we want only the first element wich is the entity wanted +// o = tab[0]; +// } catch (ClassCastException eee) { +// // no need to do something here, the cast is to test if the Object is an array +// } + if (o instanceof Object[]) { + // If it's an array, we want only the first element wich is the entity wanted + o = ((Object[])o)[0]; } + if (!mainEntityClass.isAssignableFrom(o.getClass())) { + throw new ClassCastException(o.getClass().getName() + " can't be cast to " + mainEntityClass.getName() + " o : " + o); + } E entity = (E)o; - if (!getPropertiesToLoad().isEmpty()) { - loadProperties(entity); + // Check distinct constraint for complex query where o is firstly an Object[] (potentially distinct results with existing entity to add) + if (! (distinct && results.contains(entity)) ) { + if (!getPropertiesToLoad().isEmpty()) { + loadProperties(entity); + } + results.add(entity); } - results.add(entity); } return results; } @@ -732,7 +777,7 @@ for (E elmt : executeToEntityList(transaction)) { Object value = loadProperty(elmt, keyName); if (value != null && !keyClass.isAssignableFrom(value.getClass())) { - throw new ClassCastException("Invalid cast for " + keyClass.getName()); + throw new ClassCastException(value.getClass().getName() + " can't be cast to " + keyClass.getName()); } results.put((K)value, elmt); }
participants (1)
-
fdesbois@users.nuiton.org