* @param entity
* The entity to wrap.
* @return The Atom entry object that corresponds to the given entity.
*/
public Entry toEntry(final Object entity) {
Entry result = null;
if (entity != null) {
Metadata metadata = (Metadata) getMetadata();
EntityType type = metadata.getEntityType(entity.getClass());
if (type != null) {
final SaxRepresentation r = new SaxRepresentation(
MediaType.APPLICATION_XML) {
@Override
public void write(XmlWriter writer) throws IOException {
try {
// Attribute for nullable values.
AttributesImpl nullAttrs = new AttributesImpl();
nullAttrs.addAttribute(
WCF_DATASERVICES_METADATA_NAMESPACE,
"null", null, "boolean", "true");
writer.forceNSDecl(
WCF_DATASERVICES_METADATA_NAMESPACE, "m");
writer.forceNSDecl(WCF_DATASERVICES_NAMESPACE, "d");
writer.startElement(
WCF_DATASERVICES_METADATA_NAMESPACE,
"properties");
write(writer, entity, nullAttrs);
writer.endElement(
WCF_DATASERVICES_METADATA_NAMESPACE,
"properties");
} catch (SAXException e) {
throw new IOException(e.getMessage());
}
}
private void write(XmlWriter writer, Object entity,
AttributesImpl nullAttrs) throws SAXException {
for (Field field : entity.getClass()
.getDeclaredFields()) {
String getter = "get"
+ field.getName().substring(0, 1)
.toUpperCase()
+ field.getName().substring(1);
Property prop = ((Metadata) getMetadata())
.getProperty(entity, field.getName());
if (prop != null) {
writeProperty(writer, entity, prop, getter,
nullAttrs);
}
}
}
private void writeProperty(XmlWriter writer, Object entity,
Property prop, String getter,
AttributesImpl nullAttrs) throws SAXException {
for (Method method : entity.getClass()
.getDeclaredMethods()) {
if (method.getReturnType() != null
&& getter.equals(method.getName())
&& method.getParameterTypes().length == 0) {
Object value = null;
try {
value = method.invoke(entity,
(Object[]) null);
} catch (Exception e) {
}
if (value != null) {
writer.startElement(
WCF_DATASERVICES_NAMESPACE,
prop.getName());
if (prop instanceof ComplexProperty) {
write(writer, value, nullAttrs);
} else {
writer.characters(TypeUtils.toEdm(
value, prop.getType()));
}
writer.endElement(
WCF_DATASERVICES_NAMESPACE,
prop.getName());
} else {
if (prop.isNullable()) {
writer.emptyElement(
WCF_DATASERVICES_NAMESPACE,
prop.getName(), prop.getName(),
nullAttrs);
} else {
getLogger().warning(
"The following property has a null value but is not marked as nullable: "
+ prop.getName());
writer.emptyElement(
WCF_DATASERVICES_NAMESPACE,
prop.getName());
}
}
break;
}
}
}
};
if (type.isBlob()) {
result = new Entry() {
@Override
public void writeInlineContent(XmlWriter writer)
throws SAXException {
try {
r.write(writer);
} catch (IOException e) {
throw new SAXException(e);
}
}
};
Link editLink = new Link(getValueEditRef(entity),
Relation.EDIT_MEDIA, null);
result.getLinks().add(editLink);
Content content = new Content();
// Get the external blob reference
content.setExternalRef(getValueRef(entity));
content.setToEncode(false);
result.setContent(content);
} else {
result = new Entry();
Content content = new Content();
content.setInlineContent(r);
content.setToEncode(false);
result.setContent(content);
}
}
}
return result;