Class AbstractHibernateAttachmentDao

    • Constructor Detail

      • AbstractHibernateAttachmentDao

        public AbstractHibernateAttachmentDao()
    • Method Detail

      • getInstance

        public static AttachmentDao getInstance​(AttachmentDataDao attachmentDataDao)
        Returns the correct instance of AttachmentDao for the AttachmentDataDao that is given.
        Parameters:
        attachmentDataDao - the AttachmentDataDao that should be used
        Returns:
        a matching AttachmentDao instance
        Throws:
        IllegalArgumentException - if dataDao is not supported
      • getById

        public Attachment getById​(long id)
        Description copied from interface: AttachmentDao
        Returns the attachment with the given persistent ID. Returns null if no such attachment exists.
        Specified by:
        getById in interface AttachmentDao
        Parameters:
        id - the persistent ID of the attachment to retrieve
        Returns:
        the attachment with the given ID, or null if no attachment exists with the provided ID.
      • getByIds

        public List<Attachment> getByIds​(List<Long> ids)
        Description copied from interface: AttachmentDao
        Returns a list of attachments matching the given list of IDs. Returns an empty list if no such attachments exist. In the case where some IDs exist and some don't, only the existing Attachments will be returned.
        Specified by:
        getByIds in interface AttachmentDao
        Parameters:
        ids - list of IDs to retrieve
        Returns:
        the attachments with the given IDs, or an empty list if no attachments exist with the provided IDs.
      • getLatestAttachment

        public Attachment getLatestAttachment​(ContentEntityObject content,
                                              String fileName)
        Description copied from interface: AttachmentDao
        Return the latest version of the attachment on the specified content with the given filename. Returns null if no attachment with that name on the content exists.
        Specified by:
        getLatestAttachment in interface AttachmentDao
        Parameters:
        content - the content to find the attachment on
        fileName - the file name of the attachment
        Returns:
        the latest version of attachment with the given name attached to that content, or null if the attachment does not exist.
      • getLastAddedVersionsOf

        public List<Attachment> getLastAddedVersionsOf​(Attachment attachment)
        Description copied from interface: AttachmentDao
        Retrieves all the last added versions for each user who has added a version of the attachment That is, if a user has added multiple versions, only the latest added version will be added to the list returned. Result will be sorted with the earliest version coming first.
        Specified by:
        getLastAddedVersionsOf in interface AttachmentDao
        Parameters:
        attachment - attachment (must be the latest version)
        Returns:
        the last added versions for each user who has added a version of the attachment
      • saveNewAttachment

        public void saveNewAttachment​(Attachment attachment,
                                      InputStream attachmentData)
        Description copied from interface: AttachmentDao
        Saves a new attachment
        Specified by:
        saveNewAttachment in interface AttachmentDao
        Parameters:
        attachment - the Attachment to be saved
        attachmentData - the attachment data to be saved
      • saveNewAttachmentVersion

        public void saveNewAttachmentVersion​(Attachment attachment,
                                             Attachment previousVersion,
                                             InputStream attachmentData)
        This implementation saves the Attachment and its previous version to the VersionedHibernateObjectDao.

        Due to the way Hibernate works, attachment must be the current Hibernate-managed object (without its version or ID having being changed), and previousVersion should be a clone of the unmodified Attachment object.

        Specified by:
        saveNewAttachmentVersion in interface AttachmentDao
        Parameters:
        attachment - the attachment to be saved, a clone of the old one
        previousVersion - the old attachment, which must be unmodified
        attachmentData - the new attachment data
      • isAttachmentPresent

        public boolean isAttachmentPresent​(Attachment attachment)
        Description copied from interface: AttachmentDao
        Check that an attachment exists
        Specified by:
        isAttachmentPresent in interface AttachmentDao
        Parameters:
        attachment - the Attachment to be checked
      • moveAttachment

        public void moveAttachment​(Attachment latestVersion,
                                   Attachment oldAttachment,
                                   ContentEntityObject newContent)
        Description copied from interface: AttachmentDao
        Moves an Attachment to a new file name or content object

        This method assumes that the filename and/or content of the attachment (and its previous versions) have already been updated.

        Specified by:
        moveAttachment in interface AttachmentDao
        Parameters:
        latestVersion - the Attachment to be moved
        oldAttachment - the Attachment before it has been updated
        newContent - the new content attachment belongs to
      • removeAttachment

        protected void removeAttachment​(Attachment attachment,
                                        ContentEntityObject content)
        Severs the connection between attachment and content, and removes the attachment from the database
        Parameters:
        attachment - the attachment to remove
        content - the content to remove the attachment from
      • removeAttachmentVersionFromServer

        public final void removeAttachmentVersionFromServer​(Attachment attachmentVersionToBeRemoved)
        Description copied from interface: AttachmentDao
        Removes an attachment version
        Specified by:
        removeAttachmentVersionFromServer in interface AttachmentDao
        Parameters:
        attachmentVersionToBeRemoved - the Attachment version to be removed
      • removeAttachmentVersionFromServer

        protected abstract void removeAttachmentVersionFromServer​(Attachment attachmentVersionToBeRemoved,
                                                                  @Nullable Attachment previousAttachmentVersion)
      • removeMetaData

        protected void removeMetaData​(Attachment attachment)
        Removes attachment meta data.

        The steps for removing attachment meta data are

        1. the attachment object being de-referenced from its container (e.g. page)
        2. the original version (reference to the latest version) to be set to null
        3. the row removal to be recorded
        4. the un-index to be queued
        Parameters:
        attachment - the attachment version to be removed
      • overwriteMetaData

        protected void overwriteMetaData​(Attachment sourceAttachment,
                                         Attachment targetAttachment)
        Overwrites attachment meta data.

        The latest version of an attachment inherits the EntityObject.getId() of the first version, that means the identifier which was given to the first version gets replaced by a new one if a second version gets uploaded, and the second version inherits the identifier of the first one. Thus using the identifier of the latest version in the frontend is guaranteed to never become stale.

        Thus

        1. removing a historical version is "simple", as the only thing that references it is its container (e.g. page)
        2. removing the current version is "complex", as this requires the previous version to be "promoted" to the current version

        The corresponding row of sourceAttachment in the ATTACHMENTS table will be removed, and its data will be used to overwrite the corresponding row of targetAttachment. Calling this method assumes that you removed all tuples having a referential constraint on the sourceAttachment's row ahead, since the state of the Hibernate session will be synchronised with the database several times as part of this execution. The sourceAttachment object will become untracked by Hibernate as a result of this call, and it will have the id of the targetAttachment, thus anything relying on the old state should have cached it ahead (e.g. shallow Attachment.clone()).

        Parameters:
        sourceAttachment - the attachment version to be moved
        targetAttachment - the attachment version to be overwritten
      • getMigrator

        public AttachmentDao.AttachmentMigrator getMigrator​(AttachmentDao destinationDao)
        Description copied from interface: AttachmentDao
        Get a migrator object to migrate attachments between attachment datastores.

        To be used when switching datastore implementations

        Specified by:
        getMigrator in interface AttachmentDao
        Parameters:
        destinationDao - dao to move attachments to
        Returns:
        A migrator
      • prepareForMigrationTo

        public void prepareForMigrationTo()
        This implementation removes the contents of the table and passes the migration tasks onto the AttachmentDataDao
        Specified by:
        prepareForMigrationTo in interface AttachmentDao
      • afterMigrationFrom

        public void afterMigrationFrom()
        This implementation removes all Attachments and passes the migration tasks onto the AttachmentDataDao
        Specified by:
        afterMigrationFrom in interface AttachmentDao
      • getRemappedAttachmentIds

        public Map<Long,​Long> getRemappedAttachmentIds()
        Description copied from interface: AttachmentDao
        Gets a list of remapped attachment IDs. The key is the old ID and the value is the new ID.
        Specified by:
        getRemappedAttachmentIds in interface AttachmentDao
        Returns:
        A map where the key is the original ID and the value is the new ID.
      • replaceAttachmentData

        public void replaceAttachmentData​(Attachment attachment,
                                          InputStream attachmentData)
        Description copied from interface: AttachmentDao
        Sets the attachment data for attachment

        This method will overwrite any existing data for the attachment version.

        Specified by:
        replaceAttachmentData in interface AttachmentDao
        Parameters:
        attachment - Attachment the data belongs to
        attachmentData - the data to save
      • flush

        public void flush()
        This implementation flushes the Hibernate session.
        Specified by:
        flush in interface FlushableCachingDao
      • findLatestVersionIdsIterator

        public Iterator<Long> findLatestVersionIdsIterator()
      • findLatestVersionIdsIterator

        public Iterator<Long> findLatestVersionIdsIterator​(List<Space> spacesToInclude)
        Same as findLatestVersionIdsIterator() but only gets the ids matching the list of spaces requested.
        Parameters:
        spacesToInclude - list of spaces to include. The length of this list should not exceed the allowed maximum value of an "in" clause.
        Returns:
        a list of the latest versions of attachments in the requested spaces
      • findLatestVersionIds

        public List<Long> findLatestVersionIds()
      • findLatestVersionIds

        public List<Long> findLatestVersionIds​(List<Space> spacesToInclude)
        Same as findLatestVersionIdsIterator() but only gets the ids matching the list of spaces requested.
        Parameters:
        spacesToInclude - list of spaces to include. The length of this list should not exceed the allowed maximum value of an "in" clause.
        Returns:
        a list of the ids of the latest versions of attachments in the requested spaces