use of com.orientechnologies.orient.core.db.OUserObject2RecordHandler in project orientdb by orientechnologies.
the class ORecordSerializerCSVAbstract method embeddedMapToStream.
public void embeddedMapToStream(ODatabase<?> iDatabase, final OUserObject2RecordHandler iObjHandler, final StringBuilder iOutput, final OClass iLinkedClass, OType iLinkedType, final Object iValue, final boolean iSaveOnlyDirty) {
iOutput.append(OStringSerializerHelper.MAP_BEGIN);
if (iValue != null) {
int items = 0;
// EMBEDDED OBJECTS
for (Entry<String, Object> o : ((Map<String, Object>) iValue).entrySet()) {
if (items > 0)
iOutput.append(OStringSerializerHelper.RECORD_SEPARATOR);
if (o != null) {
fieldTypeToString(iOutput, OType.STRING, o.getKey());
iOutput.append(OStringSerializerHelper.ENTRY_SEPARATOR);
if (o.getValue() instanceof ODocument && ((ODocument) o.getValue()).getIdentity().isValid()) {
fieldTypeToString(iOutput, OType.LINK, o.getValue());
} else if (o.getValue() instanceof ORecord || o.getValue() instanceof ODocumentSerializable) {
final ODocument record;
if (o.getValue() instanceof ODocument)
record = (ODocument) o.getValue();
else if (o.getValue() instanceof ODocumentSerializable) {
record = ((ODocumentSerializable) o.getValue()).toDocument();
record.field(ODocumentSerializable.CLASS_NAME, o.getValue().getClass().getName());
} else {
if (iDatabase == null && ODatabaseRecordThreadLocal.INSTANCE.isDefined())
iDatabase = ODatabaseRecordThreadLocal.INSTANCE.get();
record = OObjectSerializerHelperManager.getInstance().toStream(o.getValue(), new ODocument(o.getValue().getClass().getSimpleName()), iDatabase instanceof ODatabaseObject ? ((ODatabaseObject) iDatabase).getEntityManager() : OEntityManagerInternal.INSTANCE, iLinkedClass, iObjHandler != null ? iObjHandler : new OUserObject2RecordHandler() {
public Object getUserObjectByRecord(OIdentifiable iRecord, final String iFetchPlan) {
return iRecord;
}
public ORecord getRecordByUserObject(Object iPojo, boolean iCreateIfNotAvailable) {
return new ODocument(iLinkedClass);
}
public boolean existsUserObjectByRID(ORID iRID) {
return false;
}
public void registerUserObject(Object iObject, ORecord iRecord) {
}
public void registerUserObjectAfterLinkSave(ORecord iRecord) {
}
}, null, iSaveOnlyDirty);
}
iOutput.append(OStringSerializerHelper.EMBEDDED_BEGIN);
toString(record, iOutput, null, iObjHandler, false, true);
iOutput.append(OStringSerializerHelper.EMBEDDED_END);
} else if (o.getValue() instanceof Set<?>) {
// SUB SET
fieldTypeToString(iOutput, OType.EMBEDDEDSET, o.getValue());
} else if (o.getValue() instanceof Collection<?>) {
// SUB LIST
fieldTypeToString(iOutput, OType.EMBEDDEDLIST, o.getValue());
} else if (o.getValue() instanceof Map<?, ?>) {
// SUB MAP
fieldTypeToString(iOutput, OType.EMBEDDEDMAP, o.getValue());
} else {
// EMBEDDED LITERALS
if (iLinkedType == null && o.getValue() != null) {
fieldTypeToString(iOutput, OType.getTypeByClass(o.getValue().getClass()), o.getValue());
} else {
fieldTypeToString(iOutput, iLinkedType, o.getValue());
}
}
}
items++;
}
}
iOutput.append(OStringSerializerHelper.MAP_END);
}
use of com.orientechnologies.orient.core.db.OUserObject2RecordHandler in project orientdb by orientechnologies.
the class ORecordSerializerCSVAbstract method embeddedCollectionToStream.
public StringBuilder embeddedCollectionToStream(ODatabase<?> iDatabase, final OUserObject2RecordHandler iObjHandler, final StringBuilder iOutput, final OClass iLinkedClass, final OType iLinkedType, final Object iValue, final boolean iSaveOnlyDirty, final boolean iSet) {
iOutput.append(iSet ? OStringSerializerHelper.SET_BEGIN : OStringSerializerHelper.LIST_BEGIN);
final Iterator<Object> iterator = OMultiValue.getMultiValueIterator(iValue, false);
OType linkedType = iLinkedType;
for (int i = 0; iterator.hasNext(); ++i) {
final Object o = iterator.next();
if (i > 0)
iOutput.append(OStringSerializerHelper.RECORD_SEPARATOR);
if (o == null) {
iOutput.append("null");
continue;
}
OIdentifiable id = null;
ODocument doc = null;
final OClass linkedClass;
if (!(o instanceof OIdentifiable)) {
final String fieldBound = OObjectSerializerHelperManager.getInstance().getDocumentBoundField(o.getClass());
if (fieldBound != null) {
OObjectSerializerHelperManager.getInstance().invokeCallback(o, null, OBeforeSerialization.class);
doc = (ODocument) OObjectSerializerHelperManager.getInstance().getFieldValue(o, fieldBound);
OObjectSerializerHelperManager.getInstance().invokeCallback(o, doc, OAfterSerialization.class);
id = doc;
} else if (iLinkedType == null)
linkedType = OType.getTypeByClass(o.getClass());
linkedClass = iLinkedClass;
} else {
id = (OIdentifiable) o;
if (iLinkedType == null)
// AUTO-DETERMINE LINKED TYPE
if (id.getIdentity().isValid())
linkedType = OType.LINK;
else
linkedType = OType.EMBEDDED;
if (id instanceof ODocument) {
doc = (ODocument) id;
if (doc.hasOwners())
linkedType = OType.EMBEDDED;
assert linkedType == OType.EMBEDDED || id.getIdentity().isValid() || (ODatabaseRecordThreadLocal.INSTANCE.get().getStorage() instanceof OStorageProxy) : "Impossible to serialize invalid link " + id.getIdentity();
linkedClass = ODocumentInternal.getImmutableSchemaClass(doc);
} else
linkedClass = null;
}
if (id != null && linkedType != OType.LINK)
iOutput.append(OStringSerializerHelper.EMBEDDED_BEGIN);
if (linkedType == OType.EMBEDDED && o instanceof OIdentifiable)
toString((ORecord) ((OIdentifiable) o).getRecord(), iOutput, null);
else if (linkedType != OType.LINK && (linkedClass != null || doc != null)) {
if (id == null) {
// EMBEDDED OBJECTS
if (iDatabase == null && ODatabaseRecordThreadLocal.INSTANCE.isDefined())
iDatabase = ODatabaseRecordThreadLocal.INSTANCE.get();
id = OObjectSerializerHelperManager.getInstance().toStream(o, new ODocument(o.getClass().getSimpleName()), iDatabase instanceof ODatabaseObject ? ((ODatabaseObject) iDatabase).getEntityManager() : OEntityManagerInternal.INSTANCE, iLinkedClass, iObjHandler != null ? iObjHandler : new OUserObject2RecordHandler() {
public Object getUserObjectByRecord(OIdentifiable iRecord, final String iFetchPlan) {
return iRecord;
}
public ORecord getRecordByUserObject(Object iPojo, boolean iCreateIfNotAvailable) {
return new ODocument(linkedClass);
}
public boolean existsUserObjectByRID(ORID iRID) {
return false;
}
public void registerUserObject(Object iObject, ORecord iRecord) {
}
public void registerUserObjectAfterLinkSave(ORecord iRecord) {
}
}, null, iSaveOnlyDirty);
}
toString(doc, iOutput, null, iObjHandler, false, true);
} else {
// EMBEDDED LITERALS
if (iLinkedType == null) {
if (o != null)
linkedType = OType.getTypeByClass(o.getClass());
} else if (iLinkedType == OType.CUSTOM)
iOutput.append(OStringSerializerHelper.CUSTOM_TYPE);
fieldTypeToString(iOutput, linkedType, o);
}
if (id != null && linkedType != OType.LINK)
iOutput.append(OStringSerializerHelper.EMBEDDED_END);
}
iOutput.append(iSet ? OStringSerializerHelper.SET_END : OStringSerializerHelper.LIST_END);
return iOutput;
}
Aggregations