001package com.avaje.ebean.event.changelog;
002
003import java.util.ArrayList;
004import java.util.LinkedHashMap;
005import java.util.List;
006import java.util.Map;
007
008/**
009 * Holds a set of changes.
010 */
011public class ChangeSet {
012
013  /**
014   * A UUID transaction id specifically created for the change set.
015   */
016  String txnId;
017
018  /**
019   * For large transactions with many change sets this is an incrementing counter.
020   */
021  long txnBatch;
022
023  /**
024   * The state of the transaction (change sets can be sent prior to commit or rollback
025   * with large transactions).
026   */
027  TxnState txnState;
028
029  /**
030   * User defined 'source' such as the application name.
031   */
032  String source;
033
034  /**
035   * Application user id expected to be optionally populated by ChangeLogPrepare.
036   */
037  String userId;
038
039  /**
040   * Application user ip address expected to be optionally populated by ChangeLogPrepare.
041   */
042  String userIpAddress;
043
044  /**
045   * Arbitrary user context information expected to be optionally populated by ChangeLogPrepare.
046   */
047  Map<String,String> userContext;
048
049  /**
050   * The bean changes.
051   */
052  List<BeanChange> changes = new ArrayList<BeanChange>();
053
054  /**
055   * Construct with a txnId.
056   */
057  public ChangeSet(String txnId, long txnBatch) {
058    this.txnId = txnId;
059    this.txnBatch = txnBatch;
060    this.txnState = TxnState.IN_PROGRESS;
061  }
062
063  /**
064   * Default constructor for JSON tools.
065   */
066  public ChangeSet() {
067  }
068
069  public String toString() {
070    return "txnId:" + txnId + " txnState:" + txnState + " txnBatch:" + txnBatch;
071  }
072
073  /**
074   * Return the number of changes in the change set.
075   */
076  public int size() {
077    return changes.size();
078  }
079
080  /**
081   * Add a bean change to the change set.
082   */
083  public void addBeanChange(BeanChange beanChange) {
084    changes.add(beanChange);
085  }
086
087  /**
088   * Return the txnId.
089   */
090  public String getTxnId() {
091    return txnId;
092  }
093
094  /**
095   * Set the txnId (used by JSON tools).
096   */
097  public void setTxnId(String txnId) {
098    this.txnId = txnId;
099  }
100
101  /**
102   * Returns the batch id.
103   */
104  public long getTxnBatch() {
105    return txnBatch;
106  }
107
108  /**
109   * Sets the batch id (used by JSON tools).
110   */
111  public void setTxnBatch(long txnBatch) {
112    this.txnBatch = txnBatch;
113  }
114
115  /**
116   * Return the transaction state. This will be IN_PROGRESS for many changeSets in large transactions
117   * as the changeSets are sent in batches before the transaction has completed.
118   */
119  public TxnState getTxnState() {
120    return txnState;
121  }
122
123  /**
124   * Set the state (used by JSON tools).
125   */
126  public void setTxnState(TxnState txnState) {
127    this.txnState = txnState;
128  }
129
130  /**
131   * Return a code that identifies the source of the change (like the name of the application).
132   */
133  public String getSource() {
134    return source;
135  }
136
137  /**
138   * Set the source of the change (like the name of the application).
139   */
140  public void setSource(String source) {
141    this.source = source;
142  }
143
144  /**
145   * Return the application user Id.
146   */
147  public String getUserId() {
148    return userId;
149  }
150
151  /**
152   * Set the application user Id.
153   * <p>
154   * This can be set by the ChangeLogListener in the prepare() method which is called
155   * in the foreground thread.
156   * </p>
157   */
158  public void setUserId(String userId) {
159    this.userId = userId;
160  }
161
162  /**
163   * Return the application users ip address.
164   */
165  public String getUserIpAddress() {
166    return userIpAddress;
167  }
168
169  /**
170   * Set the application users ip address.
171   * <p>
172   * This can be set by the ChangeLogListener in the prepare() method which is called
173   * in the foreground thread.
174   * </p>
175   */
176  public void setUserIpAddress(String userIpAddress) {
177    this.userIpAddress = userIpAddress;
178  }
179
180  /**
181   * Return a user context value - anything you set yourself in ChangeLogListener prepare().
182   */
183  public Map<String,String> getUserContext() {
184    if (userContext == null) {
185      userContext = new LinkedHashMap<String, String>();
186    }
187    return userContext;
188  }
189
190  /**
191   * Set a user context value (anything you like).
192   * <p>
193   * This can be set by the ChangeLogListener in the prepare() method which is called
194   * in the foreground thread.
195   * </p>
196   */
197  public void setUserContext(Map<String,String> userContext) {
198    this.userContext = userContext;
199  }
200
201  /**
202   * Return the bean changes.
203   */
204  public List<BeanChange> getChanges() {
205    return changes;
206  }
207
208  /**
209   * Set the bean changes (used by JSON tools).
210   */
211  public void setChanges(List<BeanChange> changes) {
212    this.changes = changes;
213  }
214}