com.clougence.cloudcanal.sdk.api.modelv2.CustomData Maven / Gradle / Ivy
package com.clougence.cloudcanal.sdk.api.modelv2;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import lombok.Getter;
/**
* @author bucketli 2021/12/1 10:07:29
*/
@Getter
public class CustomData {
private final SchemaInfo schemaInfo;
private final EventTypeInSdk eventType;
private final List records;
public CustomData(SchemaInfo schemaInfo, EventTypeInSdk eventType, List records){
this.schemaInfo = schemaInfo;
this.eventType = eventType;
this.records = records;
}
public CustomData genDeleteFromUpdate() {
if (eventType != EventTypeInSdk.UPDATE) {
throw new IllegalArgumentException("current operation is not a UPDATE.");
}
if (records == null) {
throw new IllegalArgumentException("UPDATE have no actual record change!");
}
List deleteRecords = new ArrayList<>();
for (CustomRecordV2 oriRecord : records) {
CustomRecordV2 insertRecord = new CustomRecordV2();
for (Map.Entry beforeField : oriRecord.getBeforeColumnMap().entrySet()) {
CustomFieldV2 newField = beforeField.getValue().cloneField();
insertRecord.getBeforeColumnMap().put(newField.getFieldName(), newField);
if (newField.isKey()) {
insertRecord.getBeforeKeyColumnMap().put(newField.getFieldName(), newField);
}
}
deleteRecords.add(insertRecord);
}
SchemaInfo cloneSchema = cloneSchemaInfo(schemaInfo);
return new CustomData(cloneSchema, EventTypeInSdk.DELETE, deleteRecords);
}
public CustomData genInsertFromUpdate() {
if (eventType != EventTypeInSdk.UPDATE) {
throw new IllegalArgumentException("current operation is not a UPDATE.");
}
if (records == null) {
throw new IllegalArgumentException("UPDATE have no actual record change!");
}
List insertRecords = new ArrayList<>();
for (CustomRecordV2 oriRecord : records) {
CustomRecordV2 insertRecord = new CustomRecordV2();
for (Map.Entry afterField : oriRecord.getAfterColumnMap().entrySet()) {
CustomFieldV2 newField = afterField.getValue().cloneField();
newField.setUpdated(true);
insertRecord.getAfterColumnMap().put(newField.getFieldName(), newField);
if (newField.isKey()) {
insertRecord.getAfterKeyColumnMap().put(newField.getFieldName(), newField);
}
}
insertRecords.add(insertRecord);
}
SchemaInfo cloneSchema = cloneSchemaInfo(schemaInfo);
return new CustomData(cloneSchema, EventTypeInSdk.INSERT, insertRecords);
}
public CustomData genUpdateFromDelete() {
if (eventType != EventTypeInSdk.DELETE) {
throw new IllegalArgumentException("current operation is not a DELETE.");
}
if (records == null) {
throw new IllegalArgumentException("DELETE have no actual record change!");
}
List updateRecords = new ArrayList<>();
for (CustomRecordV2 oriRecord : records) {
CustomRecordV2 deleteRecord = oriRecord.cloneRecord();
deleteRecord.getAfterColumnMap().clear();
deleteRecord.getAfterKeyColumnMap().clear();
for (Map.Entry beforeField : oriRecord.getBeforeColumnMap().entrySet()) {
CustomFieldV2 afterField = beforeField.getValue().cloneField();
afterField.setValue(null);
afterField.setNull(true);
afterField.setUpdated(true);
deleteRecord.getAfterColumnMap().put(afterField.getFieldName(), afterField);
if (afterField.isKey()) {
deleteRecord.getAfterKeyColumnMap().put(afterField.getFieldName(), afterField);
}
}
updateRecords.add(deleteRecord);
}
SchemaInfo cloneSchema = cloneSchemaInfo(schemaInfo);
return new CustomData(cloneSchema, EventTypeInSdk.UPDATE, updateRecords);
}
public SchemaInfo cloneSchemaInfo(SchemaInfo oriSchema) {
return new SchemaInfo(oriSchema.getCatalog(), oriSchema.getSchema(), oriSchema.getTable());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy