Page 1 of 1

create Doc: when exist -> increase version number

PostPosted:Thu Feb 13, 2020 10:02 am
by drago
Hello everybody,

i have an issue: in my java application i want to create a document, but the "user" will not know, if a document with the same name already exist. So i want to increase the version number, if the document already exist. How can i solve this issue?

I have a while loop, which repaet until the document was created. In the catch section i will try to increase the version number, but then i need the uuid from the existing document, but how will i get the uuid? The process will be without user interaction, so the increase should be automatically, something like: actualVersionNumber + 1
Code: Select all
OKMWebservices okm = OKMWebservicesFactory.newInstance(url, user, pass);

try {
    InputStream is = null;
    String docExtension = ".pdf";
    Document doc = new Document();
    String docPath = uploadFolder + "/" + docName + docExtension;
    doc.setPath(docPath);
    doc.setDescription("");
    doc.getKeywords().add("");
    doc.getKeywords().add("");

    boolean created = false;
    while (!created) {
        try {
            is = new FileInputStream(zugferdPdfFile.getAbsolutePath());
            okm.createDocument(doc, is);
            created = true;
        } catch (ItemExistsException ex) {
            // increase version number
            // ??
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
} catch (Exception ex) {
}

Re: create Doc: when exist -> increase version number

PostPosted:Sat Feb 15, 2020 7:14 pm
by jllort
You do not want to update the current document no? that should be the most common but seems you are more interested in creating a new document.

The first step is checking if document exist, for you have hasNode method ( https://docs.openkm.com/kcenter/view/sd ... ml#hasNode ):
Code: Select all
String suffix = ""
int count = 1;
while (ws.hasNode("/okm:root/folder/document" + suffix;  + ".pdf")) {
    suffix = String.valueOf(count);
    count++;
}
String dstPath = "/okm:root/folder/document" + suffix;  + ".pdf";
Another option is taking control of the ItemExistException
Code: Select all
public void createDocument(count, folderPath, docName) {
try {
    // Needed some extra code here for setting the right docName, etc... take as an idea
    ws.createSimple(XXXXXX);
} catch (ItemExistException e ) {
    createDocument(count++, folderPath, docName);
}
}

If the ItemExisException happens a few times, the second option is better because you are not doing extra hasNode method calling