Package io.permazen

Class ExportContext

java.lang.Object
io.permazen.ExportContext

public class ExportContext extends Object
Context for exporting plain (POJO) objects from a PermazenTransaction.

Plain objects (POJO's) can be exported from a PermazenTransaction to the extent that the Permazen model class and the corresponding target POJO class share the same properties. The simplest example of this is when the Permazen model class is also the POJO class (implying a non-abstract class; see also @PermazenType.autogenNonAbstract()). Also possible are POJO classes and model classes that implement common Java interfaces.

The POJO corresponding to an exported database object is supplied by the configured objectMapper. If objectMapper returns null, the database object is not exported, and nulls replace any copied references to it. If objectMapper is null, the default behavior is to create a new POJO using the model class' default constructor, which of course implies the model class cannot be abstract.

Instances ensure that an already-exported database object will be recognized and not exported twice. The objectMapper is invoked at most once for any object ID.

When a database objext is exported, its fields are copied to the POJO. Fields for which no corresponding POJO property exists are omitted.

Reference fields are traversed and the referenced objects are automatically exported as POJO's, recursively. In other words, the entire transitive closure of objects reachable from an exported object is exported. Cycles in the graph of references are handled properly.

Conversion Details

Counter fields export to any Number property. Collection fields export to an existing collection, so that a setter method is not required; however, if the getter returns null, a setter is required and the export will attempt to use an appropriate collection class (HashSet for property of type Set, TreeSet for a property of type SortedSet, etc). To avoid potential mismatch with collection types, initialize collection properties.
See Also:
  • Constructor Details

    • ExportContext

      public ExportContext(PermazenTransaction ptx)
      Constructor.

      Uses a default objectMapper that creates new exported objects using the default constructor of the model class.

      Parameters:
      ptx - the transaction from which to export objects
      Throws:
      IllegalArgumentException - if ptx is null
    • ExportContext

      public ExportContext(PermazenTransaction ptx, Function<ObjId,Object> objectMapper)
      Constructor.
      Parameters:
      ptx - the transaction from which to export objects
      objectMapper - function returning the POJO used to export a database object (or null to skip the corresponding object)
      Throws:
      IllegalArgumentException - if either parameter is null
  • Method Details

    • getPermazenTransaction

      public PermazenTransaction getPermazenTransaction()
      Get the transaction from which objects are exported.
      Returns:
      associated transaction
    • getJObjectMap

      public Map<ObjId,Object> getJObjectMap()
      Get the mapping from already exported database object to the corresponding POJO.
      Returns:
      mapping from exported database object ID to corresponding POJO
    • exportPlain

      public Object exportPlain(PermazenObject pobj)
      Export a PermazenObject as a plain Java object, along with all other objects reachable from it via copied reference fields.

      Equivalent to exportPlain(pobj.getObjId()).

      Parameters:
      pobj - object to export; must not be null
      Returns:
      exported object, or null if the objectMapper returned null for pobj.getObjId()
      Throws:
      DeletedObjectException - if id refers to an object that does not exist in the transaction associated with this instance
      TypeNotInSchemaException - if pobj is an UntypedPermazenObject
      IllegalArgumentException - if pobj is null
    • exportPlain

      public Object exportPlain(ObjId id)
      Export the PermazenObject with the given ObjId as a plain Java object, along with all other objects reachable from it via copied reference fields.

      If the PermazenObject has already been exported, the previously returned Object is returned.

      Parameters:
      id - object ID of the object to export; must not be null
      Returns:
      exported object, or null if the objectMapper returned null for id
      Throws:
      DeletedObjectException - if id refers to an object that does not exist in the transaction associated with this instance
      TypeNotInSchemaException - if id refers to a type that does not exist in this instance's transaction's schema
      IllegalArgumentException - if id is null