use of com.orientechnologies.orient.core.exception.OSerializationException in project orientdb by orientechnologies.
the class ORecordSerializerSchemaAware2CSV method toString.
@Override
protected StringBuilder toString(ORecord iRecord, final StringBuilder iOutput, final String iFormat, OUserObject2RecordHandler iObjHandler, final boolean iOnlyDelta, final boolean autoDetectCollectionType) {
if (iRecord == null)
throw new OSerializationException("Expected a record but was null");
if (!(iRecord instanceof ODocument))
throw new OSerializationException("Cannot marshall a record of type " + iRecord.getClass().getSimpleName());
final ODocument record = (ODocument) iRecord;
if (!iOnlyDelta && ODocumentInternal.getImmutableSchemaClass(record) != null) {
iOutput.append(ODocumentInternal.getImmutableSchemaClass(record).getStreamableName());
iOutput.append(OStringSerializerHelper.CLASS_SEPARATOR);
}
OProperty prop;
OType type;
OClass linkedClass;
OType linkedType;
String fieldClassName;
int i = 0;
final String[] fieldNames = iOnlyDelta && record.isTrackingChanges() ? record.getDirtyFields() : record.fieldNames();
// MARSHALL ALL THE FIELDS OR DELTA IF TRACKING IS ENABLED
for (String fieldName : fieldNames) {
Object fieldValue = record.rawField(fieldName);
if (i > 0)
iOutput.append(OStringSerializerHelper.RECORD_SEPARATOR);
// SEARCH FOR A CONFIGURED PROPERTY
prop = ODocumentInternal.getImmutableSchemaClass(record) != null ? ODocumentInternal.getImmutableSchemaClass(record).getProperty(fieldName) : null;
fieldClassName = getClassName(fieldValue);
type = record.fieldType(fieldName);
if (type == OType.ANY)
type = null;
linkedClass = null;
linkedType = null;
if (prop != null && prop.getType() != OType.ANY) {
// RECOGNIZED PROPERTY
type = prop.getType();
linkedClass = prop.getLinkedClass();
linkedType = prop.getLinkedType();
} else if (fieldValue != null) {
// NOT FOUND: TRY TO DETERMINE THE TYPE FROM ITS CONTENT
if (type == null) {
if (fieldValue.getClass() == byte[].class)
type = OType.BINARY;
else if (ODatabaseRecordThreadLocal.INSTANCE.isDefined() && fieldValue instanceof ORecord) {
if (type == null)
// DETERMINE THE FIELD TYPE
if (fieldValue instanceof ODocument && ((ODocument) fieldValue).hasOwners())
type = OType.EMBEDDED;
else
type = OType.LINK;
linkedClass = getLinkInfo(ODatabaseRecordThreadLocal.INSTANCE.get(), fieldClassName);
} else if (fieldValue instanceof ORID)
// DETERMINE THE FIELD TYPE
type = OType.LINK;
else if (ODatabaseRecordThreadLocal.INSTANCE.isDefined() && ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner() instanceof ODatabaseObject && ((ODatabaseObject) ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner()).getEntityManager().getEntityClass(fieldClassName) != null) {
// DETERMINE THE FIELD TYPE
type = OType.LINK;
linkedClass = getLinkInfo(ODatabaseRecordThreadLocal.INSTANCE.get(), fieldClassName);
} else if (fieldValue instanceof Date)
type = OType.DATETIME;
else if (fieldValue instanceof String)
type = OType.STRING;
else if (fieldValue instanceof Integer || fieldValue instanceof BigInteger)
type = OType.INTEGER;
else if (fieldValue instanceof Long)
type = OType.LONG;
else if (fieldValue instanceof Float)
type = OType.FLOAT;
else if (fieldValue instanceof Short)
type = OType.SHORT;
else if (fieldValue instanceof Byte)
type = OType.BYTE;
else if (fieldValue instanceof Double)
type = OType.DOUBLE;
else if (fieldValue instanceof BigDecimal)
type = OType.DECIMAL;
else if (fieldValue instanceof ORidBag)
type = OType.LINKBAG;
if (fieldValue instanceof OMultiCollectionIterator<?>) {
type = ((OMultiCollectionIterator<?>) fieldValue).isEmbedded() ? OType.EMBEDDEDLIST : OType.LINKLIST;
linkedType = ((OMultiCollectionIterator<?>) fieldValue).isEmbedded() ? OType.EMBEDDED : OType.LINK;
} else if (fieldValue instanceof Collection<?> || fieldValue.getClass().isArray()) {
final int size = OMultiValue.getSize(fieldValue);
Boolean autoConvertLinks = null;
if (fieldValue instanceof ORecordLazyMultiValue) {
autoConvertLinks = ((ORecordLazyMultiValue) fieldValue).isAutoConvertToRecord();
if (autoConvertLinks)
// DISABLE AUTO CONVERT
((ORecordLazyMultiValue) fieldValue).setAutoConvertToRecord(false);
}
if (autoDetectCollectionType)
if (size > 0) {
final Object firstValue = OMultiValue.getFirstValue(fieldValue);
if (firstValue != null) {
if (firstValue instanceof ORID) {
linkedClass = null;
linkedType = OType.LINK;
if (fieldValue instanceof Set<?>)
type = OType.LINKSET;
else
type = OType.LINKLIST;
} else if (ODatabaseRecordThreadLocal.INSTANCE.isDefined() && (firstValue instanceof ODocument && !((ODocument) firstValue).isEmbedded()) && (firstValue instanceof ORecord || (ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner() instanceof ODatabaseObject && ((ODatabaseObject) ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner()).getEntityManager().getEntityClass(getClassName(firstValue)) != null))) {
linkedClass = getLinkInfo(ODatabaseRecordThreadLocal.INSTANCE.get(), getClassName(firstValue));
if (type == null) {
// LINK: GET THE CLASS
linkedType = OType.LINK;
if (fieldValue instanceof Set<?>)
type = OType.LINKSET;
else
type = OType.LINKLIST;
} else
linkedType = OType.EMBEDDED;
} else {
// EMBEDDED COLLECTION
if (firstValue instanceof ODocument && ((((ODocument) firstValue).hasOwners()) || type == OType.EMBEDDEDSET || type == OType.EMBEDDEDLIST || type == OType.EMBEDDEDMAP))
linkedType = OType.EMBEDDED;
else if (firstValue instanceof Enum<?>)
linkedType = OType.STRING;
else {
linkedType = OType.getTypeByClass(firstValue.getClass());
if (linkedType != OType.LINK)
// EMBEDDED FOR SURE DON'T USE THE LINKED TYPE
linkedType = null;
}
if (type == null)
if (fieldValue instanceof ORecordLazySet)
type = OType.LINKSET;
else if (fieldValue instanceof Set<?>)
type = OType.EMBEDDEDSET;
else
type = OType.EMBEDDEDLIST;
}
}
} else if (type == null)
type = OType.EMBEDDEDLIST;
if (fieldValue instanceof ORecordLazyMultiValue && autoConvertLinks) {
// REPLACE PREVIOUS SETTINGS
((ORecordLazyMultiValue) fieldValue).setAutoConvertToRecord(true);
}
} else if (fieldValue instanceof Map<?, ?> && type == null) {
final int size = OMultiValue.getSize(fieldValue);
Boolean autoConvertLinks = null;
if (fieldValue instanceof ORecordLazyMap) {
autoConvertLinks = ((ORecordLazyMap) fieldValue).isAutoConvertToRecord();
if (autoConvertLinks)
// DISABLE AUTO CONVERT
((ORecordLazyMap) fieldValue).setAutoConvertToRecord(false);
}
if (size > 0) {
final Object firstValue = OMultiValue.getFirstValue(fieldValue);
if (firstValue != null) {
if (ODatabaseRecordThreadLocal.INSTANCE.isDefined() && (firstValue instanceof ODocument && !((ODocument) firstValue).isEmbedded()) && (firstValue instanceof ORecord || (ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner() instanceof ODatabaseObject && ((ODatabaseObject) ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner()).getEntityManager().getEntityClass(getClassName(firstValue)) != null))) {
linkedClass = getLinkInfo(ODatabaseRecordThreadLocal.INSTANCE.get(), getClassName(firstValue));
// LINK: GET THE CLASS
linkedType = OType.LINK;
type = OType.LINKMAP;
}
}
}
if (type == null)
type = OType.EMBEDDEDMAP;
if (fieldValue instanceof ORecordLazyMap && autoConvertLinks)
// REPLACE PREVIOUS SETTINGS
((ORecordLazyMap) fieldValue).setAutoConvertToRecord(true);
}
}
}
if (type == OType.TRANSIENT)
// TRANSIENT FIELD
continue;
if (type == null)
type = OType.EMBEDDED;
iOutput.append(fieldName);
iOutput.append(FIELD_VALUE_SEPARATOR);
fieldToStream(record, iOutput, iObjHandler, type, linkedClass, linkedType, fieldName, fieldValue, true);
i++;
}
// GET THE OVERSIZE IF ANY
final float overSize;
if (ODocumentInternal.getImmutableSchemaClass(record) != null)
// GET THE CONFIGURED OVERSIZE SETTED PER CLASS
overSize = ODocumentInternal.getImmutableSchemaClass(record).getOverSize();
else
overSize = 0;
// APPEND BLANKS IF NEEDED
final int newSize;
if (record.hasOwners())
// EMBEDDED: GET REAL SIZE
newSize = iOutput.length();
else if (record.getSize() == iOutput.length())
// IDENTICAL! DO NOTHING
newSize = record.getSize();
else if (record.getSize() > iOutput.length() && !OGlobalConfiguration.RECORD_DOWNSIZING_ENABLED.getValueAsBoolean()) {
// APPEND EXTRA SPACES TO FILL ALL THE AVAILABLE SPACE AND AVOID FRAGMENTATION
newSize = record.getSize();
} else if (overSize > 0) {
// APPEND EXTRA SPACES TO GET A LARGER iOutput
newSize = (int) (iOutput.length() * overSize);
} else
// NO OVERSIZE
newSize = iOutput.length();
if (newSize > iOutput.length()) {
iOutput.ensureCapacity(newSize);
for (int b = iOutput.length(); b < newSize; ++b) iOutput.append(' ');
}
return iOutput;
}
use of com.orientechnologies.orient.core.exception.OSerializationException in project orientdb by orientechnologies.
the class OStringSerializerAnyStreamable method toStream.
/**
* Serialize the class name size + class name + object content
*
* @param iValue
*/
public StringBuilder toStream(final StringBuilder iOutput, Object iValue) {
if (iValue != null) {
if (!(iValue instanceof OSerializableStream))
throw new OSerializationException("Cannot serialize the object since it's not implements the OSerializableStream interface");
OSerializableStream stream = (OSerializableStream) iValue;
iOutput.append(iValue.getClass().getName());
iOutput.append(OStreamSerializerHelper.SEPARATOR);
iOutput.append(OBase64Utils.encodeBytes(stream.toStream()));
}
return iOutput;
}
use of com.orientechnologies.orient.core.exception.OSerializationException in project orientdb by orientechnologies.
the class OStringSerializerAnyStreamable method fromStream.
/**
* Re-Create any object if the class has a public constructor that accepts a String as unique parameter.
*/
public Object fromStream(final String iStream) {
if (iStream == null || iStream.length() == 0)
// NULL VALUE
return null;
OSerializableStream instance = null;
int propertyPos = iStream.indexOf(':');
int pos = iStream.indexOf(OStreamSerializerHelper.SEPARATOR);
if (pos < 0 || propertyPos > -1 && pos > propertyPos) {
instance = new ODocument();
pos = -1;
} else {
final String className = iStream.substring(0, pos);
try {
final Class<?> clazz = Class.forName(className);
instance = (OSerializableStream) clazz.newInstance();
} catch (Exception e) {
final String message = "Error on unmarshalling content. Class: " + className;
OLogManager.instance().error(this, message, e);
throw OException.wrapException(new OSerializationException(message), e);
}
}
instance.fromStream(OBase64Utils.decode(iStream.substring(pos + 1)));
return instance;
}
use of com.orientechnologies.orient.core.exception.OSerializationException in project orientdb by orientechnologies.
the class OStringSerializerEmbedded method toStream.
/**
* Serialize the class name size + class name + object content
*
* @param iValue
*/
public StringBuilder toStream(final StringBuilder iOutput, Object iValue) {
if (iValue != null) {
if (iValue instanceof ODocumentSerializable)
iValue = ((ODocumentSerializable) iValue).toDocument();
if (!(iValue instanceof OSerializableStream))
throw new OSerializationException("Cannot serialize the object since it's not implements the OSerializableStream interface");
OSerializableStream stream = (OSerializableStream) iValue;
iOutput.append(iValue.getClass().getName());
iOutput.append(OStreamSerializerHelper.SEPARATOR);
try {
iOutput.append(new String(stream.toStream(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw OException.wrapException(new OSerializationException("Error serializing embedded object"), e);
}
}
return iOutput;
}
use of com.orientechnologies.orient.core.exception.OSerializationException in project orientdb by orientechnologies.
the class OStringSerializerEmbedded method fromStream.
/**
* Re-Create any object if the class has a public constructor that accepts a String as unique parameter.
*/
public Object fromStream(final String iStream) {
if (iStream == null || iStream.length() == 0)
// NULL VALUE
return null;
final ODocument instance = new ODocument();
try {
ORecordSerializerSchemaAware2CSV.INSTANCE.fromStream(iStream.getBytes("UTF-8"), instance, null);
} catch (UnsupportedEncodingException e) {
throw OException.wrapException(new OSerializationException("Error decoding string"), e);
}
final String className = instance.field(ODocumentSerializable.CLASS_NAME);
if (className == null)
return instance;
Class<?> clazz = null;
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
OLogManager.instance().debug(this, "Class name provided in embedded document " + className + " does not exist.");
}
if (clazz == null)
return instance;
if (ODocumentSerializable.class.isAssignableFrom(clazz)) {
try {
final ODocumentSerializable documentSerializable = (ODocumentSerializable) clazz.newInstance();
final ODocument docClone = new ODocument();
instance.copyTo(docClone);
docClone.removeField(ODocumentSerializable.CLASS_NAME);
documentSerializable.fromDocument(docClone);
return documentSerializable;
} catch (InstantiationException e) {
throw OException.wrapException(new OSerializationException("Cannot serialize the object"), e);
} catch (IllegalAccessException e) {
throw OException.wrapException(new OSerializationException("Cannot serialize the object"), e);
}
}
return instance;
}
Aggregations