Search in sources :

Example 11 with AssociationDescriptor

use of org.qi4j.api.association.AssociationDescriptor in project qi4j-sdk by Qi4j.

the class ValueToEntityMixin method doQualifiedUpdate.

private void doQualifiedUpdate(AssociationStateHolder eState, AssociationStateDescriptor eStateDesc, AssociationStateHolder vState, AssociationStateDescriptor vStateDesc) throws NoSuchEntityException {
    for (PropertyDescriptor ePropDesc : eStateDesc.properties()) {
        if (IDENTITY_STATE_NAME.equals(ePropDesc.qualifiedName())) {
            // Ignore Identity, could be logged
            continue;
        }
        try {
            PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByQualifiedName(ePropDesc.qualifiedName());
            eState.propertyFor(ePropDesc.accessor()).set(vState.propertyFor(vPropDesc.accessor()).get());
        } catch (IllegalArgumentException propNotFoundOnValue) {
        // Property not found on Value, do nothing
        }
    }
    for (AssociationDescriptor eAssocDesc : eStateDesc.associations()) {
        Association<Object> eAssoc = eState.associationFor(eAssocDesc.accessor());
        try {
            AssociationDescriptor vAssocDesc = vStateDesc.getAssociationByQualifiedName(eAssocDesc.qualifiedName());
            eAssoc.set(vState.associationFor(vAssocDesc.accessor()).get());
        } catch (IllegalArgumentException assocNotFoundOnValue) {
            // Association not found on Value, find Property<String> and load associated Entity
            try {
                PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName(eAssocDesc.qualifiedName().name());
                if (STRING_TYPE_SPEC.satisfiedBy(vPropDesc.valueType())) {
                    String assocId = (String) vState.propertyFor(vPropDesc.accessor()).get();
                    if (assocId != null) {
                        eAssoc.set(module.currentUnitOfWork().get((Class) eAssocDesc.type(), assocId));
                    } else {
                        eAssoc.set(null);
                    }
                }
            } catch (IllegalArgumentException propNotFoundOnValue) {
            // Do nothing
            }
        }
    }
    for (AssociationDescriptor eAssocDesc : eStateDesc.manyAssociations()) {
        ManyAssociation<Object> eManyAssoc = eState.manyAssociationFor(eAssocDesc.accessor());
        try {
            AssociationDescriptor vAssocDesc = vStateDesc.getManyAssociationByQualifiedName(eAssocDesc.qualifiedName());
            ManyAssociation<Object> vManyAssoc = vState.manyAssociationFor(vAssocDesc.accessor());
            for (Object assoc : eManyAssoc.toList()) {
                eManyAssoc.remove(assoc);
            }
            for (Object assoc : vManyAssoc.toList()) {
                eManyAssoc.add(assoc);
            }
        } catch (IllegalArgumentException assocNotFoundOnValue) {
            // ManyAssociation not found on Value, find Property<List<String>> and load associated Entities
            try {
                PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName(eAssocDesc.qualifiedName().name());
                if (STRING_COLLECTION_TYPE_SPEC.satisfiedBy(vPropDesc.valueType())) {
                    Collection<String> vAssocState = (Collection) vState.propertyFor(vPropDesc.accessor()).get();
                    for (Object assoc : eManyAssoc.toList()) {
                        eManyAssoc.remove(assoc);
                    }
                    if (vAssocState != null) {
                        for (String eachAssoc : vAssocState) {
                            eManyAssoc.add(module.currentUnitOfWork().get((Class) eAssocDesc.type(), eachAssoc));
                        }
                    }
                }
            } catch (IllegalArgumentException propNotFoundOnValue) {
            // Do nothing
            }
        }
    }
    for (AssociationDescriptor eAssocDesc : eStateDesc.namedAssociations()) {
        NamedAssociation<Object> eNamedAssoc = eState.namedAssociationFor(eAssocDesc.accessor());
        try {
            AssociationDescriptor vAssocDesc = vStateDesc.getNamedAssociationByQualifiedName(eAssocDesc.qualifiedName());
            NamedAssociation<Object> vNamedAssoc = vState.namedAssociationFor(vAssocDesc.accessor());
            for (String assocName : Iterables.toList(eNamedAssoc)) {
                eNamedAssoc.remove(assocName);
            }
            for (Map.Entry<String, Object> assocEntry : vNamedAssoc.toMap().entrySet()) {
                eNamedAssoc.put(assocEntry.getKey(), assocEntry.getValue());
            }
        } catch (IllegalArgumentException assocNotFoundOnValue) {
            // NamedAssociation not found on Value, find Property<Map<String,String>> and load associated Entities
            try {
                PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName(eAssocDesc.qualifiedName().name());
                if (STRING_MAP_TYPE_SPEC.satisfiedBy(vPropDesc.valueType())) {
                    Map<String, String> vAssocState = (Map) vState.propertyFor(vPropDesc.accessor()).get();
                    for (String assocName : Iterables.toList(eNamedAssoc)) {
                        eNamedAssoc.remove(assocName);
                    }
                    if (vAssocState != null) {
                        for (Map.Entry<String, String> assocEntry : vAssocState.entrySet()) {
                            eNamedAssoc.put(assocEntry.getKey(), module.currentUnitOfWork().get((Class) eAssocDesc.type(), assocEntry.getValue()));
                        }
                    }
                }
            } catch (IllegalArgumentException propNotFoundOnValue) {
            // Do nothing
            }
        }
    }
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) Collection(java.util.Collection) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 12 with AssociationDescriptor

use of org.qi4j.api.association.AssociationDescriptor in project qi4j-sdk by Qi4j.

the class ValueCompositeCxfType method readObject.

@Override
public Object readObject(final MessageReader reader, final Context context) throws DatabindingException {
    QName qname = getSchemaType();
    final String className = (qname.getNamespaceURI() + "." + qname.getLocalPart()).substring(20);
    // Read attributes
    ValueDescriptor descriptor = module.valueDescriptor(className);
    StateDescriptor stateDescriptor = descriptor.state();
    final Map<QualifiedName, Object> values = new HashMap<>();
    while (reader.hasMoreElementReaders()) {
        MessageReader childReader = reader.getNextElementReader();
        QName childName = childReader.getName();
        QualifiedName childQualifiedName = QualifiedName.fromClass((Class) typeClass, childName.getLocalPart());
        PropertyDescriptor propertyDescriptor = stateDescriptor.findPropertyModelByQualifiedName(childQualifiedName);
        Type propertyType = propertyDescriptor.type();
        AegisType type = getTypeMapping().getType(propertyType);
        Object value = type.readObject(childReader, context);
        values.put(childQualifiedName, value);
    }
    ValueBuilder<?> builder = module.newValueBuilderWithState((Class<?>) typeClass, new Function<PropertyDescriptor, Object>() {

        @Override
        public Object map(PropertyDescriptor descriptor1) {
            return values.get(descriptor1.qualifiedName());
        }
    }, new Function<AssociationDescriptor, EntityReference>() {

        @Override
        public EntityReference map(AssociationDescriptor descriptor) {
            Object value = values.get(descriptor.qualifiedName());
            if (value == null) {
                return null;
            }
            return EntityReference.parseEntityReference(value.toString());
        }
    }, new Function<AssociationDescriptor, Iterable<EntityReference>>() {

        @Override
        public Iterable<EntityReference> map(AssociationDescriptor descriptor) {
            Object value = values.get(descriptor.qualifiedName());
            if (value == null) {
                return Iterables.empty();
            }
            String[] ids = value.toString().split(",");
            List<EntityReference> references = new ArrayList<>(ids.length);
            for (String id : ids) {
                references.add(EntityReference.parseEntityReference(id));
            }
            return references;
        }
    }, new Function<AssociationDescriptor, Map<String, EntityReference>>() {

        @Override
        public Map<String, EntityReference> map(AssociationDescriptor descriptor) {
            Object value = values.get(descriptor.qualifiedName());
            if (value == null) {
                return Collections.emptyMap();
            }
            String[] namedRefs = value.toString().split(",");
            Map<String, EntityReference> references = new HashMap<>(namedRefs.length);
            for (String namedRef : namedRefs) {
                String[] splitted = namedRef.split(":");
                references.put(splitted[0], EntityReference.parseEntityReference(splitted[1]));
            }
            return references;
        }
    });
    return builder.newInstance();
}
Also used : HashMap(java.util.HashMap) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) MessageReader(org.apache.cxf.aegis.xml.MessageReader) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) EntityReference(org.qi4j.api.entity.EntityReference) ArrayList(java.util.ArrayList) List(java.util.List) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) QualifiedName(org.qi4j.api.common.QualifiedName) CollectionType(org.apache.cxf.aegis.type.collection.CollectionType) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) MapType(org.apache.cxf.aegis.type.collection.MapType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) AegisType(org.apache.cxf.aegis.type.AegisType) StateDescriptor(org.qi4j.api.composite.StateDescriptor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 13 with AssociationDescriptor

use of org.qi4j.api.association.AssociationDescriptor in project qi4j-sdk by Qi4j.

the class EntityStateSerializer method serializeManyAssociations.

private void serializeManyAssociations(final EntityState entityState, final Graph graph, final URI entityUri, final Iterable<? extends AssociationDescriptor> associations, final boolean includeNonQueryable) {
    ValueFactory values = graph.getValueFactory();
    // Many-Associations
    for (AssociationDescriptor associationType : associations) {
        if (!(includeNonQueryable || associationType.queryable())) {
            // Skip non-queryable
            continue;
        }
        BNode collection = values.createBNode();
        graph.add(entityUri, values.createURI(associationType.qualifiedName().toURI()), collection);
        graph.add(collection, Rdfs.TYPE, Rdfs.SEQ);
        ManyAssociationState associatedIds = entityState.manyAssociationValueOf(associationType.qualifiedName());
        for (EntityReference associatedId : associatedIds) {
            URI assocEntityURI = values.createURI(associatedId.toURI());
            graph.add(collection, Rdfs.LIST_ITEM, assocEntityURI);
        }
    }
}
Also used : BNode(org.openrdf.model.BNode) EntityReference(org.qi4j.api.entity.EntityReference) ValueFactory(org.openrdf.model.ValueFactory) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) ManyAssociationState(org.qi4j.spi.entity.ManyAssociationState) URI(org.openrdf.model.URI)

Example 14 with AssociationDescriptor

use of org.qi4j.api.association.AssociationDescriptor in project qi4j-sdk by Qi4j.

the class EntityTypeSerializer method serializeManyAssociationTypes.

private void serializeManyAssociationTypes(final EntityDescriptor entityDescriptor, final Graph graph, final URI entityTypeUri) {
    ValueFactory values = graph.getValueFactory();
    // ManyAssociations
    for (AssociationDescriptor manyAssociationType : entityDescriptor.state().manyAssociations()) {
        URI associationURI = values.createURI(manyAssociationType.qualifiedName().toURI());
        graph.add(associationURI, Rdfs.DOMAIN, entityTypeUri);
        graph.add(associationURI, Rdfs.TYPE, Rdfs.SEQ);
        URI associatedURI = values.createURI(manyAssociationType.qualifiedName().toURI());
        graph.add(associationURI, Rdfs.RANGE, associatedURI);
        graph.add(associationURI, Rdfs.RANGE, XMLSchema.ANYURI);
    }
}
Also used : ValueFactory(org.openrdf.model.ValueFactory) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) URI(org.openrdf.model.URI)

Example 15 with AssociationDescriptor

use of org.qi4j.api.association.AssociationDescriptor 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)

Aggregations

AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)28 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)15 EntityReference (org.qi4j.api.entity.EntityReference)13 Map (java.util.Map)8 QualifiedName (org.qi4j.api.common.QualifiedName)7 LinkedHashMap (java.util.LinkedHashMap)6 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (java.util.List)5 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)5 Test (org.junit.Test)4 URI (org.openrdf.model.URI)4 ValueFactory (org.openrdf.model.ValueFactory)4 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)4 Collection (java.util.Collection)3 AssociationStateDescriptor (org.qi4j.api.association.AssociationStateDescriptor)3 HashSet (java.util.HashSet)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2