Class PdoUtilities

java.lang.Object
org.tentackle.pdo.PdoUtilities

@Service(PdoUtilities.class) public class PdoUtilities extends Object
Utility methods for PDOs.
Author:
harald
  • Constructor Details

    • PdoUtilities

      public PdoUtilities()
      Creates a utility instance.
  • Method Details

    • getInstance

      public static PdoUtilities getInstance()
      The singleton.
      Returns:
      the singleton
    • getSingular

      public String getSingular(Class<?> clazz)
      Gets the singular of given class.
      Parameters:
      clazz - the class
      Returns:
      the singular, null if not annotated with @Singular
    • getPlural

      public String getPlural(Class<?> clazz)
      Gets the plural of given class.
      Parameters:
      clazz - the class
      Returns:
      the plural, null if not annotated with @Plural
    • getClassNames

      public Collection<String> getClassNames()
      Gets a list of all class names.
      Sorted by name.
      Returns:
      the class names
    • getTableName

      public <T extends PersistentDomainObject<T>> String getTableName(Class<T> clazz)
      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

      public <T extends PersistentDomainObject<T>> int getClassId(Class<T> clazz)
      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

      public String[] getTableNames(Class<?>... classes)
      Gets the table name of given classes.
      Parameters:
      classes - the PDO classes
      Returns:
      the names, null if classes is null or empty
    • determineServicedClass

      public Class<?> determineServicedClass(Class<?> implementingClass)
      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

      public List<PersistentDomainObject<?>> filterPersistentDomainObjects(Collection<?> col)
      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

      public int deleteCollection(Collection<? extends PersistentDomainObject<?>> pdos)
      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 db
      newCollection - the new list of objects
      Returns:
      the number of objects deleted, -1 if some error
    • isCollectionModified

      public boolean isCollectionModified(Collection<? extends PersistentDomainObject<?>> pdos)
      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, a TrackedList must *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 save
      modifiedOnly - is true if only modified objects are saved
      usePersist - 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, a TrackedList must *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 save
      modifiedOnly - is true if only modified objects are saved
      Returns:
      the number of objects saved, -1 if some error
    • saveCollection

      public int saveCollection(Collection<? extends PersistentDomainObject<?>> pdos)
      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 by PersistentObject.toIdString().
      Parameters:
      idString - the id string "classid:objectid"
      Returns:
      the key
      Throws:
      PersistenceException - if not an id string or invalid class id
    • getMembers

      public <T extends PersistentDomainObject<T>> List<PdoMember> getMembers(Class<T> clazz)
      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 class
      withRelations - true if include non-component object relations
      Returns:
      the attributes sorted by the model's ordinal
    • listen

      public PdoListener listen(Consumer<ModificationEvent> handler, Class<?>... classes)
      Adds a listener for a modification on given PDO classes.
      This is just a convenience wrapper for PdoListener to make use of lambdas.
      Parameters:
      handler - the handler of the ModificationEvent
      classes - the PDO classes
      Returns:
      the registered PDO listener
    • listen

      public PdoListener listen(Runnable handler, Class<?>... classes)
      Adds a listener for a modification on given PDO classes.
      This is just a convenience wrapper for PdoListener to make use of lambdas.
      Same as listen(Consumer, Class[]), but for handlers that don't need to refer to the ModificationEvent.
      Parameters:
      handler - the handler to be invoked
      classes - the PDO classes
      Returns:
      the registered PDO listener
    • unlisten

      public void unlisten(PdoListener listener)
      Unregisters a registered PDO listener.
      Parameters:
      listener - the listener to remove
    • getTransactionRetryPolicy

      public TransactionRetryPolicy getTransactionRetryPolicy(String policyName)
      Gets the transaction retry policy.
      Parameters:
      policyName - the name of the policy, empty string or null for default
      Returns:
      the policy, never null
    • lockExceptionToString

      public String lockExceptionToString(LockException lockException, PersistentDomainObject<?> pdo)
      Creates a localized string from a LockException for a PDO.
      Parameters:
      lockException - the lock exception
      pdo - the PDO
      Returns:
      the message
    • getUser

      public <U extends org.tentackle.misc.Identifiable> U getUser(DomainContext context, long userId)
      Gets the identifiable corresponding to the ID of a user.
      Parameters:
      context - the domain context
      userId - 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

      public <U extends org.tentackle.misc.Identifiable> U getUser(DomainContext context)
      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