package frame.file;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import com.sun.org.apache.regexp.internal.recompile;
|
|
import frame.data.Entity;
|
import frame.data.meta.EntityMeta;
|
import frame.data.meta.Field;
|
import frame.util.Util;
|
|
public class IOMapping implements IFileload{
|
|
private String id;
|
private String name;
|
private boolean open;
|
private int typeCode;
|
private List<IOMappingItem> itemList;
|
private Map<String, IOMappingItem> fromItemMap;
|
private Map<String, IOMappingItem> toItemMap;
|
|
public IOMapping() {
|
itemList = new ArrayList<IOMappingItem>();
|
fromItemMap = new HashMap<String, IOMappingItem>();
|
toItemMap = new HashMap<String, IOMappingItem>();
|
}
|
|
@Override
|
public void initLoad(Entity entity) {
|
// TODO Auto-generated method stub
|
|
}
|
public IOMappingRuntime createRuntime(FileIOContext context, String condition, EntityMeta fromMeta, EntityMeta toMeta, Direction direction) throws Exception {
|
List<IOMappingItemRuntime> updateMappingRuntime = new ArrayList<IOMappingItemRuntime>();
|
List<IOMappingItemRuntime> insertMappingRuntime = new ArrayList<IOMappingItemRuntime>();
|
|
for (IOMappingItem mappingItem : itemList) {
|
String fromField, toField;
|
if (Direction.Normal == direction) {
|
fromField = mappingItem.getFromField();
|
toField = mappingItem.getToField();
|
}
|
else {
|
fromField = mappingItem.getToField();
|
toField = mappingItem.getFromField();
|
}
|
|
if (context != null) {
|
fromField = context.setParametersTo(fromField);
|
toField = context.setParametersTo(toField);
|
}
|
|
Field fieldByFrom = fromMeta.get(fromField);
|
Field fieldByto = fromMeta.get(toField);
|
|
if (fieldByFrom == null || fieldByto == null) {
|
if (FileIoItem.TypeCode_DB.equalsIgnoreCase(condition)) {
|
if (mappingItem.isInsertIgnore() || mappingItem.isUpdateIgnore()) {
|
continue;
|
}
|
else {
|
throw new Exception("IO Mapping fields not in table:" + mappingItem.toString() + " " + fromMeta.getName() + ", " + toMeta.getName());
|
}
|
}
|
else {
|
continue;
|
}
|
}
|
|
IOMappingItemRuntime mappingItemRuntime = new IOMappingItemRuntime(mappingItem, fromField, fieldByFrom, toField, fieldByto);
|
|
if (!mappingItem.isInsertIgnore()) {
|
insertMappingRuntime.add(mappingItemRuntime);
|
}
|
|
if (!mappingItem.isUpdateIgnore()) {
|
updateMappingRuntime.add(mappingItemRuntime);
|
}
|
|
return
|
}
|
|
if (!open) {
|
return new IOMappingRuntime(updateMappingRuntime, insertMappingRuntime);
|
}
|
List<Field> fields = fromMeta.getFields();
|
for (Field field : fields) {
|
String fieldName = field.getName();
|
if (!toMeta.contains(fieldName)) {
|
continue;
|
}
|
|
Field fromField = fromMeta.get(fieldName);
|
Field toField = toMeta.get(fieldName);
|
|
}
|
}
|
|
public int size() {
|
return itemList.size();
|
}
|
|
public List<IOMappingItem> getList() {
|
return itemList;
|
}
|
|
public void addItem(IOMappingItem item) {
|
String fromField = item.getFromField();
|
String toField = item.getToField();
|
|
itemList.add(item);
|
|
if (fromField != null) {
|
fromItemMap.put(fromField, item);
|
}
|
|
if (toField != null) {
|
toItemMap.put(toField, item);
|
}
|
}
|
|
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) {
|
IOMappingItem item = getIoMappingItem(field, isFromField);
|
|
if (isFromField) {
|
return item.getToField();
|
}
|
else {
|
return item.getFromField();
|
}
|
|
}
|
|
public void clear() {
|
if (itemList != null) {
|
itemList.clear();
|
}
|
|
if (fromItemMap != null) {
|
fromItemMap.clear();
|
}
|
|
if (toItemMap != null) {
|
toItemMap.clear();
|
}
|
}
|
|
public IOMappingItem getItemByFromField(String fromField) {
|
return getIoMappingItem(fromField, true);
|
}
|
|
public IOMappingItem getItemByToField(String toField) {
|
return getIoMappingItem(toField, false);
|
}
|
|
private IOMappingItem getIoMappingItem(String field, boolean isFromField) {
|
Map<String, IOMappingItem> itemMap = null;
|
if (Util.isEmptyStr(field)) {
|
return null;
|
}
|
|
field = field.toLowerCase();
|
|
if (isFromField) {
|
itemMap = fromItemMap;
|
}
|
else {
|
itemMap = toItemMap;
|
}
|
|
if (!itemMap.containsKey(field)) {
|
return null;
|
}
|
|
IOMappingItem item = itemMap.get(field);
|
return item;
|
}
|
|
public String getId() {
|
return id;
|
}
|
|
public String getName() {
|
return name;
|
}
|
|
public boolean isOpen() {
|
return open;
|
}
|
|
public int getTypeCode() {
|
return typeCode;
|
}
|
|
public List<IOMappingItem> getItemList() {
|
return itemList;
|
}
|
|
|
}
|