Search in sources :

Example 1 with EventImpl

use of org.apache.jackrabbit.spi.commons.EventImpl in project jackrabbit by apache.

the class EventFactory method fromJCREvent.

public Event fromJCREvent(javax.jcr.observation.Event e) throws RepositoryException {
    Path p = e.getPath() != null ? resolver.getQPath(e.getPath()) : null;
    Path parent = p != null ? p.getAncestor(1) : null;
    int type = e.getType();
    NodeId parentId = parent != null ? idFactory.createNodeId((String) null, parent) : null;
    String identifier = e.getIdentifier();
    ItemId itemId;
    Node node = null;
    if (identifier != null) {
        itemId = idFactory.fromJcrIdentifier(e.getIdentifier());
        try {
            node = session.getItem(e.getPath()).getParent();
        } catch (RepositoryException re) {
        // ignore. TODO improve
        }
    } else {
        switch(type) {
            case Event.NODE_ADDED:
            case Event.NODE_MOVED:
                node = session.getItem(e.getPath()).getParent();
            case Event.NODE_REMOVED:
                itemId = idFactory.createNodeId((String) null, p);
                break;
            case Event.PROPERTY_ADDED:
            case Event.PROPERTY_CHANGED:
                node = session.getItem(e.getPath()).getParent();
            case Event.PROPERTY_REMOVED:
                itemId = idFactory.createPropertyId(parentId, p.getName());
                break;
            case Event.PERSIST:
            default:
                itemId = null;
        }
    }
    Name nodeTypeName = null;
    Name[] mixinTypes = Name.EMPTY_ARRAY;
    if (node != null) {
        try {
            parentId = idFactory.createNodeId(node.getUUID(), null);
        } catch (UnsupportedRepositoryOperationException ex) {
        // not referenceable
        }
        nodeTypeName = resolver.getQName(node.getPrimaryNodeType().getName());
        mixinTypes = getNodeTypeNames(node.getMixinNodeTypes(), resolver);
    }
    Map<Name, QValue> info = new HashMap<Name, QValue>();
    Map<String, Object> jcrInfo = e.getInfo();
    for (Map.Entry<String, Object> entry : jcrInfo.entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
        Name name = resolver.getQName(key);
        if (value != null) {
            // event information is generated for NODE_MOVED only in which
            // case all values are of type PATH.
            QValue v = ValueFormat.getQValue(value.toString(), PropertyType.PATH, resolver, qValueFactory);
            info.put(name, v);
        } else {
            info.put(name, null);
        }
    }
    return new EventImpl(e.getType(), p, itemId, parentId, nodeTypeName, mixinTypes, e.getUserID(), e.getUserData(), e.getDate(), info);
}
Also used : Path(org.apache.jackrabbit.spi.Path) UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) QValue(org.apache.jackrabbit.spi.QValue) HashMap(java.util.HashMap) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) ItemId(org.apache.jackrabbit.spi.ItemId) Name(org.apache.jackrabbit.spi.Name) EventImpl(org.apache.jackrabbit.spi.commons.EventImpl) NodeId(org.apache.jackrabbit.spi.NodeId) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 Node (javax.jcr.Node)1 RepositoryException (javax.jcr.RepositoryException)1 UnsupportedRepositoryOperationException (javax.jcr.UnsupportedRepositoryOperationException)1 ItemId (org.apache.jackrabbit.spi.ItemId)1 Name (org.apache.jackrabbit.spi.Name)1 NodeId (org.apache.jackrabbit.spi.NodeId)1 Path (org.apache.jackrabbit.spi.Path)1 QValue (org.apache.jackrabbit.spi.QValue)1 EventImpl (org.apache.jackrabbit.spi.commons.EventImpl)1