How to modify author user of documents for paste action?

Actual Nuxeo DM interface provide a way to do a copy and paste feature. This is really cool, but when you do that the author user of documents copied are the same of original documents.

I need for my project to modify that: I need to set the user executing the action be the author of the document.

How can do that?

0 votes

1 answers

2203 views

ANSWER



To give you a short answer to explain the typical approach of achieving this type of functionality in Nuxeo, what you generally want here is to update a document property holding the document author. The property used for that is the first field in the list property called dc:contributors. Now, if it were a scalar (non-list) property it would be very easy to set this kind of functionality using Studio only. You would just need to intercept the event of creating a document by copy and update the chosen property using the Document.UpdateProperty operation. Unfortunately, that operation works currently only with scalar values.

Now for a long one - the functionality you are looking for can be achieved by combining Nuxeo Studio and Nuxeo IDE functionalities. Here is what you will need to do to update a list property (dc:contributors):

1) Using Nuxeo IDE in Eclipse create a new operation (it is available in one of the wizards). Let's call the new operation “Document.SetListProperty” where in your run method you just update the List property with your value, something similar to this:


   @OperationMethod(collector = DocumentModelCollector.class)
    public DocumentModel run(DocumentModel doc) throws Exception {
        Property p = doc.getProperty(xpath);
        Type type = p.getField().getType();
        if (!type.isListType()) {
            throw new OperationException(
                    "Only list types can be set using the update list operation");
        }
        if (value.getClass() == String.class) {
            p.setValue(DocumentHelper.readStringList((String)value));
        }
        if (save) {
            doc = session.saveDocument(doc);
        }
        return doc;
    }

To learn more about operations please read how to contribute an operation from the Nuxeo Documentation.

2) Now register your new operation in Nuxeo Studio, so that you could later associate it with your event handler.

Here is where you can do it:

Settings & Versioning > AutomationOperations 
(this section contains already a working example which you can easily modify)

Just modify the category, operation name, etc to match your new operation. If this is still a little confusing, try to compare your new method with Document.SetProperty documentation which you can find here: Studio Operation Document.SetProperty

3) In Nuxeo Studio create an automation chain (let's name it UpdateTitle) with just 2 steps:

  1. Fetch current document (the default operation)
  2. Document.SetListProperty
    value = @{CurrentUser.name}
    xpath = dc:contributors
    
    In this case the contributors list will just have one element.

4) In Nuxeo Studio create an event handler launched on the following event: Document created by copy (you can find it in the dropdown list of available events) and then select from another dropdown the existing operation that you just added: “UpdateTitle”.

5) Now, when you deploy your two jars - the Nuxeo Studio one and the one you created in Eclipse, start your Nuxeo DM and create a new document by copy, the event will be intercepted and the author updated.

0 votes