use of io.requery.proxy.PropertyState in project requery by requery.
the class EntityReader method fromResult.
final E fromResult(E entity, ResultSet results, Attribute[] selection) throws SQLException {
// if refreshing (entity not null) overwrite the properties
boolean overwrite = entity != null || stateless;
boolean wasCached = false;
if (entity == null) {
// get or create the entity object
if (cacheable) {
synchronized (type) {
// try lookup cached object
final Object key = readCacheKey(results);
if (key != null) {
entity = cache.get(type.getClassType(), key);
wasCached = entity != null;
}
// not cached create a new one
if (entity == null) {
entity = createEntity();
if (key != null) {
cache.put(type.getClassType(), key, entity);
}
}
}
} else {
entity = createEntity();
}
}
// set the properties
EntityProxy<E> proxy = type.getProxyProvider().apply(entity);
synchronized (proxy.syncObject()) {
proxy.link(this);
int index = 1;
for (Attribute expression : selection) {
@SuppressWarnings("unchecked") Attribute<E, ?> attribute = (Attribute<E, ?>) expression;
boolean isAssociation = attribute.isAssociation();
if ((attribute.isForeignKey() || attribute.isKey()) && isAssociation) {
// handle loading the foreign key into referenced object
Attribute referenced = Attributes.get(attribute.getReferencedAttribute());
Object key = mapping.read((Expression) referenced, results, index);
if (key != null) {
Object value = proxy.get(attribute, false);
if (value == null) {
// create one...
Class classType = attribute.getClassType();
EntityReader reader = context.read(classType);
value = reader.createEntity();
}
context.proxyOf(value, false).set(Attributes.get(attribute.getReferencedAttribute()), key, PropertyState.LOADED);
// leave in fetch state if only key is loaded
PropertyState state = PropertyState.LOADED;
if (!stateless) {
state = proxy.getState(attribute);
state = state == PropertyState.LOADED ? state : PropertyState.FETCH;
}
proxy.setObject(attribute, value, state);
}
} else if (isAssociation) {
continue;
} else if (overwrite || proxy.getState(attribute) != PropertyState.MODIFIED) {
if (attribute.getPrimitiveKind() != null) {
readPrimitiveField(proxy, attribute, results, index);
} else {
Object value = mapping.read((Expression) attribute, results, index);
proxy.setObject(attribute, value, PropertyState.LOADED);
}
}
index++;
}
}
if (!wasCached) {
context.getStateListener().postLoad(entity, proxy);
}
return entity;
}
use of io.requery.proxy.PropertyState in project requery by requery.
the class EntityParceler method readFromParcel.
public T readFromParcel(Parcel in) {
T entity = type.getFactory().get();
EntityProxy<T> proxy = type.getProxyProvider().apply(entity);
for (Attribute<T, ?> attribute : type.getAttributes()) {
if (attribute.isAssociation()) {
continue;
}
Class<?> typeClass = attribute.getClassType();
Object value = null;
if (typeClass.isEnum()) {
String name = (String) in.readValue(getClass().getClassLoader());
if (name == null) {
value = null;
} else {
@SuppressWarnings("unchecked") Class<? extends Enum> enumClass = (Class<? extends Enum>) typeClass;
value = Enum.valueOf(enumClass, name);
}
} else if (typeClass.isArray()) {
int length = in.readInt();
if (length >= 0) {
try {
Parcelable.Creator creator = (Parcelable.Creator<?>) typeClass.getField("CREATOR").get(null);
Object[] array = creator.newArray(length);
in.readTypedArray(array, creator);
value = array;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
} else {
value = in.readValue(getClass().getClassLoader());
}
PropertyState state = PropertyState.LOADED;
if (!type.isStateless()) {
state = PropertyState.valueOf(in.readString());
}
proxy.setObject(attribute, value, state);
}
return entity;
}
use of io.requery.proxy.PropertyState in project requery by requery.
the class EntityParceler method writeToParcel.
public void writeToParcel(T entity, Parcel out) {
EntityProxy<T> proxy = type.getProxyProvider().apply(entity);
for (Attribute<T, ?> attribute : type.getAttributes()) {
if (attribute.isAssociation()) {
continue;
}
Object value = proxy.get(attribute, false);
Class<?> typeClass = attribute.getClassType();
if (typeClass.isArray()) {
Parcelable[] array = (Parcelable[]) value;
if (array == null) {
out.writeInt(-1);
} else {
out.writeInt(array.length);
out.writeTypedArray(array, 0);
}
} else {
if (typeClass.isEnum()) {
if (value != null) {
value = value.toString();
}
}
out.writeValue(value);
}
if (!type.isStateless()) {
PropertyState state = proxy.getState(attribute);
out.writeString(state.toString());
}
}
}
Aggregations