Search in sources :

Example 1 with NamedAssociationState

use of org.qi4j.spi.entity.NamedAssociationState in project qi4j-sdk by Qi4j.

the class BuilderEntityState method namedAssociationValueOf.

@Override
public NamedAssociationState namedAssociationValueOf(QualifiedName stateName) {
    NamedAssociationState state = namedAssociations.get(stateName);
    if (state == null) {
        state = new BuilderNamedAssociationState();
        namedAssociations.put(stateName, state);
    }
    return state;
}
Also used : NamedAssociationState(org.qi4j.spi.entity.NamedAssociationState)

Example 2 with NamedAssociationState

use of org.qi4j.spi.entity.NamedAssociationState 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 3 with NamedAssociationState

use of org.qi4j.spi.entity.NamedAssociationState 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 4 with NamedAssociationState

use of org.qi4j.spi.entity.NamedAssociationState in project qi4j-sdk by Qi4j.

the class GaeEntityState method namedAssociationValueOf.

@Override
public NamedAssociationState namedAssociationValueOf(QualifiedName stateName) {
    Map<String, String> assocs = (Map<String, String>) entity.getProperty(stateName.toURI());
    NamedAssociationState state = new GaeNamedAssociationState(this, assocs);
    return state;
}
Also used : NamedAssociationState(org.qi4j.spi.entity.NamedAssociationState) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with NamedAssociationState

use of org.qi4j.spi.entity.NamedAssociationState in project qi4j-sdk by Qi4j.

the class EntityResource method post.

@Override
public Representation post(Representation entityRepresentation, Variant variant) throws ResourceException {
    Usecase usecase = UsecaseBuilder.newUsecase("Update entity");
    EntityStoreUnitOfWork unitOfWork = entityStore.newUnitOfWork(usecase, module, System.currentTimeMillis());
    EntityState entity = getEntityState(unitOfWork);
    Form form = new Form(entityRepresentation);
    try {
        final EntityDescriptor descriptor = entity.entityDescriptor();
        // Parse JSON into properties
        for (PropertyDescriptor persistentProperty : descriptor.state().properties()) {
            if (!persistentProperty.isImmutable()) {
                String formValue = form.getFirstValue(persistentProperty.qualifiedName().name(), null);
                if (formValue == null) {
                    entity.setPropertyValue(persistentProperty.qualifiedName(), null);
                } else {
                    entity.setPropertyValue(persistentProperty.qualifiedName(), valueSerialization.deserialize(persistentProperty.valueType(), formValue));
                }
            }
        }
        for (AssociationDescriptor associationType : descriptor.state().associations()) {
            String newStringAssociation = form.getFirstValue(associationType.qualifiedName().name());
            if (newStringAssociation == null || newStringAssociation.isEmpty()) {
                entity.setAssociationValue(associationType.qualifiedName(), null);
            } else {
                entity.setAssociationValue(associationType.qualifiedName(), EntityReference.parseEntityReference(newStringAssociation));
            }
        }
        for (AssociationDescriptor associationType : descriptor.state().manyAssociations()) {
            String newStringAssociation = form.getFirstValue(associationType.qualifiedName().name());
            ManyAssociationState manyAssociation = entity.manyAssociationValueOf(associationType.qualifiedName());
            if (newStringAssociation == null) {
                // Remove "left-overs"
                for (EntityReference entityReference : manyAssociation) {
                    manyAssociation.remove(entityReference);
                }
                continue;
            }
            BufferedReader bufferedReader = new BufferedReader(new StringReader(newStringAssociation));
            String identity;
            try {
                // Synchronize old and new association
                int index = 0;
                while ((identity = bufferedReader.readLine()) != null) {
                    EntityReference reference = new EntityReference(identity);
                    if (manyAssociation.count() < index && manyAssociation.get(index).equals(reference)) {
                        continue;
                    }
                    try {
                        unitOfWork.entityStateOf(reference);
                        manyAssociation.remove(reference);
                        manyAssociation.add(index++, reference);
                    } catch (EntityNotFoundException e) {
                    // Ignore this entity - doesn't exist
                    }
                }
                // Remove "left-overs"
                while (manyAssociation.count() > index) {
                    manyAssociation.remove(manyAssociation.get(index));
                }
            } catch (IOException e) {
            // Ignore
            }
        }
        for (AssociationDescriptor associationType : descriptor.state().namedAssociations()) {
            String newStringAssociation = form.getFirstValue(associationType.qualifiedName().name());
            NamedAssociationState namedAssociation = entity.namedAssociationValueOf(associationType.qualifiedName());
            if (newStringAssociation == null) {
                // Remove "left-overs"
                for (String name : namedAssociation) {
                    namedAssociation.remove(name);
                }
                continue;
            }
            Set<String> names = new HashSet<>();
            BufferedReader bufferedReader = new BufferedReader(new StringReader(newStringAssociation));
            String line;
            try {
                while ((line = bufferedReader.readLine()) != null) {
                    String name = line;
                    line = bufferedReader.readLine();
                    if (line == null) {
                        break;
                    }
                    String identity = line;
                    EntityReference reference = new EntityReference(identity);
                    try {
                        unitOfWork.entityStateOf(reference);
                        namedAssociation.remove(name);
                        namedAssociation.put(name, reference);
                        names.add(name);
                    } catch (EntityNotFoundException e) {
                    // Ignore this entity - doesn't exist
                    }
                }
                // Remove "left-overs"
                for (String assocName : Iterables.toList(namedAssociation)) {
                    if (!names.contains(assocName)) {
                        namedAssociation.remove(assocName);
                    }
                }
            } catch (IOException e) {
            // Ignore
            }
        }
    } catch (ValueSerializationException | IllegalArgumentException e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
    }
    try {
        unitOfWork.applyChanges().commit();
    } catch (ConcurrentEntityStateModificationException e) {
        throw new ResourceException(Status.CLIENT_ERROR_CONFLICT);
    } catch (EntityNotFoundException e) {
        throw new ResourceException(Status.CLIENT_ERROR_GONE);
    }
    getResponse().setStatus(Status.SUCCESS_RESET_CONTENT);
    return new EmptyRepresentation();
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) EntityStoreUnitOfWork(org.qi4j.spi.entitystore.EntityStoreUnitOfWork) Form(org.restlet.data.Form) NamedAssociationState(org.qi4j.spi.entity.NamedAssociationState) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) EntityState(org.qi4j.spi.entity.EntityState) JSONEntityState(org.qi4j.spi.entitystore.helpers.JSONEntityState) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) IOException(java.io.IOException) ManyAssociationState(org.qi4j.spi.entity.ManyAssociationState) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) ConcurrentEntityStateModificationException(org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) EntityReference(org.qi4j.api.entity.EntityReference) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) ResourceException(org.restlet.resource.ResourceException) Usecase(org.qi4j.api.usecase.Usecase) HashSet(java.util.HashSet)

Aggregations

NamedAssociationState (org.qi4j.spi.entity.NamedAssociationState)5 EntityReference (org.qi4j.api.entity.EntityReference)3 ManyAssociationState (org.qi4j.spi.entity.ManyAssociationState)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)2 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)2 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1 Writer (java.io.Writer)1 HashSet (java.util.HashSet)1 QualifiedName (org.qi4j.api.common.QualifiedName)1 Usecase (org.qi4j.api.usecase.Usecase)1 ValueSerializationException (org.qi4j.api.value.ValueSerializationException)1 EntityState (org.qi4j.spi.entity.EntityState)1 ConcurrentEntityStateModificationException (org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException)1 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)1