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.util.Map;
023
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.DocumentRef;
027import org.nuxeo.ecm.core.api.NuxeoPrincipal;
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, NuxeoPrincipal principal, DocumentModel source) {
044        super(session, principal, source, null);
045    }
046
047    public DocumentEventContext(CoreSession session, NuxeoPrincipal principal, DocumentModel source,
048            DocumentRef destDoc) {
049        super(session, principal, source, destDoc);
050    }
051
052    public DocumentModel getSourceDocument() {
053        return (DocumentModel) args[0];
054    }
055
056    public DocumentRef getDestination() {
057        return (DocumentRef) args[1];
058    }
059
060    public String getCategory() {
061        Serializable data = getProperty(CATEGORY_PROPERTY_KEY);
062        if (data instanceof String) {
063            return (String) data;
064        }
065        return null;
066    }
067
068    public void setCategory(String category) {
069        setProperty(CATEGORY_PROPERTY_KEY, category);
070    }
071
072    public String getComment() {
073        Serializable data = getProperty(COMMENT_PROPERTY_KEY);
074        if (data instanceof String) {
075            return (String) data;
076        }
077        return null;
078    }
079
080    public void setComment(String comment) {
081        setProperty(COMMENT_PROPERTY_KEY, comment);
082    }
083
084    @Override
085    public void setProperties(Map<String, Serializable> properties) {
086        // preserve Category/Comment from transparent override
087        String comment = getComment();
088        String category = getCategory();
089        super.setProperties(properties);
090        if (getComment() == null) {
091            setComment(comment);
092        }
093        if (getCategory() == null) {
094            setCategory(category);
095        }
096    }
097
098}