cancel
Showing results for 
Search instead for 
Did you mean: 

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

Romina_Racca
Champ on-the-rise
Champ on-the-rise

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.

3 REPLIES 3

Vladimir_Pasqui
Star Collaborator
Star Collaborator

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

Romina_Racca
Champ on-the-rise
Champ on-the-rise

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;

}

}

Thanks a lot!

Getting started

Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.