How can you know which document properties have been modified?How can you know which document properties have been modified?

A question that’s coming now and then is how do you know which document properties have been changed? I remember someone asking that on Nuxeo Answers but I can’t find the question anymore.

Anyway this can be useful for many things. First example would be thumbnail generation. The thumb is computed from the main document file. If this file changes, we want to update the thumb. To be aware of that change, you need to listen to the right event.

When you save modifications on a doc, several events occur. Some of the most interesting for us are beforeDocumentModification and aboutToCreate. It’s the perfect time to know what properties are going to be modified. These events context are instances of DocumentEventContext, which means we can retrieve the source document where the event occurred. All you have to do next is introspect the document properties to know which one have been modified. Here’s a listener to test this:

* (C) Copyright 2006-2012 Nuxeo SA ( and contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public License
* (LGPL) version 2.1 which accompanies this distribution, and is available at
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* Lesser General Public License for more details.
* Contributors:
* ldoguin
package org.nuxeo.ecm.core.event.test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.model.DocumentPart;
import org.nuxeo.ecm.core.api.model.Property;
import org.nuxeo.ecm.core.event.Event;
import org.nuxeo.ecm.core.event.EventListener;
import org.nuxeo.ecm.core.event.impl.DocumentEventContext;

* @author ldoguin
public class DirtyPropGetterTestListener implements EventListener {

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

public void handleEvent(Event event) throws ClientException {
if (event.getContext() instanceof DocumentEventContext) {
DocumentEventContext context = (DocumentEventContext) event.getContext();
DocumentModel doc = context.getSourceDocument();
List<String> lastDirtyFields = getDirtyPropertiesXPath(doc);
for (String xpath : lastDirtyFields) {

public List<String> getDirtyPropertiesXPath(DocumentModel doc)
throws ClientException {
List<String> dirtyPropertiesName = new ArrayList<String>();
DocumentPart[] docParts = doc.getParts();
for (DocumentPart docPart : docParts) {
Iterator<Property> dirtyChildrenIterator = docPart.getDirtyChildren();
while (dirtyChildrenIterator.hasNext()) {
Property property =;
if (!property.isContainer() && property.isDirty()) {
dirtyPropertiesName.add(docPart.getName() + ":"
+ property.getField().getName().getLocalName());
} else {
List<Property> dirtyProps = addChildrenDirtyProperties(
property, new ArrayList<Property>());
for (Property dirtyProperty : dirtyProps) {
dirtyPropertiesName.add(docPart.getName() + ":"
+ dirtyProperty.getPath().substring(1));
return dirtyPropertiesName;

private List<Property> addChildrenDirtyProperties(Property property,
List<Property> dirtyProperties) {
if (!property.isContainer() && property.isDirty()) {
return dirtyProperties;
} else {
Iterator<Property> dirtyChildrenIterator = property.getDirtyChildren();
while (dirtyChildrenIterator.hasNext()) {
Property chilProperty =;
dirtyProperties = addChildrenDirtyProperties(chilProperty,
return dirtyProperties;

What we do here is verify that the event context is indeed a DocumentEventContext, cast it and retrieve the event’s source document from it. Once we have the document, we can start introspecting its DocumentPart. It’s basically a schema representation from which you can retrieve every properties and check if they are dirty or not. Then we store the properties xPath in a list and log them with the debug level. Don’t forget the XML declaration for your listener:

<?xml version="1.0"?>
<component name="test-dirtyProplistener">
<extension target="org.nuxeo.ecm.core.event.EventServiceComponent"
<listener async="false" postCommit="false"

FYI, this is going to be added to the DocumentEventContext in Nuxeo 5.7 and is already in the trunk. That’s it for today, see you Monday :)