• create Doc: when exist -> increase version number

  • We tried to make OpenKM as intuitive as possible, but an advice is always welcome.
We tried to make OpenKM as intuitive as possible, but an advice is always welcome.
Forum rules: Please, before asking something see the documentation wiki or use the search feature of the forum. And remember we don't have a crystal ball or mental readers, so if you post about an issue tell us which OpenKM are you using and also the browser and operating system version. For more info read How to Report Bugs Effectively.
 #49649  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) {
}
 #49659  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

About Us

OpenKM is part of the management software. A management software is a program that facilitates the accomplishment of administrative tasks. OpenKM is a document management system that allows you to manage business content and workflow in a more efficient way. Document managers guarantee data protection by establishing information security for business content.