use of com.google.appengine.api.datastore.Text in project qi4j-sdk by Qi4j.
the class GaeEntityStoreMixin method get.
@Override
public Reader get(EntityReference ref) throws EntityStoreException {
try {
Key key = KeyFactory.createKey(entityKind, ref.toURI());
Entity entity = datastore.get(key);
Text serializedState = (Text) entity.getProperty("value");
if (serializedState == null) {
throw new EntityNotFoundException(ref);
}
return new StringReader(serializedState.getValue());
} catch (com.google.appengine.api.datastore.EntityNotFoundException e) {
e.printStackTrace();
throw new EntityNotFoundException(ref);
}
}
use of com.google.appengine.api.datastore.Text 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.Text in project OpenRefine by OpenRefine.
the class AppEngineRefineBrokerImpl method openProject.
// ---------------------------------------------------------------------------------
protected void openProject(HttpServletResponse response, String pid) throws Exception {
PersistenceManager pm = pmfInstance.getPersistenceManager();
try {
Project project = getProject(pm, pid);
Writer w = response.getWriter();
JSONWriter writer = new JSONWriter(w);
writer.object();
writer.key("data");
writer.value(project.data.toString());
writer.key("transformations");
writer.array();
for (Text s : project.transformations) {
writer.value(s.toString());
}
writer.endArray();
writer.endObject();
w.flush();
w.close();
} finally {
pm.close();
}
}
use of com.google.appengine.api.datastore.Text in project OpenRefine by OpenRefine.
the class AppEngineRefineBrokerImpl method addTransformations.
protected void addTransformations(HttpServletResponse response, String pid, String uid, String lid, List<String> transformations) throws Exception {
PersistenceManager pm = pmfInstance.getPersistenceManager();
try {
checkLock(pm, pid, uid, lid);
Project project = getProject(pm, pid);
if (project == null) {
throw new RuntimeException("Project '" + pid + "' not found");
}
Transaction tx = pm.currentTransaction();
try {
for (String s : transformations) {
project.transformations.add(new Text(s));
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
respond(response, OK);
} finally {
pm.close();
}
}
use of com.google.appengine.api.datastore.Text in project qi4j-sdk by Qi4j.
the class GaeEntityState method propertyValueOf.
@Override
public Object propertyValueOf(QualifiedName stateName) {
String uri = stateName.toURI();
Object value = entity.getProperty(uri);
if (value instanceof Text) {
value = ((Text) value).getValue();
}
ValueType type = valueTypes.get(stateName);
if (value != null && type != null) {
try {
value = valueSerialization.deserialize(type, value.toString());
} catch (ValueSerializationException e) {
String message = "\nqualifiedName: " + stateName + "\n stateName: " + stateName.name() + "\n uri: " + uri + "\n type: " + type + "\n value: " + value + "\n";
InternalError error = new InternalError(message);
error.initCause(e);
throw error;
}
}
System.out.println("getProperty( " + stateName + " ) --> " + uri + "=" + value);
return value;
}
Aggregations