Class RDF4JCRUDDao<ENTITY,​INPUT,​ID>

  • Type Parameters:
    ENTITY -
    INPUT -
    ID -
    Direct Known Subclasses:
    SimpleRDF4JCRUDDao

    public abstract class RDF4JCRUDDao<ENTITY,​INPUT,​ID>
    extends RDF4JDao
    Base class for DAOs providing CRUD functionality. The class allows for entities to be represented with different classes for read (ENTITY type) vs write (INPUT type) operations. DAOs that do not require this distinction must use the same class for both parameters.
    Since:
    4.0.0
    Author:
    Florian Kleedorfer
    • Constructor Detail

      • RDF4JCRUDDao

        public RDF4JCRUDDao​(RDF4JTemplate rdf4JTemplate,
                            Class<ID> idClass)
        Constructor that provides the type of the ID to the base implementation. This constructor has to be used if the ID is anything but IRI.
      • RDF4JCRUDDao

        public RDF4JCRUDDao​(RDF4JTemplate rdf4JTemplate)
        Constructor to be used by implementations that use IRI for the ID type.
    • Method Detail

      • saveAndReturnId

        public ID saveAndReturnId​(INPUT input)
      • saveAndReturnId

        public ID saveAndReturnId​(INPUT input,
                                  ID id)
        Saves the entity and returns its (possibly newly generated) ID.
        Parameters:
        input - the entity
        id - the id or null for a new entity.
        Returns:
        the id (a newly generated one if the specified id is null, otherwise just id.
      • deleteForUpdate

        protected void deleteForUpdate​(ID id)
        When updating an entity via save(Object), its triples are removed first using this method. The default implementation used RDF4JTemplate.deleteTriplesWithSubject(IRI). If more complex deletion behaviour (e.g. cascading) is needed, this method should be overriden.
      • convertIdToIri

        protected IRI convertIdToIri​(ID id)
        Converts the provided id to an IRI. The default implementation only works for DAOs that use IRI ids.
        Parameters:
        id -
        Returns:
      • generateNewId

        protected ID generateNewId​(ID providedId)
        Generates a new id for an entity. The default implementation only works for IRI ids.
        Parameters:
        providedId -
        Returns:
        a new id.
      • getById

        public final ENTITY getById​(ID id)
        Obtains the entity with the specified id, throwing an exception if none is found.
        Parameters:
        id - the id
        Returns:
        the entity
        Throws:
        IncorrectResultSetSizeException - if no entity is found with the specified id
      • getByIdOptional

        public final Optional<ENTITY> getByIdOptional​(ID id)
        Obtains an optional entity with the specified id.
        Parameters:
        id - the id
        Returns:
        an Optional maybe containing the entity
      • delete

        public void delete​(ID id)
        Naive implementation using RDF4JTemplate.delete(IRI). DAOs that need more complex deletion behaviour (e.g. cascading) should override this method.
      • getReadQuery

        protected String getReadQuery()
        Returns the SPARQL string used to read an instance of T from the database. The base implementation will cache the query string, so implementations should not try to cache the query.
      • mapSolution

        protected ENTITY mapSolution​(BindingSet querySolution)
        Map one solution of the readQuery to the type of this DAO.
      • postProcessMappedSolution

        protected ENTITY postProcessMappedSolution​(ENTITY entity)
        Callback invoked after mapping a solution to an entity, allowing subclasses to modify the entity before returning it to the client.
      • postProcessUpdate

        protected void postProcessUpdate​(INPUT input,
                                         Map<String,​Value> bindings)
        Callback invoked after a successful insert/update.
      • getInsertSparql

        protected NamedSparqlSupplier getInsertSparql​(INPUT input)
        Returns the SPARQL string used to write an instance of T to the database. The instance to be inserted is passed to the function so implementations can decide which query to use based on the instance.
      • getUpdateSparql

        protected NamedSparqlSupplier getUpdateSparql​(INPUT input)
        Returns the SPARQL string used to update an instance of T in the database. The instance to be updated is passed to the function so implementations can decide which query to use based on the instance.
      • populateIdBindings

        protected abstract void populateIdBindings​(MutableBindings bindingsBuilder,
                                                   ID id)
        Binds the instance id to query variable(s).
      • populateBindingsForUpdate

        protected void populateBindingsForUpdate​(MutableBindings bindingsBuilder,
                                                 INPUT input)
        Sets the non-id bindings on for the write query such that the instance of type I is written to the database. ID bindings are set through populateIdBindings()
      • getInputId

        protected ID getInputId​(INPUT input)
        Obtains the id of the input instance or null if it is new (or a partially populated composite key).
      • newBindingsBuilder

        protected static BindingsBuilder newBindingsBuilder()
        Returns a new BindingsBuilder for your convenience.