001package io.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<>();
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  @Override
070  public String toString() {
071    return "txnId:" + txnId + " txnState:" + txnState + " txnBatch:" + txnBatch;
072  }
073
074  /**
075   * Return the number of changes in the change set.
076   */
077  public int size() {
078    return changes.size();
079  }
080
081  /**
082   * Add a bean change to the change set.
083   */
084  public void addBeanChange(BeanChange beanChange) {
085    changes.add(beanChange);
086  }
087
088  /**
089   * Return the txnId.
090   */
091  public String getTxnId() {
092    return txnId;
093  }
094
095  /**
096   * Set the txnId (used by JSON tools).
097   */
098  public void setTxnId(String txnId) {
099    this.txnId = txnId;
100  }
101
102  /**
103   * Returns the batch id.
104   */
105  public long getTxnBatch() {
106    return txnBatch;
107  }
108
109  /**
110   * Sets the batch id (used by JSON tools).
111   */
112  public void setTxnBatch(long txnBatch) {
113    this.txnBatch = txnBatch;
114  }
115
116  /**
117   * Return the transaction state. This will be IN_PROGRESS for many changeSets in large transactions
118   * as the changeSets are sent in batches before the transaction has completed.
119   */
120  public TxnState getTxnState() {
121    return txnState;
122  }
123
124  /**
125   * Set the state (used by JSON tools).
126   */
127  public void setTxnState(TxnState txnState) {
128    this.txnState = txnState;
129  }
130
131  /**
132   * Return a code that identifies the source of the change (like the name of the application).
133   */
134  public String getSource() {
135    return source;
136  }
137
138  /**
139   * Set the source of the change (like the name of the application).
140   */
141  public void setSource(String source) {
142    this.source = source;
143  }
144
145  /**
146   * Return the application user Id.
147   */
148  public String getUserId() {
149    return userId;
150  }
151
152  /**
153   * Set the application user Id.
154   * <p>
155   * This can be set by the ChangeLogListener in the prepare() method which is called
156   * in the foreground thread.
157   * </p>
158   */
159  public void setUserId(String userId) {
160    this.userId = userId;
161  }
162
163  /**
164   * Return the application users ip address.
165   */
166  public String getUserIpAddress() {
167    return userIpAddress;
168  }
169
170  /**
171   * Set the application users ip address.
172   * <p>
173   * This can be set by the ChangeLogListener in the prepare() method which is called
174   * in the foreground thread.
175   * </p>
176   */
177  public void setUserIpAddress(String userIpAddress) {
178    this.userIpAddress = userIpAddress;
179  }
180
181  /**
182   * Return a user context value - anything you set yourself in ChangeLogListener prepare().
183   */
184  public Map<String, String> getUserContext() {
185    if (userContext == null) {
186      userContext = new LinkedHashMap<>();
187    }
188    return userContext;
189  }
190
191  /**
192   * Set a user context value (anything you like).
193   * <p>
194   * This can be set by the ChangeLogListener in the prepare() method which is called
195   * in the foreground thread.
196   * </p>
197   */
198  public void setUserContext(Map<String, String> userContext) {
199    this.userContext = userContext;
200  }
201
202  /**
203   * Return the bean changes.
204   */
205  public List<BeanChange> getChanges() {
206    return changes;
207  }
208
209  /**
210   * Set the bean changes (used by JSON tools).
211   */
212  public void setChanges(List<BeanChange> changes) {
213    this.changes = changes;
214  }
215}