Search in sources :

Example 11 with PropertyDescriptor

use of org.qi4j.api.property.PropertyDescriptor in project qi4j-sdk by Qi4j.

the class AbstractSQLIndexing method storePropertiesOfVC.

private Integer storePropertiesOfVC(Map<QualifiedName, PreparedStatement> qNameInsertPSs, PreparedStatement insertAllQNamesPS, Integer propertyPK, Long entityPK, Object property) throws SQLException {
    ValueDescriptor vDesc = this._qi4SPI.valueDescriptorFor((ValueComposite) property);
    StateHolder state = Qi4j.FUNCTION_COMPOSITE_INSTANCE_OF.map((ValueComposite) property).state();
    Integer originalPropertyPK = propertyPK;
    ++propertyPK;
    for (PropertyDescriptor pDesc : vDesc.state().properties()) {
        propertyPK = this.insertProperty(qNameInsertPSs, insertAllQNamesPS, propertyPK, entityPK, pDesc.qualifiedName(), state.propertyFor(pDesc.accessor()).get(), originalPropertyPK);
    }
    return propertyPK;
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) StateHolder(org.qi4j.api.property.StateHolder) ValueComposite(org.qi4j.api.value.ValueComposite)

Example 12 with PropertyDescriptor

use of org.qi4j.api.property.PropertyDescriptor 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 13 with PropertyDescriptor

use of org.qi4j.api.property.PropertyDescriptor in project qi4j-sdk by Qi4j.

the class ValueCompositeCxfType method writeSchema.

@Override
public void writeSchema(XmlSchema root) {
    XmlSchemaComplexType complex = new XmlSchemaComplexType(root, true);
    complex.setName(getSchemaType().getLocalPart());
    root.getItems().add(complex);
    // No clue why this?
    XmlSchemaSequence sequence = new XmlSchemaSequence();
    // No idea what this is for
    complex.setParticle(sequence);
    ValueDescriptor descriptor = module.valueDescriptor(getTypeClass().getName());
    for (PropertyDescriptor p : descriptor.state().properties()) {
        if (isValueComposite(p.type())) {
            XmlSchemaElement element = new XmlSchemaElement(root, false);
            element.setName(p.qualifiedName().name());
            // see below
            element.setNillable(p.metaInfo(Optional.class) != null);
            sequence.getItems().add(element);
            AegisType nested = getOrCreateAegisType(p.type(), root);
            element.setSchemaTypeName(nested.getSchemaType());
        } else if (isCollectionOrMap(p)) {
            XmlSchemaElement element = new XmlSchemaElement(root, false);
            element.setName(p.qualifiedName().name());
            // see below
            element.setNillable(p.metaInfo(Optional.class) != null);
            sequence.getItems().add(element);
            AegisType nested = getOrCreateAegisType(p.type(), root);
            element.setSchemaTypeName(nested.getSchemaType());
        } else {
            XmlSchemaAttribute attribute = new XmlSchemaAttribute(root, false);
            complex.getAttributes().add(attribute);
            attribute.setName(p.qualifiedName().name());
            AegisType nested = getTypeMapping().getType(p.type());
            attribute.setSchemaTypeName(nested.getSchemaType());
        }
        QName name = NamespaceUtil.convertJavaTypeToQName(p.type());
        String ns = name.getNamespaceURI();
        if (!ns.equals(root.getTargetNamespace())) {
            XmlSchemaUtils.addImportIfNeeded(root, ns);
        }
    }
}
Also used : XmlSchemaSequence(org.apache.ws.commons.schema.XmlSchemaSequence) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) Optional(org.qi4j.api.common.Optional) AegisType(org.apache.cxf.aegis.type.AegisType) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) QName(javax.xml.namespace.QName) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlSchemaAttribute(org.apache.ws.commons.schema.XmlSchemaAttribute)

Example 14 with PropertyDescriptor

use of org.qi4j.api.property.PropertyDescriptor 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 15 with PropertyDescriptor

use of org.qi4j.api.property.PropertyDescriptor in project qi4j-sdk by Qi4j.

the class ValueCompositeRequestWriter method writeRequest.

@Override
public boolean writeRequest(Object requestObject, Request request) throws ResourceException {
    if (requestObject instanceof ValueComposite) {
        // Value as parameter
        final ValueComposite valueObject = (ValueComposite) requestObject;
        if (request.getMethod().equals(Method.GET)) {
            StateHolder holder = spi.stateOf(valueObject);
            final ValueDescriptor descriptor = spi.valueDescriptorFor(valueObject);
            final Reference ref = request.getResourceRef();
            ref.setQuery(null);
            try {
                for (PropertyDescriptor propertyDescriptor : descriptor.state().properties()) {
                    Object value = holder.propertyFor(propertyDescriptor.accessor()).get();
                    String param;
                    if (value == null) {
                        param = null;
                    } else {
                        param = valueSerializer.serialize(value);
                    }
                    ref.addQueryParameter(propertyDescriptor.qualifiedName().name(), param);
                }
            } catch (ValueSerializationException e) {
                throw new ResourceException(e);
            }
        } else {
            request.setEntity(new WriterRepresentation(MediaType.APPLICATION_JSON) {

                @Override
                public void write(Writer writer) throws IOException {
                    setCharacterSet(CharacterSet.UTF_8);
                    valueSerializer.serialize(valueObject, new WriterOutputStream(writer));
                }
            });
        }
        return true;
    }
    return false;
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) Reference(org.restlet.data.Reference) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) IOException(java.io.IOException) WriterOutputStream(org.restlet.engine.io.WriterOutputStream) ValueComposite(org.qi4j.api.value.ValueComposite) WriterRepresentation(org.restlet.representation.WriterRepresentation) StateHolder(org.qi4j.api.property.StateHolder) ResourceException(org.restlet.resource.ResourceException) RequestWriter(org.qi4j.library.rest.client.spi.RequestWriter) Writer(java.io.Writer)

Aggregations

PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)37 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)15 Map (java.util.Map)12 EntityReference (org.qi4j.api.entity.EntityReference)12 ValueDescriptor (org.qi4j.api.value.ValueDescriptor)11 HashMap (java.util.HashMap)10 QualifiedName (org.qi4j.api.common.QualifiedName)10 LinkedHashMap (java.util.LinkedHashMap)9 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)8 JSONException (org.json.JSONException)5 JSONObject (org.json.JSONObject)5 ValueType (org.qi4j.api.type.ValueType)5 ValueSerializationException (org.qi4j.api.value.ValueSerializationException)5 JSONArray (org.json.JSONArray)4 Test (org.junit.Test)4 ValueCompositeType (org.qi4j.api.type.ValueCompositeType)4 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)4