org.nuiton.topia.framework
Class TopiaQuery

java.lang.Object
  extended by org.nuiton.topia.framework.TopiaQuery

public class TopiaQuery
extends Object

Query HQL managment to simplify usage of TopiaContext.find(String, Object...). TODO-FD20091224 Complete documentation of this class + JUnit Tests

 This class is used to construct a HQL query and then execute it from a
 TopiaContext. The TopiaQuery is linked to a TopiaEntity which
 is the main element manipulated in the query. There is two parts in using
 this class :
 - construction of the query, using add, addFrom, addOrder, addSelect,
   addGroup, ...
 - execution of the query, using executeToEntityList, executeToEntity,
   executeToInteger, ...

 Construction
 ============

 This class make easier the way to construct a HQL query.
 
 Example 1 :
 -----------

 SQL :
       "SELECT * FROM PersonImpl WHERE firstName LIKE 'M%' AND year > 1980"

 HQL using TopiaContext.find(String, Object...) :
       TopiaContext context = rootContext.beginTransaction();
       context.find("FROM " + Person.class.getName() + " WHERE firstName LIKE :firstName AND year > :year",
              "firstName", "M%", year, 1980);

 TopiaQuery : 
       TopiaQuery query = TopiaQuery.createQuery(Person.class).add(
           Person.FIRST_NAME, Op.LIKE, "M%").add(Person.YEAR, Op.GT, 1980);

 But the real advantage is when you have some parameters to test before adding
 them to the query. With the older method, it was tidious to construct
 and add parameters to finally use the find method from TopiaContext.

 Example 2 :
 -----------

 HQL using TopiaContext.find(String, Object...) :
       TopiaContext context = rootContext.beginTransaction();

       String query = "FROM " + Person.class.getName();
       List params = new ArrayList();
       String separator = " WHERE ";
       // company parameter can be null
       if (company != null) {
           query += separator + "company = :company";
           params.add("company");
           params.add(company);
           separator = " AND ";
       }

       // contact paramater can be null
       if (contact != null) {
           query += separator + "contact = :contact";
           params.add("contact");
           params.add(contact);
           separator = " AND ";
       }

       context.find(query, params.toArray());

 Here we have only two non obligatory params, but imagine if we must have
 almost 6 or 7 parameters like this !

 TopiaQuery :
       TopiaQuery query = TopiaQuery.createQuery(Person.class);

       if (company != null) {
           query.add(Person.COMPANY, company);
       }

       if (contact != null) {
           query.add(Person.CONTACT, contact);
       }

 Many ways to create the same query :
 ------------------------------------

 You can use multiple different manners to create a query, it depends on the
 complexicity. More complex is the query, more easier is to construct it.

 HQL : "FROM PersonImpl AS P WHERE (P.company IS NULL OR P.company = :company) AND P.firstName LIKE :firstName"

 Using TopiaQuery and an Alias (these different queries are equivalent) :
 query = TopiaQuery.createQuery(Person.class, "P");
 1- query.add("(P.company IS NULL OR P.company = :company") AND P.firstName LIKE :firstName")
         .addParam("company", company).addParam("firstName",firstName + "%");
 2- query.add("P.company IS NULL OR P.company = :company")
         .add("P.firstName LIKE :firstName").addParam("company", company)
         .addParam("firstName",firstName + "%");
 3- query.add("P.company IS NULL OR P.company = :company")
         .add("P.firstName", Op.LIKE, firstName + "%")
         .addParam("company", company);
 4- query.addNullOr("P.company", Op.EQ, company).
          add("P.firstName", Op.LIKE, firstName + "%");

 You can use TopiaQuery to create a subquery in an other TopiaQuery, you have
 to use the method fullQuery() to get the full query in HQL and give
 it as a string in the other TopiaQuery.

 Execution
 =========

 After construction, you can execute the query in different ways.

 Default method :
 ----------------

 - execute : as the same result as
 TopiaContext.find(String, Object...)

 Depends on entity type ;
 ------------------------

 - executeToEntity : only one result, the first one
 - executeToEntityList : all results returned in a List
 - executeToEntityMap : all results returned in a Map with key defined by user
 or topiaId by default

 For aggregate :
 ---------------

 These methods have in argument the SELECT to execute the query. The previous
 SELECT (if defined) will not be deleted, but temporarly not used.

 - executeToInteger : for example for "SUM", "COUNT"
 - executeToString : for example for "MAX"
 - executeCount : directly a "count(*)"
 - executeToObject : for other type of possible result (Long, Boolean, Double, ...)

 Property loading
 ================

 When using Hibernate, some times, Entities linked to the main one will be
 lazy initialized, but you want them directly when the query will be executed
 to avoid problems when closing context. You can use the method
 addLoad(String...) to tell the TopiaQuery to load some
 properties when executing the query. After that, you don't need to call them
 for loading them in Hibernate.

 The syntax is the same as a property in HQL query using delegation :
 "person.company" where person and company are entities.

 Note : loading only available on collection or entities but not property
 on a collection of entities which must be made manually.

 For a Contact which is linked to a person (entity) and the person linked to
 company (entity) you can add to a TopiaQuery :
       query.addLoad("person.company")

 For a list of addresses (entity) in the contact you can do :
       query.addLoad("addresses")

 But it's not possible to do for example with meeting (entity) linked to the
 contact and responsible (entity) linked to a meeting :
       query.addLoad("meetings.responsible")

 

 Created: 21 déc. 2009

