OKMDocument

Basics

In most methods you'll see a parameter named "docId". The value of this parameter can be a valid document UUID node.

Example of docId:

  • Using UUID -> "c41f9ea0-0d6c-45da-bae4-d72b66f42d0f";

About the parameter named "fldId", the value of this parameter can be a valid folder UUID or record node.

Example of fldId:

  • Using UUID -> "c6785440-0d6c-45da-1234-d9874563d0f";

About the parameter named "dstId", the value of this parameter can be a valid folder UUID or record node.

Example of dstId:

  • Using UUID -> "c4455667-0d6c-45da-1234-d98564323e0";

Also, in all methods you'll see a parameter named "token". Because Cron tasks are executed in the background without authentication, the methods used in this scenario might use the token parameter. From the default application execution context you must use the "null" value, which indicates the application must use the "user session".

In special cases you might be "promoted to Administrator" using the "administrator token".

String systemToken = DbSessionManager.getInstance().getSystemToken();

Methods

create

Description:

Method Return values Description

create(String token, Document doc, InputStream is)

Document

Create a new document.

The parameter doc must be initialized with the OpenKM path value in the repository. Optionally, you can also set keywords and title values.

The other parameters are not taken into consideration for document creation.

Example:

package com.openkm;

import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

import com.openkm.api.OKMDocument;
import com.openkm.bean.Document;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        InputStream is = null;
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            is = new FileInputStream("/home/openkm/sample.pdf");
            Document doc = new Document();
            doc.setPath("/okm:root/sample.pdf");
            // Optional
            doc.setTitle("sample document");
            doc.getKeywords().add("key1");
            doc.getKeywords().add("key2");
            // Create document
            doc = okmDocument.create(null, doc, is);
            System.out.println(doc);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
}

createSimple

Description:

Method Return values Description

createSimple(String token, String docPath, InputStream is)

Document

Create a new document.

The parameter docPath must be initialized with the OpenKM path value in the repository.

Example:

package com.openkm;

import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

import com.openkm.api.OKMDocument;
import com.openkm.bean.Document;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        InputStream is = null;
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);

            is = new FileInputStream("/home/openkm/sample.pdf");
            String docPath = "/okm:root/sample.pdf";
            Document doc = okmDocument.createSimple(null, docPath, is);
            System.out.println(doc);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
}

delete

Description:

Method Return values Description

delete(String token, String docId)

void

Delete a document.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            okmDocument.delete(null, "d50133e3-dbfa-4d01-a109-28785cd48f40");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getProperties

Description:

Method Return values Description

getProperties(String token, String docId)

Document

Returns a document's properties.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.bean.Document;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            Document doc = okmDocument.getProperties(null, "3887ae75-dd67-4019-b4d1-2f6962815403");
            System.out.println(doc);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getContent

Description:

Method Return values Description

getContent(String token, String docId, boolean checkout)

InputStream

Returns the document content.

When the parameter checkout is true, the document is marked for editing.

Example:

package com.openkm;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        InputStream is = null;
        OutputStream fos = null;
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            is = okmDocument.getContent(null, "3887ae75-dd67-4019-b4d1-2f6962815403", false);
            File tmp = new File("/home/openkm/sample.pdf");
            fos = new FileOutputStream(tmp);
            IOUtils.copy(is, fos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(fos);
        }
    }
}

getContentByVersion

Description:

Method Return values Description

getContentByVersion(String token, String docId, String versionId)

InputStream

Returns the document content for a specific version.

The parameter versionId is the name of the document version.

Example:

package com.openkm;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        InputStream is = null;
        OutputStream fos = null;
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            is = okmDocument.getContentByVersion(null, "f3a977f7-abfc-4f58-a033-988903b31473", "1.1");
            File tmp = new File("/home/openkm/sample.pdf");
            fos = new FileOutputStream(tmp);
            IOUtils.copy(is, fos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(fos);
        }
    }
}

getExtractedText

Description:

Method Return values Description

getExtractedText(String token, String docId)

String

Return the extracted text of the document.

When a document is uploaded to OpenKM, it goes into the search engine queue to be processed. Until the process is finished, this method will return an empty string.

From the Database query view, you can execute a query to verify whether a document has already been processed:

SELECT NDC_TEXT_EXTRACTED FROM OKM_NODE_DOCUMENT where NBS_UUID='1048eb46-2f32-4011-90e4-e084d838a592'

The NBS_UUID value is the document UUID. You can get it from the properties tab in the desktop view.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            String indexedText = okmDocument.getExtractedText(null, "f3a977f7-abfc-4f58-a033-988903b31473");
            System.out.println(indexedText);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getChildren

Description:

Method Return values Description

getChildren(String token, String fldId)

List<Document>

Returns a list of all documents whose parent is fldId.

The parameter fldId can be a folder or a record node.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.bean.Document;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            for (Document doc : okmDocument.getChildren(null, "b67fe3ae-bcca-4947-b8ea-0cc7aedd4474")) {
                System.out.println(doc);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

rename

Description:

Method Return values Description

rename(String token, String docId, String newName)

Document

Changes the name of a document.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.bean.Document;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            Document doc = okmDocument.rename(null, "f3a977f7-abfc-4f58-a033-988903b31473", "samples_renamed.pdf");
            System.out.println(doc);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

setProperties

Description:

Method Return values Description

setProperties(String token, Document doc)

void

Changes some document properties.

Variables allowed to be changed:

  • Title
  • Description
  • Language
  • Associated categories
  • Associated keywords

Only non-null and non-empty variables will be taken into consideration.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.bean.Document;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            Document doc = okmDocument.getProperties(null, "f3a977f7-abfc-4f58-a033-988903b31473");
            doc.getKeywords().add("key1");
            doc.setDescription("some description");
            okmDocument.setProperties(null, doc);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

setLanguage

Description:

Method Return values Description

setLanguage(String token, String docId, String lang)

void

Sets the document language.

The parameter lang must be ISO 639-1 compliant.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            okmDocument.setLanguage(null, "f3a977f7-abfc-4f58-a033-988903b31473", "en");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

setTitle

Description:

Method Return values Description

setTitle(String token, String docId, String title)

void

Sets the document title.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            okmDocument.setTitle(null, "f3a977f7-abfc-4f58-a033-988903b31473", "Document title");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

checkout

Description:

Method Return values Description

checkout(String token, String docId)

void

Marks the document for editing.

Only one user can modify a document at a time.

Before starting editing you must perform a checkout action that locks the editing process for other users and allows editing only to the user who executed the action.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            okmDocument.checkout(null, "f3a977f7-abfc-4f58-a033-988903b31473");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

cancelCheckout

Description:

Method Return values Description

cancelCheckout(String token, String docId)

void

Cancels document editing.

This action can only be done by the user who previously executed the checkout action.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            okmDocument.cancelCheckout(null, "f3a977f7-abfc-4f58-a033-988903b31473");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

forceCancelCheckout

Description:

Method Return values Description

forceCancelCheckout(String token, String docId)

void

Cancels document editing.

This method allows cancelling editing on any document.

It is not mandatory to execute this action by the same user who previously executed the checkout action.

This action can only be done by a superuser (user with ROLE_ADMIN).

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            okmDocument.forceCancelCheckout(null, "f3a977f7-abfc-4f58-a033-988903b31473");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

isCheckedOut

Description:

Method Return values Description

isCheckedOut(String token, String docId)

void

Returns a boolean that indicates whether the document is being edited or not.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            System.out.println(okmDocument.isCheckedOut(null, "f3a977f7-abfc-4f58-a033-988903b31473"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

checkin

Description:

Method Return values Description

checkin(String token, String docId, InputStream is, String comment)

Version

Updates a document with a new version and returns an object with new Version values.

Only the user who started the editing - checkout - is allowed to update the document.

Example:

package com.openkm;

import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        InputStream is = null;
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            is = new FileInputStream("/home/openkm/sample.pdf");
            okmDocument.checkin(null, "f3a977f7-abfc-4f58-a033-988903b31473", is, "optional comment");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
}

checkin

Description:

Method Return values Description

checkin(String token, String docId, InputStream is, String comment, int increment)

Version

Updates a document with a new version and returns an object with new Version values.

The increment value depends on VersionNumeration implementation. Common values are:

  • VersionNumerationAdapter.INCREASE_DEFAULT for default.
  • VersionNumerationAdapter.INCREASE_MINOR for increasing a minor version.
  • VersionNumerationAdapter.INCREASE_MAJOR for increasing a major version.
  • VersionNumerationAdapter.INCREASE_MAJOR_MINOR for increasing a major-minor version.

More information about VersionNumeration at Creating your own Version Number Adapter.

Only the user who started the editing - checkout - is allowed to update the document.

Example:

package com.openkm;

import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

import com.openkm.api.OKMDocument;
import com.openkm.plugin.vernum.VersionNumerationAdapter;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        InputStream is = null;
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            is = new FileInputStream("/home/openkm/sample.pdf");
            okmDocument.checkin(null, "f3a977f7-abfc-4f58-a033-988903b31473", is, "optional comment",
                    VersionNumerationAdapter.INCREASE_MINOR);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
}

lock

Description:

Method Return values Description

lock(String token, String docId)

LockInfo

Locks a document and returns an object with the Lock information.

Only the user who locked the document is allowed to unlock.

A locked document cannot be modified by other users.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.bean.LockInfo;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            LockInfo lockInfo = okmDocument.lock(null, "f3a977f7-abfc-4f58-a033-988903b31473");
            System.out.println(lockInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

unlock

Description:

Method Return values Description

unlock(String token, String docId)

LockInfo

Unlocks a locked document.

Only the user who locked the document is allowed to unlock.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            okmDocument.unlock(null, "f3a977f7-abfc-4f58-a033-988903b31473");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

forceUnlock

Description:

Method Return values Description

forceUnlock(String token, String docId)

LockInfo

Unlocks a locked document.

This method allows to unlock any locked document.

It is not mandatory execute this action by the same user who previously executed the checkout lock action.

This action can only be done by a super user ( user with ROLE_ADMIN ).

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            okmDocument.forceUnlock(null, "f3a977f7-abfc-4f58-a033-988903b31473");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

isLocked

Description:

Method Return values Description

isLocked(String token, String docId)

void

Returns a boolean that indicates if the document is locked or not.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            System.out.println(okmDocument.isLocked(null, "f3a977f7-abfc-4f58-a033-988903b31473"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getLockInfo

Description:

Method Return values Description

getLockInfo(String token, String docId)

LockInfo

Returns an object with the Lock information.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.bean.LockInfo;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            LockInfo lockInfo = okmDocument.getLockInfo(null, "f3a977f7-abfc-4f58-a033-988903b31473");
            System.out.println(lockInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

purge

Description:

Method Return values Description

purge(String token, String docId)

void

The document is permanently removed from the repository.

Usually you will purge documents into /okm:trash/userId - the personal trash user location - but it is possible to directly purge any document from the whole repository.

When a document is purged it will only be able to be restored from a previous repository backup. The purge action removes the document permanently from the repository.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            okmDocument.purge(null, "f3a977f7-abfc-4f58-a033-988903b31473");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

move

Description:

Method Return values Description

move(String token, String docId, String dstId)

void

Move a document into a folder or record.

The value of the dstId parameter should be a folder or record UUID.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            okmDocument.move(null, "f3a977f7-abfc-4f58-a033-988903b31473", "b67fe3ae-bcca-4947-b8ea-0cc7aedd4474");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

copy

Description:

Method Return values Description

copy(String token, String docId, String dstId)

void

Copy a document into a folder or record.

The values of the dstId parameter should be a folder or record UUID.

Only the binary data and the security grants are copied to the destination; the metadata, keywords, etc. of the document are not copied.

See "extendedCopy" method for this feature.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            okmDocument.copy(null, "f3a977f7-abfc-4f58-a033-988903b31473", "b67fe3ae-bcca-4947-b8ea-0cc7aedd4474");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

copy

Description:

Method Return values Description

copy(String token, String docId, String dstId, String newName)

void

Copy a document into some folder or record.

The values of the dstId parameter should be a folder or record UUID.

When the parameter newName is null, the document will preserve the same name.

Only the binary data and the security grants are copied to destination, the metadata, keywords, etc. of the document are not copied.

See "extendedCopy" method for this feature.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            okmDocument.copy(null, "f3a977f7-abfc-4f58-a033-988903b31473", "b67fe3ae-bcca-4947-b8ea-0cc7aedd4474",
                    "samples_renamed.pdf");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

extendedCopy

Description:

Method Return values Description

extendedCopy(String token, String docId, String dstId, String docName, ExtendedAttributes extAttr)

void

Copies a document with associated data into some folder or record.

The values of the dstId parameter should be a folder or a record UUID.

When the parameter docName value is null, the document will preserve the same name.

By default only the binary data and the security grants are copied; the metadata, keywords, etc. of the document are not copied.

Additional:

  • When the category parameter is true the original values of the categories will be copied.
  • When the keywords parameter is true the original values of the keywords will be copied.
  • When the propertyGroups parameter is true the original values of the metadata groups will be copied.
  • When the notes parameter is true the original value of the notes will be copied.
  • When wiki parameter is true the original value of the wiki will be copied.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.bean.ExtendedAttributes;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            ExtendedAttributes extAttr = new ExtendedAttributes();
            // copy keywords
            extAttr.setKeywords(true);
            extAttr.setCategories(true);
            extAttr.setNotes(true);
            extAttr.setPropertyGroups(true);

            okmDocument.extendedCopy(null, "f3a977f7-abfc-4f58-a033-988903b31473", "b67fe3ae-bcca-4947-b8ea-0cc7aedd4474", "samples_renamed.pdf", extAttr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getVersionHistorySize

Description:

Method Return values Description

getVersionHistorySize(String token, String docId)

long

Returns the sum in bytes of all documents in the document's history.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;
import com.openkm.util.FormatUtil;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            System.out.println(FormatUtil.formatSize(okmDocument.getVersionHistorySize(null, "f3a977f7-abfc-4f58-a033-988903b31473")));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

isValid

Description:

Method Return values Description

isValid(String token, String docId)

boolean

Returns a boolean that indicates if the node is a document or not.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            boolean valid = okmDocument.isValid(null, "f3a977f7-abfc-4f58-a033-988903b31473");
            System.out.println(valid);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getPath

Description:

Method Return values Description

getPath(String token, String uuid)

String

Converts a document UUID to document path.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            String docPath = okmDocument.getPath(null, "f3a977f7-abfc-4f58-a033-988903b31473");
            System.out.println(docPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getDetectedLanguages

Description:

Method Return values Description

getDetectedLanguages(String token)

List<String>

Returns a list of available document languages that OpenKM can identify.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            for (String languages : okmDocument.getDetectedLanguages(null)) {
                System.out.println(languages);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

createFromTemplate

Description:

Method Return values Description

createFromTemplate(String token, String docId, String dstPath, Map<String, String> properties, ExtendedAttributes attributes)

Document

Creates a new document from a template and returns a Document object.

The dstPath can be a folder or a record UUID.

The parameter language is optional.

When the template uses metadata groups to fill fields, then these values are mandatory and must be set in the properties parameter. 

For more information about Templates and metadata check: Creating templates.

Additional attribute parameters:

  • When category parameter is true the original values of the categories will be copied.
  • When keywords parameter is true the original values of the keywords will be copied.
  • When propertyGroups parameter is true the original values of the metadata groups will be copied.
  • When notes parameter is true the original values of the notes will be copied.
  • When wiki parameter is true the original values of the wiki will be copied.

Example:

The example below is based on Creating PDF template sample.

package com.openkm;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

import com.google.gson.Gson;
import com.openkm.api.OKMDocument;
import com.openkm.bean.ExtendedAttributes;
import com.openkm.util.ContextWrapper;
import com.openkm.util.ISO8601;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            Map<String, String> properties = new HashMap<>();
            properties.put("okp:tpl.name", "Some name");
            Calendar cal = Calendar.getInstance();
            // Value must be converted to String ISO 8601 compliant
            properties.put("okp:tpl.bird_date", ISO8601.formatBasic(cal));

            List<String> list = new ArrayList<>();
            list.add("java");
            properties.put("okp:tpl.language", new Gson().toJson(list).toString());

            okmDocument.createFromTemplate(null, "86633fe8-1b0d-48ac-a716-7685022adad8", "/okm:root/templates.pdf", properties, new ExtendedAttributes());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

updateFromTemplate

Description:

Method Return values Description

updateFromTemplate(String token, String docId, String dstId, Map<String, String> properties)

void

Updates a document previously created from the template.

This method only makes sense when the template uses metadata groups to fill fields in.

For more information about Templates and metadata check: Creating templates.

Example:

The example below is based on Creating PDF template sample.

package com.openkm;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;
import com.openkm.util.ISO8601;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            Map<String, String> properties = new HashMap<>();
            properties.put("okp:tpl.name", "Some name");
            Calendar cal = Calendar.getInstance();
            // Value must be converted to String ISO 8601 compliant
            properties.put("okp:tpl.bird_date", ISO8601.formatBasic(cal));

            List<String> list = new ArrayList<>();
            list.add("java");
            properties.put("okp:tpl.language", new Gson().toJson(list).toString());

            okmDocument.updateFromTemplate(null, "86633fe8-1b0d-48ac-a716-7685022adad8", "b67fe3ae-bcca-4947-b8ea-0cc7aedd4474", properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

setAnnotations

Description:

Method Return values Description

setAnnotations(String token, String docId, String verName, String annotation)

void

Sets annotations for a document version.

Example:

package com.openkm;
import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            okmDocument.setAnnotations(null, "3887ae75-dd67-4019-b4d1-2f6962815403", "1.1", "The annotation text.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getAnnotations

Description:

Method Return values Description

getAnnotations(String token, String docId, String verName)

String

Returns the document annotations for a document version.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            String annotation = okmDocument.getAnnotations(null, "3887ae75-dd67-4019-b4d1-2f6962815403", "1.1");
            System.out.println(annotation);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getCheckedOut

Description:

Method Return values Description

getCheckedOut(String token)

List<Document>

Returns the list of all checked-out documents in the repository.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.bean.Document;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            for (Document doc : okmDocument.getCheckedOut(null)) {
                System.out.println(doc);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

stamp

Description:

Method Return values Description

stamp(String token, String docId, int type, long stId)

void

Stamps a document.

The parameter type indicates stamp type:

  • "text" with value 0;
  • "image" with value 1;

The parameter stId is the unique identifier of the stamp definition. You can get it from Administration > Stamp view.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            int type = 0; // indicate text stamp definition
            long stId = 1; // unique identifier of the stamp definition
            okmDocument.stamp(null, "3887ae75-dd67-4019-b4d1-2f6962815403", type, stId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

stampText

Description:

Method Return values Description

stampText(String token, String docId, long stId, String range, String exprX, String exprY, String text)

void

Stamp a document with custom text.

The id is the id of an image stamp created by the administrator.

The exprX is the expression to set the position in X coordinates.

The exprY is the expression to set the position in Y coordinates.

In Expr. X and Expr. Y input fields you can enter more than a simple number. Currently, the following macros are defined:

  • IMAGE_WIDTH
  • IMAGE_HEIGHT
  • PAGE_WIDTH
  • PAGE_HEIGHT
  • PAGE_CENTER
  • PAGE_MIDDLE

So to center a stamp on the page you can use:

Expr. X

PAGE_CENTER

Expr. Y

PAGE_MIDDLE

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper; import java.io.FileInputStream; import java.io.InputStream; public class Test { public static void main(String[] args) { try { long stampTextId = 1; String text = "OpenKM"; String exprX = "PAGE_CENTER"; String exprY = "PAGE_MIDDLE"; InputStream is = new FileInputStream("/home/gnujavasergio/okm/logo.png"); OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class); okmDocument.stampText(null, "05b14eee5-2e57-4d1d-925a-c9bc3f526e24", stampTextId, "", exprX, exprY, text); } catch (Exception e) { e.printStackTrace(); } } }

stampImage

Description:

Method Return values Description

stampImage(String token, String docId, long stId, String range, String exprX, String exprY, InputStream is)

void

Stamp a document with a custom image.

The id is the id of an image stamp created by the administrator.

The exprX is the expression to set the position in X coordinates.

The exprY is the expression to set the position in Y coordinates.

In Expr. X and Expr. Y input fields you can enter more than a simple number. Currently, the following macros are defined:

  • IMAGE_WIDTH
  • IMAGE_HEIGHT
  • PAGE_WIDTH
  • PAGE_HEIGHT
  • PAGE_CENTER
  • PAGE_MIDDLE

So to center a stamp on the page you can use:

Expr. X

PAGE_CENTER

Expr. Y

PAGE_MIDDLE

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper; import java.io.FileInputStream; import java.io.InputStream; public class Test { public static void main(String[] args) { try { long id = 1; String exprX = "PAGE_CENTER - IMAGE_WIDTH / 2"; String exprY = "PAGE_MIDDLE - IMAGE_HEIGHT / 2"; InputStream is = new FileInputStream("/home/gnujavasergio/okm/logo.png");
OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class); okmDocument.stampImage(null, "055b5206-35d0-4351-ba92-c304bbba7ddf", id, "", exprX, exprY, is); } catch (Exception e) { e.printStackTrace(); } } }

setDescription

Description:

Method Return values Description

setDescription(String token, String docId, String description)

void

Set a description.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            okmDocument.setDescription(null, "7ce1b4a8-4ade-4dce-8d7d-4e99a6cd368b", "some description");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

setNodeClass

Description:

Method Return values Description

setNodeClass(String token, String docId, long ncId)

void

Set the NodeClass.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            long ncId = 2;
            okmDocument.setNodeClass(null, "7ce1b4a8-4ade-4dce-8d7d-4e99a6cd368b", ncId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

setDispositionStage

Description:

Method Return values Description

setDispositionStage(String token, String docId, long stage)

void

Set the disposition stage

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            long stage = 1;
            okmDocument.setDispositionStage(null, "7ce1b4a8-4ade-4dce-8d7d-4e99a6cd368b", stage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

createWizard

Description:

Method Return values Description

createWizard(String token, String docPath, long nodeClass, InputStream is)

WizardNode

Create a new document with a wizard.

The parameter docPath should be a valid folder or record PATH.

The WizardNode contains a list of pending actions that should be done to complete the document creation process. These might be:

  • Add keyword
  • Add Categories
  • Add Metadata

Example:

package com.openkm;

import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

import com.openkm.api.OKMDocument;
import com.openkm.bean.WizardNode;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            InputStream is = new FileInputStream("/home/gnujavasergio/okm/logo.png");
            WizardNode wn = okmDocument.createWizard(null, "/okm:root/test/logo.png", 0, is);
            System.out.print(wn);
            IOUtils.closeQuietly(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getNumberOfPages

Description:

Method Return values Description

getNumberOfPages(String token, String uuid)

int

Get the number of pages in a document.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            System.out.println("The number of pages is : " + okmDocument.getNumberOfPages(null, "b2f88679-e3fd-4f97-bf0e-abf76f9ec499"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getPageAsImage

Description:

Method Return values Description

getPageAsImage(String token, String uuid, int pageNumber, int maxWidth, int maxHeight)

String

Return a specific page as an image encoded as a Base64 string.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            System.out.println(okmDocument.getPageAsImage(null, "3fe350e2-69e8-4681-a97c-6ac4ba6c1524", 1, 150, 150));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

isConvertibleToPDF

Description:

Method Return values Description

isConvertibleToPDF(String docId)

Boolean

Returns a boolean that indicates whether the node is convertible to PDF.

 

In the case of a node of type PDF file, a false value will be returned.

 

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper; public class Test { public static void main(String[] args) { try { OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class); System.out.println(okmDocument.isConvertibleToPDF("9ed5c7b1-9314-4479-8f80-fd8e2b47f55e")); } catch (Exception e) { e.printStackTrace(); } } }

setIndexable

Description:

Method Return values Description

setIndexable(String uuid, boolean enabled)

void

Sets whether the document is indexable.

Example:

package com.openkm;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper; public class Test { public static void main(String[] args) { try { OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class); okmDocument.setIndexable("19938640-fd34-421c-90c8-6b435e4b7d79", true); Document document = okmDocument.getDocumentProperties("19938640-fd34-421c-90c8-6b435e4b7d79"); System.out.println("Document indexable:" + document.isIndexable()); } catch (Exception e) { e.printStackTrace(); } } }

mergePdf

Description:

Method Return values Description

mergePdf(String token, String destinationPath, String docName, List<String> paths)

void

Merge two or more PDF files.

The parameter destinationPath should be a valid folder or record PATH.

Example:

package com.openkm;

import java.util.ArrayList;
import java.util.List;

import com.openkm.api.OKMDocument;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
            List<String> uuids = new ArrayList<>();
            uuids.add("/okm:root/test/document.pdf");
            uuids.add("/okm:root/test/document1.pdf");
            String fldUuid = "/okm:root/mergue";
            okmDocument.mergePdf(null, fldUuid, "MergePdf.pdf", uuids);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}