Class CrudAggregateRepository<T,​K extends Serializable>

  • Type Parameters:
    T - @Aggregate class.
    K - @AggregateIdentifier class.
    All Implemented Interfaces:
    AggregateRepository<T,​K>

    public abstract class CrudAggregateRepository<T,​K extends Serializable>
    extends Object
    implements AggregateRepository<T,​K>
    Repository implementation to store @Aggregate as documents in databases with JSON support.
    Author:
    Thiago A. de Souza Weber
    • Constructor Detail

      • CrudAggregateRepository

        protected CrudAggregateRepository​(javax.persistence.EntityManager em,
                                          com.fasterxml.jackson.databind.ObjectMapper mapper)
    • Method Detail

      • getTableName

        @Deprecated(since="2.0.0-RELEASE",
                    forRemoval=true)
        protected String getTableName()
        Deprecated, for removal: This API element is subject to removal in a future version.
        in favor of Table annotation
        Checks if show SQL is enabled.
        Returns:
        the @Aggregate table name
      • isShowSqlEnabled

        protected boolean isShowSqlEnabled()
        Checks if show SQL is enabled.
        Returns:
        true if show SQL is enabled, otherwise false.
      • getSession

        protected org.hibernate.Session getSession()
        Retrieve the current Session from the EntityManager.
        Returns:
        the current Session.
        See Also:
        EntityManager.unwrap(Class), Session
      • getObjectMapper

        protected com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
        Retrieves the ObjectMapper to be used to serialize and deserialize the @Aggregate.
        Returns:
        the ObjectMapper
      • configureObjectMapper

        protected com.fasterxml.jackson.databind.ObjectMapper configureObjectMapper​(com.fasterxml.jackson.databind.ObjectMapper mapper)
        Configure the ObjectMapper for serialize and deserialize the @Aggregate.
        The standard configuration stays as:
        • Only non nulls (see JsonInclude.Include.NON_NULL for more information).
        • Default typing specified by AggregateTypeResolverBuilder.
        • Write BigDecimal as plain (see JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN for more information).
        • Disable date as timestamps (see SerializationFeature.WRITE_DATES_AS_TIMESTAMPS for more information).
        • Disable the time zone adjustment when deserialize dates (see DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE for more information).
        • Serialize only fields (see PropertyAccessor.FIELD for more information).
        Parameters:
        mapper - to be configured
        Returns:
        the configured ObjectMapper
      • serialize

        protected String serialize​(@Nullable
                                   T aggregate)
                            throws com.fasterxml.jackson.core.JsonProcessingException
        Serialize the given @Aggregate to a json.
        Parameters:
        aggregate - instance to be serialized
        Returns:
        @Aggregate as json string.
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException
        See Also:
        ObjectMapper.writeValueAsString(Object)
      • deserialize

        protected T deserialize​(byte[] stream)
                         throws IOException
        Deserialize the given data stream (JSON string) to an @Aggregate instance.
        Parameters:
        stream - of JSON string
        Returns:
        an @Aggregate instance.
        Throws:
        IOException
        See Also:
        ObjectMapper.readValue(com.fasterxml.jackson.core.JsonParser, java.lang.Class<T>)
      • getAggregateAsDatabaseParameter

        protected Object getAggregateAsDatabaseParameter​(String dbName,
                                                         String json)
                                                  throws SQLException
        Build the correct database parameter type to store the serialized @Aggregate instance based on the database name.
        Parameters:
        dbName - the database name.
        json - the serialized @Aggregate instance.
        Returns:
        the corrected parameter type to database.
        Throws:
        SQLException
      • getDataFromResultSet

        protected byte[] getDataFromResultSet​(String dbName,
                                              ResultSet result)
                                       throws SQLException
        Read the ResultSet data parameter based on the correct database type.
        Parameters:
        dbName - the database name.
        result - the ResultSet from the query.
        Returns:
        the data to be deserialized.
        Throws:
        SQLException
      • exists

        @Transactional(readOnly=true)
        public boolean exists​(K identifier)
        Description copied from interface: AggregateRepository
        Checks if the @Aggregate exists with the given identifier.
        Specified by:
        exists in interface AggregateRepository<T,​K extends Serializable>
        Parameters:
        identifier - can't be null.
        Returns:
        true if exists, otherwise false.
      • exists

        @Transactional(readOnly=true)
        public boolean exists​(String clause,
                              Object... arguments)
        Description copied from interface: AggregateRepository
        Checks if, at least one, @Aggregate exists matching the given clause and arguments.
        Specified by:
        exists in interface AggregateRepository<T,​K extends Serializable>
        Parameters:
        clause - can be null.
        arguments - the arguments to be used for lookup, can be null.
        Returns:
        true if found at least one, otherwise false.
      • exists

        @Transactional(readOnly=true)
        public boolean exists​(@Nullable
                              String clause,
                              org.springframework.jdbc.core.SqlParameterValue... arguments)
        Description copied from interface: AggregateRepository
        Checks if, at least one, @Aggregate exists matching the given clause and arguments.
        Specified by:
        exists in interface AggregateRepository<T,​K extends Serializable>
        Parameters:
        clause - can be null.
        arguments - the arguments to be used for lookup, can be null.
        Returns:
        true if found at least one, otherwise false.
      • findOne

        @Transactional(readOnly=true)
        public Optional<T> findOne​(@Nullable
                                   String clause,
                                   org.springframework.jdbc.core.SqlParameterValue... arguments)
        Description copied from interface: AggregateRepository
        Returns a single @Aggregate, matching the given clause and arguments or Optional.empty() if none found.
        Specified by:
        findOne in interface AggregateRepository<T,​K extends Serializable>
        Parameters:
        clause - can be null.
        arguments - the arguments to be used for lookup, can be null.
        Returns:
        an Optional instance with the @Aggregate if found any, otherwise Optional.empty().
      • findOne

        public Optional<T> findOne​(@Nullable
                                   String clause,
                                   javax.persistence.LockModeType lockModeType,
                                   org.springframework.jdbc.core.SqlParameterValue... arguments)
        Description copied from interface: AggregateRepository
        Returns a single @Aggregate, matching the given clause and arguments or Optional.empty() if none found.
        Specified by:
        findOne in interface AggregateRepository<T,​K extends Serializable>
        Parameters:
        clause - can be null.
        lockModeType - pass lock mode type if necessary.
        arguments - the arguments to be used for lookup, can be null.
        Returns:
        an Optional instance with the @Aggregate if found any, otherwise Optional.empty().
      • findAll

        @Transactional(readOnly=true)
        public com.totvs.tjf.core.aggregate.repository.AggregateCollectionResult<T> findAll​(@Nullable
                                                                                            String clause,
                                                                                            @Nullable
                                                                                            String orderby,
                                                                                            @Nullable
                                                                                            Integer limit,
                                                                                            @Nullable
                                                                                            Integer offset,
                                                                                            org.springframework.jdbc.core.SqlParameterValue... arguments)
        Description copied from interface: AggregateRepository
        Returns all @Aggregates, matching the given clause and arguments or Collections.emptyList() if none found.
        Specified by:
        findAll in interface AggregateRepository<T,​K extends Serializable>
        Parameters:
        clause - can be null.
        orderby - can be null.
        limit - can be null.
        offset - can be null.
        arguments - the arguments to be used for lookup, can be null.
        Returns:
        a AggregateCollectionResult instance with all the @Aggregate found, otherwise items Collections.emptyList().
      • findAll

        public Collection<T> findAll​(@Nullable
                                     String clause,
                                     javax.persistence.LockModeType lockModeType,
                                     @Nullable
                                     String orderby,
                                     org.springframework.jdbc.core.SqlParameterValue... arguments)
        Description copied from interface: AggregateRepository
        Returns all @Aggregates, matching the given clause and arguments or Collections.emptyList() if none found.
        Specified by:
        findAll in interface AggregateRepository<T,​K extends Serializable>
        Parameters:
        clause - can be null.
        lockModeType - pass lock mode type if necessary.
        orderby - can be null.
        arguments - the arguments to be used for lookup, can be null.
        Returns:
        a Collection instance with all the @Aggregate found, otherwise Collections.emptyList().
      • count

        public Long count​(@Nullable
                          String clause,
                          @Nullable
                          org.springframework.jdbc.core.SqlParameterValue... arguments)
        Description copied from interface: AggregateRepository
        Returns total registers of a table, matching the given clause and arguments or 0 if none found.
        Specified by:
        count in interface AggregateRepository<T,​K extends Serializable>
        Parameters:
        clause - can be null.
        arguments - the arguments to be used for lookup, can be null.
        Returns:
        total registers of a table if found any, otherwise 0.
      • getAggregate

        protected Optional<T> getAggregate​(Connection connection,
                                           String clause,
                                           javax.persistence.LockModeType lockModeType,
                                           org.springframework.jdbc.core.SqlParameterValue... arguments)
                                    throws SQLException
        Returns a single @Aggregate, matching the given clause and arguments or Optional.empty() if none found.
        Parameters:
        connection - connection
        clause - can be null.
        lockModeType - pass lock mode type, can be PESSIMISTIC_READ or NONE.
        arguments - the arguments to be used for lookup, can be null.
        Returns:
        an Optional instance with the @Aggregate if found any, otherwise Optional.empty().
        Throws:
        SQLException
      • getAggregates

        protected com.totvs.tjf.core.aggregate.repository.AggregateCollectionResult<T> getAggregates​(Connection connection,
                                                                                                     String clause,
                                                                                                     @Nullable
                                                                                                     javax.persistence.LockModeType lockModeType,
                                                                                                     @Nullable
                                                                                                     String orderby,
                                                                                                     Integer limit,
                                                                                                     Integer offset,
                                                                                                     org.springframework.jdbc.core.SqlParameterValue... arguments)
                                                                                              throws SQLException
        Returns all @Aggregates, matching the given clause and arguments or Collections.emptyList() if none found.
        Parameters:
        connection - connection
        clause - can be null.
        lockModeType - pass lock mode type if necessary, can be PESSIMISTIC_READ or NONE.
        orderby - pass order by definition if necessary.
        limit - limit of query rows
        offset - result starting at row
        arguments - the arguments to be used for lookup, can be null.
        Returns:
        Collection instance with all the @Aggregate found, otherwise Collections.emptyList().
        Throws:
        SQLException
      • insert

        @Transactional
        public T insert​(T aggregate)
        Description copied from interface: AggregateRepository
        Serialize the given @Aggregate state and store at the database as a document.
        Specified by:
        insert in interface AggregateRepository<T,​K extends Serializable>
        Parameters:
        aggregate - instance to be created
        Returns:
        the saved aggregate
      • update

        @Transactional
        public T update​(T aggregate)
        Description copied from interface: AggregateRepository
        Serialize the given @Aggregate state and update document in the database .
        Specified by:
        update in interface AggregateRepository<T,​K extends Serializable>
        Parameters:
        aggregate - instance to be store
        Returns:
        the modified aggregate
      • getCount

        protected long getCount​(Connection connection,
                                @Nullable
                                String clause,
                                @Nullable
                                org.springframework.jdbc.core.SqlParameterValue... arguments)
                         throws SQLException
        Throws:
        SQLException
      • convertObjectToSqlParameterValue

        protected org.springframework.jdbc.core.SqlParameterValue[] convertObjectToSqlParameterValue​(Object... arguments)
      • getPublisher

        public org.springframework.context.ApplicationEventPublisher getPublisher()
      • setPublisher

        public void setPublisher​(org.springframework.context.ApplicationEventPublisher publisher)