Since:
2.3.0 Mise a jour: $Date: 2010-04-20 00:41:25 +0200 (mar., 20 avril 2010) $ par : $Author: fdesbois $
Version:
$Revision: 1897 $
Author:
fdesbois

Nested Class Summary
static class TopiaQuery.Op
          Enum to simmplify using operation in query
 
Field Summary
protected  TopiaDAO<? extends TopiaEntity> dao
           
protected  boolean distinct
           
protected  Integer endIndex
           
protected  StringBuilder from
          From part of the query
protected  StringBuilder groupBy
          Group By part of the query
protected  String mainAlias
           
protected  StringBuilder orderBy
          Order By part of the query
protected  List<Object> params
          Params for HQL query
protected  boolean parentheses
          Used to determine if parentheses are needed for Where statement
protected  List<String> propertiesToLoad
           
protected  StringBuilder select
          Select part of the query
protected  Integer startIndex
           
protected  StringBuilder where
          Where part of the query
 
Constructor Summary
TopiaQuery()
           
TopiaQuery(Class<? extends TopiaEntity> mainEntityClass)
          Create a TopiaQuery based on the entityClass.
TopiaQuery(Class<? extends TopiaEntity> mainEntityClass, String alias)
          Create a TopiaQuery based on the entityClass.
TopiaQuery(TopiaDAO<? extends TopiaEntity> dao)
          Create a TopiaQuery from a DAO.
TopiaQuery(TopiaDAO<? extends TopiaEntity> dao, String alias)
          Create a TopiaQuery from a DAO with an Alias.
 
Method Summary
 TopiaQuery add(Map<String,Object> properties)
          Add a map of properties to the where clause of the query.
 TopiaQuery add(String where)
          Add a where element to the Query.
 TopiaQuery add(String paramName, Collection<Object> values, boolean isNull)
          Deprecated. use add(String, Object...) with a null or not
 TopiaQuery add(String paramName, Object... paramValue)
          Add an element to the query.
 TopiaQuery add(String paramName, TopiaQuery.Op constraint, Object paramValue)
          Add an element to the query.
 TopiaQuery addDistinct()
          Add the distinct key word in the query.
 TopiaQuery addFrom(Class<? extends TopiaEntity> entityClass)
          Add an other entity type to the from in the query.
 TopiaQuery addFrom(Class<? extends TopiaEntity> entityClass, String alias)
          Add an other entity type to the from in the query with an alias.
 TopiaQuery addFrom(String str)
          Add an element to the from in the query.
 TopiaQuery addGroup(String... group)
          Add an element to the group of the query.
 TopiaQuery addLoad(String... properties)
          Add a property to load when query is executed.
 TopiaQuery addNotNull(String paramName)
          Add an element to the query with the constraint Not null.
 TopiaQuery addNullOr(String paramName, TopiaQuery.Op constraint, Object paramValue)
          Add an element to the query.
 TopiaQuery addOrder(String... order)
          Add an element to the order in the query.
 TopiaQuery addOrderDesc(String order)
           
 TopiaQuery addParam(String id, Object paramValue)
          Add a HQL parameter to the Query.
 TopiaQuery addParams(List<Object> params)
          Add muliple paramaters to the Query.
 TopiaQuery addSelect(String... select)
          Add an element to the select in the query.
