Class AbstractSimpleDAO

  • All Implemented Interfaces:
    com.helger.commons.log.IHasConditionalLogger, IAutoSaveAware, IDAO

    @ThreadSafe
    public abstract class AbstractSimpleDAO
    extends AbstractDAO
    Base class for a simple DAO.
    Author:
    Philip Helger
    • Constructor Detail

      • AbstractSimpleDAO

        protected AbstractSimpleDAO​(@Nonnull
                                    com.helger.commons.io.relative.IFileRelativeIO aIO,
                                    @Nonnull
                                    Supplier<String> aFilenameProvider)
    • Method Detail

      • getIO

        @Nonnull
        protected final com.helger.commons.io.relative.IFileRelativeIO getIO()
        Returns:
        The file-relative IO as passed in the constructor. Never null.
      • getFilenameProvider

        @Nonnull
        public final Supplier<String> getFilenameProvider()
        Returns:
        The filename provider used internally to build filenames. Never null.
      • onInit

        @Nonnull
        @OverrideOnDemand
        protected com.helger.commons.state.EChange onInit()
        Custom initialization routine. Called only if the underlying file does not exist yet. This method is only called within a write lock!
        Returns:
        EChange.CHANGED if something was modified inside this method
      • onRead

        @Nonnull
        @MustBeLocked(WRITE)
        protected abstract com.helger.commons.state.EChange onRead​(@Nonnull
                                                                   com.helger.xml.microdom.IMicroDocument aDoc)
        Fill the internal structures with from the passed XML document. This method is only called within a write lock!
        Parameters:
        aDoc - The XML document to read from. Never null.
        Returns:
        EChange.CHANGED if reading the data changed something in the internal structures that requires a writing.
      • triggerExceptionHandlersRead

        protected static void triggerExceptionHandlersRead​(@Nonnull
                                                           Throwable t,
                                                           boolean bIsInitialization,
                                                           @Nullable
                                                           File aFile)
        Trigger the registered custom exception handlers for read errors.
        Parameters:
        t - Thrown exception. Never null.
        bIsInitialization - true if this happened during initialization of a new file, false if it happened during regular reading.
        aFile - The file that was read. May be null for in-memory DAOs.
      • initialRead

        @MustBeLocked(WRITE)
        protected final void initialRead()
                                  throws DAOException
        Call this method inside the constructor to read the file contents directly. This method is write locked!
        Throws:
        DAOException - in case initialization or reading failed!
      • onFilenameChange

        @OverrideOnDemand
        @MustBeLocked(WRITE)
        protected void onFilenameChange​(@Nullable
                                        String sPreviousFilename,
                                        @Nonnull
                                        String sNewFilename)
        Called after a successful write of the file, if the filename is different from the previous filename. This can e.g. be used to clear old data.
        Parameters:
        sPreviousFilename - The previous filename. May be null.
        sNewFilename - The new filename. Never null.
      • createWriteData

        @Nonnull
        @MustBeLocked(WRITE)
        protected abstract com.helger.xml.microdom.IMicroDocument createWriteData()
        Create the XML document that should be saved to the file. This method is only called within a write lock!
        Returns:
        The non-null document to write to the file.
      • modifyWriteData

        @OverrideOnDemand
        @MustBeLocked(WRITE)
        protected void modifyWriteData​(@Nonnull
                                       com.helger.xml.microdom.IMicroDocument aDoc)
        Modify the created document by e.g. adding some comment or digital signature or whatsoever.
        Parameters:
        aDoc - The created non-null document.
      • beforeWriteToFile

        @OverrideOnDemand
        @MustBeLocked(WRITE)
        protected void beforeWriteToFile​(@Nonnull
                                         String sFilename,
                                         @Nonnull
                                         File aFile)
        Optional callback method that is invoked before the file handle gets opened. This method can e.g. be used to create backups.
        Parameters:
        sFilename - The filename provided by the internal filename provider. Never null.
        aFile - The resolved file. It is already consistency checked. Never null.
      • getXMLWriterSettings

        @Nonnull
        @OverrideOnDemand
        protected com.helger.xml.serialize.write.IXMLWriterSettings getXMLWriterSettings()
        Returns:
        The IXMLWriterSettings to be used to serialize the data.
      • getLastFilename

        @Nullable
        public final String getLastFilename()
        Returns:
        The filename to which was written last. May be null if no wrote action was performed yet.
      • triggerExceptionHandlersWrite

        protected static void triggerExceptionHandlersWrite​(@Nonnull
                                                            Throwable t,
                                                            @Nonnull
                                                            String sErrorFilename,
                                                            @Nullable
                                                            com.helger.xml.microdom.IMicroDocument aDoc)
        Trigger the registered custom exception handlers for read errors.
        Parameters:
        t - Thrown exception. Never null.
        sErrorFilename - The filename tried to write to. Never null.
        aDoc - The XML content that should be written. May be null if the error occurred in XML creation.
      • markAsChanged

        @MustBeLocked(WRITE)
        protected final void markAsChanged()
        This method must be called every time something changed in the DAO. It triggers the writing to a file if auto-save is active. This method must be called within a write-lock as it is not locked!
      • writeToFileOnPendingChanges

        public final void writeToFileOnPendingChanges()
        In case there are pending changes write them to the file. This method is write locked!
      • getInitCount

        @Nonnegative
        public int getInitCount()
        Returns:
        The number of times this DAO was initialized. Always ≥ 0. Usually this field is not persistent and only is valid until the application ends.
      • getLastInitDateTime

        @Nullable
        public final LocalDateTime getLastInitDateTime()
        Returns:
        The last time this DAO was initialized (without error). May be null if it wasn't read before. Usually this field is not persistent and only is valid until the application ends.
      • getReadCount

        @Nonnegative
        public int getReadCount()
        Returns:
        The number of times this DAO was initialized. Always ≥ 0. Usually this field is not persistent and only is valid until the application ends.
      • getLastReadDateTime

        @Nullable
        public final LocalDateTime getLastReadDateTime()
        Returns:
        The last time this DAO was read (without error). May be null if it wasn't read before. Usually this field is not persistent and only is valid until the application ends.
      • getWriteCount

        @Nonnegative
        public int getWriteCount()
        Returns:
        The number of times this DAO was initialized. Always ≥ 0. Usually this field is not persistent and only is valid until the application ends.
      • getLastWriteDateTime

        @Nullable
        public final LocalDateTime getLastWriteDateTime()
        Returns:
        The last time this DAO was written (without error). May be null if it wasn't written before. Usually this field is not persistent and only is valid until the application ends.