Liferay 6.2-ce-ga5

com.liferay.portlet.documentlibrary.store
Class DLStoreUtil

java.lang.Object
  extended by com.liferay.portlet.documentlibrary.store.DLStoreUtil

public class DLStoreUtil
extends Object

Provides methods for storing files in the portal. The file storage implementation can be selected in portal.properties under the property dl.store.impl. Virus checking can also be enabled under the property dl.store.antivirus.impl.

The main client for this class is the Document Library portlet. It is also used by other portlets like Wiki and Message Boards to store file attachments. For the Document Library portlet, the repositoryId can be obtained by calling DLFolderConstants.getDataRepositoryId( long,long). For all other portlets, the repositoryId should be set to CompanyConstants.SYSTEM. These methods can be used in plugins and other portlets, as shown below.

 
 long repositoryId = CompanyConstants.SYSTEM;
 String dirName = "portlet_name/1234";

 try {
 DLStoreUtil.addDirectory(companyId, repositoryId, dirName);
 }
 catch (DuplicateDirectoryException dde) {
 }

 DLStoreUtil.addFile(
 companyId, repositoryId, dirName + "/" + fileName, file);
 
 

See Also:
DLStoreImpl

Constructor Summary
DLStoreUtil()
           
 
Method Summary
static void addDirectory(long companyId, long repositoryId, String dirName)
          Adds a directory.
static void addFile(long companyId, long repositoryId, String fileName, boolean validateFileExtension, byte[] bytes)
          Adds a file based on a byte array.
static void addFile(long companyId, long repositoryId, String fileName, boolean validateFileExtension, File file)
          Adds a file based on a File object.
static void addFile(long companyId, long repositoryId, String fileName, boolean validateFileExtension, InputStream is)
          Adds a file based on a InputStream object.
static void addFile(long companyId, long repositoryId, String fileName, byte[] bytes)
          Adds a file based on a byte array.
static void addFile(long companyId, long repositoryId, String fileName, File file)
          Adds a file based on a File object.
static void addFile(long companyId, long repositoryId, String fileName, InputStream is)
          Adds a file based on an InputStream object.
static void checkRoot(long companyId)
          Ensures company's root directory exists.
static void copyFileVersion(long companyId, long repositoryId, String fileName, String fromVersionLabel, String toVersionLabel)
          Creates a new copy of the file version.
static void deleteDirectory(long companyId, long repositoryId, String dirName)
          Deletes a directory.
static void deleteFile(long companyId, long repositoryId, String fileName)
          Deletes a file.
static void deleteFile(long companyId, long repositoryId, String fileName, String versionLabel)
          Deletes a file at a particular version.
static File getFile(long companyId, long repositoryId, String fileName)
          Returns the file as a File object.
static File getFile(long companyId, long repositoryId, String fileName, String versionLabel)
          Returns the file as a File object.
static byte[] getFileAsBytes(long companyId, long repositoryId, String fileName)
          Returns the file as a byte array.
static byte[] getFileAsBytes(long companyId, long repositoryId, String fileName, String versionLabel)
          Returns the file as a byte array.
static InputStream getFileAsStream(long companyId, long repositoryId, String fileName)
          Returns the file as an InputStream object.
static InputStream getFileAsStream(long companyId, long repositoryId, String fileName, String versionLabel)
          Returns the file as an InputStream object.
static String[] getFileNames(long companyId, long repositoryId, String dirName)
          Returns all files of the directory.
static long getFileSize(long companyId, long repositoryId, String fileName)
          Returns the size of the file.
static DLStore getStore()
          Returns the DLStore object.
static boolean hasDirectory(long companyId, long repositoryId, String dirName)
          Returns true if the directory exists.
static boolean hasFile(long companyId, long repositoryId, String fileName)
          Returns true if the file exists.
static boolean hasFile(long companyId, long repositoryId, String fileName, String versionLabel)
          Returns true if the file exists.
static boolean isValidName(String name)
           
static void move(String srcDir, String destDir)
          Moves an existing directory.
 void setStore(DLStore store)
          Set's the DLStore object.
static void updateFile(long companyId, long repositoryId, long newRepositoryId, String fileName)
          Moves a file to a new data repository.
static void updateFile(long companyId, long repositoryId, String fileName, String newFileName)
          Update's the file's name
static void updateFile(long companyId, long repositoryId, String fileName, String fileExtension, boolean validateFileExtension, String versionLabel, String sourceFileName, File file)
          Updates a file based on a File object.
static void updateFile(long companyId, long repositoryId, String fileName, String fileExtension, boolean validateFileExtension, String versionLabel, String sourceFileName, InputStream is)
          Updates a file based on a InputStream object.