protected  String convertStringArray(String... array)
          Helper method for array type.
static
<T extends TopiaEntity>
TopiaQuery
createQuery(Class<T> entityClass)
          Deprecated. use constructor instead : TopiaQuery(TopiaDAO)
static
<T extends TopiaEntity>
TopiaQuery
createQuery(Class<T> entityClass, String alias)
          Deprecated. use constructor instead : TopiaQuery(Class, String)
static
<T extends TopiaEntity>
TopiaQuery
createQuery(TopiaDAO<T> dao)
          Deprecated. use constructor instead : TopiaQuery(TopiaDAO)
static
<T extends TopiaEntity>
TopiaQuery
createQuery(TopiaDAO<T> dao, String alias)
          Deprecated. use constructor instead : java.lang.Class)
 List execute()
          DAO must be defined to use this method.
 List execute(TopiaContext transaction)
          Simple execution of the query.
 int executeCount()
          DAO must be defined to use this method.
 int executeCount(TopiaContext transaction)
          Execute a simple count on the query, i.e. the number of results get from the query.
<E extends TopiaEntity>
E
executeToEntity()
          Deprecated. use dao method instead : TopiaDAO.findByQuery(TopiaQuery)
<E extends TopiaEntity>
E
executeToEntity(TopiaContext transaction, Class<E> entityClass)
          Execute the query and get the first result entity.
<E extends TopiaEntity>
List<E>
executeToEntityList()
          Deprecated. use dao method instead : TopiaDAO.findAllByQuery(TopiaQuery)
<E extends TopiaEntity>
List<E>
executeToEntityList(TopiaContext transaction, Class<E> entityClass)
          Execute the query and get a List of entity.
<E extends TopiaEntity>
Map<String,E>
executeToEntityMap()
          Deprecated. use dao method instead : TopiaDAO.findAllMappedByQuery(TopiaQuery)
<E extends TopiaEntity,K>
Map<K,E>
executeToEntityMap(String keyName, Class<K> keyClass)
          Deprecated. use dao method instead : TopiaDAO.findAllMappedByQuery(TopiaQuery, String, Class)
<E extends TopiaEntity>
Map<String,E>
executeToEntityMap(TopiaContext transaction, Class<E> entityClass)
          Execute the query and get a Map of entity with topiaId in key.
<E extends TopiaEntity,K>
Map<K,E>
executeToEntityMap(TopiaContext transaction, Class<E> entityClass, String keyName, Class<K> keyClass)
          Execute the query and get a Map of entity with key type in argument.
 int executeToInteger(String select)
          DAO must be defined to use this method.
 int executeToInteger(TopiaContext transaction, String select)
          Execute the query and get an Integer for result.
 Object executeToObject(String select)
          DAO must be defined to use this method.
 Object executeToObject(TopiaContext transaction, String select)
          Execute the query and get an Object for result.
 String executeToString(String select)
          DAO must be defined to use this method.
 String executeToString(TopiaContext transaction, String select)
          Execute the query and get a String for result.
protected  void finalize()
           
 String fullQuery()
          Get the full query.
 String getMainAlias()
          Return the mainAlias set from constructor.
 List<Object> getParams()
           
protected  List<String> getPropertiesToLoad()
           
static String getProperty(String... entityProperty)
          This method is used to concat properties from entities.
protected  String getValueName(String paramName)
           
