001package io.ebean.event.readaudit;
002
003import java.util.LinkedHashMap;
004import java.util.List;
005import java.util.Map;
006
007/**
008 * Read event sent to the ReadEventLogger.
009 * <p>
010 * This is a flattened in that it contains either a read bean or list of beans. It is flattened
011 * in this way to simplify logging and processing and simply means that it either contains an
012 * id or a list of ids.
013 * </p>
014 */
015public class ReadEvent {
016
017  /**
018   * User defined 'source' such as the application name.
019   */
020  protected String source;
021
022  /**
023   * Application user id expected to be optionally populated by ChangeLogPrepare.
024   */
025  protected String userId;
026
027  /**
028   * Application user ip address expected to be optionally populated by ChangeLogPrepare.
029   */
030  protected String userIpAddress;
031
032  /**
033   * Arbitrary user context information expected to be optionally populated by ChangeLogPrepare.
034   */
035  protected Map<String, String> userContext;
036
037  /**
038   * The time the bean change was created.
039   */
040  protected long eventTime;
041
042  /**
043   * The type of the bean(s) read.
044   */
045  protected String beanType;
046
047  /**
048   * The query key (relative to the bean type).
049   */
050  protected String queryKey;
051
052  /**
053   * The bind log when the query was executed.
054   */
055  protected String bindLog;
056
057  /**
058   * The id of the bean read.
059   */
060  protected Object id;
061
062  /**
063   * The ids of the beans read.
064   */
065  protected List<Object> ids;
066
067  /**
068   * Common constructor for single bean and multi-bean read events.
069   */
070  protected ReadEvent(String beanType, String queryKey, String bindLog) {
071    this.beanType = beanType;
072    this.queryKey = queryKey;
073    this.bindLog = bindLog;
074    this.eventTime = System.currentTimeMillis();
075  }
076
077  /**
078   * Construct for a single bean read.
079   */
080  public ReadEvent(String beanType, String queryKey, String bindLog, Object id) {
081    this(beanType, queryKey, bindLog);
082    this.id = id;
083  }
084
085  /**
086   * Construct for many beans read.
087   */
088  public ReadEvent(String beanType, String queryKey, String bindLog, List<Object> ids) {
089    this(beanType, queryKey, bindLog);
090    this.ids = ids;
091  }
092
093  /**
094   * Construct for many future list query.
095   */
096  public ReadEvent(String beanType) {
097    this.beanType = beanType;
098    this.eventTime = System.currentTimeMillis();
099  }
100
101  /**
102   * Constructor for JSON tools.
103   */
104  public ReadEvent() {
105  }
106
107  /**
108   * Return a code that identifies the source of the change (like the name of the application).
109   */
110  public String getSource() {
111    return source;
112  }
113
114  /**
115   * Set the source of the change (like the name of the application).
116   */
117  public void setSource(String source) {
118    this.source = source;
119  }
120
121  /**
122   * Return the application user Id.
123   */
124  public String getUserId() {
125    return userId;
126  }
127
128  /**
129   * Set the application user Id.
130   * <p>
131   * This can be set by the ChangeLogListener in the prepare() method which is called
132   * in the foreground thread.
133   * </p>
134   */
135  public void setUserId(String userId) {
136    this.userId = userId;
137  }
138
139  /**
140   * Return the application users ip address.
141   */
142  public String getUserIpAddress() {
143    return userIpAddress;
144  }
145
146  /**
147   * Set the application users ip address.
148   * <p>
149   * This can be set by the ChangeLogListener in the prepare() method which is called
150   * in the foreground thread.
151   * </p>
152   */
153  public void setUserIpAddress(String userIpAddress) {
154    this.userIpAddress = userIpAddress;
155  }
156
157  /**
158   * Return a user context value - anything you set yourself in ChangeLogListener prepare().
159   */
160  public Map<String, String> getUserContext() {
161    if (userContext == null) {
162      userContext = new LinkedHashMap<>();
163    }
164    return userContext;
165  }
166
167  /**
168   * Set a user context value (anything you like).
169   * <p>
170   * This can be set by the ChangeLogListener in the prepare() method which is called
171   * in the foreground thread.
172   * </p>
173   */
174  public void setUserContext(Map<String, String> userContext) {
175    this.userContext = userContext;
176  }
177
178  /**
179   * Return the type of bean read.
180   */
181  public String getBeanType() {
182    return beanType;
183  }
184
185  /**
186   * Set the type of bean read.
187   */
188  public void setBeanType(String beanType) {
189    this.beanType = beanType;
190  }
191
192  /**
193   * Return the query key (relative to the bean type).
194   */
195  public String getQueryKey() {
196    return queryKey;
197  }
198
199  /**
200   * Set the query key (relative to the bean type).
201   */
202  public void setQueryKey(String queryKey) {
203    this.queryKey = queryKey;
204  }
205
206  /**
207   * Return the bind log used when executing the query.
208   */
209  public String getBindLog() {
210    return bindLog;
211  }
212
213  /**
214   * Set the bind log used when executing the query.
215   */
216  public void setBindLog(String bindLog) {
217    this.bindLog = bindLog;
218  }
219
220  /**
221   * Return the event date time.
222   */
223  public long getEventTime() {
224    return eventTime;
225  }
226
227  /**
228   * Set the event date time.
229   */
230  public void setEventTime(long eventTime) {
231    this.eventTime = eventTime;
232  }
233
234  /**
235   * Return the id of the bean read.
236   */
237  public Object getId() {
238    return id;
239  }
240
241  /**
242   * Set the id of the bean read.
243   */
244  public void setId(Object id) {
245    this.id = id;
246  }
247
248  /**
249   * Return the ids of the beans read.
250   */
251  public List<Object> getIds() {
252    return ids;
253  }
254
255  /**
256   * Set the ids of the beans read.
257   */
258  public void setIds(List<Object> ids) {
259    this.ids = ids;
260  }
261}