static void updateFileVersion(long companyId, long repositoryId, String fileName, String fromVersionLabel, String toVersionLabel)
          Update's a file version label.
static void validate(String fileName, boolean validateFileExtension)
          Validates a file's name.
static void validate(String fileName, boolean validateFileExtension, byte[] bytes)
          Validates a file's name and data.
static void validate(String fileName, boolean validateFileExtension, File file)
          Validates a file's name and data.
static void validate(String fileName, boolean validateFileExtension, InputStream is)
          Validates a file's name and data.
static void validate(String fileName, String fileExtension, String sourceFileName, boolean validateFileExtension, File file)
          Validates a file's name and data.
static void validate(String fileName, String fileExtension, String sourceFileName, boolean validateFileExtension, InputStream is)
          Validates a file's name and data.
static void validateDirectoryName(String directoryName)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DLStoreUtil

public DLStoreUtil()
Method Detail

addDirectory

public static void addDirectory(long companyId,
                                long repositoryId,
                                String dirName)
                         throws PortalException,
                                SystemException
Adds a directory.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
dirName - the directory's name
Throws:
PortalException - if the directory's information was invalid
SystemException - if a system exception occurred

addFile

public static void addFile(long companyId,
                           long repositoryId,
                           String fileName,
                           boolean validateFileExtension,
                           byte[] bytes)
                    throws PortalException,
                           SystemException
Adds a file based on a byte array.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file name
validateFileExtension - whether to validate the file's extension
bytes - the files's data
Throws:
PortalException - if the file's information was invalid or is found to contain a virus
SystemException - if a system exception occurred

addFile

public static void addFile(long companyId,
                           long repositoryId,
                           String fileName,
                           boolean validateFileExtension,
                           File file)
                    throws PortalException,
                           SystemException
Adds a file based on a File object.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file name
validateFileExtension - whether to validate the file's extension
file - Name the file name
Throws:
PortalException - if the file's information was invalid or is found to contain a virus
SystemException - if a system exception occurred

addFile

public static void addFile(long companyId,
                           long repositoryId,
                           String fileName,
                           boolean validateFileExtension,
                           InputStream is)
                    throws PortalException,
                           SystemException
Adds a file based on a InputStream object.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file name
validateFileExtension - whether to validate the file's extension
is - the files's data
Throws:
PortalException - if the file's information was invalid or is found to contain a virus
SystemException - if a system exception occurred

addFile

public static void addFile(long companyId,
                           long repositoryId,
                           String fileName,
                           byte[] bytes)
                    throws PortalException,
                           SystemException
Adds a file based on a byte array. Enforces validation of file's extension.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file name
bytes - the files's data
Throws:
PortalException - if the file's information was invalid or is found to contain a virus
SystemException - if a system exception occurred

addFile

public static void addFile(long companyId,
                           long repositoryId,
                           String fileName,
                           File file)
                    throws PortalException,
                           SystemException
Adds a file based on a File object. Enforces validation of file's extension.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file name
file - Name the file name
Throws:
PortalException - if the file's information was invalid or is found to contain a virus
SystemException - if a system exception occurred

addFile

public static void addFile(long companyId,
                           long repositoryId,
                           String fileName,
                           InputStream is)
                    throws PortalException,
                           SystemException
Adds a file based on an InputStream object. Enforces validation of file's extension.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file name
is - the files's data
Throws:
PortalException - if the file's information was invalid or is found to contain a virus
SystemException - if a system exception occurred

checkRoot

public static void checkRoot(long companyId)
                      throws SystemException
Ensures company's root directory exists. Only implemented by JCRStore.checkRoot(long).

Parameters:
companyId - the primary key of the company
Throws:
SystemException - if a system exception occurred

copyFileVersion

public static void copyFileVersion(long companyId,
                                   long repositoryId,
                                   String fileName,
                                   String fromVersionLabel,
                                   String toVersionLabel)
                            throws PortalException,
                                   SystemException
Creates a new copy of the file version.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the original's file name
fromVersionLabel - the original file's version label
toVersionLabel - the new version label
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

deleteDirectory

public static void deleteDirectory(long companyId,
                                   long repositoryId,
                                   String dirName)
                            throws PortalException,
                                   SystemException
Deletes a directory.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
dirName - the directory's name
Throws:
PortalException - if the directory's information was invalid
SystemException - if a system exception occurred

deleteFile

public static void deleteFile(long companyId,
                              long repositoryId,
                              String fileName)
                       throws PortalException,
                              SystemException