protected
<T extends TopiaEntity>
TopiaEntity
loadEntityProperty(T entity, String property)
          Load a property of type TopiaEntity from an other entity.
protected
<T extends TopiaEntity>
void
loadProperties(T entity)
          Load all properties for the entity.
protected
<T extends TopiaEntity>
Object
loadProperty(T entity, String property)
          Load a property from an entity.
 TopiaQuery resetLimit()
          Remove limits previously set
 TopiaQuery setFrom(Class<? extends TopiaEntity> mainEntityClass)
          Set the mainEntity in the from part of the query.
 TopiaQuery setFrom(Class<? extends TopiaEntity> mainEntityClass, String alias)
          Set the mainEntity in the from part of the query and use an alias for this mainEntity.
 TopiaQuery setLimit(int start, int end)
          Limit the result of the query with startIndex and endIndex.
 TopiaQuery setMaxResults(int max)
          Set the max results wanted for the query.
 TopiaQuery setSelect(String... select)
          Set the select in the query.
 String toString()
           
protected  boolean validateDAO()
           
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

params

protected List<Object> params
Params for HQL query


select

protected StringBuilder select
Select part of the query


distinct

protected boolean distinct

from

protected StringBuilder from
From part of the query


where

protected StringBuilder where
Where part of the query


orderBy

protected StringBuilder orderBy
Order By part of the query


groupBy

protected StringBuilder groupBy
Group By part of the query


startIndex

protected Integer startIndex

endIndex

protected Integer endIndex

parentheses

protected boolean parentheses
Used to determine if parentheses are needed for Where statement


propertiesToLoad

protected List<String> propertiesToLoad

mainAlias

protected String mainAlias

dao

protected TopiaDAO<? extends TopiaEntity> dao
Constructor Detail

TopiaQuery

public TopiaQuery()

TopiaQuery

public TopiaQuery(Class<? extends TopiaEntity> mainEntityClass)
Create a TopiaQuery based on the entityClass. The from statement is automatically set.

Parameters:
mainEntityClass -

TopiaQuery

public TopiaQuery(Class<? extends TopiaEntity> mainEntityClass,
                  String alias)
Create a TopiaQuery based on the entityClass. The from statement is automatically set, the select statement must be necessary in some case, the query will manage this case using the mainAlias by default.

Parameters:
mainEntityClass -
alias - for the mainEntityClass

TopiaQuery

public TopiaQuery(TopiaDAO<? extends TopiaEntity> dao)
Create a TopiaQuery from a DAO. The main entity will be automatically added to the select part of the query if it is needed.

Parameters:
dao - DAO linked to the entity to threat

TopiaQuery

public TopiaQuery(TopiaDAO<? extends TopiaEntity> dao,
                  String 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.

Parameters:
dao - DAO linked to the entity to threat
alias - of the main entity in the query
Method Detail

setFrom

public TopiaQuery setFrom(Class<? extends TopiaEntity> mainEntityClass)
Set the mainEntity in the from part of the query.

Parameters:
mainEntityClass - type of the mainEntity
Returns:
the TopiaQuery

setFrom

public TopiaQuery setFrom(Class<? extends TopiaEntity> mainEntityClass,
                          String alias)
Set the mainEntity in the from part of the query and use an alias for this mainEntity.

Parameters:
mainEntityClass - type of the mainEntity
alias - for the entity in the query
Returns:
the TopiaQuery

addFrom

public TopiaQuery addFrom(String str)
Add an element to the from in the query. Used to add some other data in the query or for join.

Parameters:
str - the element to add
Returns:
the TopiaQuery

addFrom

public TopiaQuery addFrom(Class<? extends TopiaEntity> entityClass)
Add an other entity type to the from in the query.

Parameters:
entityClass - different from the mainEntity in the DAO
Returns:
the TopiaQuery

addFrom

public TopiaQuery addFrom(Class<? extends TopiaEntity> entityClass,
                          String alias)
Add an other entity type to the from in the query with an alias.

Parameters:
entityClass - different from the mainEntity in the DAO
alias - of the entity in the query
Returns:
the TopiaQuery

createQuery

@Deprecated
public static <T extends TopiaEntity> TopiaQuery createQuery(Class<T> entityClass)
Deprecated. use constructor instead : TopiaQuery(TopiaDAO)

Create a TopiaQuery with entityClass initialization.

Type Parameters:
T - entity type extends TopiaEntity
Parameters:
entityClass - Class for an entity Query
Returns:
the new TopiaQuery

createQuery

@Deprecated
public static <T extends TopiaEntity> TopiaQuery createQuery(TopiaDAO<T> dao)
Deprecated. use constructor instead : TopiaQuery(TopiaDAO)

Create a TopiaQuery from a DAO.

Type Parameters:
T - entity type in the dao extends TopiaEntity
Parameters:
dao - DAO linked to the entity to threat
Returns:
the new TopiaQuery

createQuery

@Deprecated
public static <T extends TopiaEntity> TopiaQuery createQuery(Class<T> entityClass,
                                                                        String alias)
Deprecated. use constructor instead : TopiaQuery(Class, String)

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.

Type Parameters:
T - entity type in the dao extends TopiaEntity
Parameters:
entityClass - Class for an entity Query
alias - of the main entity in the query
Returns:
the new TopiaQuery

createQuery

@Deprecated
public static <T extends TopiaEntity> TopiaQuery createQuery(TopiaDAO<T> dao,
                                                                        String alias)
Deprecated. use constructor instead : java.lang.Class)

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.

