001/*
002 * Copyright (c) 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 * $Id$
013 */
014
015package org.nuxeo.ecm.core.event.impl;
016
017import java.io.Serializable;
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022import java.util.Map.Entry;
023
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.core.api.DocumentRef;
026import org.nuxeo.ecm.core.event.Event;
027import org.nuxeo.ecm.core.event.EventBundle;
028import org.nuxeo.ecm.core.event.EventContext;
029
030/**
031 * Light Event implementation. Used to reduce memory footprint of {@link Event} stacked in {@link EventBundle}.
032 *
033 * @author Thierry Delprat
034 */
035public class ShallowEvent extends EventImpl {
036
037    private static final long serialVersionUID = 1L;
038
039    public static ShallowEvent create(Event event) {
040        EventContext ctx = event.getContext();
041        List<Object> newArgs = new ArrayList<Object>();
042        for (Object arg : ctx.getArguments()) {
043            Object newArg = arg;
044            if (arg instanceof DocumentModel) {
045                DocumentModel oldDoc = (DocumentModel) arg;
046                DocumentRef ref = oldDoc.getRef();
047                if (ref != null) {
048                    newArg = new ShallowDocumentModel(oldDoc);
049                } else {
050                    newArg = null;
051                }
052            }
053            // XXX treat here other cases !!!!
054            newArgs.add(newArg);
055        }
056
057        EventContext newCtx = null;
058        if (ctx instanceof DocumentEventContext) {
059            newCtx = new DocumentEventContext(null, ctx.getPrincipal(), (DocumentModel) newArgs.get(0),
060                    (DocumentRef) newArgs.get(1));
061        } else {
062            newCtx = new EventContextImpl(null, ctx.getPrincipal());
063            ((EventContextImpl) newCtx).setArgs(newArgs.toArray());
064        }
065
066        newCtx.setRepositoryName(ctx.getRepositoryName());
067        Map<String, Serializable> newProps = new HashMap<String, Serializable>();
068        for (Entry<String, Serializable> prop : ctx.getProperties().entrySet()) {
069            Serializable propValue = prop.getValue();
070            if (propValue instanceof DocumentModel) {
071                DocumentModel oldDoc = (DocumentModel) propValue;
072                propValue = new ShallowDocumentModel(oldDoc);
073            }
074            // XXX treat here other cases !!!!
075            newProps.put(prop.getKey(), propValue);
076        }
077        newCtx.setProperties(newProps);
078        return new ShallowEvent(event.getName(), newCtx, event.getFlags(), event.getTime());
079    }
080
081    public ShallowEvent(String name, EventContext ctx, int flags, long creationTime) {
082        super(name, ctx, flags, creationTime);
083    }
084
085}