if (LOG.isDebugEnabled()) {
LOG.debug("objectToElement(" + log("object", adapter) + ")");
}
final ObjectSpecification nos = adapter.getSpecification();
if (LOG.isDebugEnabled()) {
LOG.debug("objectToElement(NO): create element and isis:title");
}
final Element element = schema.createElement(getXmlDocument(), nos.getShortIdentifier(), nos.getFullIdentifier(), nos.getSingularName(), nos.getPluralName());
isisMetaModel.appendIsisTitle(element, adapter.titleString());
if (LOG.isDebugEnabled()) {
LOG.debug("objectToElement(NO): create XS element for Isis class");
}
final Element xsElement = schema.createXsElementForNofClass(getXsdDocument(), element, topLevelElementWritten, FacetUtil.getFacetsByType(nos));
// hack: every element in the XSD schema apart from first needs minimum
// cardinality setting.
topLevelElementWritten = true;
final Place place = new Place(adapter, element);
isisMetaModel.setAttributesForClass(element, oidAsString(adapter).toString());
final List<ObjectAssociation> fields = nos.getAssociations(Contributed.EXCLUDED);
if (LOG.isDebugEnabled()) {
LOG.debug("objectToElement(NO): processing fields");
}
eachField: for (int i = 0; i < fields.size(); i++) {
final ObjectAssociation field = fields.get(i);
final String fieldName = field.getId();
if (LOG.isDebugEnabled()) {
LOG.debug("objectToElement(NO): " + log("field", fieldName));
}
// Skip field if we have seen the name already
// This is a workaround for getLastActivity(). This method exists
// in AbstractObjectAdapter, but is not (at some level) being picked
// up
// by the dot-net reflector as a property. On the other hand it does
// exist as a field in the meta model (ObjectSpecification).
//
// Now, to re-expose the lastactivity field for .Net, a
// deriveLastActivity()
// has been added to BusinessObject. This caused another field of
// the
// same name, ultimately breaking the XSD.
for (int j = 0; j < i; j++) {
if (fieldName.equals(fields.get(i).getName())) {
LOG.debug("objectToElement(NO): " + log("field", fieldName) + " SKIPPED");
continue eachField;
}
}
Element xmlFieldElement = getXmlDocument().createElementNS(schema.getUri(), // scoped
// by
// namespace
// of class of
// containing object
schema.getPrefix() + ":" + fieldName);
Element xsdFieldElement = null;
if (field.getSpecification().containsFacet(ValueFacet.class)) {
if (LOG.isDebugEnabled()) {
LOG.debug("objectToElement(NO): " + log("field", fieldName) + " is value");
}
final ObjectSpecification fieldNos = field.getSpecification();
// skip fields of type XmlValue
if (fieldNos == null) {
continue eachField;
}
if (fieldNos.getFullIdentifier() != null && fieldNos.getFullIdentifier().endsWith("XmlValue")) {
continue eachField;
}
final OneToOneAssociation valueAssociation = ((OneToOneAssociation) field);
final Element xmlValueElement = xmlFieldElement; // more
// meaningful
// locally
// scoped name
ObjectAdapter value;
try {
value = valueAssociation.get(adapter);
final ObjectSpecification valueNos = value.getSpecification();
// XML
isisMetaModel.setAttributesForValue(xmlValueElement, valueNos.getShortIdentifier());
// return parsed string, else encoded string, else title.
String valueStr;
final ParseableFacet parseableFacet = fieldNos.getFacet(ParseableFacet.class);
final EncodableFacet encodeableFacet = fieldNos.getFacet(EncodableFacet.class);
if (parseableFacet != null) {
valueStr = parseableFacet.parseableTitle(value);
} else if (encodeableFacet != null) {
valueStr = encodeableFacet.toEncodedString(value);
} else {
valueStr = value.titleString();
}
final boolean notEmpty = (valueStr.length() > 0);
if (notEmpty) {
xmlValueElement.appendChild(getXmlDocument().createTextNode(valueStr));
} else {
isisMetaModel.setIsEmptyAttribute(xmlValueElement, true);
}
} catch (final Exception ex) {
LOG.warn("objectToElement(NO): " + log("field", fieldName) + ": getField() threw exception - skipping XML generation");
}
// XSD
xsdFieldElement = schema.createXsElementForNofValue(xsElement, xmlValueElement, FacetUtil.getFacetsByType(valueAssociation));
} else if (field instanceof OneToOneAssociation) {
if (LOG.isDebugEnabled()) {
LOG.debug("objectToElement(NO): " + log("field", fieldName) + " is OneToOneAssociation");
}
final OneToOneAssociation oneToOneAssociation = ((OneToOneAssociation) field);
final String fullyQualifiedClassName = nos.getFullIdentifier();
final Element xmlReferenceElement = xmlFieldElement; // more
// meaningful
// locally
// scoped
// name
ObjectAdapter referencedObjectAdapter;
try {
referencedObjectAdapter = oneToOneAssociation.get(adapter);
// XML
isisMetaModel.setAttributesForReference(xmlReferenceElement, schema.getPrefix(), fullyQualifiedClassName);
if (referencedObjectAdapter != null) {
isisMetaModel.appendIsisTitle(xmlReferenceElement, referencedObjectAdapter.titleString());
} else {
isisMetaModel.setIsEmptyAttribute(xmlReferenceElement, true);
}
} catch (final Exception ex) {
LOG.warn("objectToElement(NO): " + log("field", fieldName) + ": getAssociation() threw exception - skipping XML generation");
}
// XSD
xsdFieldElement = schema.createXsElementForNofReference(xsElement, xmlReferenceElement, oneToOneAssociation.getSpecification().getFullIdentifier(), FacetUtil.getFacetsByType(oneToOneAssociation));
} else if (field instanceof OneToManyAssociation) {
if (LOG.isDebugEnabled()) {
LOG.debug("objectToElement(NO): " + log("field", fieldName) + " is OneToManyAssociation");
}
final OneToManyAssociation oneToManyAssociation = (OneToManyAssociation) field;
final Element xmlCollectionElement = xmlFieldElement; // more
// meaningful
// locally
// scoped
// name
ObjectAdapter collection;
try {
collection = oneToManyAssociation.get(adapter);
final ObjectSpecification referencedTypeNos = oneToManyAssociation.getSpecification();
final String fullyQualifiedClassName = referencedTypeNos.getFullIdentifier();
// XML
isisMetaModel.setIsisCollection(xmlCollectionElement, schema.getPrefix(), fullyQualifiedClassName, collection);
} catch (final Exception ex) {
LOG.warn("objectToElement(NO): " + log("field", fieldName) + ": get(obj) threw exception - skipping XML generation");