Deletes a file. If a file has multiple versions, all versions will be deleted.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file's name
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

deleteFile

public static void deleteFile(long companyId,
                              long repositoryId,
                              String fileName,
                              String versionLabel)
                       throws PortalException,
                              SystemException
Deletes a file at a particular version.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file's name
versionLabel - the file's version label
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

getFile

public static File getFile(long companyId,
                           long repositoryId,
                           String fileName)
                    throws PortalException,
                           SystemException
Returns the file as a File object.

This method is useful when optimizing low-level file operations like copy. The client must not delete or change the returned File object in any way. This method is only supported in certain stores. If not supported, this method will throw an UnsupportedOperationException.

If using an S3 store, it is preferable for performance reasons to use getFileAsStream(long, long, String) instead of this method wherever possible.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file's name
Returns:
Returns the File object with the file's name
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

getFile

public static File getFile(long companyId,
                           long repositoryId,
                           String fileName,
                           String versionLabel)
                    throws PortalException,
                           SystemException
Returns the file as a File object.

This method is useful when optimizing low-level file operations like copy. The client must not delete or change the returned File object in any way. This method is only supported in certain stores. If not supported, this method will throw an UnsupportedOperationException.

If using an S3 store, it is preferable for performance reasons to use getFileAsStream(long, long, String, String) instead of this method wherever possible.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file's name
versionLabel - the file's version label
Returns:
Returns the File object with the file's name
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

getFileAsBytes

public static byte[] getFileAsBytes(long companyId,
                                    long repositoryId,
                                    String fileName)
                             throws PortalException,
                                    SystemException
Returns the file as a byte array.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file's name
Returns:
Returns the byte array with the file's name
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

getFileAsBytes

public static byte[] getFileAsBytes(long companyId,
                                    long repositoryId,
                                    String fileName,
                                    String versionLabel)
                             throws PortalException,
                                    SystemException
Returns the file as a byte array.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file's name
versionLabel - the file's version label
Returns:
Returns the byte array with the file's name
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

getFileAsStream

public static InputStream getFileAsStream(long companyId,
                                          long repositoryId,
                                          String fileName)
                                   throws PortalException,
                                          SystemException
Returns the file as an InputStream object.

If using an S3 store, it is preferable for performance reasons to use this method to get the file as an InputStream instead of using other methods to get the file as a File.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file's name
Returns:
Returns the InputStream object with the file's name
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

getFileAsStream

public static InputStream getFileAsStream(long companyId,
                                          long repositoryId,
                                          String fileName,
                                          String versionLabel)
                                   throws PortalException,
                                          SystemException
Returns the file as an InputStream object.

If using an S3 store, it is preferable for performance reasons to use this method to get the file as an InputStream instead of using other methods to get the file as a File.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file's name
versionLabel - the file's version label
Returns:
Returns the InputStream object with the file's name
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

getFileNames

public static String[] getFileNames(long companyId,
                                    long repositoryId,
                                    String dirName)
                             throws PortalException,
                                    SystemException
Returns all files of the directory.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
dirName - the directory's name
Returns:
Returns all files of the directory
Throws:
PortalException - if the directory's information was invalid
SystemException - if a system exception occurred

getFileSize

public static long getFileSize(long companyId,
                               long repositoryId,
                               String fileName)
                        throws PortalException,
                               SystemException
Returns the size of the file.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file's name
Returns:
Returns the size of the file
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

getStore

public static DLStore getStore()
Returns the DLStore object. Used primarily by Spring and should not be used by the client.

Returns:
Returns the DLStore object

hasDirectory

public static boolean hasDirectory(long companyId,
                                   long repositoryId,
                                   String dirName)
                            throws PortalException,
                                   SystemException
Returns true if the directory exists.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
dirName - the directory's name
Returns:
true if the directory exists; false otherwise
Throws:
PortalException - if the directory's information was invalid
SystemException - if a system exception occurred

hasFile

public static boolean hasFile(long companyId,
                              long repositoryId,
                              String fileName)
                       throws PortalException,
                              SystemException
Returns true if the file exists.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file's name
Returns:
true if the file exists; false otherwise
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

hasFile

public static boolean hasFile(long companyId,
                              long repositoryId,
                              String fileName,
                              String versionLabel)
                       throws PortalException,
                              SystemException
Returns true if the file exists.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file's name
versionLabel - the file's version label
Returns:
true if the file exists; false otherwise
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

isValidName

public static boolean isValidName(String name)

move

public static void move(String srcDir,
                        String destDir)
                 throws SystemException
Moves an existing directory. Only implemented by JCRStore.move(String, String).

