Class DocTool


  • public class DocTool
    extends Object
    Utility methods to handle documents from within Automation
    Since:
    7.3
    Author:
    Marco Meschieri - LogicalDOC
    • Constructor Detail

      • DocTool

        public DocTool()
    • Method Detail

      • downloadUrl

        public String downloadUrl​(long docId)
        Builds the download url of a document(download permalink)
        Parameters:
        docId - identifier of the document
        Returns:
        the download permalink
      • displayUrl

        public String displayUrl​(long tenantId,
                                 long docId)
        Builds the display url of a document(display permalink)
        Parameters:
        tenantId - identifier of the tenant
        docId - identifier of the document
        Returns:
        the display permalink
      • downloadUrl

        public String downloadUrl​(Document doc)
        Builds the download url of a document(download permalink)
        Parameters:
        doc - the document
        Returns:
        the download permalink
      • downloadUrl

        public String downloadUrl​(DocumentHistory history)
        Builds the download url of a document(download permalink)
        Parameters:
        history - object representing an event on a document
        Returns:
        the download permalink
      • displayUrl

        public String displayUrl​(Document doc)
        Builds the display url of a document(display permalink)
        Parameters:
        doc - the document
        Returns:
        the display permalink
      • displayUrl

        public String displayUrl​(DocumentHistory history)
        Builds the display url of a document(display permalink)
        Parameters:
        history - object representing an event on a document
        Returns:
        the display permalink
      • downloadTicket

        public String downloadTicket​(long docId,
                                     boolean pdfConversion,
                                     Integer expireHours,
                                     Date expireDate,
                                     Integer maxDownloads,
                                     String username)
                              throws Exception
        Creates a new download ticket for a document
        Parameters:
        docId - identifier of the document
        pdfConversion - if the pdf conversion should be downloaded instead of the original file
        expireHours - number of hours after which the ticket expires
        expireDate - a specific expiration date
        maxDownloads - number of downloads over which the ticket expires
        username - the user in whose name the method is run
        Returns:
        the complete download ticket's URL
        Throws:
        Exception - a generic error happened
      • displayFileSize

        public String displayFileSize​(Long size)
        Prints the size of a file in human readable form
        Parameters:
        size - the file size
        Returns:
        the human readable representation
      • store

        public void store​(Document doc,
                          DocumentHistory transaction)
        Saves / updates a document into the database
        Parameters:
        doc - the document to save
        transaction - entry to log the event
      • store

        public void store​(Document doc)
        Saves / updates a document into the database
        Parameters:
        doc - the document to save
      • store

        public void store​(Document doc,
                          String username)
        Saves / updates a document into the database
        Parameters:
        doc - the document to save
        username - the user in whose name the method is run
      • initialize

        public void initialize​(Document doc)
        Initializes lazy loaded collections
        Parameters:
        doc - the document to initialize
      • move

        public void move​(Document doc,
                         String targetPath,
                         String username)
                  throws Exception
        Moves a document into a target folder
        Parameters:
        doc - the document
        targetPath - the full path of the target folder
        username - the user in whose name the method is run
        Throws:
        Exception - a generic error happened
      • copy

        public Document copy​(Document doc,
                             String targetPath,
                             String username)
                      throws Exception
        Copies a document into a target folder
        Parameters:
        doc - the document
        targetPath - the full path of the target folder
        username - the user in whose name the method is run
        Returns:
        the new document created
        Throws:
        Exception - a generic error happened
      • link

        public void link​(Document doc1,
                         Document doc2,
                         String type)
                  throws Exception
        Links two documents
        Parameters:
        doc1 - first document
        doc2 - second document
        type - type of link(optional)
        Throws:
        Exception - a generic error happened
      • createAlias

        public Document createAlias​(Document originalDoc,
                                    String targetPath,
                                    String type,
                                    String username)
                             throws Exception
        Creates an alias to a document
        Parameters:
        originalDoc - the original document to be referenced
        targetPath - the full path of the target folder
        type - the alias type(null for the original file or pdf for it's pdf conversion)
        username - the user in whose name the method is run
        Returns:
        The new alias created
        Throws:
        Exception - a generic error happened
      • createAlias

        public Document createAlias​(Document originalDoc,
                                    Folder targetFolder,
                                    String type,
                                    String username)
                             throws Exception
        Creates an alias to a document
        Parameters:
        originalDoc - the original document to be referenced
        targetFolder - the folder that will contain the alias
        type - the alias type(null for the original file or pdf for it's pdf conversion)
        username - the user in whose name the method is run
        Returns:
        The new alias created
        Throws:
        Exception - a generic error happened
      • lock

        public void lock​(long docId,
                         String username)
                  throws Exception
        Locks a document
        Parameters:
        docId - identifier of the document
        username - the user that locks the document
        Throws:
        Exception - a generic error happened
      • unlock

        public void unlock​(long docId,
                           String username)
                    throws Exception
        Unlocks a document
        Parameters:
        docId - identifier of the document
        username - the user that locks the document
        Throws:
        Exception - a generic error happened
      • delete

        public void delete​(long docId,
                           String username)
                    throws Exception
        Delete a document
        Parameters:
        docId - identifier of the document
        username - the user in whose name the method is run
        Throws:
        Exception - a generic error happened
      • copyResource

        public Document copyResource​(Document doc,
                                     String fileVersion,
                                     String suffix,
                                     String newFileName,
                                     String username)
                              throws Exception
        Copies a resource in a file in the same folder of the original document
        Parameters:
        doc - The document
        fileVersion - The file version
        suffix - the suffix (eg conversion.pdf)
        newFileName - the file name of the new file
        username - the user in whose name the method is run
        Returns:
        the document
        Throws:
        Exception - a generic error happened
      • writeToFile

        public void writeToFile​(long docId,
                                String fileVersion,
                                String suffix,
                                String outputFile)
                         throws Exception
        Writes a resource in a file in the local file system
        Parameters:
        docId - The document's identifier
        fileVersion - The file version
        suffix - the suffix (eg conversion.pdf)
        outputFile - the user in name of which to take this action
        Throws:
        Exception - error writing the file
      • convertPDF

        public void convertPDF​(Document doc)
                        throws Exception
        Converts a document in PDF format and saves it as ancillary resource of the document with suffix FormatConverterManager.PDF_CONVERSION_SUFFIX. If the conversion already exists, nothing will be done.
        Parameters:
        doc - the document to convert
        Throws:
        Exception - generic error happened
      • convert

        public Document convert​(Document doc,
                                String format,
                                String username)
                         throws Exception
        Convert a document in another format and saves the result in another file in the same folder
        Parameters:
        doc - the document to convert
        format - it is the output format(e.g. pdf, txt, ...)
        username - the user in whose name the method is run
        Returns:
        the generated conversion document
        Throws:
        Exception - generic error happened
      • merge

        public Document merge​(Collection<Document> documents,
                              long targetFolderId,
                              String fileName,
                              String username)
                       throws Exception
        Merges a set of documents into a single PDF file
        Parameters:
        documents - the list of documents to merge(the order counts)
        targetFolderId - identifier of the target folder
        fileName - name of the output file(must ends with .pdf)
        username - the user in whose name the method is run
        Returns:
        the generated merged document
        Throws:
        Exception - generic error happened
      • findById

        public Document findById​(long docId)
        Retrieves a document by it's identifier
        Parameters:
        docId - identifier of the document
        Returns:
        the document object
      • findByPath

        public Document findByPath​(String path)
        Retrieves a document by it's path
        Parameters:
        path - full path of the document
        Returns:
        the document object
      • findByPath

        public Document findByPath​(String path,
                                   Long tenantId)
        Retrieves a document by it's path
        Parameters:
        path - full path of the document
        tenantId - identifier of the tenant(optional)
        Returns:
        the document object
      • getPath

        public String getPath​(Document doc)
        Calculates the full path of a document
        Parameters:
        doc - the document to inspect
        Returns:
        the full path
      • getIds

        public List<Long> getIds​(Collection<Document> docs)
        Converts a collection of documents in a collection of identifiers
        Parameters:
        docs - the input collection
        Returns:
        the list of all the IDs of the documents in the input collection
      • createPath

        public Folder createPath​(Document doc,
                                 String targetPath,
                                 String username)
        Creates a path, all existing nodes in the specified path will be reused
        Parameters:
        doc - document used to take the root folder in case the targetPath represents a relative path
        targetPath - absolute or relative(in relation to the document's parent folder) path to be created
        username - the user in whose name the method is run
        Returns:
        the folder leaf of the path
      • getHistories

        public List<DocumentHistory> getHistories​(long docId,
                                                  String event)
        Retrieve the list of events of a document
        Parameters:
        docId - identifier of the document
        event - event name, optional
        Returns:
        list of histories
      • addNote

        public void addNote​(Document doc,
                            String text,
                            String username)
        Creates a new note for the whole document
        Parameters:
        doc - the document
        text - text of the note
        username - username of the user in name of which this action is taken
      • getNotes

        public List<DocumentNote> getNotes​(long docId,
                                           String fileVersion)
        Lists the notes of a given document
        Parameters:
        docId - identifier of the document
        fileVersion - version of the file, optional
        Returns:
        the list of notes
      • calculateNextVersion

        public String calculateNextVersion​(String currentVersion,
                                           boolean major)
        Calculates what will be the next version specification. @see Version.getNewVersionName(String, boolean)
        Parameters:
        currentVersion - the current version(e.g. 1.1, 2.15, ...)
        major - if the new release must be a major release
        Returns:
        the new version
      • findTemplateByName

        public Template findTemplateByName​(String name,
                                           long tenantId)
        This method finds a template by name
        Parameters:
        name - Name of the template
        tenantId - Identifier of the owning tenant
        Returns:
        Template with given name
      • findTemplateById

        public Template findTemplateById​(long templateId)
        This method finds a template by ID
        Parameters:
        templateId - Identifier of the template
        Returns:
        Template with given name
      • countPages

        public int countPages​(Document document)
        Counts the number of pages of a document
        Parameters:
        document - the document
        Returns:
        the number of pages