Type Parameters:
T - entity type in the dao extends TopiaEntity
Parameters:
dao - DAO linked to the entity to threat
alias - of the main entity in the query
Returns:
the new TopiaQuery

toString

public String toString()
Overrides:
toString in class Object

fullQuery

public String fullQuery()
Get the full query.

Returns:
a String corresponding to the full query.

addParam

public TopiaQuery addParam(String id,
                           Object paramValue)
Add a HQL parameter to the Query.

Parameters:
id - identification of the param in the query
paramValue - value of the param
Returns:
the TopiaQuery

addParams

public TopiaQuery addParams(List<Object> params)
Add muliple paramaters to the Query. The key of each param will be tested if not already exist in the existing params list and will be renamed in this case.

Parameters:
params - a list of HQL params with key and value in order.
Returns:
the TopiaQuery
See Also:
getValueName(String)

getParams

public List<Object> getParams()

getMainAlias

public String getMainAlias()
Return the mainAlias set from constructor.

Returns:
a String or null if no alias is set

addLoad

public TopiaQuery addLoad(String... properties)
Add a property to load when query is executed. Used to avoid LazyInitializationException for property needed after closing context. The property is a string like those in HQL query.
 Exemples :
 - "person.company" (Property TopiaEntity person linked to the result
   entity in query and company linked to person)
   --> calling myEntity.getPerson().getCompany();
 - "partyRoles" (Property Collection partyRoles linked to the result
   entity in query)
   --> calling myEntity.getPartyRoles().size();
 

Parameters:
properties -
Returns:
the TopiaQuery

getPropertiesToLoad

protected List<String> getPropertiesToLoad()

add

public TopiaQuery add(String where)
Add a where element to the Query. Could be anything. Parentheses are added automatically (even if there are not needed).

Parameters:
where - element to add
Returns:
the TopiaQuery

add

public TopiaQuery add(String paramName,
                      TopiaQuery.Op constraint,
                      Object 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).

Parameters:
paramName - the name of the parameter in the query (attribute of the entity)
constraint - the operation concerned
paramValue - the value of the parameter (an other entity, a String, ...)
Returns:
the TopiaQuery

addNullOr

public TopiaQuery addNullOr(String paramName,
                            TopiaQuery.Op constraint,
                            Object paramValue)
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()).

Parameters:
paramName - the name of the parameter in the query (attribute of the entity)
constraint - the operation concerned by the or
paramValue - the value of the parameter (an other entity, a String, ...)
Returns:
the TopiaQuery

getValueName

protected String getValueName(String paramName)

addNotNull

public TopiaQuery addNotNull(String paramName)
Add an element to the query with the constraint Not null.

