package foundation.data.mapping;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
|
import foundation.data.entity.Entity;
|
import foundation.data.meta.field.Field;
|
import foundation.data.meta.field.FieldsRuntime;
|
import foundation.util.ContentBuilder;
|
import foundation.util.Util;
|
|
public class Mappings implements Iterable<FieldMapping> {
|
|
private String id;
|
private String name;
|
private boolean open;
|
private List<FieldMapping> mappingList;
|
private Map<String, FieldMapping> fromFieldMap;
|
private Map<String, FieldMapping> toFieldMap;
|
|
|
public Mappings() {
|
mappingList = new ArrayList<FieldMapping>();
|
fromFieldMap = new HashMap<String, FieldMapping>();
|
toFieldMap = new HashMap<String, FieldMapping>();
|
}
|
|
public Mappings(String id) {
|
this.id = id;
|
mappingList = new ArrayList<FieldMapping>();
|
fromFieldMap = new HashMap<String, FieldMapping>();
|
toFieldMap = new HashMap<String, FieldMapping>();
|
}
|
|
public static Mappings getInstance(FieldsRuntime one, FieldsRuntime another) {
|
Mappings result = new Mappings();
|
String fieldName = null; FieldMapping mapping = null; int idx = 0;
|
|
for (Field field: one) {
|
fieldName = field.getName();
|
|
if (!another.contains(fieldName)) {
|
continue;
|
}
|
|
mapping = new FieldMapping();
|
mapping.setFromName(fieldName);
|
mapping.setToName(fieldName);
|
mapping.setIndexNo(idx++);
|
|
result.loadOne(mapping);
|
}
|
|
return result;
|
}
|
|
public void load(Entity entity) throws Exception {
|
id = entity.getString("id");
|
name = entity.getString("name");
|
open = entity.getBoolean("is_open", true);
|
}
|
|
public MappingsRuntime createRuntime(FieldsRuntime fromMeta, FieldsRuntime toMeta, MappingDirection direction) throws Exception {
|
MappingsRuntime mappingSetRuntime = new MappingsRuntime();
|
|
for (FieldMapping fieldMapping : mappingList) {
|
String fromName, toName;
|
if (MappingDirection.Normal == direction) {
|
fromName = fieldMapping.getFromName();
|
toName = fieldMapping.getToName();
|
}
|
else {
|
fromName = fieldMapping.getToName();
|
toName = fieldMapping.getFromName();
|
}
|
|
Field formField = fromMeta.get(fromName);
|
Field toField = toMeta.get(toName);
|
|
if (formField == null || toField == null) {
|
continue;
|
}
|
|
int fromIndex = fromMeta.getIndex(fromName);
|
int toIndex = toMeta.getIndex(toName);
|
|
FieldMappingRuntime mappingRuntime = new FieldMappingRuntime(fieldMapping, formField, toField, fromIndex, toIndex);
|
mappingSetRuntime.addItem(mappingRuntime);
|
}
|
|
if (open) {
|
List<Field> fields = fromMeta.getFields();
|
for (Field field : fields) {
|
String fieldName = field.getName();
|
|
if (mappingSetRuntime.containsFromField(fieldName)) {
|
continue;
|
}
|
if (!toMeta.contains(fieldName)) {
|
continue;
|
}
|
|
Field fromField = fromMeta.get(fieldName);
|
Field toField = toMeta.get(fieldName);
|
|
int fromIndex = fromMeta.getIndex(fieldName);
|
int toIndex = toMeta.getIndex(fieldName);
|
|
FieldMappingRuntime mappingRuntime = new FieldMappingRuntime(null, fromField, toField, fromIndex, toIndex);
|
mappingSetRuntime.addItem(mappingRuntime);
|
}
|
}
|
|
return mappingSetRuntime;
|
}
|
|
public int size() {
|
return mappingList.size();
|
}
|
|
public List<FieldMapping> getList() {
|
return mappingList;
|
}
|
|
public void loadOne(FieldMapping mapping) {
|
String fromField = mapping.getFromName();
|
String toField = mapping.getToName();
|
|
mappingList.add(mapping);
|
|
if (fromField != null) {
|
fromFieldMap.put(fromField, mapping);
|
}
|
|
if (toField != null) {
|
toFieldMap.put(toField, mapping);
|
}
|
}
|
|
public String getFromField(String toField) {
|
return convertField(toField, true);
|
}
|
|
public String getToField(String fromField) {
|
return convertField(fromField, false);
|
}
|
|
private String convertField(String field, boolean isFromField) {
|
FieldMapping item = getIoMappingItem(field, isFromField);
|
|
if (isFromField) {
|
return item.getToName();
|
}
|
else {
|
return item.getFromName();
|
}
|
}
|
|
public void clear() {
|
if (mappingList != null) {
|
mappingList.clear();
|
}
|
|
if (fromFieldMap != null) {
|
fromFieldMap.clear();
|
}
|
|
if (toFieldMap != null) {
|
toFieldMap.clear();
|
}
|
}
|
|
public FieldMapping getItemByFromField(String fromField) {
|
return getIoMappingItem(fromField, true);
|
}
|
|
public FieldMapping getItemByToField(String toField) {
|
return getIoMappingItem(toField, false);
|
}
|
|
private FieldMapping getIoMappingItem(String field, boolean isFromField) {
|
Map<String, FieldMapping> itemMap = null;
|
if (Util.isEmpty(field)) {
|
return null;
|
}
|
|
field = field.toLowerCase();
|
|
if (isFromField) {
|
itemMap = fromFieldMap;
|
}
|
else {
|
itemMap = toFieldMap;
|
}
|
|
if (!itemMap.containsKey(field)) {
|
return null;
|
}
|
|
FieldMapping item = itemMap.get(field);
|
return item;
|
}
|
|
public String getId() {
|
return id;
|
}
|
|
public void setId(String id) {
|
this.id = id;
|
}
|
|
public String getName() {
|
return name;
|
}
|
|
public void setName(String name) {
|
this.name = name;
|
}
|
|
public boolean isOpen() {
|
return open;
|
}
|
|
public void setOpen(boolean open) {
|
this.open = open;
|
}
|
|
public List<FieldMapping> getItemList() {
|
return mappingList;
|
}
|
|
@Override
|
public Iterator<FieldMapping> iterator() {
|
return mappingList.iterator();
|
}
|
|
public String getFromFieldsString() {
|
ContentBuilder builder = new ContentBuilder(", ");
|
|
for (FieldMapping mapping: mappingList) {
|
builder.append(mapping.getFromName());
|
}
|
|
return builder.toString();
|
}
|
|
public String getToFieldsString() {
|
ContentBuilder builder = new ContentBuilder(", ");
|
|
for (FieldMapping mapping: mappingList) {
|
builder.append(mapping.getToName());
|
}
|
|
return builder.toString();
|
}
|
|
}
|