Package com.avoka.core.util
Class CayenneUtils
- java.lang.Object
-
- com.avoka.core.util.CayenneUtils
-
public class CayenneUtils extends Object
Provides Cayenne utility methods.
-
-
Field Summary
Fields Modifier and Type Field Description static String
TH_NODE_NAME
The Transaction History node name : "transactionHistory-JNDI".static String
TM_NODE_NAME
The TM node name : "formcenterNode-JNDI".
-
Constructor Summary
Constructors Constructor Description CayenneUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
applyMultipleNameValuePredicates(org.apache.cayenne.query.SelectQuery query, Map<String,Set<String>> aliasPaths, String dbPath, String dbNamePath, String dbValuePath, Map<String,String> valueSet)
Applies multiple name/value predicates to the Cayenne Select Query storing any generated aliases in aliasPaths for the dbPath matching all of the passed valuesstatic void
applyMultiplePredicates(org.apache.cayenne.query.SelectQuery query, Map<String,Set<String>> aliasPaths, String dbPath, Set<String> valueSet)
Applies multiple predicates to the Cayenne Select Query storing any generated aliases in aliasPaths for the dbPath matching all of the passed valuesstatic BaseEntity
cloneEntity(BaseEntity entity)
Perform a shallow clone of an entity, register it with the data context and return the new entity.static BaseEntity
cloneEntity(BaseEntity entity, String[] copyPaths, String[] clonePaths)
Perform a deep clone of an entity, progressing only down the paths specified, register it with the data context and return the new entity.static org.apache.cayenne.access.DataContext
createDataContext()
Create a new data context.static org.apache.cayenne.ObjectId
createObjectId(Class dataObjectClass, Object id)
Create a new ObjectId for the given data object class and primary key id.static org.apache.cayenne.query.SelectQuery
createQuery(Class dataObjectClass, Object id)
Create a SelectQuery for the given data object class and id primary key value.static org.apache.cayenne.query.SelectQuery
createQuery(org.apache.cayenne.DataObject dataObject)
Create a SelectQuery from the given data object.static Object
ensureOnObjectContext(org.apache.cayenne.DataObject entity, org.apache.cayenne.ObjectContext objectContext)
Ensure the given data object in in the specified objectContext, otherwise refetch the entity on the specified objectContext.static org.apache.cayenne.configuration.server.ServerRuntime
getCayenneRuntime()
Gets the cayenne runtime.static int
getColumnMaxLength(Class entityClass, String objAttributeName)
Return the maximum length of an entity attributestatic Connection
getConnection()
Return a pooled Cayenne connection for the shared configuration return TM node if exists, otherwise it will return the first configured DataNode.static Connection
getConnection(String nodeName)
Return a pooled Cayenne connection for the shared configuration and the given DataNode name.static Connection
getConnection(org.apache.cayenne.access.DataContext dataContext)
Return a pooled connection for the given Cayenne data context.static org.apache.cayenne.access.DataContext
getDataContext()
Return the thread local DataContext.static String
getEntityName(Class dataObjectClass)
Return the object entity name for the given data object class.static org.apache.cayenne.DataObject
getObjectForPK(Class doClass, Object id)
Perform a database query returning the data object specified by the class and the primary key.static org.apache.cayenne.DataObject
getObjectForPK(Class doClass, Object id, boolean refresh)
Perform a query returning the data object specified by the class and the primary key value.static org.apache.cayenne.DataObject
getObjectForPK(String entityName, Object id)
Perform a database query returning the data object specified by entity name and the primary key.static String
getPkName(Class dataObjectClass)
Return the database primary key column name for the given data object.static String
getTrimmedPropertyValue(Class dataObjectClass, String propertyName, String propertyValue)
Calculate and return the trimmed value for the specified propertyValue.static int
performDelete(Class entityClass, String sqlQualifier, Map<String,Object> params)
Perform a SQL delete query on the given entity for the specified SQL and parameter.static <T extends BaseEntity>
TrefetchEntity(T entity)
Refetch the given entity if associated with a different data context.static <T extends BaseEntity>
TrefetchForced(T entity, org.apache.cayenne.access.DataContext dataContext)
Refetch the given entity.static Object
refreshObject(org.apache.cayenne.DataObject dataObject)
Return an refreshed version of the object specified by the given objectId.static Object
refreshObject(org.apache.cayenne.ObjectContext dataContext, org.apache.cayenne.ObjectId objectId)
Return an refreshed version of the object specified by the given objectId.static boolean
retrieveBooleanAttribute(org.apache.cayenne.DataRow dataRow, String attributeName)
Returns a boolean attribute from a data row.static Double
retrieveDoubleAttribute(org.apache.cayenne.DataRow dataRow, String attributeName)
Returns an double attribute from a data row.static Integer
retrieveIntegerAttribute(org.apache.cayenne.DataRow dataRow, String attributeName)
Returns an integer attribute from a data row.static Long
retrieveLongAttribute(org.apache.cayenne.DataRow dataRow, String attributeName)
Returns a long attribute from a data row.static <T> T
runInTransactionAndUnwrap(org.apache.cayenne.tx.TransactionalOperation<T> op)
Run the provided operation wrapped in a single transaction.static int
selectCount(org.apache.cayenne.access.DataContext context, org.apache.cayenne.query.SelectQuery query, org.apache.cayenne.access.DataNode node)
Modifies a query to select the number of results instead.static int
selectCountV2(org.apache.cayenne.access.DataContext context, org.apache.cayenne.query.SelectQuery query, org.apache.cayenne.access.DataNode node)
Modifies a query to select the number of results instead.static Object
selectTranslated(org.apache.cayenne.access.DataContext dataContext, org.apache.cayenne.query.SelectQuery query, org.apache.cayenne.access.translator.select.SelectTranslator translator, boolean isCountQuery, Class resultEntityClass)
Perform a SelectQuery using a SelectTranslator.static void
setPropertyValueTrimmed(org.apache.cayenne.DataObject dataObject, String propertyName, String propertyValue)
Set the String property value on the given dataObject.
-
-
-
Field Detail
-
TM_NODE_NAME
public static final String TM_NODE_NAME
The TM node name : "formcenterNode-JNDI".- See Also:
- Constant Field Values
-
TH_NODE_NAME
public static final String TH_NODE_NAME
The Transaction History node name : "transactionHistory-JNDI".- See Also:
- Constant Field Values
-
-
Method Detail
-
createQuery
public static org.apache.cayenne.query.SelectQuery createQuery(Class dataObjectClass, Object id)
Create a SelectQuery for the given data object class and id primary key value.- Parameters:
dataObjectClass
- the data object classid
- the data object primary key value- Returns:
- the SelectQuery for the given data object class and primary key id
-
createQuery
public static org.apache.cayenne.query.SelectQuery createQuery(org.apache.cayenne.DataObject dataObject)
Create a SelectQuery from the given data object.- Parameters:
dataObject
- the data object class- Returns:
- the SelectQuery for the given data object
-
createObjectId
public static org.apache.cayenne.ObjectId createObjectId(Class dataObjectClass, Object id)
Create a new ObjectId for the given data object class and primary key id.- Parameters:
dataObjectClass
- the data object classid
- the data object primary key (id) value- Returns:
- the ObjectId for the given data object class and id
-
getEntityName
public static String getEntityName(Class dataObjectClass)
Return the object entity name for the given data object class.- Parameters:
dataObjectClass
- the data object class- Returns:
- the object entity name
-
getObjectForPK
public static org.apache.cayenne.DataObject getObjectForPK(Class doClass, Object id)
Perform a database query returning the data object specified by the class and the primary key. This method will perform a database query and refresh the object cache.- Parameters:
doClass
- the data object class to retrieveid
- the data object primary key- Returns:
- the data object for the given class and id
-
getObjectForPK
public static org.apache.cayenne.DataObject getObjectForPK(String entityName, Object id)
Perform a database query returning the data object specified by entity name and the primary key. This method will perform a database query and refresh the object cache.- Parameters:
entityName
- the entity name of the object to retrieveid
- the data object primary key- Returns:
- the data object for the given class and id
-
getObjectForPK
public static org.apache.cayenne.DataObject getObjectForPK(Class doClass, Object id, boolean refresh)
Perform a query returning the data object specified by the class and the primary key value. If the refresh parameter is true a database query will be performed, otherwise the a query against the object cache will be performed first.- Parameters:
doClass
- the data object class to retrieveid
- the data object primary keyrefresh
- the refresh the object cache mode- Returns:
- the data object for the given class and id
-
getPkName
public static String getPkName(Class dataObjectClass)
Return the database primary key column name for the given data object.- Parameters:
dataObjectClass
- the class of the data object- Returns:
- the primary key column name
-
getConnection
public static Connection getConnection(org.apache.cayenne.access.DataContext dataContext) throws SQLException
Return a pooled connection for the given Cayenne data context.- Parameters:
dataContext
- the Cayenne data context (required)- Returns:
- a pooled SQL connection
- Throws:
SQLException
- if a database connection could not be obtained- Since:
- 5.1.8
-
getConnection
public static Connection getConnection() throws SQLException
Return a pooled Cayenne connection for the shared configuration return TM node if exists, otherwise it will return the first configured DataNode.- Returns:
- a pooled SQL connection
- Throws:
SQLException
- if a database connection could not be obtained- Since:
- 4.0.0
-
getConnection
public static Connection getConnection(String nodeName) throws SQLException
Return a pooled Cayenne connection for the shared configuration and the given DataNode name.- Parameters:
nodeName
- the name of the Cayenne node- Returns:
- a pooled SQL connection
- Throws:
SQLException
- if a database connection could not be obtained
-
cloneEntity
public static BaseEntity cloneEntity(BaseEntity entity)
Perform a shallow clone of an entity, register it with the data context and return the new entity.- Parameters:
entity
- The entity to clone- Returns:
- the new entity
-
cloneEntity
public static BaseEntity cloneEntity(BaseEntity entity, String[] copyPaths, String[] clonePaths)
Perform a deep clone of an entity, progressing only down the paths specified, register it with the data context and return the new entity.
Note: This method only supports a single level of depth.- Parameters:
entity
- The entity to clonecopyPaths
- The deep entity terminating paths to copy. These must be explicitly specified (wildcards not supported). If a clone path is multi-level (eg 'customers.address') then all levels in the path are cloned.clonePaths
- The deep entity terminating paths to clone. These must be explicitly specified (wildcards not supported). If a clone path is multi-level (eg 'customers.address') then all levels in the path are cloned.- Returns:
- the new entity
-
getDataContext
public static org.apache.cayenne.access.DataContext getDataContext()
Return the thread local DataContext.- Returns:
- the thread local DataContext
-
createDataContext
public static org.apache.cayenne.access.DataContext createDataContext()
Create a new data context.- Returns:
- the new data context
- Since:
- 4.0.0
-
refreshObject
public static Object refreshObject(org.apache.cayenne.DataObject dataObject)
Return an refreshed version of the object specified by the given objectId.- Parameters:
dataObject
- the data object to refresh- Returns:
- the refreshed data object
- Since:
- 4.2.0
-
refreshObject
public static Object refreshObject(org.apache.cayenne.ObjectContext dataContext, org.apache.cayenne.ObjectId objectId)
Return an refreshed version of the object specified by the given objectId.- Parameters:
dataContext
- the current data contextobjectId
- the object ID- Returns:
- the refreshed object from the database
- Since:
- 4.0.0
-
refetchEntity
public static <T extends BaseEntity> T refetchEntity(T entity)
Refetch the given entity if associated with a different data context.- Parameters:
entity
- to check- Returns:
- the refetched entity if associted with a different data context.
- Since:
- 4.3.4
-
refetchForced
public static <T extends BaseEntity> T refetchForced(T entity, org.apache.cayenne.access.DataContext dataContext)
Refetch the given entity.- Parameters:
entity
- to check (required)dataContext
- the data context (required)- Since:
- 17.10.0
-
getCayenneRuntime
public static org.apache.cayenne.configuration.server.ServerRuntime getCayenneRuntime()
Gets the cayenne runtime.- Returns:
- the cayenne runtime
-
selectCount
public static int selectCount(org.apache.cayenne.access.DataContext context, org.apache.cayenne.query.SelectQuery query, org.apache.cayenne.access.DataNode node)
Modifies a query to select the number of results instead. IMPORTANT: SQL Server will not be able to execute the count query if the original query contains unaggregated fields in an ORDER BY clause. Do not use sorting in the query.- Parameters:
context
- the data context for the applicationquery
- a SelectQuery that does not use sortingnode
- data node for the application- Returns:
- the number of rows returned by the select query
-
selectCountV2
public static int selectCountV2(org.apache.cayenne.access.DataContext context, org.apache.cayenne.query.SelectQuery query, org.apache.cayenne.access.DataNode node)
Modifies a query to select the number of results instead. This also works on queries with one to many joins that use distinct. IMPORTANT: SQL Server will not be able to execute the count query if the original query contains unaggregated fields in an ORDER BY clause. Do not use sorting in the query.- Parameters:
context
- the data context for the applicationquery
- a SelectQuery that does not use sortingnode
- data node for the application- Returns:
- the number of rows returned by the select query
- Since:
- 18.11.0
-
retrieveIntegerAttribute
public static Integer retrieveIntegerAttribute(org.apache.cayenne.DataRow dataRow, String attributeName)
Returns an integer attribute from a data row.- Parameters:
dataRow
- data row objectattributeName
- the attribute name to retrieve- Returns:
- the integer value if the attribute is an number, otherwise the method returns null
-
retrieveDoubleAttribute
public static Double retrieveDoubleAttribute(org.apache.cayenne.DataRow dataRow, String attributeName)
Returns an double attribute from a data row.- Parameters:
dataRow
- data row objectattributeName
- the attribute name to retrieve- Returns:
- the double value if the attribute is an number, otherwise the method returns null
- Since:
- 4.1.0
-
retrieveLongAttribute
public static Long retrieveLongAttribute(org.apache.cayenne.DataRow dataRow, String attributeName)
Returns a long attribute from a data row.- Parameters:
dataRow
- data row objectattributeName
- the attribute name to retrieve- Returns:
- the long value if the attribute is an number, otherwise the method returns null
-
retrieveBooleanAttribute
public static boolean retrieveBooleanAttribute(org.apache.cayenne.DataRow dataRow, String attributeName)
Returns a boolean attribute from a data row.- Parameters:
dataRow
- data row objectattributeName
- the attribute name to retrieve- Returns:
- the boolean value if the attribute is a boolean, otherwise the method returns null
-
getTrimmedPropertyValue
public static String getTrimmedPropertyValue(Class dataObjectClass, String propertyName, String propertyValue)
Calculate and return the trimmed value for the specified propertyValue. The maximum length of the DB column underlying the property determines the trimmed length.- Parameters:
dataObjectClass
- the data object class (required)propertyName
- the data object property name (required)propertyValue
- the property value to set, can be null- Returns:
- the trimmed property value
- Since:
- 4.0.0
-
setPropertyValueTrimmed
public static void setPropertyValueTrimmed(org.apache.cayenne.DataObject dataObject, String propertyName, String propertyValue)
Set the String property value on the given dataObject. If the property value is too long for the database column, its value will be trimmed.- Parameters:
dataObject
- the data object to set the property on (required)propertyName
- the data object property name (required)propertyValue
- the property value to set, can be null- Since:
- 3.6.0
-
getColumnMaxLength
public static int getColumnMaxLength(Class entityClass, String objAttributeName)
Return the maximum length of an entity attribute- Parameters:
entityClass
- the entity classobjAttributeName
- the name of the object attribute- Returns:
- the maximum length of the column attribute associated with the given entity attribute
-
ensureOnObjectContext
public static Object ensureOnObjectContext(org.apache.cayenne.DataObject entity, org.apache.cayenne.ObjectContext objectContext)
Ensure the given data object in in the specified objectContext, otherwise refetch the entity on the specified objectContext.- Parameters:
entity
- the entity to checkobjectContext
- the object context- Returns:
- the entity on the same object context
- Since:
- 4.1.0
-
performDelete
public static int performDelete(Class entityClass, String sqlQualifier, Map<String,Object> params)
Perform a SQL delete query on the given entity for the specified SQL and parameter.- Parameters:
entityClass
- the entity class to delete (required)sqlQualifier
- SQL delete qualifier (required)params
- the sql qualifer parameters (required)- Returns:
- the number of rows deleted
- Since:
- 4.3.4
-
applyMultiplePredicates
public static void applyMultiplePredicates(org.apache.cayenne.query.SelectQuery query, Map<String,Set<String>> aliasPaths, String dbPath, Set<String> valueSet)
Applies multiple predicates to the Cayenne Select Query storing any generated aliases in aliasPaths for the dbPath matching all of the passed values- Parameters:
query
- Cayenne SelectQueryaliasPaths
- generated alias paths to be added back into the Select QuerydbPath
- Cayenne database pathvalueSet
- values to all be ANDed- Since:
- 19.05
-
applyMultipleNameValuePredicates
public static void applyMultipleNameValuePredicates(org.apache.cayenne.query.SelectQuery query, Map<String,Set<String>> aliasPaths, String dbPath, String dbNamePath, String dbValuePath, Map<String,String> valueSet)
Applies multiple name/value predicates to the Cayenne Select Query storing any generated aliases in aliasPaths for the dbPath matching all of the passed values- Parameters:
query
- Cayenne SelectQueryaliasPaths
- generated alias paths to be added back into the Select QuerydbPath
- Cayenne database pathdbNamePath
- Cayenne column path to the NamedbValuePath
- Cayenne column path to the ValuevalueSet
- values to all be ANDed- Since:
- 19.05
-
selectTranslated
public static Object selectTranslated(org.apache.cayenne.access.DataContext dataContext, org.apache.cayenne.query.SelectQuery query, org.apache.cayenne.access.translator.select.SelectTranslator translator, boolean isCountQuery, Class resultEntityClass)
Perform a SelectQuery using a SelectTranslator. The resultset is either treated as the result of a SELECT COUNT (CountTranslator, CountTranslatorV2, SelectQueryJoinTranslator with useCountQuery) and the count is returned as a Number, or the list of results is returned (either as plain DataRows, or, if resultEntityClass is set, as that entity) Note that this method currently does NOT support Cayenne's "suppress DISTINCT" logic.- Parameters:
dataContext
- the data context (required)query
- the select query (required)translator
- the select translator to translate the query with (required)isCountQuery
- if set, the query is using a SELECT COUNT expression and the result set is assumed to contain a single number rather than a list of DataRowsresultEntityClass
- the entity class to marshal rows in the query resultset to (optional)- Returns:
- the query result, either (1) the count if the query is a SELECT COUNT or (2) the list of DataRows if entityClass is null or (3) the list of entities marshalled from the resultset
- Since:
- 19.5.0
-
runInTransactionAndUnwrap
public static <T> T runInTransactionAndUnwrap(org.apache.cayenne.tx.TransactionalOperation<T> op) throws Throwable
Run the provided operation wrapped in a single transaction. Based onServerRuntime.performInTransaction(TransactionalOperation)
This method will automatically unwrap CayenneRuntimeExceptions thrown and rethrow their cause.- Type Parameters:
T
- the result type- Parameters:
op
- the code to run in a transaction- Returns:
- the result of the transactional operation
- Throws:
Throwable
- Since:
- 19.5.1
-
-