How to detect that there was a metadata modification to automatically increment the version number?

I want to know how to detect when a metadata has been modified. I want to compare the last number of version of the document and the working copy before I do Check In. When I modified some metadada (not a file) I want to increment minor version number automatically. And when I modified a file I want to increment major version number automatically.

Thanks in advance,

Kind regards.

1 votes

2 answers

1146 views

ANSWER



Hello,

You have to use a listener (http://doc.nuxeo.com/x/C4AO). You can create it under Studio (if you use it) or under Eclipse Nuxeo IDE plugin (wizard listener).

Subscribe to the event ABOUT_TO_CREATE (about to create listener) to check metadata updates before any changes have been commited.

Thanks

2 votes



My solution as contribution to the community.

Event: DOCUMENT_MODIFIED

public class ModificationListener implements EventListener {

private static final Log log = LogFactory.getLog(ModificationListener.class);

public void handleEvent(Event event) throws ClientException {

    EventContext ctx = event.getContext();
    if (!(ctx instanceof DocumentEventContext)) {
        // event is not tied to a document, we should not be here
        return;
    }

    // Fetch the document from the event context
    DocumentModel doc = ((DocumentEventContext) ctx).getSourceDocument();

    if (doc == null)
        return;

    if (!doc.isVersionable())
        return;

    try {
        BlobProperty currentBlob = (BlobProperty) doc.getProperty("file:content");
        BlobProperty lastBlob = getLastBlob(ctx, doc.getRef());

         //If currentDoc hasn't a file...
        if(currentBlob.getValue() == null){
            //... and never had a file ...
            if(lastBlob.getValue() == null){
                doc.checkIn(VersioningOption.MINOR, "Metadata modification.");  
            }else{
            //... or file delete    
                doc.checkIn(VersioningOption.MAJOR, "File delete");
            }
            return;
        }

        //If currentDoc has a file but lastDoc hasn't a file    
        if(lastBlob.getValue() == null){
            doc.checkIn(VersioningOption.MAJOR, "File creation");
            return;
        }   

        //If currentDoc has a file and lastDoc has a file ==> compare the digesto
        if(currentBlob.getValue("digest").equals(lastBlob.getValue("digest"))){
            doc.checkIn(VersioningOption.MINOR, "Metadata modification.");
        }else{
            doc.checkIn(VersioningOption.MAJOR, "File modification.");
        }

    } catch (PropertyException e) {
        //If currentDoc hasn't a file:contenct Ej: Nota
        doc.checkIn(VersioningOption.MINOR, "Metadata modification.");
        log.warn("[ERROR_1 Automatizacion de versionado]: "+e.getMessage());
    }

}


/**
 * 
 * @param ctx Contexto
 * @param ref Referencia al documento
 * @return la propiedad Blob del ultimo documento
 */
public BlobProperty getLastBlob(EventContext ctx, DocumentRef ref){
    DocumentModel lastDocVersion;
    try {
        lastDocVersion = ctx.getCoreSession().getVersions(ref).get(
                ctx.getCoreSession().getVersions(ref).size()-1);

        BlobProperty lastBlob = (BlobProperty) lastDocVersion.getProperty("file:content");  

        return lastBlob;

    } catch (PropertyException e) {
        log.warn("[ERROR_2 Automatizacion de versionado]: "+e.getMessage());
    } catch (ClientException e1) {
        log.warn("[ERROR_3 Automatizacion de versionado]: "+e1.getMessage());
    }
    return null;

}

}

1 votes



Thanks a lot!:D
02/11/2015