Search in sources :

Example 1 with OUserObject2RecordHandler

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);
}
Also used : OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) Set(java.util.Set) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ORecord(com.orientechnologies.orient.core.record.ORecord) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ODocumentSerializable(com.orientechnologies.orient.core.serialization.ODocumentSerializable) OUserObject2RecordHandler(com.orientechnologies.orient.core.db.OUserObject2RecordHandler) ORID(com.orientechnologies.orient.core.id.ORID) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Map(java.util.Map) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 2 with OUserObject2RecordHandler

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;
}
Also used : OStorageProxy(com.orientechnologies.orient.core.storage.OStorageProxy) OType(com.orientechnologies.orient.core.metadata.schema.OType) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ORecord(com.orientechnologies.orient.core.record.ORecord) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) OUserObject2RecordHandler(com.orientechnologies.orient.core.db.OUserObject2RecordHandler) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OUserObject2RecordHandler (com.orientechnologies.orient.core.db.OUserObject2RecordHandler)2 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)2 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)2 ORID (com.orientechnologies.orient.core.id.ORID)2 ORecord (com.orientechnologies.orient.core.record.ORecord)2 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)2 ORecordLazyMap (com.orientechnologies.orient.core.db.record.ORecordLazyMap)1 ORecordLazySet (com.orientechnologies.orient.core.db.record.ORecordLazySet)1 OTrackedMap (com.orientechnologies.orient.core.db.record.OTrackedMap)1 OTrackedSet (com.orientechnologies.orient.core.db.record.OTrackedSet)1 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)1 OType (com.orientechnologies.orient.core.metadata.schema.OType)1 ODocumentSerializable (com.orientechnologies.orient.core.serialization.ODocumentSerializable)1 OStorageProxy (com.orientechnologies.orient.core.storage.OStorageProxy)1 Map (java.util.Map)1 Set (java.util.Set)1