001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.ecm.core.event.impl;
020
021import java.io.Serializable;
022import java.security.Principal;
023import java.util.Map;
024
025import org.nuxeo.ecm.core.api.CoreSession;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.DocumentRef;
028
029/**
030 * Specialized implementation to be used with an abstract session
031 *
032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
033 * @author tiry
034 */
035public class DocumentEventContext extends EventContextImpl {
036
037    private static final long serialVersionUID = 1L;
038
039    public static final String CATEGORY_PROPERTY_KEY = "category";
040
041    public static final String COMMENT_PROPERTY_KEY = "comment";
042
043    public DocumentEventContext(CoreSession session, Principal principal, DocumentModel source) {
044        super(session, principal, source, null);
045    }
046
047    public DocumentEventContext(CoreSession session, Principal principal, DocumentModel source, DocumentRef destDoc) {
048        super(session, principal, source, destDoc);
049    }
050
051    public DocumentModel getSourceDocument() {
052        return (DocumentModel) args[0];
053    }
054
055    public DocumentRef getDestination() {
056        return (DocumentRef) args[1];
057    }
058
059    public String getCategory() {
060        Serializable data = getProperty(CATEGORY_PROPERTY_KEY);
061        if (data instanceof String) {
062            return (String) data;
063        }
064        return null;
065    }
066
067    public void setCategory(String category) {
068        setProperty(CATEGORY_PROPERTY_KEY, category);
069    }
070
071    public String getComment() {
072        Serializable data = getProperty(COMMENT_PROPERTY_KEY);
073        if (data instanceof String) {
074            return (String) data;
075        }
076        return null;
077    }
078
079    public void setComment(String comment) {
080        setProperty(COMMENT_PROPERTY_KEY, comment);
081    }
082
083    @Override
084    public void setProperties(Map<String, Serializable> properties) {
085        // preserve Category/Comment from transparent override
086        String comment = getComment();
087        String category = getCategory();
088        super.setProperties(properties);
089        if (getComment() == null) {
090            setComment(comment);
091        }
092        if (getCategory() == null) {
093            setCategory(category);
094        }
095    }
096
097}