mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-24 23:56:36 +00:00
#390 - ChangeLog - Change userContext to be Map<String,String>
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package com.avaje.ebean.event.changelog;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Holds a set of changes.
|
||||
@@ -42,7 +44,7 @@ public class ChangeSet {
|
||||
/**
|
||||
* Arbitrary user context information expected to be optionally populated by ChangeLogPrepare.
|
||||
*/
|
||||
String userContext;
|
||||
Map<String,String> userContext;
|
||||
|
||||
/**
|
||||
* The bean changes.
|
||||
@@ -178,7 +180,10 @@ public class ChangeSet {
|
||||
/**
|
||||
* Return a user context value - anything you set yourself in ChangeLogListener prepare().
|
||||
*/
|
||||
public String getUserContext() {
|
||||
public Map<String,String> getUserContext() {
|
||||
if (userContext == null) {
|
||||
userContext = new LinkedHashMap<String, String>();
|
||||
}
|
||||
return userContext;
|
||||
}
|
||||
|
||||
@@ -189,7 +194,7 @@ public class ChangeSet {
|
||||
* in the foreground thread.
|
||||
* </p>
|
||||
*/
|
||||
public void setUserContext(String userContext) {
|
||||
public void setUserContext(Map<String,String> userContext) {
|
||||
this.userContext = userContext;
|
||||
}
|
||||
|
||||
|
||||
@@ -67,14 +67,25 @@ public class ChangeJsonBuilder {
|
||||
gen.writeStringField("txnState", changeSet.getTxnState().getCode());
|
||||
gen.writeNumberField("txnBatch", changeSet.getTxnBatch());
|
||||
gen.writeNumberField("txnPosition", position);
|
||||
gen.writeStringField("userId", changeSet.getUserId());
|
||||
String source = changeSet.getSource();
|
||||
if (source != null) {
|
||||
gen.writeStringField("source", source);
|
||||
}
|
||||
String userId = changeSet.getUserId();
|
||||
if (userId != null) {
|
||||
gen.writeStringField("userId", userId);
|
||||
}
|
||||
String userIpAddress = changeSet.getUserIpAddress();
|
||||
if (userIpAddress != null) {
|
||||
gen.writeStringField("userIpAddress", userIpAddress);
|
||||
}
|
||||
String userContext = changeSet.getUserContext();
|
||||
if (userContext != null) {
|
||||
gen.writeStringField("userContext", userContext);
|
||||
Map<String, String> userContext = changeSet.getUserContext();
|
||||
if (userContext != null && !userContext.isEmpty()) {
|
||||
gen.writeObjectFieldStart("userContext");
|
||||
for (Map.Entry<String, String> entry : userContext.entrySet()) {
|
||||
gen.writeStringField(entry.getKey(), entry.getValue());
|
||||
}
|
||||
gen.writeEndObject();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ import org.junit.Test;
|
||||
import java.io.StringWriter;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
|
||||
|
||||
public class ChangeJsonBuilderTest extends BaseTestCase {
|
||||
|
||||
@@ -22,11 +24,17 @@ public class ChangeJsonBuilderTest extends BaseTestCase {
|
||||
ChangeJsonBuilder builder = new ChangeJsonBuilder(jsonContext);
|
||||
|
||||
ChangeSet changeSet = helper.createChangeSet("ABCD", 10);
|
||||
changeSet.getUserContext().put("altUser", "role user");
|
||||
|
||||
List<BeanChange> changes = changeSet.getChanges();
|
||||
for (int i = 0; i < changes.size(); i++) {
|
||||
StringWriter buffer = new StringWriter();
|
||||
builder.writeBeanJson(buffer, changes.get(i), changeSet, i);
|
||||
System.out.println(buffer.toString());
|
||||
assertThat(buffer.toString()).contains("\"source\"");
|
||||
assertThat(buffer.toString()).contains("\"userId\"");
|
||||
assertThat(buffer.toString()).contains("\"userIpAddress\"");
|
||||
assertThat(buffer.toString()).contains("\"userContext\"");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ public class Helper {
|
||||
cs.setSource("myApp");
|
||||
cs.setUserId("user234");
|
||||
cs.setUserIpAddress("123.4.5.6");
|
||||
cs.setUserContext("user defined input");
|
||||
cs.getUserContext().put("someKey","user defined input");
|
||||
|
||||
List<BeanChange> changes = cs.getChanges();
|
||||
|
||||
|
||||
+17
-225
@@ -1,228 +1,20 @@
|
||||
[
|
||||
{
|
||||
"id": 4,
|
||||
"status": "ACTIVE",
|
||||
"name": "NocCust",
|
||||
"billingAddress": {
|
||||
"id": 5,
|
||||
"line1": "Bos town",
|
||||
"city": "Auckland",
|
||||
"country": {
|
||||
"code": "NZ",
|
||||
"name": "New Zealand",
|
||||
"countryExtra": "extra2"
|
||||
},
|
||||
"billingExtra": "extra1"
|
||||
{
|
||||
"txnId": "ABCD",
|
||||
"txnState": "C",
|
||||
"txnBatch": 0,
|
||||
"txnPosition": 0,
|
||||
"userId": "user234",
|
||||
"userIpAddress": "123.4.5.6",
|
||||
"object": "mytable",
|
||||
"objectId": "11",
|
||||
"change": "I",
|
||||
"eventTime": 1440465178268,
|
||||
"values": {
|
||||
"name": {
|
||||
"new": "rob"
|
||||
},
|
||||
"contacts": [
|
||||
{
|
||||
"id": 10,
|
||||
"firstName": "Jim1",
|
||||
"email": null,
|
||||
"contactExtra": "extra3"
|
||||
},
|
||||
{
|
||||
"id": 11,
|
||||
"firstName": "Fred1",
|
||||
"email": null,
|
||||
"contactExtra": "extra3"
|
||||
},
|
||||
{
|
||||
"id": 12,
|
||||
"firstName": "Bugs1",
|
||||
"email": null,
|
||||
"contactExtra": "extra3"
|
||||
}
|
||||
],
|
||||
"selected": null,
|
||||
"customerExtra": "extra4",
|
||||
"startObj": {
|
||||
"yesno": true
|
||||
},
|
||||
"secObj": {
|
||||
"yesno": false
|
||||
},
|
||||
"startList": [
|
||||
true,
|
||||
"one",
|
||||
1,
|
||||
2,
|
||||
34.45,
|
||||
null
|
||||
],
|
||||
"secondList": [
|
||||
3,
|
||||
"rawVal"
|
||||
],
|
||||
"rawField": {
|
||||
"ra": 42
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"status": "ACTIVE",
|
||||
"name": "Fiona",
|
||||
"billingAddress": {
|
||||
"id": 3,
|
||||
"line1": "West Coast Rd",
|
||||
"city": "Auckland",
|
||||
"country": {
|
||||
"code": "NZ",
|
||||
"name": "New Zealand",
|
||||
"countryExtra": "extra2"
|
||||
},
|
||||
"billingExtra": "extra1"
|
||||
},
|
||||
"contacts": [
|
||||
{
|
||||
"id": 5,
|
||||
"firstName": "Jim1",
|
||||
"email": null,
|
||||
"contactExtra": "extra3"
|
||||
},
|
||||
{
|
||||
"id": 6,
|
||||
"firstName": "Fred1",
|
||||
"email": null,
|
||||
"contactExtra": "extra3"
|
||||
},
|
||||
{
|
||||
"id": 7,
|
||||
"firstName": "Bugs1",
|
||||
"email": null,
|
||||
"contactExtra": "extra3"
|
||||
},
|
||||
{
|
||||
"id": 8,
|
||||
"firstName": "Fiona",
|
||||
"email": "black2@test.com",
|
||||
"contactExtra": "extra3"
|
||||
},
|
||||
{
|
||||
"id": 9,
|
||||
"firstName": "Tracy",
|
||||
"email": "red3@test.com",
|
||||
"contactExtra": "extra3"
|
||||
}
|
||||
],
|
||||
"selected": null,
|
||||
"customerExtra": "extra4",
|
||||
"startObj": {
|
||||
"yesno": true
|
||||
},
|
||||
"secObj": {
|
||||
"yesno": false
|
||||
},
|
||||
"startList": [
|
||||
true,
|
||||
"one",
|
||||
1,
|
||||
2,
|
||||
34.45,
|
||||
null
|
||||
],
|
||||
"secondList": [
|
||||
3,
|
||||
"rawVal"
|
||||
],
|
||||
"rawField": {
|
||||
"ra": 42
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"status": "NEW",
|
||||
"name": "Cust NoAddress",
|
||||
"billingAddress": null,
|
||||
"contacts": [
|
||||
{
|
||||
"id": 4,
|
||||
"firstName": "Jack",
|
||||
"email": "black1@test.com",
|
||||
"contactExtra": "extra3"
|
||||
}
|
||||
],
|
||||
"selected": null,
|
||||
"customerExtra": "extra4",
|
||||
"startObj": {
|
||||
"yesno": true
|
||||
},
|
||||
"secObj": {
|
||||
"yesno": false
|
||||
},
|
||||
"startList": [
|
||||
true,
|
||||
"one",
|
||||
1,
|
||||
2,
|
||||
34.45,
|
||||
null
|
||||
],
|
||||
"secondList": [
|
||||
3,
|
||||
"rawVal"
|
||||
],
|
||||
"rawField": {
|
||||
"ra": 42
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"status": "NEW",
|
||||
"name": "Rob",
|
||||
"billingAddress": {
|
||||
"id": 1,
|
||||
"line1": "P.O.Box 1234",
|
||||
"city": "Auckland",
|
||||
"country": {
|
||||
"code": "NZ",
|
||||
"name": "New Zealand",
|
||||
"countryExtra": "extra2"
|
||||
},
|
||||
"billingExtra": "extra1"
|
||||
},
|
||||
"contacts": [
|
||||
{
|
||||
"id": 1,
|
||||
"firstName": "Jim1",
|
||||
"email": null,
|
||||
"contactExtra": "extra3"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"firstName": "Fred1",
|
||||
"email": null,
|
||||
"contactExtra": "extra3"
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"firstName": "Bugs1",
|
||||
"email": null,
|
||||
"contactExtra": "extra3"
|
||||
}
|
||||
],
|
||||
"selected": null,
|
||||
"customerExtra": "extra4",
|
||||
"startObj": {
|
||||
"yesno": true
|
||||
},
|
||||
"secObj": {
|
||||
"yesno": false
|
||||
},
|
||||
"startList": [
|
||||
true,
|
||||
"one",
|
||||
1,
|
||||
2,
|
||||
34.45,
|
||||
null
|
||||
],
|
||||
"secondList": [
|
||||
3,
|
||||
"rawVal"
|
||||
],
|
||||
"rawField": {
|
||||
"ra": 42
|
||||
"modified": {
|
||||
"new": 1440465178267
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user