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