Parameters:
srcDir - the original directory's name
destDir - the new directory's name
Throws:
SystemException - if a system exception occurred

updateFile

public static void updateFile(long companyId,
                              long repositoryId,
                              long newRepositoryId,
                              String fileName)
                       throws PortalException,
                              SystemException
Moves a file to a new data repository.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository
newRepositoryId - the primary key of the new data repository
fileName - the file's name
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

updateFile

public static void updateFile(long companyId,
                              long repositoryId,
                              String fileName,
                              String newFileName)
                       throws PortalException,
                              SystemException
Update's the file's name

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file's name
newFileName - the file's new name
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

updateFile

public static void updateFile(long companyId,
                              long repositoryId,
                              String fileName,
                              String fileExtension,
                              boolean validateFileExtension,
                              String versionLabel,
                              String sourceFileName,
                              File file)
                       throws PortalException,
                              SystemException
Updates a file based on a File object.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file name
fileExtension - the file's extension
validateFileExtension - whether to validate the file's extension
versionLabel - the file's new version label
sourceFileName - the new file's original name
file - Name the file name
Throws:
PortalException - if the file's information was invalid or is found to contain a virus
SystemException - if a system exception occurred

updateFile

public static void updateFile(long companyId,
                              long repositoryId,
                              String fileName,
                              String fileExtension,
                              boolean validateFileExtension,
                              String versionLabel,
                              String sourceFileName,
                              InputStream is)
                       throws PortalException,
                              SystemException
Updates a file based on a InputStream object.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file name
fileExtension - the file's extension
validateFileExtension - whether to validate the file's extension
versionLabel - the file's new version label
sourceFileName - the new file's original name
is - the new file's data
Throws:
PortalException - if the file's information was invalid or is found to contain a virus
SystemException - if a system exception occurred

updateFileVersion

public static void updateFileVersion(long companyId,
                                     long repositoryId,
                                     String fileName,
                                     String fromVersionLabel,
                                     String toVersionLabel)
                              throws PortalException,
                                     SystemException
Update's a file version label. Similar to copyFileVersion(long, long, String, String, String) except that the old file version is deleted.

Parameters:
companyId - the primary key of the company
repositoryId - the primary key of the data repository (optionally CompanyConstants.SYSTEM)
fileName - the file's name
fromVersionLabel - the file's version label
toVersionLabel - the file's new version label
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

validate

public static void validate(String fileName,
                            boolean validateFileExtension)
                     throws PortalException,
                            SystemException
Validates a file's name.

Parameters:
fileName - the file's name
validateFileExtension - whether to validate the file's extension
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

validate

public static void validate(String fileName,
                            boolean validateFileExtension,
                            byte[] bytes)
                     throws PortalException,
                            SystemException
Validates a file's name and data.

Parameters:
fileName - the file's name
validateFileExtension - whether to validate the file's extension
bytes - the file's data (optionally null)
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

validate

public static void validate(String fileName,
                            boolean validateFileExtension,
                            File file)
                     throws PortalException,
                            SystemException
Validates a file's name and data.

Parameters:
fileName - the file's name
validateFileExtension - whether to validate the file's extension
file - Name the file's name
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

validate

public static void validate(String fileName,
                            boolean validateFileExtension,
                            InputStream is)
                     throws PortalException,
                            SystemException
Validates a file's name and data.

Parameters:
fileName - the file's name
validateFileExtension - whether to validate the file's extension
is - the file's data (optionally null)
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

validate

public static void validate(String fileName,
                            String fileExtension,
                            String sourceFileName,
                            boolean validateFileExtension,
                            File file)
                     throws PortalException,
                            SystemException
Validates a file's name and data.

Parameters:
fileName - the file's name
fileExtension - the file's extension
sourceFileName - the file's original name
validateFileExtension - whether to validate the file's extension
file - Name the file's name
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

validate

public static void validate(String fileName,
                            String fileExtension,
                            String sourceFileName,
                            boolean validateFileExtension,
                            InputStream is)
                     throws PortalException,
                            SystemException
Validates a file's name and data.

Parameters:
fileName - the file's name
fileExtension - the file's extension
sourceFileName - the file's original name
validateFileExtension - whether to validate the file's extension
is - the file's data (optionally null)
Throws:
PortalException - if the file's information was invalid
SystemException - if a system exception occurred

validateDirectoryName

public static void validateDirectoryName(String directoryName)
                                  throws PortalException
Throws:
PortalException

setStore

public void setStore(DLStore store)
Set's the DLStore object. Used primarily by Spring and should not be used by the client.

Parameters:
store - the DLStore object

Liferay 6.2-ce-ga5