Search in sources :

Example 31 with EntityReference

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

the class EntityResource method representHtml.

private Representation representHtml(final EntityState entity) {
    return new WriterRepresentation(MediaType.TEXT_HTML) {

        @Override
        public void write(Writer writer) throws IOException {
            PrintWriter out = new PrintWriter(writer);
            out.println("<html><head><title>" + entity.identity() + "</title>" + "<link rel=\"alternate\" type=\"application/rdf+xml\" " + "href=\"" + entity.identity() + ".rdf\"/></head><body>");
            out.println("<h1>" + entity.identity() + "</h1>");
            out.println("<form method=\"post\" action=\"" + getRequest().getResourceRef().getPath() + "\">\n");
            out.println("<fieldset><legend>Properties</legend>\n<table>");
            final EntityDescriptor descriptor = entity.entityDescriptor();
            for (PropertyDescriptor persistentProperty : descriptor.state().properties()) {
                Object value = entity.propertyValueOf(persistentProperty.qualifiedName());
                out.println("<tr><td>" + "<label for=\"" + persistentProperty.qualifiedName() + "\" >" + persistentProperty.qualifiedName().name() + "</label></td>\n" + "<td><input " + "size=\"80\" " + "type=\"text\" " + (persistentProperty.isImmutable() ? "readonly=\"true\" " : "") + "name=\"" + persistentProperty.qualifiedName() + "\" " + "value=\"" + (value == null ? "" : valueSerialization.serialize(value)) + "\"/></td></tr>");
            }
            out.println("</table></fieldset>\n");
            out.println("<fieldset><legend>Associations</legend>\n<table>");
            for (AssociationDescriptor associationType : descriptor.state().associations()) {
                Object value = entity.associationValueOf(associationType.qualifiedName());
                if (value == null) {
                    value = "";
                }
                out.println("<tr><td>" + "<label for=\"" + associationType.qualifiedName() + "\" >" + associationType.qualifiedName().name() + "</label></td>\n" + "<td><input " + "type=\"text\" " + "size=\"80\" " + "name=\"" + associationType.qualifiedName() + "\" " + "value=\"" + value + "\"/></td></tr>");
            }
            out.println("</table></fieldset>\n");
            out.println("<fieldset><legend>ManyAssociations</legend>\n<table>");
            for (AssociationDescriptor associationType : descriptor.state().manyAssociations()) {
                ManyAssociationState identities = entity.manyAssociationValueOf(associationType.qualifiedName());
                String value = "";
                for (EntityReference identity : identities) {
                    value += identity.identity() + "\n";
                }
                out.println("<tr><td>" + "<label for=\"" + associationType.qualifiedName() + "\" >" + associationType.qualifiedName().name() + "</label></td>\n" + "<td><textarea " + "rows=\"10\" " + "cols=\"80\" " + "name=\"" + associationType.qualifiedName() + "\" >" + value + "</textarea></td></tr>");
            }
            out.println("</table></fieldset>\n");
            out.println("<fieldset><legend>NamedAssociations</legend>\n<table>");
            for (AssociationDescriptor associationType : descriptor.state().namedAssociations()) {
                NamedAssociationState identities = entity.namedAssociationValueOf(associationType.qualifiedName());
                String value = "";
                for (String name : identities) {
                    value += name + "\n" + identities.get(name).identity() + "\n";
                }
                out.println("<tr><td>" + "<label for=\"" + associationType.qualifiedName() + "\" >" + associationType.qualifiedName().name() + "</label></td>\n" + "<td><textarea " + "rows=\"10\" " + "cols=\"80\" " + "name=\"" + associationType.qualifiedName() + "\" >" + value + "</textarea></td></tr>");
            }
            out.println("</table></fieldset>\n");
            out.println("<input type=\"submit\" value=\"Update\"/></form>\n");
            out.println("</body></html>\n");
        }
    };
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) WriterRepresentation(org.restlet.representation.WriterRepresentation) NamedAssociationState(org.qi4j.spi.entity.NamedAssociationState) EntityReference(org.qi4j.api.entity.EntityReference) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) ManyAssociationState(org.qi4j.spi.entity.ManyAssociationState) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 32 with EntityReference

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

