package foundation.dao.version;
|
|
import foundation.data.entity.Entity;
|
import foundation.data.entity.EntitySet;
|
import foundation.json.IJSONProvider;
|
import foundation.json.IJSONWriter;
|
import foundation.util.MapList;
|
|
public class PackageChange implements IJSONProvider {
|
|
private MapList<String, IDataChange> items;
|
|
public PackageChange() {
|
items = new MapList<String, IDataChange>();
|
}
|
|
public void compareEntity(String name, Entity one, Entity another) throws Exception {
|
EntityChange entityChanges = doCompareEntity(name, one, another);
|
|
if (entityChanges == null) {
|
return;
|
}
|
|
items.add(name, entityChanges);
|
}
|
|
public void compareEntitySet(String name, EntitySet one, EntitySet another) throws Exception {
|
if (one == null && another == null) {
|
return;
|
}
|
|
EntitySetChange entitySetChanges = new EntitySetChange(name);
|
items.add(name, entitySetChanges);
|
|
//1. 如果当前记录没有--已经删除
|
if (one == null) {
|
entitySetChanges.setChangeType(ChangeType.Delete);
|
return;
|
}
|
|
//2. 如果对照记录没有--新增加
|
if (another == null) {
|
entitySetChanges.setChangeType(ChangeType.Add);
|
return;
|
}
|
|
//3. 按Entity: one-->another 对比
|
for (Entity oneEntity: one) {
|
String id = oneEntity.getId();
|
Entity anotherEntity = another.getEntity("id", id);
|
|
EntityChange entityChange = doCompareEntity(id, oneEntity, anotherEntity);
|
|
if (entityChange == null) {
|
continue;
|
}
|
|
entitySetChanges.addOneEntityChange(id, entityChange);
|
}
|
|
//4. 按Entity: another-->one 对比
|
for (Entity anotherEntity: another) {
|
String id = anotherEntity.getId();
|
|
if (!one.contains("id", id)) {
|
EntityChange entityChange = new EntityChange(name);
|
entityChange.setChangeType(ChangeType.Delete);
|
|
entitySetChanges.addOneEntityChange(id, entityChange);
|
}
|
}
|
}
|
|
private EntityChange doCompareEntity(String name, Entity one, Entity another) throws Exception {
|
EntityChange entityChange = new EntityChange(name);
|
|
if (one == null && another == null) {
|
return null;
|
}
|
|
//1. 如果当前记录没有--已经删除
|
if (one == null) {
|
entityChange.setChangeType(ChangeType.Delete);
|
return entityChange;
|
}
|
|
//2. 如果对照记录没有--新增加
|
if (another == null) {
|
entityChange.setChangeType(ChangeType.Add);
|
return entityChange;
|
}
|
|
//3. 按字段对比数据
|
String[] fields = one.getFieldsMeta().getFieldNames();
|
int max = fields.length;
|
|
for (int i = 0; i < max; i++) {
|
Object oneValue = one.getValue(i);
|
Object anotherValue = another.getValue(i);
|
|
if (valueEquals(oneValue, anotherValue)) {
|
continue;
|
}
|
|
String oneJSON = one.getJSONString(i);
|
String anotherJSON = another.getJSONString(i);
|
|
entityChange.addOneFieldChange(fields[i], ChangeType.Change, oneValue, anotherValue, oneJSON, anotherJSON);
|
}
|
|
if (entityChange.isEmpty()) {
|
entityChange.setChangeType(ChangeType.NoChange);
|
return null;
|
}
|
|
entityChange.setChangeType(ChangeType.Change);
|
return entityChange;
|
}
|
|
private boolean valueEquals(Object oneValue, Object anotherValue) {
|
if (oneValue == null) {
|
if (anotherValue == null) {
|
return true;
|
}
|
else {
|
return false;
|
}
|
}
|
else {
|
if (anotherValue == null) {
|
return false;
|
}
|
|
return oneValue.equals(anotherValue);
|
}
|
}
|
|
@Override
|
public void writeJSON(IJSONWriter writer) {
|
writer.beginObject();
|
|
//1. write list
|
writer.beginArray("list");
|
|
for (IDataChange dataChange: items) {
|
writer.write(dataChange.getDataName());
|
}
|
|
writer.endArray();
|
|
//2. write changes
|
writer.beginArray("changes");
|
|
for (IDataChange dataChange: items) {
|
dataChange.writeJSON(writer);
|
}
|
|
writer.endArray();
|
}
|
|
}
|