com.atlassian.confluence.pages.attachments
Class CachingAttachmentDao

java.lang.Object
  extended by com.atlassian.confluence.pages.attachments.CachingAttachmentDao
All Implemented Interfaces:
DelegatingAttachmentDao, AttachmentDao, FlushableCachingDao

public class CachingAttachmentDao
extends java.lang.Object
implements AttachmentDao, FlushableCachingDao, DelegatingAttachmentDao

Caches the IDs of attachments retrieved with getLatestAttachment(ContentEntityObject, String) in an AttachmentCache. Other methods simply delegate to the underlying DAO.

Because the persistent ID of the latest version of an attachment never changes, there's no need to synchronise the cache when an attachment is modified. However, the cache is updated when attachments are removed through this interface.

If for some reason the cache does get out of sync with the database, the lookup using the cached ID will fail. In this situation, we fall back to using the lookup by content ID + file name. The cache will then be corrected once the right result is retrieved from the database.

Since:
3.0
See Also:
AttachmentCache

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.atlassian.confluence.pages.persistence.dao.AttachmentDao
AttachmentDao.AttachmentCopier, AttachmentDao.AttachmentMigrator
 
Constructor Summary
CachingAttachmentDao(AttachmentDao delegate, AttachmentCache cache)
           
 
Method Summary
 void afterMigrationFrom()
          Perform any tasks necessary after Attachment data has been moved from this data store to another.
 java.util.List<Attachment> findAll()
          Retrieves the latest versions of all Attachments Use of this method is discouraged when there are a large amount of Attachments.
 java.util.List<Attachment> findAllVersions(Attachment attachment)
           
 java.util.Iterator<Attachment> findLatestVersionsIterator()
          Retrieves the latest versions of all Attachments and returns an Iterator to the results.
 void flush()
          Flush the caches managed by this DAO.
 Attachment getAttachment(ContentEntityObject content, java.lang.String fileName, int version)
          Return the attachment on the specified content with the given filename and version.
 java.io.InputStream getAttachmentData(Attachment attachment)
          Retrieves the InputStream representing the data for attachment
 Attachment getById(long id)
          Returns the attachment with the given persistent ID.
 AttachmentDao.AttachmentCopier getCopier(AttachmentDao destinationDao)
          Get a copier object to copy attachments between data stores The source data store will remain untouched
 AttachmentDao getDelegate()
          Returns the attachment DAO which this instance delegates to.
 java.util.List<Attachment> getLastAddedVersionsOf(Attachment attachment)
          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.
 Attachment getLatestAttachment(ContentEntityObject content, java.lang.String fileName)
          Return the latest version of the attachment on the specified content with the given filename.
 java.util.List<Attachment> getLatestVersionsOfAttachments(ContentEntityObject ceo)
          Retrieves the latest versions of all attachments to ceo
 AttachmentDao.AttachmentMigrator getMigrator(AttachmentDao destinationDao)
          Get a migrator object to migrate attachments between attachment datastores.
 void moveAttachment(Attachment attachment, Attachment oldAttachment, ContentEntityObject newContent)
          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.

 void prepareForMigrationTo()
          Prepare the data store for a migration from another type of data store.
 void removeAttachmentFromServer(Attachment attachment)
          Removes attachment (and all of its versions)
 void replaceAttachmentData(Attachment attachment, java.io.InputStream attachmentData)
          Sets the attachment data for attachment This method will overwrite any existing data for the attachment version.
 void saveNewAttachment(Attachment attachment, java.io.InputStream attachmentData)
          Saves a new attachment
 void saveNewAttachmentVersion(Attachment attachment, Attachment previousVersion, java.io.InputStream attachmentData)
          Saves a new version of attachment
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CachingAttachmentDao

public CachingAttachmentDao(AttachmentDao delegate,
                            AttachmentCache cache)
Method Detail

getDelegate

public AttachmentDao getDelegate()
Description copied from interface: DelegatingAttachmentDao
Returns the attachment DAO which this instance delegates to.

Specified by:
getDelegate in interface DelegatingAttachmentDao

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.

getLatestAttachment