the class BuilderEntityState method copyTo.

public void copyTo(EntityState newEntityState) {
    for (Map.Entry<QualifiedName, Object> fromPropertyEntry : properties.entrySet()) {
        newEntityState.setPropertyValue(fromPropertyEntry.getKey(), fromPropertyEntry.getValue());
    }
    for (Map.Entry<QualifiedName, EntityReference> fromAssociationEntry : associations.entrySet()) {
        newEntityState.setAssociationValue(fromAssociationEntry.getKey(), fromAssociationEntry.getValue());
    }
    for (Map.Entry<QualifiedName, ManyAssociationState> fromManyAssociationEntry : manyAssociations.entrySet()) {
        QualifiedName qName = fromManyAssociationEntry.getKey();
        ManyAssociationState fromManyAssoc = fromManyAssociationEntry.getValue();
        ManyAssociationState toManyAssoc = newEntityState.manyAssociationValueOf(qName);
        for (EntityReference entityReference : fromManyAssoc) {
            toManyAssoc.add(0, entityReference);
        }
    }
    for (Map.Entry<QualifiedName, NamedAssociationState> fromNamedAssociationEntry : namedAssociations.entrySet()) {
        QualifiedName qName = fromNamedAssociationEntry.getKey();
        NamedAssociationState fromNamedAssoc = fromNamedAssociationEntry.getValue();
        NamedAssociationState toNamedAssoc = newEntityState.namedAssociationValueOf(qName);
        for (String name : fromNamedAssoc) {
            toNamedAssoc.put(name, fromNamedAssoc.get(name));
        }
    }
}
Also used : NamedAssociationState(org.qi4j.spi.entity.NamedAssociationState) QualifiedName(org.qi4j.api.common.QualifiedName) EntityReference(org.qi4j.api.entity.EntityReference) ManyAssociationState(org.qi4j.spi.entity.ManyAssociationState) Map(java.util.Map) HashMap(java.util.HashMap)

Example 33 with EntityReference

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

the class UnitOfWorkInstance method applyChanges.

private List<StateCommitter> applyChanges() throws UnitOfWorkCompletionException {
    List<StateCommitter> committers = new ArrayList<>();
    for (EntityStoreUnitOfWork entityStoreUnitOfWork : storeUnitOfWork.values()) {
        try {
            StateCommitter committer = entityStoreUnitOfWork.applyChanges();
            committers.add(committer);
        } catch (Exception e) {
            // Cancel all previously prepared stores
            for (StateCommitter committer : committers) {
                committer.cancel();
            }
            if (e instanceof ConcurrentEntityStateModificationException) {
                // If we cancelled due to concurrent modification, then create the proper exception for it!
                ConcurrentEntityStateModificationException mee = (ConcurrentEntityStateModificationException) e;
                Collection<EntityReference> modifiedEntityIdentities = mee.modifiedEntities();
                Collection<EntityComposite> modifiedEntities = new ArrayList<>();
                for (EntityReference modifiedEntityIdentity : modifiedEntityIdentities) {
                    Collection<EntityInstance> instances = instanceCache.values();
                    for (EntityInstance instance : instances) {
                        if (instance.identity().equals(modifiedEntityIdentity)) {
                            modifiedEntities.add(instance.<EntityComposite>proxy());
                        }
                    }
                }
                throw new ConcurrentEntityModificationException(modifiedEntities);
            } else {
                throw new UnitOfWorkCompletionException(e);
            }
        }
    }
    return committers;
}
Also used : EntityComposite(org.qi4j.api.entity.EntityComposite) EntityStoreUnitOfWork(org.qi4j.spi.entitystore.EntityStoreUnitOfWork) ArrayList(java.util.ArrayList) EntityInstance(org.qi4j.runtime.entity.EntityInstance) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) UnitOfWorkException(org.qi4j.api.unitofwork.UnitOfWorkException) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException) ConcurrentEntityStateModificationException(org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) ConcurrentEntityStateModificationException(org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException) EntityReference(org.qi4j.api.entity.EntityReference) Collection(java.util.Collection) StateCommitter(org.qi4j.spi.entitystore.StateCommitter)

