use of org.qi4j.api.composite.StateDescriptor 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();
}
use of org.qi4j.api.composite.StateDescriptor in project qi4j-sdk by Qi4j.
the class StateInjectionProviderFactory method newInjectionProvider.
@Override
public InjectionProvider newInjectionProvider(Resolution resolution, DependencyModel dependencyModel) throws InvalidInjectionException {
if (StateHolder.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
// @State StateHolder properties;
return new StateInjectionProvider();
} else if (UnitOfWork.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
if (!(resolution.model() instanceof EntityDescriptor)) {
throw new InvalidInjectionException("Only EntityComposites can be injected with '@State UnitOfWork'");
}
return new UnitOfWorkInjectionProvider();
} else if (Property.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
// @State Property<String> name;
StateDescriptor descriptor;
descriptor = ((StatefulCompositeDescriptor) resolution.model()).state();
State annotation = (State) dependencyModel.injectionAnnotation();
String name;
if (annotation.value().isEmpty()) {
name = resolution.field().getName();
} else {
name = annotation.value();
}
PropertyDescriptor propertyDescriptor = descriptor.findPropertyModelByName(name);
return new PropertyInjectionProvider(propertyDescriptor);
} else if (Association.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
// @State Association<MyEntity> name;
AssociationStateDescriptor descriptor = ((EntityDescriptor) resolution.model()).state();
State annotation = (State) dependencyModel.injectionAnnotation();
String name;
if (annotation.value().isEmpty()) {
name = resolution.field().getName();
} else {
name = annotation.value();
}
AssociationDescriptor model = descriptor.getAssociationByName(name);
return new AssociationInjectionProvider(model);
} else if (ManyAssociation.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
// @State ManyAssociation<MyEntity> name;
AssociationStateDescriptor descriptor = ((EntityDescriptor) resolution.model()).state();
State annotation = (State) dependencyModel.injectionAnnotation();
String name;
if (annotation.value().isEmpty()) {
name = resolution.field().getName();
} else {
name = annotation.value();
}
AssociationDescriptor model = descriptor.getManyAssociationByName(name);
return new ManyAssociationInjectionProvider(model);
} else if (NamedAssociation.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
// @State NamedAssociation<MyEntity> name;
AssociationStateDescriptor descriptor = ((EntityDescriptor) resolution.model()).state();
State annotation = (State) dependencyModel.injectionAnnotation();
String name;
if (annotation.value().isEmpty()) {
name = resolution.field().getName();
} else {
name = annotation.value();
}
AssociationDescriptor model = descriptor.getNamedAssociationByName(name);
return new NamedAssociationInjectionProvider(model);
}
throw new InjectionProviderException("Injected value has invalid type");
}
Aggregations