001/*
002 * Copyright (c) 2006-2011 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 *     bstefanescu
011 */
012package org.nuxeo.ecm.core.event.impl;
013
014import java.io.Serializable;
015import java.security.Principal;
016import java.util.Map;
017
018import org.nuxeo.ecm.core.api.CoreSession;
019import org.nuxeo.ecm.core.api.DocumentModel;
020import org.nuxeo.ecm.core.api.DocumentRef;
021
022/**
023 * Specialized implementation to be used with an abstract session
024 *
025 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
026 * @author tiry
027 */
028public class DocumentEventContext extends EventContextImpl {
029
030    private static final long serialVersionUID = 1L;
031
032    public static final String CATEGORY_PROPERTY_KEY = "category";
033
034    public static final String COMMENT_PROPERTY_KEY = "comment";
035
036    public DocumentEventContext(CoreSession session, Principal principal, DocumentModel source) {
037        super(session, principal, source, null);
038    }
039
040    public DocumentEventContext(CoreSession session, Principal principal, DocumentModel source, DocumentRef destDoc) {
041        super(session, principal, source, destDoc);
042    }
043
044    public DocumentModel getSourceDocument() {
045        return (DocumentModel) args[0];
046    }
047
048    public DocumentRef getDestination() {
049        return (DocumentRef) args[1];
050    }
051
052    public String getCategory() {
053        Serializable data = getProperty(CATEGORY_PROPERTY_KEY);
054        if (data instanceof String) {
055            return (String) data;
056        }
057        return null;
058    }
059
060    public void setCategory(String category) {
061        setProperty(CATEGORY_PROPERTY_KEY, category);
062    }
063
064    public String getComment() {
065        Serializable data = getProperty(COMMENT_PROPERTY_KEY);
066        if (data instanceof String) {
067            return (String) data;
068        }
069        return null;
070    }
071
072    public void setComment(String comment) {
073        setProperty(COMMENT_PROPERTY_KEY, comment);
074    }
075
076    @Override
077    public void setProperties(Map<String, Serializable> properties) {
078        // preserve Category/Comment from transparent override
079        String comment = getComment();
080        String category = getCategory();
081        super.setProperties(properties);
082        if (getComment() == null) {
083            setComment(comment);
084        }
085        if (getCategory() == null) {
086            setCategory(category);
087        }
088    }
089
090}