Example 34 with EntityReference

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

the class JSONEntityState method associationValueOf.

@Override
public EntityReference associationValueOf(QualifiedName stateName) {
    try {
        Object jsonValue = state.getJSONObject(JSONKeys.ASSOCIATIONS).opt(stateName.name());
        if (jsonValue == null) {
            return null;
        }
        EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference((String) jsonValue);
        return value;
    } catch (JSONException e) {
        throw new EntityStoreException(e);
    }
}
Also used : EntityReference(org.qi4j.api.entity.EntityReference) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException)

Example 35 with EntityReference

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

the class MapEntityStoreMixin method writeEntityState.

protected void writeEntityState(DefaultEntityState state, Writer writer, String version, long lastModified) throws EntityStoreException {
    try {
        JSONWriter json = new JSONWriter(writer);
        JSONWriter properties = json.object().key(JSONKeys.IDENTITY).value(state.identity().identity()).key(JSONKeys.APPLICATION_VERSION).value(application.version()).key(JSONKeys.TYPE).value(first(state.entityDescriptor().types()).getName()).key(JSONKeys.VERSION).value(version).key(JSONKeys.MODIFIED).value(lastModified).key(JSONKeys.PROPERTIES).object();
        EntityDescriptor entityType = state.entityDescriptor();
        for (PropertyDescriptor persistentProperty : entityType.state().properties()) {
            Object value = state.properties().get(persistentProperty.qualifiedName());
            json.key(persistentProperty.qualifiedName().name());
            if (value == null || ValueType.isPrimitiveValue(value)) {
                json.value(value);
            } else {
                String serialized = valueSerialization.serialize(value);
                if (serialized.startsWith("{")) {
                    json.value(new JSONObject(serialized));
                } else if (serialized.startsWith("[")) {
                    json.value(new JSONArray(serialized));
                } else {
                    json.value(serialized);
                }
            }
        }
        JSONWriter associations = properties.endObject().key(JSONKeys.ASSOCIATIONS).object();
        for (Map.Entry<QualifiedName, EntityReference> stateNameEntityReferenceEntry : state.associations().entrySet()) {
            EntityReference value = stateNameEntityReferenceEntry.getValue();
            associations.key(stateNameEntityReferenceEntry.getKey().name()).value(value != null ? value.identity() : null);
        }
        JSONWriter manyAssociations = associations.endObject().key(JSONKeys.MANY_ASSOCIATIONS).object();
        for (Map.Entry<QualifiedName, List<EntityReference>> stateNameListEntry : state.manyAssociations().entrySet()) {
            JSONWriter assocs = manyAssociations.key(stateNameListEntry.getKey().name()).array();
            for (EntityReference entityReference : stateNameListEntry.getValue()) {
                assocs.value(entityReference.identity());
            }
            assocs.endArray();
        }
        JSONWriter namedAssociations = manyAssociations.endObject().key(JSONKeys.NAMED_ASSOCIATIONS).object();
        for (Map.Entry<QualifiedName, Map<String, EntityReference>> stateNameMapEntry : state.namedAssociations().entrySet()) {
            JSONWriter assocs = namedAssociations.key(stateNameMapEntry.getKey().name()).object();
            for (Map.Entry<String, EntityReference> namedRef : stateNameMapEntry.getValue().entrySet()) {
                assocs.key(namedRef.getKey()).value(namedRef.getValue().identity());
            }
            assocs.endObject();
        }
        namedAssociations.endObject().endObject();
    } catch (JSONException e) {
        throw new EntityStoreException("Could not store EntityState", e);
    }
}
Also used : JSONWriter(org.json.JSONWriter) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) QualifiedName(org.qi4j.api.common.QualifiedName) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) JSONObject(org.json.JSONObject) EntityReference(org.qi4j.api.entity.EntityReference) JSONObject(org.json.JSONObject) List(java.util.List) ArrayList(java.util.ArrayList) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

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