use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.
the class OSecurityShared method createOrUpdateOUserClass.
private void createOrUpdateOUserClass(final ODatabaseDocument database, OClass identityClass, OClass roleClass) {
boolean unsafe = false;
OClass userClass = database.getMetadata().getSchema().getClass("OUser");
if (userClass == null) {
userClass = database.getMetadata().getSchema().createClass("OUser", identityClass);
unsafe = true;
} else if (!userClass.getSuperClasses().contains(identityClass))
// MIGRATE AUTOMATICALLY TO 1.2.0
userClass.setSuperClasses(Arrays.asList(identityClass));
if (!userClass.existsProperty("name")) {
((OClassImpl) userClass).createProperty("name", OType.STRING, (OType) null, unsafe).setMandatory(true).setNotNull(true).setCollate("ci").setMin("1").setRegexp("\\S+(.*\\S+)*");
userClass.createIndex("OUser.name", INDEX_TYPE.UNIQUE, ONullOutputListener.INSTANCE, "name");
} else {
final OProperty name = userClass.getProperty("name");
if (name.getAllIndexes().isEmpty())
userClass.createIndex("OUser.name", INDEX_TYPE.UNIQUE, ONullOutputListener.INSTANCE, "name");
}
if (!userClass.existsProperty(OUser.PASSWORD_FIELD))
userClass.createProperty(OUser.PASSWORD_FIELD, OType.STRING, (OType) null, unsafe).setMandatory(true).setNotNull(true);
if (!userClass.existsProperty("roles"))
userClass.createProperty("roles", OType.LINKSET, roleClass, unsafe);
if (!userClass.existsProperty("status"))
userClass.createProperty("status", OType.STRING, (OType) null, unsafe).setMandatory(true).setNotNull(true);
}
use of com.orientechnologies.orient.core.metadata.schema.OType 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;
}
use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.
the class ORecordSerializerJSON method fromString.
public ORecord fromString(String iSource, ORecord iRecord, final String[] iFields, final String iOptions, boolean needReload) {
iSource = unwrapSource(iSource);
boolean noMap = false;
if (iOptions != null) {
final String[] format = iOptions.split(",");
for (String f : format) if (f.equalsIgnoreCase("noMap"))
noMap = true;
}
if (iRecord != null)
// RESET ALL THE FIELDS
iRecord.clear();
final List<String> fields = OStringSerializerHelper.smartSplit(iSource, PARAMETER_SEPARATOR, 0, -1, true, true, false, false, ' ', '\n', '\r', '\t');
if (fields.size() % 2 != 0)
throw new OSerializationException("Error on unmarshalling JSON content: wrong format \"" + iSource + "\". Use <field> : <value>");
Map<String, Character> fieldTypes = null;
if (fields != null && fields.size() > 0) {
// SEARCH FOR FIELD TYPES IF ANY
for (int i = 0; i < fields.size(); i += 2) {
final String fieldName = OIOUtils.getStringContent(fields.get(i));
final String fieldValue = fields.get(i + 1);
final String fieldValueAsString = OIOUtils.getStringContent(fieldValue);
if (fieldName.equals(ATTRIBUTE_FIELD_TYPES) && iRecord instanceof ODocument) {
fieldTypes = loadFieldTypes(fieldTypes, fieldValueAsString);
} else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_TYPE)) {
if (iRecord == null || ORecordInternal.getRecordType(iRecord) != fieldValueAsString.charAt(0)) {
// CREATE THE RIGHT RECORD INSTANCE
iRecord = Orient.instance().getRecordFactoryManager().newInstance((byte) fieldValueAsString.charAt(0));
}
} else if (needReload && fieldName.equals(ODocumentHelper.ATTRIBUTE_RID) && iRecord instanceof ODocument) {
if (fieldValue != null && fieldValue.length() > 0) {
ORecord localRecord = ODatabaseRecordThreadLocal.INSTANCE.get().load(new ORecordId(fieldValueAsString));
if (localRecord != null)
iRecord = localRecord;
}
} else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_CLASS) && iRecord instanceof ODocument) {
ODocumentInternal.fillClassNameIfNeeded(((ODocument) iRecord), "null".equals(fieldValueAsString) ? null : fieldValueAsString);
}
}
if (iRecord == null)
iRecord = new ODocument();
try {
for (int i = 0; i < fields.size(); i += 2) {
final String fieldName = OIOUtils.getStringContent(fields.get(i));
final String fieldValue = fields.get(i + 1);
final String fieldValueAsString = OIOUtils.getStringContent(fieldValue);
// RECORD ATTRIBUTES
if (fieldName.equals(ODocumentHelper.ATTRIBUTE_RID))
ORecordInternal.setIdentity(iRecord, new ORecordId(fieldValueAsString));
else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_VERSION))
ORecordInternal.setVersion(iRecord, Integer.parseInt(fieldValue));
else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_TYPE)) {
continue;
} else if (fieldName.equals(ATTRIBUTE_FIELD_TYPES) && iRecord instanceof ODocument) {
continue;
} else if (fieldName.equals("value") && !(iRecord instanceof ODocument)) {
// RECORD VALUE(S)
if ("null".equals(fieldValue))
iRecord.fromStream(OCommonConst.EMPTY_BYTE_ARRAY);
else if (iRecord instanceof OBlob) {
// BYTES
iRecord.fromStream(OBase64Utils.decode(fieldValueAsString));
} else if (iRecord instanceof ORecordStringable) {
((ORecordStringable) iRecord).value(fieldValueAsString);
} else
throw new IllegalArgumentException("unsupported type of record");
} else if (iRecord instanceof ODocument) {
final ODocument doc = ((ODocument) iRecord);
// DETERMINE THE TYPE FROM THE SCHEMA
OType type = determineType(doc, fieldName);
final Object v = getValue(doc, fieldName, fieldValue, fieldValueAsString, type, null, fieldTypes, noMap, iOptions);
if (v != null)
if (v instanceof Collection<?> && !((Collection<?>) v).isEmpty()) {
if (v instanceof ORecordLazyMultiValue)
((ORecordLazyMultiValue) v).setAutoConvertToRecord(false);
// CHECK IF THE COLLECTION IS EMBEDDED
if (type == null) {
// TRY TO UNDERSTAND BY FIRST ITEM
Object first = ((Collection<?>) v).iterator().next();
if (first != null && first instanceof ORecord && !((ORecord) first).getIdentity().isValid())
type = v instanceof Set<?> ? OType.EMBEDDEDSET : OType.EMBEDDEDLIST;
}
if (type != null) {
// TREAT IT AS EMBEDDED
doc.field(fieldName, v, type);
continue;
}
} else if (v instanceof Map<?, ?> && !((Map<?, ?>) v).isEmpty()) {
// CHECK IF THE MAP IS EMBEDDED
Object first = ((Map<?, ?>) v).values().iterator().next();
if (first != null && first instanceof ORecord && !((ORecord) first).getIdentity().isValid()) {
doc.field(fieldName, v, OType.EMBEDDEDMAP);
continue;
}
} else if (v instanceof ODocument && type != null && type.isLink()) {
String className = ((ODocument) v).getClassName();
if (className != null && className.length() > 0)
((ODocument) v).save();
}
if (type == null && fieldTypes != null && fieldTypes.containsKey(fieldName))
type = ORecordSerializerStringAbstract.getType(fieldValue, fieldTypes.get(fieldName));
if (v instanceof OTrackedSet<?>) {
if (OMultiValue.getFirstValue((Set<?>) v) instanceof OIdentifiable)
type = OType.LINKSET;
} else if (v instanceof OTrackedList<?>) {
if (OMultiValue.getFirstValue((List<?>) v) instanceof OIdentifiable)
type = OType.LINKLIST;
}
if (type != null)
doc.field(fieldName, v, type);
else
doc.field(fieldName, v);
}
}
} catch (Exception e) {
if (iRecord.getIdentity().isValid())
throw OException.wrapException(new OSerializationException("Error on unmarshalling JSON content for record " + iRecord.getIdentity()), e);
else
throw OException.wrapException(new OSerializationException("Error on unmarshalling JSON content for record: " + iSource), e);
}
}
return iRecord;
}
use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.
the class ORecordSerializerJSON method determineType.
private OType determineType(ODocument doc, String fieldName) {
OType type = null;
final OClass cls = ODocumentInternal.getImmutableSchemaClass(doc);
if (cls != null) {
final OProperty prop = cls.getProperty(fieldName);
if (prop != null)
type = prop.getType();
}
return type;
}
use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.
the class ORecordSerializerSchemaAware2CSV method fromString.
@Override
public ORecord fromString(String iContent, final ORecord iRecord, final String[] iFields) {
iContent = iContent.trim();
if (iContent.length() == 0)
return iRecord;
// UNMARSHALL THE CLASS NAME
final ODocument record = (ODocument) iRecord;
int pos;
final ODatabaseDocumentInternal database = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
final int posFirstValue = iContent.indexOf(OStringSerializerHelper.ENTRY_SEPARATOR);
pos = iContent.indexOf(OStringSerializerHelper.CLASS_SEPARATOR);
if (pos > -1 && (pos < posFirstValue || posFirstValue == -1)) {
if ((record.getIdentity().getClusterId() < 0 || database == null || !database.getStorageVersions().classesAreDetectedByClusterId()))
ODocumentInternal.fillClassNameIfNeeded(((ODocument) iRecord), iContent.substring(0, pos));
iContent = iContent.substring(pos + 1);
} else
record.setClassNameIfExists(null);
if (iFields != null && iFields.length == 1 && iFields[0].equals("@class"))
// ONLY THE CLASS NAME HAS BEEN REQUESTED: RETURN NOW WITHOUT UNMARSHALL THE ENTIRE RECORD
return iRecord;
final List<String> fields = OStringSerializerHelper.smartSplit(iContent, OStringSerializerHelper.RECORD_SEPARATOR, true, true);
String fieldName = null;
String fieldValue;
OType type;
OClass linkedClass;
OType linkedType;
OProperty prop;
final Set<String> fieldSet;
if (iFields != null && iFields.length > 0) {
fieldSet = new HashSet<String>(iFields.length);
for (String f : iFields) fieldSet.add(f);
} else
fieldSet = null;
// UNMARSHALL ALL THE FIELDS
for (String fieldEntry : fields) {
fieldEntry = fieldEntry.trim();
boolean uncertainType = false;
try {
pos = fieldEntry.indexOf(FIELD_VALUE_SEPARATOR);
if (pos > -1) {
// GET THE FIELD NAME
fieldName = fieldEntry.substring(0, pos);
// CHECK IF THE FIELD IS REQUESTED TO BEING UNMARSHALLED
if (fieldSet != null && !fieldSet.contains(fieldName))
continue;
if (record.containsField(fieldName))
// ALREADY UNMARSHALLED: DON'T OVERWRITE IT
continue;
// GET THE FIELD VALUE
fieldValue = fieldEntry.length() > pos + 1 ? fieldEntry.substring(pos + 1) : null;
boolean setFieldType = false;
// SEARCH FOR A CONFIGURED PROPERTY
prop = ODocumentInternal.getImmutableSchemaClass(record) != null ? ODocumentInternal.getImmutableSchemaClass(record).getProperty(fieldName) : null;
if (prop != null && prop.getType() != OType.ANY) {
// RECOGNIZED PROPERTY
type = prop.getType();
linkedClass = prop.getLinkedClass();
linkedType = prop.getLinkedType();
} else {
// SCHEMA PROPERTY NOT FOUND FOR THIS FIELD: TRY TO AUTODETERMINE THE BEST TYPE
type = record.fieldType(fieldName);
if (type == OType.ANY)
type = null;
if (type != null)
setFieldType = true;
linkedClass = null;
linkedType = null;
// NOT FOUND: TRY TO DETERMINE THE TYPE FROM ITS CONTENT
if (fieldValue != null && type == null) {
if (fieldValue.length() > 1 && fieldValue.charAt(0) == '"' && fieldValue.charAt(fieldValue.length() - 1) == '"') {
type = OType.STRING;
} else if (fieldValue.startsWith(OStringSerializerHelper.LINKSET_PREFIX)) {
type = OType.LINKSET;
} else if (fieldValue.charAt(0) == OStringSerializerHelper.LIST_BEGIN && fieldValue.charAt(fieldValue.length() - 1) == OStringSerializerHelper.LIST_END || fieldValue.charAt(0) == OStringSerializerHelper.SET_BEGIN && fieldValue.charAt(fieldValue.length() - 1) == OStringSerializerHelper.SET_END) {
// EMBEDDED LIST/SET
type = fieldValue.charAt(0) == OStringSerializerHelper.LIST_BEGIN ? OType.EMBEDDEDLIST : OType.EMBEDDEDSET;
final String value = fieldValue.substring(1, fieldValue.length() - 1);
if (!value.isEmpty()) {
if (value.charAt(0) == OStringSerializerHelper.LINK) {
// TODO replace with regex
// ASSURE ALL THE ITEMS ARE RID
int max = value.length();
boolean allLinks = true;
boolean checkRid = true;
for (int i = 0; i < max; ++i) {
char c = value.charAt(i);
if (checkRid) {
if (c != '#') {
allLinks = false;
break;
}
checkRid = false;
} else if (c == ',')
checkRid = true;
}
if (allLinks) {
type = fieldValue.charAt(0) == OStringSerializerHelper.LIST_BEGIN ? OType.LINKLIST : OType.LINKSET;
linkedType = OType.LINK;
}
} else if (value.charAt(0) == OStringSerializerHelper.EMBEDDED_BEGIN) {
linkedType = OType.EMBEDDED;
} else if (value.charAt(0) == OStringSerializerHelper.CUSTOM_TYPE) {
linkedType = OType.CUSTOM;
} else if (Character.isDigit(value.charAt(0)) || value.charAt(0) == '+' || value.charAt(0) == '-') {
String[] items = value.split(",");
linkedType = getType(items[0]);
} else if (value.charAt(0) == '\'' || value.charAt(0) == '"')
linkedType = OType.STRING;
} else
uncertainType = true;
} else if (fieldValue.charAt(0) == OStringSerializerHelper.MAP_BEGIN && fieldValue.charAt(fieldValue.length() - 1) == OStringSerializerHelper.MAP_END) {
type = OType.EMBEDDEDMAP;
} else if (fieldValue.charAt(0) == OStringSerializerHelper.LINK)
type = OType.LINK;
else if (fieldValue.charAt(0) == OStringSerializerHelper.EMBEDDED_BEGIN) {
// TEMPORARY PATCH
if (fieldValue.startsWith("(ORIDs"))
type = OType.LINKSET;
else
type = OType.EMBEDDED;
} else if (fieldValue.charAt(0) == OStringSerializerHelper.BAG_BEGIN) {
type = OType.LINKBAG;
} else if (fieldValue.equals("true") || fieldValue.equals("false"))
type = OType.BOOLEAN;
else
type = getType(fieldValue);
}
}
final Object value = fieldFromStream(iRecord, type, linkedClass, linkedType, fieldName, fieldValue);
if ("@class".equals(fieldName)) {
ODocumentInternal.fillClassNameIfNeeded(((ODocument) iRecord), value.toString());
} else {
record.field(fieldName, value, type);
}
if (uncertainType)
record.setFieldType(fieldName, null);
}
} catch (Exception e) {
throw OException.wrapException(new OSerializationException("Error on unmarshalling field '" + fieldName + "' in record " + iRecord.getIdentity() + " with value: " + fieldEntry), e);
}
}
return iRecord;
}
Aggregations