Package com.helger.dao.simple
Class AbstractSimpleDAO
- java.lang.Object
-
- com.helger.dao.AbstractDAO
-
- com.helger.dao.simple.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
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.helger.dao.IDAO
IDAO.EMode
-
-
Field Summary
-
Fields inherited from class com.helger.dao.AbstractDAO
CONDLOG, DEFAULT_AUTO_SAVE_ENABLED, FILENAME_EXTENSION_NEW, FILENAME_EXTENSION_PREV, m_aRWLock, RW_LOCK
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractSimpleDAO(com.helger.commons.io.relative.IFileRelativeIO aIO, Supplier<String> aFilenameProvider)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidbeforeWriteToFile(String sFilename, File aFile)Optional callback method that is invoked before the file handle gets opened.protected abstract com.helger.xml.microdom.IMicroDocumentcreateWriteData()Create the XML document that should be saved to the file.Supplier<String>getFilenameProvider()intgetInitCount()protected com.helger.commons.io.relative.IFileRelativeIOgetIO()StringgetLastFilename()LocalDateTimegetLastInitDateTime()LocalDateTimegetLastReadDateTime()LocalDateTimegetLastWriteDateTime()intgetReadCount()protected FilegetSafeFile(String sFilename, IDAO.EMode eMode)intgetWriteCount()protected com.helger.xml.serialize.write.IXMLWriterSettingsgetXMLWriterSettings()protected voidinitialRead()Call this method inside the constructor to read the file contents directly.protected voidmarkAsChanged()This method must be called every time something changed in the DAO.protected voidmodifyWriteData(com.helger.xml.microdom.IMicroDocument aDoc)Modify the created document by e.g. adding some comment or digital signature or whatsoever.protected voidonFilenameChange(String sPreviousFilename, String sNewFilename)Called after a successful write of the file, if the filename is different from the previous filename.protected com.helger.commons.state.EChangeonInit()Custom initialization routine.protected abstract com.helger.commons.state.EChangeonRead(com.helger.xml.microdom.IMicroDocument aDoc)Fill the internal structures with from the passed XML document.StringtoString()protected static voidtriggerExceptionHandlersRead(Throwable t, boolean bIsInitialization, File aFile)Trigger the registered custom exception handlers for read errors.protected static voidtriggerExceptionHandlersWrite(Throwable t, String sErrorFilename, com.helger.xml.microdom.IMicroDocument aDoc)Trigger the registered custom exception handlers for read errors.voidwriteToFileOnPendingChanges()In case there are pending changes write them to the file.-
Methods inherited from class com.helger.dao.AbstractDAO
beginWithoutAutoSave, checkFileAccess, endWithoutAutoSave, exceptionHandlersRead, exceptionHandlersWrite, hasPendingChanges, internalHasPendingChanges, internalIsAutoSaveEnabled, internalSetPendingChanges, isAutoSaveEnabled, isSilentMode, setSilentMode
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.helger.dao.IAutoSaveAware
performWithoutAutoSave, performWithoutAutoSave, performWithoutAutoSaveThrowing, performWithoutAutoSaveThrowing
-
Methods inherited from interface com.helger.dao.IDAO
isReloadable, reload
-
-
-
-
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.CHANGEDif 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. Nevernull.- Returns:
EChange.CHANGEDif reading the data changed something in the internal structures that requires a writing.
-
getSafeFile
@Nonnull protected final File getSafeFile(@Nonnull String sFilename, @Nonnull IDAO.EMode eMode) throws DAOException
- Throws:
DAOException
-
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. Nevernull.bIsInitialization-trueif this happened during initialization of a new file,falseif it happened during regular reading.aFile- The file that was read. May benullfor in-memory DAOs.
-
initialRead
@MustBeLocked(WRITE) protected final void initialRead() throws DAOExceptionCall 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 benull.sNewFilename- The new filename. Nevernull.
-
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-
nulldocument 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-nulldocument.
-
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. Nevernull.aFile- The resolved file. It is already consistency checked. Nevernull.
-
getXMLWriterSettings
@Nonnull @OverrideOnDemand protected com.helger.xml.serialize.write.IXMLWriterSettings getXMLWriterSettings()
- Returns:
- The
IXMLWriterSettingsto be used to serialize the data.
-
getLastFilename
@Nullable public final String getLastFilename()
- Returns:
- The filename to which was written last. May be
nullif 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. Nevernull.sErrorFilename- The filename tried to write to. Nevernull.aDoc- The XML content that should be written. May benullif 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
nullif 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
nullif 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
nullif it wasn't written before. Usually this field is not persistent and only is valid until the application ends.
-
toString
public String toString()
- Overrides:
toStringin classAbstractDAO
-
-