001/*
002 * (C) Copyright 2006-2012 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 org.nuxeo.ecm.core.event.Event;
022import org.nuxeo.ecm.core.event.EventContext;
023
024/**
025 * Event implementation.
026 *
027 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
028 */
029public class EventImpl implements Event {
030
031    private static final long serialVersionUID = 1L;
032
033    protected final String name;
034
035    protected final long time;
036
037    protected final EventContext ctx;
038
039    protected int flags;
040
041    protected Exception rollbackException;
042
043    protected String rollbackMessage;
044
045    public EventImpl(String name, EventContext ctx, int flags, long creationTime) {
046        this.name = name;
047        this.ctx = ctx;
048        time = creationTime;
049        this.flags = flags;
050    }
051
052    public EventImpl(String name, EventContext ctx, int flags) {
053        this(name, ctx, flags, System.currentTimeMillis());
054    }
055
056    public EventImpl(String name, EventContext ctx) {
057        this(name, ctx, FLAG_NONE);
058    }
059
060    @Override
061    public int getFlags() {
062        return flags;
063    }
064
065    @Override
066    public EventContext getContext() {
067        return ctx;
068    }
069
070    @Override
071    public String getName() {
072        return name;
073    }
074
075    @Override
076    public long getTime() {
077        return time;
078    }
079
080    @Override
081    public void cancel() {
082        flags |= FLAG_CANCEL;
083    }
084
085    @Override
086    public void markRollBack() {
087        flags |= FLAG_ROLLBACK;
088    }
089
090    @Override
091    public void markRollBack(String message, Exception exception) {
092        markRollBack();
093        if (message == null && exception != null) {
094            message = exception.getMessage();
095        }
096        rollbackMessage = message;
097        rollbackException = exception;
098    }
099
100    @Override
101    public void markBubbleException() {
102        flags |= FLAG_BUBBLE_EXCEPTION;
103    }
104
105    @Override
106    public boolean isBubbleException() {
107        return (flags & FLAG_BUBBLE_EXCEPTION) != 0;
108    }
109
110    @Override
111    public boolean isMarkedForRollBack() {
112        return (flags & FLAG_ROLLBACK) != 0;
113    }
114
115    @Override
116    public boolean isCanceled() {
117        return (flags & FLAG_CANCEL) != 0;
118    }
119
120    @Override
121    public boolean isInline() {
122        return (flags & FLAG_INLINE) != 0;
123    }
124
125    @Override
126    public void setInline(boolean isInline) {
127        if (isInline) {
128            flags |= FLAG_INLINE;
129        } else {
130            flags &= ~FLAG_INLINE;
131        }
132    }
133
134    @Override
135    public boolean isCommitEvent() {
136        return (flags & FLAG_COMMIT) != 0;
137    }
138
139    @Override
140    public void setIsCommitEvent(boolean isCommit) {
141        if (isCommit) {
142            flags |= FLAG_COMMIT;
143        } else {
144            flags &= ~FLAG_COMMIT;
145        }
146    }
147
148    @Override
149    public boolean isLocal() {
150        return (flags & FLAG_LOCAL) != 0;
151    }
152
153    @Override
154    public void setLocal(boolean isLocal) {
155        if (isLocal) {
156            flags |= FLAG_LOCAL;
157        } else {
158            flags &= ~FLAG_LOCAL;
159        }
160    }
161
162    @Override
163    public boolean isPublic() {
164        return !isLocal();
165    }
166
167    @Override
168    public void setPublic(boolean isPublic) {
169        setLocal(!isPublic);
170    }
171
172    @Override
173    public boolean isImmediate() {
174        return (flags & FLAG_IMMEDIATE) != 0;
175    }
176
177    @Override
178    public void setImmediate(boolean immediate) {
179        if (immediate) {
180            flags |= FLAG_IMMEDIATE;
181        } else {
182            flags &= ~FLAG_IMMEDIATE;
183        }
184    }
185
186    @Override
187    public Exception getRollbackException() {
188        return rollbackException;
189    }
190
191    @Override
192    public String getRollbackMessage() {
193        return rollbackMessage;
194    }
195
196}