Element root = doc.createElement(ELEMENT_INSTANCE);
parent.appendChild(root);
root.setAttribute(ATTR_ID, ior(sm));
Element child = null;
BitSet loaded = sm.getLoaded();
StoreContext ctx = (StoreContext)sm.getGenericContext();
List<Attribute<?, ?>> attrs = MetamodelHelper.getAttributesInOrder(sm.getMetaData(), model);
for (int i = 0; i < attrs.size(); child = null, i++) {
Members.Member<?, ?> attr = (Members.Member<?, ?>) attrs.get(i);
FieldMetaData fmd = attr.fmd;
if (!loaded.get(fmd.getIndex()))
continue;
String tag = MetamodelHelper.getTagByAttributeType(attr);
Object value = sm.fetch(fmd.getIndex());
switch (fmd.getDeclaredTypeCode()) {
case JavaTypes.BOOLEAN:
case JavaTypes.BYTE:
case JavaTypes.CHAR:
case JavaTypes.DOUBLE:
case JavaTypes.FLOAT:
case JavaTypes.INT:
case JavaTypes.LONG:
case JavaTypes.SHORT:
case JavaTypes.BOOLEAN_OBJ:
case JavaTypes.BYTE_OBJ:
case JavaTypes.CHAR_OBJ:
case JavaTypes.DOUBLE_OBJ:
case JavaTypes.FLOAT_OBJ:
case JavaTypes.INT_OBJ:
case JavaTypes.LONG_OBJ:
case JavaTypes.SHORT_OBJ:
case JavaTypes.BIGDECIMAL:
case JavaTypes.BIGINTEGER:
case JavaTypes.DATE:
case JavaTypes.NUMBER:
case JavaTypes.CALENDAR:
case JavaTypes.LOCALE:
case JavaTypes.STRING:
case JavaTypes.ENUM:
child = doc.createElement(tag);
child.setAttribute(ATTR_NAME, fmd.getName());
if (value == null) {
encodeNull(child);
} else {
encodeBasic(child, value, fmd.getDeclaredType());
}
break;
case JavaTypes.OID:
child = doc.createElement(ELEMENT_REF);
child.setAttribute(ATTR_NAME, fmd.getName());
if (value == null) {
encodeNull(child);
} else {
encodeBasic(child, value, fmd.getDeclaredType());
}
break;
case JavaTypes.PC:
child = doc.createElement(tag);
child.setAttribute(ATTR_NAME, fmd.getName());
child.setAttribute(ATTR_TYPE, typeOf(fmd));
OpenJPAStateManager other = ctx.getStateManager(value);
encodeManagedInstance(other, child, true, model);
break;
case JavaTypes.ARRAY:
Object[] values = (Object[])value;
value = Arrays.asList(values);
// no break;
case JavaTypes.COLLECTION:
child = doc.createElement(tag);
child.setAttribute(ATTR_NAME, fmd.getName());
child.setAttribute(ATTR_TYPE, typeOf(fmd));
child.setAttribute(ATTR_MEMBER_TYPE, typeOf(fmd.getElement().getDeclaredType()));
if (value == null) {
encodeNull(child);
break;
}
Collection<?> members = (Collection<?>)value;
boolean basic = fmd.getElement().getTypeMetaData() == null;
for (Object o : members) {
Element member = doc.createElement(ELEMENT_MEMBER);
child.appendChild(member);
if (o == null) {
encodeNull(member);
} else {
if (basic) {
encodeBasic(member, o, o.getClass());
} else {
encodeManagedInstance(ctx.getStateManager(o), member, true, model);
}
}
}
break;
case JavaTypes.MAP:
child = doc.createElement(tag);
child.setAttribute(ATTR_NAME, fmd.getName());
child.setAttribute(ATTR_TYPE, typeOf(fmd));
child.setAttribute(ATTR_KEY_TYPE, typeOf(fmd.getElement().getDeclaredType()));
child.setAttribute(ATTR_VALUE_TYPE, typeOf(fmd.getValue().getDeclaredType()));
if (value == null) {
encodeNull(child);
break;
}
Set<Map.Entry<?,?>> entries = ((Map)value).entrySet();
boolean basicKey = fmd.getElement().getTypeMetaData() == null;
boolean basicValue = fmd.getValue().getTypeMetaData() == null;
for (Map.Entry<?,?> e : entries) {
Element entry = doc.createElement(ELEMENT_ENTRY);
Element entryKey = doc.createElement(ELEMENT_ENTRY_KEY);
Element entryValue = doc.createElement(ELEMENT_ENTRY_VALUE);
entry.appendChild(entryKey);
entry.appendChild(entryValue);
child.appendChild(entry);
if (e.getKey() == null) {
encodeNull(entryKey);
} else {
if (basicKey) {
encodeBasic(entryKey, e.getKey(), e.getKey().getClass());
} else {
encodeManagedInstance(ctx.getStateManager(e.getKey()), entryKey, true, model);
}
}
if (e.getValue() == null) {
encodeNull(entryValue);
} else {
if (basicValue) {
encodeBasic(entryValue, e.getValue(), e.getValue().getClass());
} else {
encodeManagedInstance(ctx.getStateManager(e.getValue()), entryValue, true, model);
}
}
}
break;