Search in sources :

Example 1 with EmbeddedEntity

use of com.google.appengine.api.datastore.EmbeddedEntity in project activityinfo by bedatadriven.

the class FormConverter method fromPropertyValue.

public static JsonValue fromPropertyValue(Object propertyValue) {
    if (propertyValue == null) {
        return Json.createNull();
    } else if (propertyValue instanceof EmbeddedEntity) {
        return fromEmbeddedEntity(((EmbeddedEntity) propertyValue));
    } else if (propertyValue instanceof List) {
        List<Object> propertyValueList = (List<Object>) propertyValue;
        JsonValue convertedList = Json.createArray();
        for (Object propertyValueListItem : propertyValueList) {
            convertedList.add(fromPropertyValue(propertyValueListItem));
        }
        return convertedList;
    } else if (propertyValue instanceof String) {
        return Json.create((String) propertyValue);
    } else if (propertyValue instanceof Number) {
        return Json.create(((Number) propertyValue).doubleValue());
    } else if (propertyValue instanceof Boolean) {
        return Json.create((Boolean) propertyValue);
    } else {
        throw new UnsupportedOperationException("type: " + propertyValue.getClass().getName());
    }
}
Also used : JsonValue(org.activityinfo.json.JsonValue) List(java.util.List) EmbeddedEntity(com.google.appengine.api.datastore.EmbeddedEntity)

Example 2 with EmbeddedEntity

use of com.google.appengine.api.datastore.EmbeddedEntity in project java-docs-samples by GoogleCloudPlatform.

the class EntitiesTest method embeddedEntity_fromExisting_canRecover.

@Test
public void embeddedEntity_fromExisting_canRecover() throws Exception {
    Entity initialContactInfo = new Entity("Contact");
    initialContactInfo.setProperty("homeAddress", "123 Fake St, Made, UP 45678");
    initialContactInfo.setProperty("phoneNumber", "555-555-5555");
    initialContactInfo.setProperty("emailAddress", "test@example.com");
    datastore.put(initialContactInfo);
    Key employeeKey = putEmployeeWithContactInfo(initialContactInfo);
    // [START embedded_entities_3]
    Entity employee = datastore.get(employeeKey);
    EmbeddedEntity embeddedContactInfo = (EmbeddedEntity) employee.getProperty("contactInfo");
    Key infoKey = embeddedContactInfo.getKey();
    Entity contactInfo = new Entity(infoKey);
    contactInfo.setPropertiesFrom(embeddedContactInfo);
    // [END embedded_entities_3]
    datastore.put(contactInfo);
    Entity got = datastore.get(infoKey);
    assertThat(got.getKey()).isEqualTo(initialContactInfo.getKey());
    assertThat((String) got.getProperty("homeAddress")).named("got.homeAddress").isEqualTo("123 Fake St, Made, UP 45678");
}
Also used : Entity(com.google.appengine.api.datastore.Entity) EmbeddedEntity(com.google.appengine.api.datastore.EmbeddedEntity) EmbeddedEntity(com.google.appengine.api.datastore.EmbeddedEntity) Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Example 3 with EmbeddedEntity

use of com.google.appengine.api.datastore.EmbeddedEntity in project java-docs-samples by GoogleCloudPlatform.

the class EntitiesTest method embeddedEntity_fromEmbedded_embedsProperties.

@Test
public void embeddedEntity_fromEmbedded_embedsProperties() throws Exception {
    // CHECKSTYLE.OFF: VariableDeclarationUsageDistance - Increased clarity in sample
    Entity employee = new Entity("Employee");
    // CHECKSTYLE.ON: VariableDeclarationUsageDistance
    // [START embedded_entities_1]
    // Entity employee = ...;
    EmbeddedEntity embeddedContactInfo = new EmbeddedEntity();
    embeddedContactInfo.setProperty("homeAddress", "123 Fake St, Made, UP 45678");
    embeddedContactInfo.setProperty("phoneNumber", "555-555-5555");
    embeddedContactInfo.setProperty("emailAddress", "test@example.com");
    employee.setProperty("contactInfo", embeddedContactInfo);
    // [END embedded_entities_1]
    datastore.put(employee);
    Entity gotEmployee = datastore.get(employee.getKey());
    EmbeddedEntity got = (EmbeddedEntity) gotEmployee.getProperty("contactInfo");
    assertThat((String) got.getProperty("homeAddress")).named("got.homeAddress").isEqualTo("123 Fake St, Made, UP 45678");
}
Also used : Entity(com.google.appengine.api.datastore.Entity) EmbeddedEntity(com.google.appengine.api.datastore.EmbeddedEntity) EmbeddedEntity(com.google.appengine.api.datastore.EmbeddedEntity) Test(org.junit.Test)

Example 4 with EmbeddedEntity

use of com.google.appengine.api.datastore.EmbeddedEntity in project Cached-Datastore by Emperorlou.

the class EntityPool method addEmbeddedEntityDirectly.

public void addEmbeddedEntityDirectly(EmbeddedEntity... entity) {
    for (EmbeddedEntity e : entity) {
        // if (e.getKey()==null) throw new RuntimeException("Cannot add an embedded entity if it doesn't have a key.");
        CachedEntity wrapper = new CachedEntity(e.getKey());
        for (String fieldName : e.getProperties().keySet()) {
            wrapper.setProperty(fieldName, e.getProperty(fieldName));
        }
        pool.put(e, wrapper);
    }
}
Also used : EmbeddedEntity(com.google.appengine.api.datastore.EmbeddedEntity)

Example 5 with EmbeddedEntity

use of com.google.appengine.api.datastore.EmbeddedEntity in project java-docs-samples by GoogleCloudPlatform.

the class EntitiesTest method putEmployeeWithContactInfo.

private Key putEmployeeWithContactInfo(Entity contactInfo) {
    Entity employee = new Entity("Employee");
    // [START embedded_entities_2]
    // Entity employee = ...;
    // Entity contactInfo = ...;
    EmbeddedEntity embeddedContactInfo = new EmbeddedEntity();
    // Optional, used so we can recover original.
    embeddedContactInfo.setKey(contactInfo.getKey());
    embeddedContactInfo.setPropertiesFrom(contactInfo);
    employee.setProperty("contactInfo", embeddedContactInfo);
    // [END embedded_entities_2]
    datastore.put(employee);
    return employee.getKey();
}
Also used : Entity(com.google.appengine.api.datastore.Entity) EmbeddedEntity(com.google.appengine.api.datastore.EmbeddedEntity) EmbeddedEntity(com.google.appengine.api.datastore.EmbeddedEntity)

Aggregations

EmbeddedEntity (com.google.appengine.api.datastore.EmbeddedEntity)7 Entity (com.google.appengine.api.datastore.Entity)3 Test (org.junit.Test)3 JsonValue (org.activityinfo.json.JsonValue)2 Key (com.google.appengine.api.datastore.Key)1 List (java.util.List)1 FormField (org.activityinfo.model.form.FormField)1 FieldValue (org.activityinfo.model.type.FieldValue)1 EnumType (org.activityinfo.model.type.enumerated.EnumType)1 FieldConverter (org.activityinfo.store.hrd.FieldConverter)1