public Attachment getLatestAttachment(ContentEntityObject content,
                                      java.lang.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 java.util.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

removeAttachmentFromServer

public void removeAttachmentFromServer(Attachment attachment)
Description copied from interface: AttachmentDao
Removes attachment (and all of its versions)

Specified by:
removeAttachmentFromServer in interface AttachmentDao
Parameters:
attachment - the Attachment to be removed

flush

public void flush()
Description copied from interface: FlushableCachingDao
Flush the caches managed by this DAO.

Specified by:
flush in interface FlushableCachingDao

getLatestVersionsOfAttachments

public java.util.List<Attachment> getLatestVersionsOfAttachments(ContentEntityObject ceo)
Description copied from interface: AttachmentDao
Retrieves the latest versions of all attachments to ceo

Specified by:
getLatestVersionsOfAttachments in interface AttachmentDao
Parameters:
ceo - the content to look up attachments
Returns:
a List of Attachment objects

findAll

public java.util.List<Attachment> findAll()
Description copied from interface: AttachmentDao
Retrieves the latest versions of all Attachments Use of this method is discouraged when there are a large amount of Attachments.

Specified by:
findAll in interface AttachmentDao
Returns:
A List of Attachment objects
See Also:
AttachmentDao.findLatestVersionsIterator()

findLatestVersionsIterator

public java.util.Iterator<Attachment> findLatestVersionsIterator()
Description copied from interface: AttachmentDao
Retrieves the latest versions of all Attachments and returns an Iterator to the results.

Specified by:
findLatestVersionsIterator in interface AttachmentDao
Returns:
Iterator to the results

getAttachmentData

public java.io.InputStream getAttachmentData(Attachment attachment)
                                      throws AttachmentDataNotFoundException
Description copied from interface: AttachmentDao
Retrieves the InputStream representing the data for attachment

Specified by:
getAttachmentData in interface AttachmentDao
Parameters:
attachment - the attachment whose data will be returned
Returns:
InputStream of the data
Throws:
AttachmentDataNotFoundException - if the data could not be found

saveNewAttachment

public void saveNewAttachment(Attachment attachment,
                              java.io.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,
                                     java.io.InputStream attachmentData)
Description copied from interface: AttachmentDao
Saves a new version of attachment

Specified by:
saveNewAttachmentVersion in interface AttachmentDao
Parameters:
attachment - the Attachment to be saved
previousVersion - the previous version of the Attachment
attachmentData - the attachment data to be saved

moveAttachment

public void moveAttachment(Attachment attachment,
                           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:
attachment - the Attachment to be moved
oldAttachment - the Attachment before it has been updated
newContent - the new content attachment belongs to

replaceAttachmentData

public void replaceAttachmentData(Attachment attachment,
                                  java.io.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

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

getCopier

public AttachmentDao.AttachmentCopier getCopier(AttachmentDao destinationDao)
Description copied from interface: AttachmentDao
Get a copier object to copy attachments between data stores The source data store will remain untouched

Specified by:
getCopier in interface AttachmentDao
Parameters:
destinationDao - dao to copy attachments to
Returns:
A copier

prepareForMigrationTo

public void prepareForMigrationTo()
Description copied from interface: AttachmentDao
Prepare the data store for a migration from another type of data store.

Classes implementing this method will perform any tasks necessary to allow the data store to be used.

Specified by:
prepareForMigrationTo in interface AttachmentDao

afterMigrationFrom

public void afterMigrationFrom()
Description copied from interface: AttachmentDao
Perform any tasks necessary after Attachment data has been moved from this data store to another.

Specified by:
afterMigrationFrom in interface AttachmentDao

findAllVersions

public java.util.List<Attachment> findAllVersions(Attachment attachment)
Specified by:
findAllVersions in interface AttachmentDao

getAttachment

public Attachment getAttachment(ContentEntityObject content,
                                java.lang.String fileName,
                                int version)
Description copied from interface: AttachmentDao
Return the attachment on the specified content with the given filename and version. Returns null if no such attachment exists.

Note: unlike AttachmentManager.getAttachment(ContentEntityObject, String, int), this method does not accept zero as a special flag to indicate the latest version of an attachment. To retrieve the latest version of an attachment, use AttachmentDao.getLatestAttachment(ContentEntityObject, String).

Specified by:
getAttachment in interface AttachmentDao
Parameters:
content - the content to find the attachment on
fileName - the file name of the attachment, treated case-insensitively
version - the version of the attachment
Returns:
the attachment with the given name and version attached to that content, or null if the attachment does not exist.


Copyright © 2003-2010 Atlassian. All Rights Reserved.