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
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
ConstructorsModifierConstructorDescriptionprotectedAbstractSimpleDAO(com.helger.commons.io.relative.IFileRelativeIO aIO, Supplier<String> aFilenameProvider) -
Method Summary
Modifier and TypeMethodDescriptionprotected voidbeforeWriteToFile(String sFilename, File aFile) Optional callback method that is invoked before the file handle gets opened.protected abstract com.helger.xml.microdom.IMicroDocumentCreate the XML document that should be saved to the file.intprotected final com.helger.commons.io.relative.IFileRelativeIOgetIO()final Stringfinal LocalDateTimefinal LocalDateTimefinal LocalDateTimeintprotected final FilegetSafeFile(String sFilename, IDAO.EMode eMode) intprotected com.helger.xml.serialize.write.IXMLWriterSettingsprotected final voidCall this method inside the constructor to read the file contents directly.protected final voidThis 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.toString()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.final voidIn 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, setSilentModeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface com.helger.dao.IAutoSaveAware
performWithoutAutoSave, performWithoutAutoSave, performWithoutAutoSaveThrowing, performWithoutAutoSaveThrowingMethods inherited from interface com.helger.dao.IDAO
isReloadable, reload
-
Constructor Details
-
AbstractSimpleDAO
-
-
Method Details
-
getIO
- Returns:
- The file-relative IO as passed in the constructor. Never
null.
-
getFilenameProvider
- Returns:
- The filename provider used internally to build filenames. Never
null.
-
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
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 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- Overrides:
toStringin classAbstractDAO
-