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 *     Nuxeo - initial API and implementation
011 *
012 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
013 */
014
015package org.nuxeo.ecm.core.api.event.impl;
016
017import java.security.Principal;
018import java.util.Date;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.nuxeo.ecm.core.api.event.CoreEvent;
024
025/**
026 * Nuxeo core event implementation.
027 *
028 * @author <a href="mailto:ja@nuxeo.com">Julien Anguenot</a>
029 * @author <a href="mailto:tmartins@nuxeo.com">Thierry Martins</a>
030 */
031public class CoreEventImpl implements CoreEvent {
032
033    protected final String eventId;
034
035    protected final Object source;
036
037    protected final Map<String, ?> info;
038
039    protected final Date date;
040
041    protected final Principal principal;
042
043    protected final String category;
044
045    protected final String comment;
046
047    // Interesting attributes to make accessible in the eventInfo
048    public static final String COMMENT_ATTRIBUTE = "comment";
049
050    public static final String CATEGORY_ATTRIBUTE = "category";
051
052    @SuppressWarnings("unchecked")
053    public CoreEventImpl(String eventId, Object source, Map<String, ?> info, Principal principal, String category,
054            String comment) {
055        date = new Date();
056        if (eventId != null) {
057            this.eventId = eventId.intern();
058        } else {
059            this.eventId = null;
060        }
061        this.source = source;
062        if (info == null) {
063            this.info = new HashMap<String, Object>();
064        } else {
065            this.info = new HashMap<String, Object>(info);
066        }
067        this.principal = principal;
068
069        // CB: NXP-2253 - Values passed as parameters will be put into the info
070        // map only if the map doesn't contain the corresponding keys.
071        if (!this.info.containsKey(COMMENT_ATTRIBUTE)) {
072            ((Map) this.info).put(COMMENT_ATTRIBUTE, comment);
073        }
074        if (!this.info.containsKey(CATEGORY_ATTRIBUTE)) {
075            ((Map) this.info).put(CATEGORY_ATTRIBUTE, category);
076        }
077
078        this.comment = comment;
079        this.category = category;
080    }
081
082    public boolean isComposite() {
083        return false;
084    }
085
086    public List<CoreEvent> getNestedEvents() {
087        return null;
088    }
089
090    @Override
091    public String getEventId() {
092        return eventId;
093    }
094
095    @Override
096    public Map<String, ?> getInfo() {
097        return info;
098    }
099
100    @Override
101    public Object getSource() {
102        return source;
103    }
104
105    @Override
106    public String getCategory() {
107        if (category != null) {
108            return category;
109        } else {
110            Object categoryObj = info.get(CATEGORY_ATTRIBUTE);
111            if (categoryObj instanceof String) {
112                return (String) categoryObj;
113            } else {
114                return null;
115            }
116        }
117    }
118
119    @Override
120    public String getComment() {
121        if (comment != null) {
122            return comment;
123        } else {
124            Object commentObj = info.get(COMMENT_ATTRIBUTE);
125            if (commentObj instanceof String) {
126                return (String) commentObj;
127            } else {
128                return null;
129            }
130        }
131    }
132
133    @Override
134    public Date getDate() {
135        return date;
136    }
137
138    @Override
139    public Principal getPrincipal() {
140        return principal;
141    }
142
143    @Override
144    public String toString() {
145        final StringBuilder buf = new StringBuilder();
146
147        buf.append(CoreEventImpl.class.getSimpleName());
148        buf.append(" {");
149        buf.append(" eventId: ");
150        buf.append(eventId);
151        buf.append(", source: ");
152        buf.append(source);
153        buf.append(", info: ");
154        buf.append(info);
155        buf.append(", date: ");
156        buf.append(date);
157        buf.append(", principal name: ");
158        if (principal != null) {
159            buf.append(principal.getName());
160        }
161        buf.append(", comment: ");
162        buf.append(getComment());
163        buf.append(", category: ");
164        buf.append(getCategory());
165        buf.append('}');
166
167        return buf.toString();
168    }
169
170}