Parameters:
paramName - name of the parameter in the query
Returns:
the TopiaQuery

add

public TopiaQuery add(String paramName,
                      Object... paramValue)
Add an element to the query. The parameter will be automatically added. The default constrainst operation is Op.EQ for EQUALS. Ex : add("boat", boat) means -> boat = :boat. If you add more than one values, the statement IN will be used. You can also have a null value in the paramValue list (except if it's the only one value, it's ambiguous). Note : this method do nothing if the paramValue is not defined. If you want to add the constraint null on the paramName use add(String, TopiaQuery.Op, Object) as : add("param", Op.EQ, null);

Parameters:
paramName - name of the parameter in the query
paramValue - values of the parameter
Returns:
the TopiaQuery
Since:
2.3.1
See Also:
add(String, Op, Object)

add

@Deprecated
public TopiaQuery add(String paramName,
                                 Collection<Object> values,
                                 boolean isNull)
Deprecated. use add(String, Object...) with a null or not

Add an element to the query with a list of different values. The IN key word will be used to set the different values. An other constraint on nullity can be set with isNull argument. The element can have one of the value from the collection or can be null if the isNull argument is true.

Parameters:
paramName - name of the parameter in the query
values - different values for this parameter
isNull - use it to test the nullity of parameter
Returns:
the TopiaQuery
See Also:
add(String)

add

