package foundation.io.mapping;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import foundation.data.entity.DictionaryTranslator;
|
import foundation.data.entity.Entity;
|
import foundation.data.meta.field.Field;
|
import foundation.data.meta.field.FieldsRuntime;
|
import foundation.io.define.IODirection;
|
import foundation.io.define.IOSource;
|
import foundation.io.object.Header;
|
import foundation.io.object.Headers;
|
import foundation.io.object.Titles;
|
import foundation.util.Util;
|
|
public class IOMappings {
|
|
private IOSource source;
|
private String id;
|
private String name;
|
private boolean open;
|
private List<FieldMapping> mappingList;
|
private Map<String, FieldMapping> fromFieldMap;
|
private Map<String, FieldMapping> toFieldMap;
|
private Map<String, DictionaryTranslator> dictionaryList;
|
|
|
public IOMappings(IOSource source) {
|
this.source = source;
|
|
mappingList = new ArrayList<FieldMapping>();
|
fromFieldMap = new HashMap<String, FieldMapping>();
|
toFieldMap = new HashMap<String, FieldMapping>();
|
dictionaryList = new HashMap<String, DictionaryTranslator>();
|
}
|
|
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 {
|
return createRuntime(fromMeta, toMeta, direction, false);
|
}
|
|
public MappingsRuntime createRuntime(FieldsRuntime fromMeta, FieldsRuntime toMeta, MappingDirection direction, boolean open) 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 MappingsRuntime createRuntime(Titles fromTitles, FieldsRuntime toMeta, MappingDirection direction) throws Exception {
|
MappingsRuntime mappingsRuntime = 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();
|
}
|
|
Integer fromIndex = fromTitles.getIndex(fromName);
|
Field toField = toMeta.get(toName);
|
|
if (fromIndex == null || toField == null) {
|
continue;
|
}
|
|
int toIndex = toMeta.getIndex(toName);
|
|
FieldMappingRuntime mappingRuntime = new FieldMappingRuntime(fieldMapping, fromName, toField, fromIndex, toIndex);
|
mappingsRuntime.addItem(mappingRuntime);
|
}
|
|
return mappingsRuntime;
|
}
|
|
public MappingsRuntime createRuntime(FieldsRuntime fromMeta, Headers toHeaders, MappingDirection direction, IODirection ioDirection) throws Exception {
|
MappingsRuntime mappingSetRuntime = new MappingsRuntime();
|
|
//1. if to titles is empty
|
if (toHeaders.isEmpty()) {
|
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 fromField = fromMeta.get(fromName);
|
|
if (fromField == null) {
|
continue;
|
}
|
|
int fromIndex = fromMeta.getIndex(fromName);
|
|
FieldMappingRuntime mappingRuntime = new FieldMappingRuntime(fieldMapping, fromField, toName, fromIndex, fromIndex);
|
mappingSetRuntime.addItem(mappingRuntime);
|
|
toHeaders.addOne(toName);
|
}
|
|
if (IODirection.ErrorsToSheet == ioDirection) {
|
String fromName = "error_message";
|
String toName = "导入反馈";
|
Field fromField = fromMeta.get(fromName);
|
int fromIndex = fromMeta.getIndex(fromName);
|
FieldMapping fieldMapping = new FieldMapping();
|
FieldMappingRuntime mappingRuntime = new FieldMappingRuntime(fieldMapping, fromField, toName, fromIndex, fromIndex);
|
mappingSetRuntime.addItem(mappingRuntime);
|
|
toHeaders.addOne(toName);
|
}
|
|
return mappingSetRuntime;
|
}
|
|
//2. if to headers is not empty
|
for (Header header: toHeaders) {
|
Field fromField = fromMeta.get(header.getFieldName());
|
|
if (fromField == null) {
|
continue;
|
}
|
|
FieldMappingRuntime mappingRuntime = new FieldMappingRuntime(null, fromField, header.getTitle(), fromField.getIndexNo(), header.getIndex());
|
mappingSetRuntime.addItem(mappingRuntime);
|
|
}
|
|
return mappingSetRuntime;
|
}
|
|
public int size() {
|
return mappingList.size();
|
}
|
|
public List<FieldMapping> getList() {
|
return mappingList;
|
}
|
|
public void addFieldMapping(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();
|
}
|
|
if (dictionaryList != null) {
|
dictionaryList.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 boolean containsToField(String toField) {
|
if (Util.isEmpty(toField)) {
|
return false;
|
}
|
|
toField = toField.toLowerCase();
|
return toFieldMap.containsKey(toField);
|
}
|
|
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;
|
}
|
|
public IOSource getSource() {
|
return source;
|
}
|
}
|