org.tenio.interstellar.context.mybatis.DataObjectWrapper Maven / Gradle / Ivy
package org.tenio.interstellar.context.mybatis;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.apache.ibatis.reflection.factory.ObjectFactory;
import org.apache.ibatis.reflection.property.PropertyTokenizer;
import org.apache.ibatis.reflection.wrapper.MapWrapper;
import org.tenio.interstellar.context.DataArray;
import org.tenio.interstellar.context.DataObject;
import java.util.List;
/**
* TODO
*
* @author: Ban Tenio
* @version: 1.0
*/
public class DataObjectWrapper extends MapWrapper {
private DataObject dataObject;
/**
*
* TODO
*
* @param metaObject TODO
* @param dataObject TODO
*/
public DataObjectWrapper(MetaObject metaObject, DataObject dataObject) {
super(metaObject, dataObject.getMap());
this.dataObject = dataObject;
}
@Override
public Object get(PropertyTokenizer prop) {
if (prop.getIndex() != null) {
Object collection = resolveCollection(prop, dataObject);
return getCollectionValue(prop, collection);
} else {
return dataObject.getValue(prop.getName());
}
}
@Override
public void set(PropertyTokenizer prop, Object value) {
if (prop.getIndex() != null) {
Object collection = resolveCollection(prop, dataObject);
setCollectionValue(prop, collection, value);
} else {
dataObject.put(prop.getName(), value);
}
}
@Override
protected Object getCollectionValue(PropertyTokenizer prop, Object collection) {
if (collection == null) {
return null;
}
if (collection instanceof DataObject) {
return ((DataObject) collection).getValue(prop.getIndex());
} else if (collection instanceof DataArray) {
int i = Integer.parseInt(prop.getIndex());
((DataArray) collection).getValue(i);
}
return super.getCollectionValue(prop, collection);
}
@Override
public String findProperty(String name, boolean useCamelCaseMapping) {
return name;
}
@Override
public String[] getGetterNames() {
return dataObject.getMap().keySet().toArray(new String[0]);
}
@Override
public String[] getSetterNames() {
return dataObject.getMap().keySet().toArray(new String[0]);
}
@Override
public Class> getSetterType(String name) {
PropertyTokenizer prop = new PropertyTokenizer(name);
if (prop.hasNext()) {
MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
return Object.class;
} else {
return metaValue.getSetterType(prop.getChildren());
}
} else {
if (dataObject.getValue(name) != null) {
return dataObject.getValue(name).getClass();
} else {
return Object.class;
}
}
}
@Override
public Class> getGetterType(String name) {
PropertyTokenizer prop = new PropertyTokenizer(name);
if (prop.hasNext()) {
MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
return Object.class;
} else {
return metaValue.getGetterType(prop.getChildren());
}
} else {
if (dataObject.getValue(name) != null) {
return dataObject.getValue(name).getClass();
} else {
return Object.class;
}
}
}
@Override
public boolean hasSetter(String name) {
return true;
}
@Override
public boolean hasGetter(String name) {
PropertyTokenizer prop = new PropertyTokenizer(name);
if (prop.hasNext()) {
if (dataObject.containsKey(prop.getIndexedName())) {
MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
return true;
} else {
return metaValue.hasGetter(prop.getChildren());
}
} else {
return false;
}
} else {
return dataObject.containsKey(prop.getName());
}
}
@Override
public MetaObject instantiatePropertyValue(String name, PropertyTokenizer prop, ObjectFactory objectFactory) {
DataObject newDataObject = new DataObject();
set(prop, newDataObject);
return MetaObject.forObject(newDataObject, metaObject.getObjectFactory(), metaObject.getObjectWrapperFactory(), metaObject.getReflectorFactory());
}
@Override
public boolean isCollection() {
return false;
}
@Override
public void add(Object element) {
throw new UnsupportedOperationException();
}
@Override
public void addAll(List element) {
throw new UnsupportedOperationException();
}
}