public TopiaQuery add(Map<String,Object> 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 query.

Parameters:
properties -
Returns:
the TopiaQuery

addSelect

public TopiaQuery addSelect(String... select)
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 setSelect(String...) method instead.

Parameters:
select - element to add
Returns:
the TopiaQuery

setSelect

public TopiaQuery setSelect(String... select)
Set the select in the query. Depends on the result wanted in execute methods.

Parameters:
select - element to set
Returns:
the TopiaQuery

addDistinct

public TopiaQuery addDistinct()
Add the distinct key word in the query. The result will not have multiple same values.

Returns:
the TopiaQuery

addOrder

public TopiaQuery addOrder(String... order)
Add an element to the order in the query. Used to add some parameters to order by.

Parameters:
order - element to add
Returns:
the TopiaQuery

addOrderDesc

public TopiaQuery addOrderDesc(String order)

addGroup

public TopiaQuery addGroup(String... group)
Add an element to the group of the query. Used to add some paramters to group by.

Parameters:
group - element to add
Returns:
the TopiaQuery

convertStringArray

protected String convertStringArray(String... array)
Helper method for array type. Each value will be separated by a comma.

Parameters:
array - of String
Returns:
a String with values of the array separated by a comma

setLimit

public TopiaQuery setLimit(int start,
                           int end)
Limit the result of the query with startIndex and endIndex.

Parameters:
start - first index to get from the results
end - last index to get from the results
Returns:
the TopiaQuery

resetLimit

public TopiaQuery resetLimit()
Remove limits previously set

Returns:
the TopiaQuery

setMaxResults

public TopiaQuery setMaxResults(int max)
Set the max results wanted for the query.

Parameters:
max - the number of elements wanted
Returns:
the TopiaQuery

execute

public List execute(TopiaContext transaction)
             throws TopiaException
Simple execution of the query. This method use directly the find method in TopiaContext interface.

Parameters:
transaction - the TopiaContext to use for execution
Returns:
a List of results
Throws:
TopiaException
See Also:
TopiaContext.find(String, Object...)

execute

public List execute()
             throws TopiaException
DAO must be defined to use this method.

Returns:
a List of results
Throws:
TopiaException
See Also:
execute(TopiaContext)

executeToEntityList

public <E extends TopiaEntity> List<E> executeToEntityList(TopiaContext transaction,
                                                           Class<E> entityClass)
                                                throws TopiaException,
                                                       ClassCastException
Execute the query and get a List of entity. Some properties will be loaded if they are prealably set using $addLoad(String...).

Type Parameters:
E -
Parameters:
transaction - the TopiaContext to use for execution
entityClass -
Returns:
a List of TopiaEntity corresponding to the entityClass in argument
Throws:
TopiaException
ClassCastException

executeToEntityList

@Deprecated
public <E extends TopiaEntity> List<E> executeToEntityList()
                                                throws TopiaException,
                                                       ClassCastException
Deprecated. use dao method instead : TopiaDAO.findAllByQuery(TopiaQuery)

DAO must be defined to use this method.

Type Parameters:
E -
Returns:
a List of TopiaEntity corresponding to the entityClass in argument
Throws:
TopiaException
ClassCastException
See Also:
executeToEntityList(TopiaContext,Class)

executeToEntityMap

public <E extends TopiaEntity,K> Map<K,E> executeToEntityMap(TopiaContext transaction,
                                                             Class<E> entityClass,
                                                             String keyName,
                                                             Class<K> keyClass)
                                                throws TopiaException,
                                                       ClassCastException
Execute the query and get a Map of entity with key type in argument. Some properties will be loaded if they are prealably set using $addLoad(String...).

Type Parameters:
E -
K - the type of the map key
Parameters:
transaction - the TopiaContext to use for execution
entityClass -
keyName - the property name of the key in the entity
keyClass - the key class for the result map
Returns:
a Map with the key type defined and the entity in value
Throws:
TopiaException
ClassCastException

executeToEntityMap

@Deprecated
public <E extends TopiaEntity,K> Map<K,E> executeToEntityMap(String keyName,
                                                                        Class<K> keyClass)
                                                throws TopiaException,
                                                       ClassCastException
Deprecated. use dao method instead : TopiaDAO.findAllMappedByQuery(TopiaQuery, String, Class)

DAO must be defined to use this method.

Type Parameters:
E -
K -
Parameters:
keyName -
keyClass -
Returns:
a Map with the key type defined and the entity in value
Throws:
TopiaException
ClassCastException
See Also:
executeToEntityMap(TopiaContext, Class)

executeToEntityMap

public <E extends TopiaEntity> Map<String,E> executeToEntityMap(TopiaContext transaction,
                                                                Class<E> entityClass)
                                                     throws TopiaException,
                                                            ClassCastException
Execute the query and get a Map of entity with topiaId in key. Some properties will be loaded if they are prealably set using $addLoad(String...).

Type Parameters:
E -
Parameters:
transaction - the TopiaContext to use for execution
entityClass -
Returns:
a Map with the key type defined and the entity in value
Throws:
TopiaException
ClassCastException

executeToEntityMap

@Deprecated
public <E extends TopiaEntity> Map<String,E> executeToEntityMap()
                                                     throws TopiaException,
                                                            ClassCastException
Deprecated. use dao method instead : TopiaDAO.findAllMappedByQuery(TopiaQuery)

DAO must be defined to use this method.

Type Parameters:
E -
Returns:
a Map with the key type defined and the entity in value
Throws:
TopiaException
ClassCastException
See Also:
executeToEntityMap(TopiaContext,Class)

executeToEntity

public <E extends TopiaEntity> E executeToEntity(TopiaContext transaction,
                                                 Class<E> entityClass)
                                      throws TopiaException,
                                             ClassCastException
Execute the query and get the first result entity. Some properties will be loaded if they are prealably set using $addLoad(String...).

Type Parameters:
E -
Parameters:
transaction - the TopiaContext to use for execution
entityClass -
Returns:
a TopiaEntity corresponding to the entityClass in argument
Throws:
TopiaException
ClassCastException

executeToEntity

@Deprecated
public <E extends TopiaEntity> E executeToEntity()
                                      throws TopiaException,
                                             ClassCastException
Deprecated. use dao method instead : TopiaDAO.findByQuery(TopiaQuery)

DAO must be defined to use this method.

Type Parameters:
E -
Returns:
a TopiaEntity corresponding to the entityClass in argument
Throws:
TopiaException
ClassCastException
See Also:
executeToEntity(TopiaContext,Class)

executeToObject

public Object executeToObject(TopiaContext transaction,
                              String select)
                       throws TopiaException
Execute the query and get an Object for result. The select is overriden to get only the right value for return.

Parameters:
transaction - the TopiaContext to use for execution
select - the Select overriden
Returns:
an Object
Throws:
TopiaException

executeToInteger

public int executeToInteger(TopiaContext transaction,
                            String select)
                     throws TopiaException
Execute the query and get an Integer for result. Used only for query with aggration select which return a Long : COUNT, SUM ... The select is overriden to get only the right value for return.

Parameters:
transaction - the TopiaContext to use for execution
select - the Select overriden (ex : SUM(myParam))
Returns:
an Integer
Throws:
TopiaException

executeToInteger

public int executeToInteger(String select)
                     throws TopiaException
DAO must be defined to use this method.

Parameters:
select -
Returns:
an Integer
Throws:
TopiaException
See Also:
executeToInteger(TopiaContext, String)

executeToString

public String executeToString(TopiaContext transaction,
                              String select)
                       throws TopiaException
Execute the query and get a String for result. Used for query with MAX, ... The select is overriden to get only the right value for return.

Parameters:
transaction - the TopiaContext to use for execution
select - the Select overriden (ex : MAX(myParam))
Returns:
a String
Throws:
TopiaException

executeToString

public String executeToString(String select)
                       throws TopiaException
DAO must be defined to use this method.

Parameters:
select -
Returns:
a String result
Throws:
TopiaException
See Also:
executeToString(TopiaContext, String)

executeToObject

public Object executeToObject(String select)
                       throws TopiaException
DAO must be defined to use this method.

Parameters:
select -
Returns:
an Object
Throws:
TopiaException
See Also:
executeToObject(TopiaContext, String)

executeCount

public int executeCount(TopiaContext transaction)
                 throws TopiaException
Execute a simple count on the query, i.e. the number of results get from the query.

Parameters:
transaction - the TopiaContext to use for execution
Returns:
an int corresponding to the number of result in the query
Throws:
TopiaException

executeCount

public int executeCount()
                 throws TopiaException
DAO must be defined to use this method.

Returns:
an int corresponding to the number of result in the query
Throws:
TopiaException
See Also:
executeCount(TopiaContext)

validateDAO

protected boolean validateDAO()
                       throws TopiaException
Throws:
TopiaException

loadProperties

protected <T extends TopiaEntity> void loadProperties(T entity)
                       throws TopiaException
Load all properties for the entity.

Type Parameters:
T - type of the entity extends TopiaEntity
Parameters:
entity - used to load properties
Throws:
TopiaException

loadEntityProperty

protected <T extends TopiaEntity> TopiaEntity loadEntityProperty(T entity,
                                                                 String property)
                                  throws TopiaException
Load a property of type TopiaEntity from an other entity.

Type Parameters:
T - type of the entity extends TopiaEntity
Parameters:
entity - used to load the property
property - name of the property in the entity
Returns:
a TopiaEntity corresponding to the property loaded
Throws:
TopiaException

loadProperty

protected <T extends TopiaEntity> Object loadProperty(T entity,
                                                      String property)
                       throws TopiaException
Load a property from an entity.

Type Parameters:
T - type of the entity extends TopiaEntity
Parameters:
entity - used to load the property
property - name of the property in the entity
Returns:
an Object corresponding to the property loaded
Throws:
TopiaException

finalize

protected void finalize()
Overrides:
finalize in class Object

getProperty

public static String getProperty(String... entityProperty)
This method is used to concat properties from entities. Ex in HQL you can have boat.shipOwner.name, these properties are defined as constants in each entity associated (SHIP_OWNER in Boat entity, NAME in ShipOwner entity) so you just have to call this method as : getProperty("boat", Boat.SHIP_OWNER, ShipOwner.NAME); to have the correct property to use : boat.shipOwner.name. It's better to use constants instead of directly the string chain to avoid problems on changing property name in model. Furthermore it's better to use this method instead of doing : "boat." + Boat.SHIP_OWNER + "." + ShipOwner.NAME

Parameters:
entityProperty - to concat
Returns:
the string chain with properties separated with a dot


Copyright © 2004-2010 CodeLutin. All Rights Reserved.