Search in sources :

Example 1 with PropertyDescriptor

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

the class EntityBuilderWithStateTest method test.

@Test
public void test() throws UnitOfWorkCompletionException {
    final String associatedIdentity;
    try (UnitOfWork uow = module.newUnitOfWork()) {
        EntityBuilder<SomeEntity> builder = uow.newEntityBuilder(SomeEntity.class);
        builder.instance().prop().set("Associated");
        SomeEntity entity = builder.newInstance();
        associatedIdentity = entity.identity().get();
        uow.complete();
    }
    try (UnitOfWork uow = module.newUnitOfWork()) {
        SomeEntity entity = uow.newEntityBuilderWithState(SomeEntity.class, new Function<PropertyDescriptor, Object>() {

            @Override
            public Object map(PropertyDescriptor descriptor) {
                if ("prop".equals(descriptor.qualifiedName().name())) {
                    return "Foo";
                }
                return null;
            }
        }, new Function<AssociationDescriptor, EntityReference>() {

            @Override
            public EntityReference map(AssociationDescriptor descriptor) {
                if ("ass".equals(descriptor.qualifiedName().name())) {
                    return EntityReference.parseEntityReference(associatedIdentity);
                }
                return null;
            }
        }, new Function<AssociationDescriptor, Iterable<EntityReference>>() {

            @Override
            public Iterable<EntityReference> map(AssociationDescriptor descriptor) {
                if ("manyAss".equals(descriptor.qualifiedName().name())) {
                    return Arrays.asList(EntityReference.parseEntityReference(associatedIdentity));
                }
                return null;
            }
        }, new Function<AssociationDescriptor, Map<String, EntityReference>>() {

            @Override
            public Map<String, EntityReference> map(AssociationDescriptor descriptor) {
                if ("namedAss".equals(descriptor.qualifiedName().name())) {
                    return Collections.singletonMap("foo", EntityReference.parseEntityReference(associatedIdentity));
                }
                return null;
            }
        }).newInstance();
        assertThat(entity.prop().get(), equalTo("Foo"));
        assertThat(entity.ass().get().identity().get(), equalTo(associatedIdentity));
        assertThat(entity.manyAss().get(0).identity().get(), equalTo(associatedIdentity));
        assertThat(entity.namedAss().get("foo").identity().get(), equalTo(associatedIdentity));
        uow.complete();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Function(org.qi4j.functional.Function) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) EntityReference(org.qi4j.api.entity.EntityReference) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 2 with PropertyDescriptor

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

the class ValueSerializerAdapter method serializeValueComposite.

private void serializeValueComposite(Options options, Object object, OutputType output, boolean rootPass) throws Exception {
    CompositeInstance valueInstance = Qi4j.FUNCTION_COMPOSITE_INSTANCE_OF.map((ValueComposite) object);
    ValueDescriptor descriptor = (ValueDescriptor) valueInstance.descriptor();
    AssociationStateHolder state = (AssociationStateHolder) valueInstance.state();
    onObjectStart(output);
    if (options.getBoolean(Options.INCLUDE_TYPE_INFO) && !rootPass) {
        onFieldStart(output, "_type");
        onValueStart(output);
        onValue(output, first(descriptor.valueType().types()).getName());
        onValueEnd(output);
        onFieldEnd(output);
    }
    for (PropertyDescriptor persistentProperty : descriptor.valueType().properties()) {
        Property<?> property = state.propertyFor(persistentProperty.accessor());
        onFieldStart(output, persistentProperty.qualifiedName().name());
        onValueStart(output);
        doSerialize(options, property.get(), output, false);
        onValueEnd(output);
        onFieldEnd(output);
    }
    for (AssociationDescriptor associationDescriptor : descriptor.valueType().associations()) {
        Association<?> association = state.associationFor(associationDescriptor.accessor());
        Object instance = association.get();
        onFieldStart(output, associationDescriptor.qualifiedName().name());
        onValueStart(output);
        if (instance == null) {
            onValue(output, null);
        } else {
            onValue(output, ((Identity) instance).identity().get());
        }
        onValueEnd(output);
        onFieldEnd(output);
    }
    for (AssociationDescriptor associationDescriptor : descriptor.valueType().manyAssociations()) {
        ManyAssociation<?> manyAssociation = state.manyAssociationFor(associationDescriptor.accessor());
        onFieldStart(output, associationDescriptor.qualifiedName().name());
        onValueStart(output);
        onArrayStart(output);
        for (Object instance : manyAssociation) {
            onValueStart(output);
            onValue(output, ((Identity) instance).identity().get());
            onValueEnd(output);
        }
        onArrayEnd(output);
        onValueEnd(output);
        onFieldEnd(output);
    }
    for (AssociationDescriptor associationDescriptor : descriptor.valueType().namedAssociations()) {
        NamedAssociation<?> namedAssociation = state.namedAssociationFor(associationDescriptor.accessor());
        onFieldStart(output, associationDescriptor.qualifiedName().name());
        onValueStart(output);
        onObjectStart(output);
        for (String name : namedAssociation) {
            onFieldStart(output, name);
            onValueStart(output);
            onValue(output, ((Identity) namedAssociation.get(name)).identity().get());
            onValueEnd(output);
            onFieldEnd(output);
        }
        onObjectEnd(output);
        onValueEnd(output);
        onFieldEnd(output);
    }
    onObjectEnd(output);
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) CompositeInstance(org.qi4j.api.composite.CompositeInstance) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) AssociationStateHolder(org.qi4j.api.association.AssociationStateHolder) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) Identity(org.qi4j.api.entity.Identity)

Example 3 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 4 with PropertyDescriptor

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

the class GaeEntityState method initializeValueTypes.

private HashMap<QualifiedName, ValueType> initializeValueTypes(EntityDescriptor descriptor) {
    HashMap<QualifiedName, ValueType> result = new HashMap<QualifiedName, ValueType>();
    for (PropertyDescriptor persistent : descriptor.state().properties()) {
        if (persistent.valueType() instanceof ValueCompositeType) {
            QualifiedName name = persistent.qualifiedName();
            result.put(name, persistent.valueType());
        }
    }
    return result;
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) ValueType(org.qi4j.api.type.ValueType) HashMap(java.util.HashMap) QualifiedName(org.qi4j.api.common.QualifiedName) ValueCompositeType(org.qi4j.api.type.ValueCompositeType)

Example 5 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<QualifiedName, Object>();
    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;
            else
                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();
            else {
                String[] ids = value.toString().split(",");
                List<EntityReference> references = new ArrayList<EntityReference>();
                for (int i = 0; i < ids.length; i++) {
                    String id = ids[i];
                    references.add(EntityReference.parseEntityReference(id));
                }
                return references;
            }
        }
    });
    return builder.newInstance();
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) QualifiedName(org.qi4j.api.common.QualifiedName) MessageReader(org.apache.cxf.aegis.xml.MessageReader) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) CollectionType(org.apache.cxf.aegis.type.collection.CollectionType) 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) EntityReference(org.qi4j.api.entity.EntityReference)

Aggregations

PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)38 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)16 EntityReference (org.qi4j.api.entity.EntityReference)12 QualifiedName (org.qi4j.api.common.QualifiedName)10 ValueDescriptor (org.qi4j.api.value.ValueDescriptor)10 HashMap (java.util.HashMap)9 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)9 Map (java.util.Map)8 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 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 Collection (java.util.Collection)3