use of com.google.appengine.api.datastore.Blob in project siena by mandubian.
the class GaeMappingUtils method fillEntity.
public static void fillEntity(Object obj, Entity entity) {
Class<?> clazz = obj.getClass();
for (Field field : ClassInfo.getClassInfo(clazz).updateFields) {
String property = ClassInfo.getColumnNames(field)[0];
Object value = Util.readField(obj, field);
Class<?> fieldClass = field.getType();
if (ClassInfo.isModel(fieldClass) && !ClassInfo.isEmbedded(field)) /*&& !ClassInfo.isAggregated(field)
&& !ClassInfo.isOwned(field)*/
{
if (value == null) {
entity.setProperty(property, null);
} else {
Key key = getKey(value);
entity.setProperty(property, key);
}
} else {
if (value != null) {
if (fieldClass == Json.class) {
value = value.toString();
} else if (value instanceof String) {
String s = (String) value;
if (s.length() > 500)
value = new Text(s);
} else if (value instanceof byte[]) {
byte[] arr = (byte[]) value;
// GAE Blob doesn't accept more than 1MB
if (arr.length < 1000000)
value = new Blob(arr);
else
value = new Blob(Arrays.copyOf(arr, 1000000));
} else if (ClassInfo.isEmbedded(field)) {
Embedded embed = field.getAnnotation(Embedded.class);
switch(embed.mode()) {
case SERIALIZE_JSON:
value = JsonSerializer.serialize(value).toString();
String s = (String) value;
if (s.length() > 500)
value = new Text(s);
break;
case SERIALIZE_JAVA:
// this embedding mode doesn't manage @EmbedIgnores
try {
byte[] b = JavaSerializer.serialize(value);
// if length is less than 1Mb, can store in a blob else???
if (b.length <= 1000000) {
value = new Blob(b);
} else {
throw new SienaException("object can be java serialized because it's too large >1mb");
}
} catch (IOException ex) {
throw new SienaException(ex);
}
break;
case NATIVE:
GaeNativeSerializer.embed(entity, ClassInfo.getSingleColumnName(field), value, 0);
// has set several new properties in entity so go to next field
continue;
}
} else /*else if (ClassInfo.isAggregated(field)){
// can't save it now as it requires its parent key to be mapped
// so don't do anything for the time being
continue;
}
else if (ClassInfo.isOwned(field)){
// can't save it now as it requires its parent key to be mapped
// so don't do anything for the time being
continue;
}*/
if (fieldClass == BigDecimal.class) {
DecimalPrecision ann = field.getAnnotation(DecimalPrecision.class);
if (ann == null) {
value = ((BigDecimal) value).toPlainString();
} else {
switch(ann.storageType()) {
case DOUBLE:
value = ((BigDecimal) value).doubleValue();
break;
case STRING:
case NATIVE:
value = ((BigDecimal) value).toPlainString();
break;
}
}
} else // don't know if anyone will use it but it will work :)
if (Enum.class.isAssignableFrom(field.getType())) {
value = value.toString();
}
}
Unindexed ui = field.getAnnotation(Unindexed.class);
if (ui == null) {
entity.setProperty(property, value);
} else {
entity.setUnindexedProperty(property, value);
}
}
}
}
use of com.google.appengine.api.datastore.Blob in project siena by mandubian.
the class GaeNativeSerializer method embed.
public static void embed(Entity entity, String embeddingColumnName, Object embeddedObj, int level) {
// the level prevents from stackoverflow in case of a circular ref
if (level > 2)
return;
Class<?> clazz = embeddedObj.getClass();
if (clazz.isArray() || Collection.class.isAssignableFrom(clazz)) {
throw new SienaException("can't serializer Array/Collection in native mode");
}
for (Field f : ClassInfo.getClassInfo(clazz).allFields) {
// doesn't try to analyze fields, just try to store it
Class<?> fieldClass = f.getType();
String propName = embeddingColumnName + "." + ClassInfo.getSingleColumnName(f);
Object propValue = Util.readField(embeddedObj, f);
if (propValue != null) {
if (fieldClass == Json.class) {
propValue = propValue.toString();
} else if (propValue instanceof String) {
String s = (String) propValue;
if (s.length() > 500)
propValue = new Text(s);
} else if (propValue instanceof byte[]) {
byte[] arr = (byte[]) propValue;
// GAE Blob doesn't accept more than 1MB
if (arr.length < 1000000)
propValue = new Blob(arr);
else
propValue = new Blob(Arrays.copyOf(arr, 1000000));
} else if (ClassInfo.isEmbedded(f)) {
Embedded embed = f.getAnnotation(Embedded.class);
switch(embed.mode()) {
case SERIALIZE_JSON:
propValue = JsonSerializer.serialize(propValue).toString();
String s = (String) propValue;
if (s.length() > 500)
propValue = new Text(s);
break;
case SERIALIZE_JAVA:
// this embedding mode doesn't manage @EmbedIgnores
try {
byte[] b = JavaSerializer.serialize(propValue);
// if length is less than 1Mb, can store in a blob else???
if (b.length <= 1000000) {
propValue = new Blob(b);
} else {
throw new SienaException("object can be java serialized because it's too large >1mb");
}
} catch (IOException ex) {
throw new SienaException(ex);
}
break;
case NATIVE:
GaeNativeSerializer.embed(entity, embeddingColumnName + "." + ClassInfo.getSingleColumnName(f), propValue, level + 1);
// has set several new properties in entity so go to next field
continue;
}
} else if (fieldClass == BigDecimal.class) {
DecimalPrecision ann = f.getAnnotation(DecimalPrecision.class);
if (ann == null) {
propValue = ((BigDecimal) propValue).toPlainString();
} else {
switch(ann.storageType()) {
case DOUBLE:
propValue = ((BigDecimal) propValue).doubleValue();
break;
case STRING:
case NATIVE:
propValue = ((BigDecimal) propValue).toPlainString();
break;
}
}
} else // don't know if anyone will use it but it will work :)
if (Enum.class.isAssignableFrom(fieldClass)) {
propValue = propValue.toString();
} else if (ClassInfo.isModel(fieldClass)) {
// if it's a model and as there a no join, we can't fetch everything! So only the key is embedded
//GaeNativeSerializer.embed(entity, embeddingColumnName + "." + ClassInfo.getSingleColumnName(f), propValue, level + 1);
propValue = Util.readField(propValue, ClassInfo.getIdField(fieldClass));
}
}
Unindexed ui = f.getAnnotation(Unindexed.class);
if (ui == null) {
entity.setProperty(propName, propValue);
} else {
entity.setUnindexedProperty(propName, propValue);
}
}
}
Aggregations