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"));
}
}
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);
}
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));
}
}
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();
}
}
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);
}
Aggregations