Search in sources :

Example 1 with Identity

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

the class InterfaceCollisionWithRelatedReturnTypesTest method shouldBeAbleToSetNameToTheCompany.

@Test
public void shouldBeAbleToSetNameToTheCompany() throws UnitOfWorkCompletionException {
    String identity;
    try (UnitOfWork uow = module.newUnitOfWork()) {
        Company startUp = uow.newEntity(Company.class);
        startUp.name().set("Acme");
        identity = ((Identity) startUp).identity().get();
        uow.complete();
    }
    try (UnitOfWork uow = module.newUnitOfWork()) {
        Company startUp = uow.get(Company.class, identity);
        assertThat(startUp.name().get(), equalTo("Acme"));
        SalesTeam sales = uow.get(SalesTeam.class, identity);
        assertThat(sales.name().get(), equalTo("Acme"));
        ResearchTeam research = uow.get(ResearchTeam.class, identity);
        assertThat(research.name().get(), equalTo("Acme"));
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Identity(org.qi4j.api.entity.Identity) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 2 with Identity

use of org.qi4j.api.entity.Identity 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 Identity

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

the class InterfaceCollisionWithRelatedReturnTypesTest method shouldBeAbleToSetLeadToTheCompany.

@Test
public void shouldBeAbleToSetLeadToTheCompany() throws UnitOfWorkCompletionException {
    String identity;
    try (UnitOfWork uow = module.newUnitOfWork()) {
        Company startUp = uow.newEntity(Company.class);
        Employee niclas = uow.newEntity(Employee.class);
        startUp.lead().set(niclas);
        identity = ((Identity) startUp).identity().get();
        uow.complete();
    }
    try (UnitOfWork uow = module.newUnitOfWork()) {
        Company startUp = uow.get(Company.class, identity);
        Employee niclas = startUp.lead().get();
        assertThat(niclas, notNullValue());
        SalesTeam sales = uow.get(SalesTeam.class, identity);
        assertThat(sales.lead().get(), equalTo(niclas));
        ResearchTeam research = uow.get(ResearchTeam.class, identity);
        assertThat(research.lead().get(), equalTo(niclas));
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Identity(org.qi4j.api.entity.Identity) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 4 with Identity

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

the class ValueCompositeCxfType method writeObject.

@Override
public void writeObject(Object object, final MessageWriter writer, final Context context) throws DatabindingException {
    ValueComposite composite = (ValueComposite) object;
    writer.writeXsiType(NamespaceUtil.convertJavaTypeToQName(first(Qi4j.FUNCTION_DESCRIPTOR_FOR.map(composite).types())));
    AssociationStateHolder state = spi.stateOf(composite);
    for (Property<?> property : state.properties()) {
        Object value = property.get();
        AegisType type = null;
        if (value instanceof ValueComposite) {
            ValueComposite compositeValue = (ValueComposite) value;
            type = getTypeMapping().getType(NamespaceUtil.convertJavaTypeToQName(first(Qi4j.FUNCTION_DESCRIPTOR_FOR.map(compositeValue).types())));
        } else {
            if (value != null) {
                type = getOrCreateNonQi4jType(value);
            }
        }
        QName childName = new QName("", spi.propertyDescriptorFor(property).qualifiedName().name());
        MessageWriter cwriter = writer.getElementWriter(childName);
        if (type != null) {
            type.writeObject(value, cwriter, context);
        } else {
        // cwriter.writeXsiNil();
        }
        cwriter.close();
    }
    AegisType type = getTypeMapping().getType(NamespaceUtil.convertJavaTypeToQName(String.class));
    for (Association association : state.allAssociations()) {
        QName childName = new QName("", spi.associationDescriptorFor(association).qualifiedName().name());
        MessageWriter cwriter = writer.getElementWriter(childName);
        if (association.get() != null) {
            type.writeObject(((Identity) association.get()).identity().get(), cwriter, context);
        }
        cwriter.close();
    }
    for (ManyAssociation association : state.allManyAssociations()) {
        QName childName = new QName("", spi.associationDescriptorFor(association).qualifiedName().name());
        MessageWriter cwriter = writer.getElementWriter(childName);
        String ids = null;
        for (Object entity : association) {
            String id = EntityReference.entityReferenceFor(entity).identity();
            if (ids != null)
                ids += ",";
            ids += ids;
        }
        if (ids == null) {
            ids = "";
        }
        type.writeObject(ids, cwriter, context);
        cwriter.close();
    }
}
Also used : Association(org.qi4j.api.association.Association) ManyAssociation(org.qi4j.api.association.ManyAssociation) AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) MessageWriter(org.apache.cxf.aegis.xml.MessageWriter) AssociationStateHolder(org.qi4j.api.association.AssociationStateHolder) Identity(org.qi4j.api.entity.Identity) ManyAssociation(org.qi4j.api.association.ManyAssociation)

Example 5 with Identity

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

the class ValueSerializerAdapter method serializeValueComposite.

private void serializeValueComposite(Object object, OutputType output, boolean includeTypeInfo, 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 (includeTypeInfo && !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(property.get(), output, includeTypeInfo, 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);
    }
    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)

Aggregations

Identity (org.qi4j.api.entity.Identity)6 Test (org.junit.Test)3 AssociationStateHolder (org.qi4j.api.association.AssociationStateHolder)3 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)3 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)2 CompositeInstance (org.qi4j.api.composite.CompositeInstance)2 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)2 ValueDescriptor (org.qi4j.api.value.ValueDescriptor)2 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)2 QName (javax.xml.namespace.QName)1 AegisType (org.apache.cxf.aegis.type.AegisType)1 MessageWriter (org.apache.cxf.aegis.xml.MessageWriter)1 Association (org.qi4j.api.association.Association)1 ManyAssociation (org.qi4j.api.association.ManyAssociation)1 Application (org.qi4j.api.structure.Application)1 Module (org.qi4j.api.structure.Module)1 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)1 ApplicationAssembler (org.qi4j.bootstrap.ApplicationAssembler)1 ApplicationAssembly (org.qi4j.bootstrap.ApplicationAssembly)1 ApplicationAssemblyFactory (org.qi4j.bootstrap.ApplicationAssemblyFactory)1