Search in sources :

Example 6 with EntityReference

use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.

the class MapEntityStoreMixin method readEntityState.

protected EntityState readEntityState(DefaultEntityStoreUnitOfWork unitOfWork, Reader entityState) throws EntityStoreException {
    try {
        Module module = unitOfWork.module();
        JSONObject jsonObject = new JSONObject(new JSONTokener(entityState));
        EntityStatus status = EntityStatus.LOADED;
        String version = jsonObject.getString(JSONKeys.VERSION);
        long modified = jsonObject.getLong(JSONKeys.MODIFIED);
        String identity = jsonObject.getString(JSONKeys.IDENTITY);
        // Check if version is correct
        String currentAppVersion = jsonObject.optString(JSONKeys.APPLICATION_VERSION, "0.0");
        if (!currentAppVersion.equals(application.version())) {
            if (migration != null) {
                migration.migrate(jsonObject, application.version(), this);
            } else {
                // Do nothing - set version to be correct
                jsonObject.put(JSONKeys.APPLICATION_VERSION, application.version());
            }
            LoggerFactory.getLogger(MapEntityStoreMixin.class).debug("Updated version nr on " + identity + " from " + currentAppVersion + " to " + application.version());
            // State changed
            status = EntityStatus.UPDATED;
        }
        String type = jsonObject.getString(JSONKeys.TYPE);
        EntityDescriptor entityDescriptor = module.entityDescriptor(type);
        if (entityDescriptor == null) {
            throw new EntityTypeNotFoundException(type);
        }
        Map<QualifiedName, Object> properties = new HashMap<>();
        JSONObject props = jsonObject.getJSONObject(JSONKeys.PROPERTIES);
        for (PropertyDescriptor propertyDescriptor : entityDescriptor.state().properties()) {
            Object jsonValue;
            try {
                jsonValue = props.get(propertyDescriptor.qualifiedName().name());
            } catch (JSONException e) {
                // Value not found, default it
                Object initialValue = propertyDescriptor.initialValue(module);
                properties.put(propertyDescriptor.qualifiedName(), initialValue);
                status = EntityStatus.UPDATED;
                continue;
            }
            if (JSONObject.NULL.equals(jsonValue)) {
                properties.put(propertyDescriptor.qualifiedName(), null);
            } else {
                Object value = valueSerialization.deserialize(propertyDescriptor.valueType(), jsonValue.toString());
                properties.put(propertyDescriptor.qualifiedName(), value);
            }
        }
        Map<QualifiedName, EntityReference> associations = new HashMap<>();
        JSONObject assocs = jsonObject.getJSONObject(JSONKeys.ASSOCIATIONS);
        for (AssociationDescriptor associationType : entityDescriptor.state().associations()) {
            try {
                Object jsonValue = assocs.get(associationType.qualifiedName().name());
                EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference((String) jsonValue);
                associations.put(associationType.qualifiedName(), value);
            } catch (JSONException e) {
                // Association not found, default it to null
                associations.put(associationType.qualifiedName(), null);
                status = EntityStatus.UPDATED;
            }
        }
        JSONObject manyAssocs = jsonObject.getJSONObject(JSONKeys.MANY_ASSOCIATIONS);
        Map<QualifiedName, List<EntityReference>> manyAssociations = new HashMap<>();
        for (AssociationDescriptor manyAssociationType : entityDescriptor.state().manyAssociations()) {
            List<EntityReference> references = new ArrayList<>();
            try {
                JSONArray jsonValues = manyAssocs.getJSONArray(manyAssociationType.qualifiedName().name());
                for (int i = 0; i < jsonValues.length(); i++) {
                    Object jsonValue = jsonValues.getString(i);
                    EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference((String) jsonValue);
                    references.add(value);
                }
                manyAssociations.put(manyAssociationType.qualifiedName(), references);
            } catch (JSONException e) {
                // ManyAssociation not found, default to empty one
                manyAssociations.put(manyAssociationType.qualifiedName(), references);
            }
        }
        JSONObject namedAssocs = jsonObject.getJSONObject(JSONKeys.NAMED_ASSOCIATIONS);
        Map<QualifiedName, Map<String, EntityReference>> namedAssociations = new HashMap<>();
        for (AssociationDescriptor namedAssociationType : entityDescriptor.state().namedAssociations()) {
            Map<String, EntityReference> references = new LinkedHashMap<>();
            try {
                JSONObject jsonValues = namedAssocs.getJSONObject(namedAssociationType.qualifiedName().name());
                JSONArray names = jsonValues.names();
                if (names != null) {
                    for (int idx = 0; idx < names.length(); idx++) {
                        String name = names.getString(idx);
                        Object value = jsonValues.get(name);
                        EntityReference ref = value == JSONObject.NULL ? null : EntityReference.parseEntityReference((String) value);
                        references.put(name, ref);
                    }
                }
                namedAssociations.put(namedAssociationType.qualifiedName(), references);
            } catch (JSONException e) {
                // NamedAssociation not found, default to empty one
                namedAssociations.put(namedAssociationType.qualifiedName(), references);
            }
        }
        return new DefaultEntityState(unitOfWork, version, modified, EntityReference.parseEntityReference(identity), status, entityDescriptor, properties, associations, manyAssociations, namedAssociations);
    } catch (JSONException e) {
        throw new EntityStoreException(e);
    }
}
Also used : EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) LinkedHashMap(java.util.LinkedHashMap) EntityReference(org.qi4j.api.entity.EntityReference) EntityStatus(org.qi4j.spi.entity.EntityStatus) List(java.util.List) ArrayList(java.util.ArrayList) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) QualifiedName(org.qi4j.api.common.QualifiedName) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONTokener(org.json.JSONTokener) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) Module(org.qi4j.api.structure.Module) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with EntityReference

