use of org.qi4j.api.value.ValueComposite in project qi4j-sdk by Qi4j.
the class PropertyInstance method prepareBuilderState.
@SuppressWarnings({ "raw", "unchecked" })
public void prepareBuilderState(PropertyModel propertyDescriptor) {
// Check if state has to be modified
if (propertyDescriptor.valueType() instanceof ValueCompositeType) {
Object value = get();
if (value != null) {
ValueInstance.valueInstanceOf((ValueComposite) value).prepareBuilderState();
}
} else if (propertyDescriptor.valueType() instanceof CollectionType) {
T value = get();
if (value != null) {
if (propertyDescriptor.isImmutable()) {
if (value instanceof List) {
value = (T) Collections.unmodifiableList((List<? extends Object>) value);
} else if (value instanceof Set) {
value = (T) Collections.unmodifiableSet((Set<? extends Object>) value);
} else {
value = (T) Collections.unmodifiableCollection((Collection<? extends Object>) value);
}
this.value = value;
}
CollectionType collection = (CollectionType) propertyDescriptor.valueType();
if (collection.collectedType() instanceof ValueCompositeType) {
Collection coll = (Collection) value;
for (Object instance : coll) {
ValueInstance.valueInstanceOf((ValueComposite) instance).prepareBuilderState();
}
}
}
} else if (propertyDescriptor.valueType() instanceof MapType) {
T value = get();
if (value != null) {
MapType mapType = (MapType) propertyDescriptor.valueType();
if (mapType.keyType() instanceof ValueCompositeType) {
Map map = (Map) value;
for (Object instance : map.keySet()) {
ValueInstance.valueInstanceOf((ValueComposite) instance).prepareBuilderState();
}
}
if (mapType.valueType() instanceof ValueCompositeType) {
Map map = (Map) value;
for (Object instance : map.values()) {
ValueInstance.valueInstanceOf((ValueComposite) instance).prepareBuilderState();
}
}
if (propertyDescriptor.isImmutable()) {
value = (T) Collections.unmodifiableMap((Map<?, ?>) value);
}
this.value = value;
}
}
model = propertyDescriptor;
}
use of org.qi4j.api.value.ValueComposite 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 += id;
}
if (ids == null) {
ids = "";
}
type.writeObject(ids, cwriter, context);
cwriter.close();
}
for (NamedAssociation<?> association : state.allNamedAssociations()) {
QName childName = new QName("", spi.associationDescriptorFor(association).qualifiedName().name());
MessageWriter cwriter = writer.getElementWriter(childName);
String ids = null;
for (String name : association) {
String id = EntityReference.entityReferenceFor(association.get(name)).identity();
if (ids != null) {
ids += ",";
}
ids += name + ":" + id;
}
if (ids == null) {
ids = "";
}
type.writeObject(ids, cwriter, context);
cwriter.close();
}
}
use of org.qi4j.api.value.ValueComposite in project qi4j-sdk by Qi4j.
the class ContextResource method resource.
private void resource() {
Request request = Request.getCurrent();
Response response = Response.getCurrent();
if (!request.getMethod().equals(org.restlet.data.Method.GET)) {
response.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
return;
}
ObjectSelection objectSelection = current();
// Check for interaction->method mappings
if (ResourceDelete.class.isAssignableFrom(getClass())) {
response.getAllowedMethods().add(org.restlet.data.Method.DELETE);
}
if (ResourceUpdate.class.isAssignableFrom(getClass())) {
response.getAllowedMethods().add(org.restlet.data.Method.PUT);
}
// Construct resource
ValueBuilder<Resource> builder = module.newValueBuilder(Resource.class);
List<Link> queriesProperty = builder.prototype().queries().get();
for (Method query : resourceQueries) {
if (constraints.isValid(query, objectSelection, module)) {
ValueBuilder<Link> linkBuilder = module.newValueBuilder(Link.class);
Link prototype = linkBuilder.prototype();
prototype.classes().set("query");
prototype.text().set(humanReadable(query.getName()));
prototype.href().set(query.getName().toLowerCase());
prototype.rel().set(query.getName().toLowerCase());
prototype.id().set(query.getName().toLowerCase());
queriesProperty.add(linkBuilder.newInstance());
}
}
List<Link> commandsProperty = builder.prototype().commands().get();
for (Method command : resourceCommands) {
if (constraints.isValid(command, objectSelection, module)) {
ValueBuilder<Link> linkBuilder = module.newValueBuilder(Link.class);
Link prototype = linkBuilder.prototype();
prototype.classes().set("command");
prototype.text().set(humanReadable(command.getName()));
prototype.href().set(command.getName().toLowerCase());
prototype.rel().set(command.getName().toLowerCase());
prototype.id().set(command.getName().toLowerCase());
commandsProperty.add(linkBuilder.newInstance());
}
}
List<Link> resourcesProperty = builder.prototype().resources().get();
for (Method subResource : subResources.values()) {
if (constraints.isValid(subResource, objectSelection, module)) {
ValueBuilder<Link> linkBuilder = module.newValueBuilder(Link.class);
Link prototype = linkBuilder.prototype();
prototype.classes().set("resource");
prototype.text().set(humanReadable(subResource.getName()));
prototype.href().set(subResource.getName().toLowerCase() + "/");
prototype.rel().set(subResource.getName().toLowerCase());
prototype.id().set(subResource.getName().toLowerCase());
resourcesProperty.add(linkBuilder.newInstance());
}
}
try {
Method indexMethod = resourceMethodQueries.get("index");
if (indexMethod != null) {
Object index = convert(indexMethod.invoke(this));
if (index != null && index instanceof ValueComposite) {
builder.prototype().index().set((ValueComposite) index);
}
}
} catch (Throwable e) {
// Ignore
}
try {
responseWriter.writeResponse(builder.newInstance(), response);
} catch (Throwable e) {
handleException(response, e);
}
}
use of org.qi4j.api.value.ValueComposite in project qi4j-sdk by Qi4j.
the class DefaultRequestReader method readRequest.
@Override
@SuppressWarnings("unchecked")
public Object[] readRequest(Request request, Method method) throws ResourceException {
if (request.getMethod().equals(org.restlet.data.Method.GET)) {
Object[] args = new Object[method.getParameterTypes().length];
Form queryAsForm = Request.getCurrent().getResourceRef().getQueryAsForm();
Form entityAsForm;
Representation representation = Request.getCurrent().getEntity();
if (representation != null && !EmptyRepresentation.class.isInstance(representation)) {
entityAsForm = new Form(representation);
} else {
entityAsForm = new Form();
}
if (queryAsForm.isEmpty() && entityAsForm.isEmpty()) {
// Nothing submitted yet - show form
return null;
}
if (args.length == 1) {
if (ValueComposite.class.isAssignableFrom(method.getParameterTypes()[0])) {
Class<?> valueType = method.getParameterTypes()[0];
args[0] = getValueFromForm((Class<ValueComposite>) valueType, queryAsForm, entityAsForm);
return args;
} else if (Form.class.equals(method.getParameterTypes()[0])) {
args[0] = queryAsForm.isEmpty() ? entityAsForm : queryAsForm;
return args;
} else if (Response.class.equals(method.getParameterTypes()[0])) {
args[0] = Response.getCurrent();
return args;
}
}
parseMethodArguments(method, args, queryAsForm, entityAsForm);
return args;
} else {
Object[] args = new Object[method.getParameterTypes().length];
Class<? extends ValueComposite> commandType = (Class<? extends ValueComposite>) method.getParameterTypes()[0];
if (method.getParameterTypes()[0].equals(Response.class)) {
return new Object[] { Response.getCurrent() };
}
Representation representation = Request.getCurrent().getEntity();
MediaType type = representation.getMediaType();
if (type == null) {
Form queryAsForm = Request.getCurrent().getResourceRef().getQueryAsForm(CharacterSet.UTF_8);
if (ValueComposite.class.isAssignableFrom(method.getParameterTypes()[0])) {
args[0] = getValueFromForm(commandType, queryAsForm, new Form());
} else {
parseMethodArguments(method, args, queryAsForm, new Form());
}
return args;
} else {
if (method.getParameterTypes()[0].equals(Representation.class)) {
// Command method takes Representation as input
return new Object[] { representation };
} else if (method.getParameterTypes()[0].equals(Form.class)) {
// Command method takes Form as input
return new Object[] { new Form(representation) };
} else if (ValueComposite.class.isAssignableFrom(method.getParameterTypes()[0])) {
// Need to parse input into ValueComposite
if (type.equals(MediaType.APPLICATION_JSON)) {
String json = Request.getCurrent().getEntityAsText();
if (json == null) {
LOGGER.error("Restlet bug http://restlet.tigris.org/issues/show_bug.cgi?id=843 detected. " + "Notify developers!");
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Bug in Restlet encountered; notify developers!");
}
Object command = module.newValueFromSerializedState(commandType, json);
args[0] = command;
return args;
} else if (type.equals(MediaType.TEXT_PLAIN)) {
String text = Request.getCurrent().getEntityAsText();
if (text == null) {
LOGGER.error("Restlet bug http://restlet.tigris.org/issues/show_bug.cgi?id=843 detected. " + "Notify developers!");
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Bug in Restlet encountered; notify developers!");
}
args[0] = text;
return args;
} else if (type.equals((MediaType.APPLICATION_WWW_FORM))) {
Form queryAsForm = Request.getCurrent().getResourceRef().getQueryAsForm();
Form entityAsForm;
if (representation != null && !EmptyRepresentation.class.isInstance(representation) && representation.isAvailable()) {
entityAsForm = new Form(representation);
} else {
entityAsForm = new Form();
}
Class<?> valueType = method.getParameterTypes()[0];
args[0] = getValueFromForm((Class<ValueComposite>) valueType, queryAsForm, entityAsForm);
return args;
} else {
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Command has to be in JSON format");
}
} else if (method.getParameterTypes()[0].isInterface() && method.getParameterTypes().length == 1) {
Form queryAsForm = Request.getCurrent().getResourceRef().getQueryAsForm();
Form entityAsForm;
if (representation != null && !EmptyRepresentation.class.isInstance(representation) && representation.isAvailable()) {
entityAsForm = new Form(representation);
} else {
entityAsForm = new Form();
}
args[0] = module.currentUnitOfWork().get(method.getParameterTypes()[0], getValue("entity", queryAsForm, entityAsForm));
return args;
} else {
Form queryAsForm = Request.getCurrent().getResourceRef().getQueryAsForm();
Form entityAsForm;
if (representation != null && !EmptyRepresentation.class.isInstance(representation) && representation.isAvailable()) {
entityAsForm = new Form(representation);
} else {
entityAsForm = new Form();
}
parseMethodArguments(method, args, queryAsForm, entityAsForm);
return args;
}
}
}
}
Aggregations