java.lang.Object
org.tentackle.pdo.PdoUtilities
Utility methods for PDOs.
- Author:
- harald
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintdeleteCollection(Collection<? extends PersistentDomainObject<?>> pdos) Deletes a List of objects.intdeleteMissingInCollection(Collection<? extends PersistentDomainObject<?>> oldCollection, Collection<? extends PersistentDomainObject<?>> newCollection) Deletes all objects in oldList that are not in newList.
The method is handy for deleting cascaded composite lists.Class<?> determineServicedClass(Class<?> implementingClass) Determines the serviced class according to the annotation.Filters a collection of Objects, returning only those that are persistent domain objects.<T extends PersistentDomainObject<T>>
List<PdoMember> getAttributes(Class<T> clazz, boolean withRelations) Determines all member attributes of a PDO class.<T extends PersistentDomainObject<T>>
intgetClassId(Class<T> clazz) Gets the table name of given class.Gets a list of all class names.
Sorted by name.static PdoUtilitiesThe singleton.<T extends PersistentDomainObject<T>>
List<PdoMember> getMembers(Class<T> clazz) Determines all members of a PDO class.Gets the plural of given class.getSingular(Class<?> clazz) Gets the singular of given class.<T extends PersistentDomainObject<T>>
StringgetTableName(Class<T> clazz) Gets the table name of given class.String[]getTableNames(Class<?>... classes) Gets the table name of given classes.getTransactionRetryPolicy(String policyName) Gets the transaction retry policy.<U extends org.tentackle.misc.Identifiable>
UgetUser(DomainContext context) Gets the identifiable corresponding to the current user related to the session of a domain context.<U extends org.tentackle.misc.Identifiable>
UgetUser(DomainContext context, long userId) Gets the identifiable corresponding to the ID of a user.org.tentackle.misc.IdentifiableKey<PersistentDomainObject<?>> idStringToIdentifiableKey(String idString) Determines the identifiable key from a string created byPersistentObject.toIdString().booleanisCollectionModified(Collection<? extends PersistentDomainObject<?>> pdos) Checks whether some objects in the list are modified.
Useful for recursive optimizations.Adds a listener for a modification on given PDO classes.
This is just a convenience wrapper forPdoListenerto make use of lambdas.
Same aslisten(Consumer, Class[]), but for handlers that don't need to refer to theModificationEvent.listen(Consumer<ModificationEvent> handler, Class<?>... classes) Adds a listener for a modification on given PDO classes.
This is just a convenience wrapper forPdoListenerto make use of lambdas.lockExceptionToString(LockException lockException, PersistentDomainObject<?> pdo) Creates a localized string from aLockExceptionfor a PDO.intsaveCollection(Collection<? extends PersistentDomainObject<?>> pdos) Saves a collection of PDOs.intsaveCollection(Collection<? extends PersistentDomainObject<?>> pdos, boolean modifiedOnly) Saves a list of PDOs.
All objects with isPersistable() == true will be saved.intsaveCollection(Collection<? extends PersistentDomainObject<?>> pdos, boolean modifiedOnly, boolean usePersist) Saves a list of PDOs.
All objects with isPersistable() == true will be saved.voidunlisten(PdoListener listener) Unregisters a registered PDO listener.
-
Constructor Details
-
PdoUtilities
public PdoUtilities()Creates a utility instance.
-
-
Method Details
-
getInstance
The singleton.- Returns:
- the singleton
-
getSingular
Gets the singular of given class.- Parameters:
clazz- the class- Returns:
- the singular, null if not annotated with @Singular
-
getPlural
Gets the plural of given class.- Parameters:
clazz- the class- Returns:
- the plural, null if not annotated with @Plural
-
getClassNames
Gets a list of all class names.
Sorted by name.- Returns:
- the class names
-
getTableName
Gets the table name of given class.- Type Parameters:
T- the pdo type- Parameters:
clazz- the pdo class- Returns:
- the tablename, null if no tablename
-
getClassId
Gets the table name of given class.- Type Parameters:
T- the pdo type- Parameters:
clazz- the pdo class- Returns:
- the class id, 0 if no class id
-
getTableNames
Gets the table name of given classes.- Parameters:
classes- the PDO classes- Returns:
- the names, null if classes is null or empty
-
determineServicedClass
Determines the serviced class according to the annotation.- Parameters:
implementingClass- the implementing class- Returns:
- the serviced class, null if no annotation found
- See Also:
-
filterPersistentDomainObjects
Filters a collection of Objects, returning only those that are persistent domain objects.- Parameters:
col- the collection of objects- Returns:
- a list of persistent domain objects, the rest is ignored. Never null.
-
deleteCollection
Deletes a List of objects. Virgin objects are not deleted.- Parameters:
pdos- the list of object to delete- Returns:
- the number of objects deleted, -1 if error (some object wasn't deleted)
-
deleteMissingInCollection
public int deleteMissingInCollection(Collection<? extends PersistentDomainObject<?>> oldCollection, Collection<? extends PersistentDomainObject<?>> newCollection) Deletes all objects in oldList that are not in newList.
The method is handy for deleting cascaded composite lists.- Parameters:
oldCollection- the list of objects stored in dbnewCollection- the new list of objects- Returns:
- the number of objects deleted, -1 if some error
-
isCollectionModified
Checks whether some objects in the list are modified.
Useful for recursive optimizations.- Parameters:
pdos- the objects- Returns:
- true if modified
-
saveCollection
public int saveCollection(Collection<? extends PersistentDomainObject<?>> pdos, boolean modifiedOnly, boolean usePersist) Saves a list of PDOs.
All objects with isPersistable() == true will be saved. If modifiedOnly is true only isModified() objects will be saved. All objects with isPersistable() == false and isNew() == false are removed! By definition, aTrackedListmust *NOT* contain untracked objects. The errorhandler will be invoked if such an object is detected. This is a quality measure to ensure code consistency. The wurblets automatically take care of that.- Parameters:
pdos- the list to savemodifiedOnly- is true if only modified objects are savedusePersist- true to use persist() instead of save() and replace objects in collection- Returns:
- the number of objects saved, -1 if some error
-
saveCollection
public int saveCollection(Collection<? extends PersistentDomainObject<?>> pdos, boolean modifiedOnly) Saves a list of PDOs.
All objects with isPersistable() == true will be saved. If modifiedOnly is true only isModified() objects will be saved. All objects with isPersistable() == false and isNew() == false are removed! By definition, aTrackedListmust *NOT* contain untracked objects. The errorhandler will be invoked if such an object is detected. This is a quality measure to ensure code consistency. The wurblets automatically take care of that.- Parameters:
pdos- the list to savemodifiedOnly- is true if only modified objects are saved- Returns:
- the number of objects saved, -1 if some error
-
saveCollection
Saves a collection of PDOs.- Parameters:
pdos- the collection of PDOs- Returns:
- the number of objects saved, -1 if some error
-
idStringToIdentifiableKey
public org.tentackle.misc.IdentifiableKey<PersistentDomainObject<?>> idStringToIdentifiableKey(String idString) Determines the identifiable key from a string created byPersistentObject.toIdString().- Parameters:
idString- the id string "classid:objectid"- Returns:
- the key
- Throws:
PersistenceException- if not an id string or invalid class id
-
getMembers
Determines all members of a PDO class.- Type Parameters:
T- the PDO type- Parameters:
clazz- the PDO class- Returns:
- the members sorted by the model's ordinal
-
getAttributes
public <T extends PersistentDomainObject<T>> List<PdoMember> getAttributes(Class<T> clazz, boolean withRelations) Determines all member attributes of a PDO class.- Type Parameters:
T- the PDO type- Parameters:
clazz- the PDO classwithRelations- true if include non-component object relations- Returns:
- the attributes sorted by the model's ordinal
-
listen
Adds a listener for a modification on given PDO classes.
This is just a convenience wrapper forPdoListenerto make use of lambdas.- Parameters:
handler- the handler of theModificationEventclasses- the PDO classes- Returns:
- the registered PDO listener
-
listen
Adds a listener for a modification on given PDO classes.
This is just a convenience wrapper forPdoListenerto make use of lambdas.
Same aslisten(Consumer, Class[]), but for handlers that don't need to refer to theModificationEvent.- Parameters:
handler- the handler to be invokedclasses- the PDO classes- Returns:
- the registered PDO listener
-
unlisten
Unregisters a registered PDO listener.- Parameters:
listener- the listener to remove
-
getTransactionRetryPolicy
Gets the transaction retry policy.- Parameters:
policyName- the name of the policy, empty string or null for default- Returns:
- the policy, never null
-
lockExceptionToString
Creates a localized string from aLockExceptionfor a PDO.- Parameters:
lockException- the lock exceptionpdo- the PDO- Returns:
- the message
-
getUser
Gets the identifiable corresponding to the ID of a user.- Parameters:
context- the domain contextuserId- the ID of the identifiable (user)- Returns:
- the user, null if session not attached to a user (background server thread, for example) or no running
Application
-
getUser
Gets the identifiable corresponding to the current user related to the session of a domain context.- Parameters:
context- the domain context- Returns:
- the user, null if session not attached to a user (background server thread, for example) or no running
Application
-