use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.

the class JSONManyAssociationStateTest method givenJSONManyAssociationStateWhenChangingReferencesExpectCorrectBehavior.

@Test
public void givenJSONManyAssociationStateWhenChangingReferencesExpectCorrectBehavior() throws JSONException {
    // Fake JSONManyAssociationState
    JSONObject state = new JSONObject();
    state.put(JSONKeys.PROPERTIES, new JSONObject());
    state.put(JSONKeys.ASSOCIATIONS, new JSONObject());
    state.put(JSONKeys.MANY_ASSOCIATIONS, new JSONObject());
    state.put(JSONKeys.NAMED_ASSOCIATIONS, new JSONObject());
    JSONEntityState entityState = new JSONEntityState(null, null, "0", System.currentTimeMillis(), EntityReference.parseEntityReference("123"), EntityStatus.NEW, null, state);
    JSONManyAssociationState jsonState = new JSONManyAssociationState(entityState, new JSONArray());
    assertThat(jsonState.contains(EntityReference.parseEntityReference("NOT_PRESENT")), is(false));
    jsonState.add(0, EntityReference.parseEntityReference("0"));
    jsonState.add(1, EntityReference.parseEntityReference("1"));
    jsonState.add(2, EntityReference.parseEntityReference("2"));
    assertThat(jsonState.contains(EntityReference.parseEntityReference("1")), is(true));
    assertThat(jsonState.get(0).identity(), equalTo("0"));
    assertThat(jsonState.get(1).identity(), equalTo("1"));
    assertThat(jsonState.get(2).identity(), equalTo("2"));
    assertThat(jsonState.count(), equalTo(3));
    jsonState.remove(EntityReference.parseEntityReference("1"));
    assertThat(jsonState.count(), equalTo(2));
    assertThat(jsonState.contains(EntityReference.parseEntityReference("1")), is(false));
    assertThat(jsonState.get(0).identity(), equalTo("0"));
    assertThat(jsonState.get(1).identity(), equalTo("2"));
    jsonState.add(2, EntityReference.parseEntityReference("1"));
    assertThat(jsonState.count(), equalTo(3));
    jsonState.add(0, EntityReference.parseEntityReference("A"));
    jsonState.add(0, EntityReference.parseEntityReference("B"));
    jsonState.add(0, EntityReference.parseEntityReference("C"));
    assertThat(jsonState.count(), equalTo(6));
    assertThat(jsonState.get(0).identity(), equalTo("C"));
    assertThat(jsonState.get(1).identity(), equalTo("B"));
    assertThat(jsonState.get(2).identity(), equalTo("A"));
    assertThat(jsonState.contains(EntityReference.parseEntityReference("C")), is(true));
    assertThat(jsonState.contains(EntityReference.parseEntityReference("B")), is(true));
    assertThat(jsonState.contains(EntityReference.parseEntityReference("A")), is(true));
    assertThat(jsonState.contains(EntityReference.parseEntityReference("0")), is(true));
    assertThat(jsonState.contains(EntityReference.parseEntityReference("2")), is(true));
    assertThat(jsonState.contains(EntityReference.parseEntityReference("1")), is(true));
    List<String> refList = toList(map(new Function<EntityReference, String>() {

        @Override
        public String map(EntityReference from) {
            return from.identity();
        }
    }, jsonState));
    assertThat(refList.isEmpty(), is(false));
    assertArrayEquals(new String[] { "C", "B", "A", "0", "2", "1" }, refList.toArray());
}
Also used : Function(org.qi4j.functional.Function) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) EntityReference(org.qi4j.api.entity.EntityReference) Test(org.junit.Test)

Example 8 with EntityReference

use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.

the class GaeEntityState method identity.

@Override
public EntityReference identity() {
    EntityReference ref = new EntityReference(entity.getKey().getName());
    System.out.println("identity()  -->  " + ref);
    return ref;
}
Also used : EntityReference(org.qi4j.api.entity.EntityReference)

Example 9 with EntityReference

use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.

the class NameableAssert method getNames.

public static List<String> getNames(List<EntityReference> references) {
    List<String> result = new ArrayList<>(references.size());
    for (EntityReference reference : references) {
        final String name = getName(reference);
        assertThat("Name of " + reference, name, notNullValue());
        result.add(name);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) EntityReference(org.qi4j.api.entity.EntityReference)

Example 10 with EntityReference

use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.

the class AbstractPlainValueSerializationTest method givenEntityReferenceValueWhenSerializingAndDeserializingExpectEquals.

@Test
public void givenEntityReferenceValueWhenSerializingAndDeserializingExpectEquals() {
    String serialized = valueSerialization.serialize(EntityReference.parseEntityReference("ABCD-1234"));
    assertThat(serialized, equalTo("ABCD-1234"));
    EntityReference deserialized = valueSerialization.deserialize(EntityReference.class, serialized);
    assertThat(deserialized, equalTo(EntityReference.parseEntityReference("ABCD-1234")));
}
Also used : EntityReference(org.qi4j.api.entity.EntityReference) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Aggregations

EntityReference (org.qi4j.api.entity.EntityReference)60 Test (org.junit.Test)23 ArrayList (java.util.ArrayList)13 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)12 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)11 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)11 Person (org.qi4j.test.indexing.model.Person)11 HashMap (java.util.HashMap)9 Map (java.util.Map)9 QualifiedName (org.qi4j.api.common.QualifiedName)9 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)9 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)9 Writer (java.io.Writer)8 IOException (java.io.IOException)7 LinkedHashMap (java.util.LinkedHashMap)7 List (java.util.List)7 JSONException (org.json.JSONException)6 JSONObject (org.json.JSONObject)6 StringWriter (java.io.StringWriter)5 JSONArray (org.json.JSONArray)5