Search in sources :

Example 46 with OProperty

use of com.orientechnologies.orient.core.metadata.schema.OProperty in project orientdb by orientechnologies.

the class OIndexDefinitionFactory method checkTypes.

private static void checkTypes(OClass oClass, List<String> fieldNames, List<OType> types) {
    if (fieldNames.size() != types.size())
        throw new IllegalArgumentException("Count of field names doesn't match count of field types. It was " + fieldNames.size() + " fields, but " + types.size() + " types.");
    for (int i = 0, fieldNamesSize = fieldNames.size(); i < fieldNamesSize; i++) {
        String fieldName = fieldNames.get(i);
        OType type = types.get(i);
        final OProperty property = oClass.getProperty(fieldName);
        if (property != null && !type.equals(property.getType())) {
            throw new IllegalArgumentException("Property type list not match with real property types");
        }
    }
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OType(com.orientechnologies.orient.core.metadata.schema.OType)

Example 47 with OProperty

use of com.orientechnologies.orient.core.metadata.schema.OProperty in project orientdb by orientechnologies.

the class ODatabaseCompare method compareSchema.

private void compareSchema() {
    OSchema schema1 = databaseOne.getMetadata().getImmutableSchemaSnapshot();
    OSchema schema2 = databaseTwo.getMetadata().getImmutableSchemaSnapshot();
    boolean ok = true;
    for (OClass clazz : schema1.getClasses()) {
        OClass clazz2 = schema2.getClass(clazz.getName());
        if (clazz2 == null) {
            listener.onMessage("\n- ERR: Class definition " + clazz.getName() + " for DB2 is null.");
            continue;
        }
        final List<String> sc1 = clazz.getSuperClassesNames();
        final List<String> sc2 = clazz2.getSuperClassesNames();
        if (!sc1.isEmpty() || !sc2.isEmpty()) {
            if (!sc1.containsAll(sc2) || !sc2.containsAll(sc1)) {
                listener.onMessage("\n- ERR: Class definition for " + clazz.getName() + " in DB1 is not equals in superclasses in DB2.");
                ok = false;
            }
        }
        if (!clazz.getClassIndexes().equals(clazz2.getClassIndexes())) {
            listener.onMessage("\n- ERR: Class definition for " + clazz.getName() + " in DB1 is not equals in indexes in DB2.");
            ok = false;
        }
        if (!Arrays.equals(clazz.getClusterIds(), clazz2.getClusterIds())) {
            listener.onMessage("\n- ERR: Class definition for " + clazz.getName() + " in DB1 is not equals in clusters in DB2.");
            ok = false;
        }
        if (!clazz.getCustomKeys().equals(clazz2.getCustomKeys())) {
            listener.onMessage("\n- ERR: Class definition for " + clazz.getName() + " in DB1 is not equals in custom keys in DB2.");
            ok = false;
        }
        if (clazz.getOverSize() != clazz2.getOverSize()) {
            listener.onMessage("\n- ERR: Class definition for " + clazz.getName() + " in DB1 is not equals in overSize in DB2.");
            ok = false;
        }
        if (clazz.getDefaultClusterId() != clazz2.getDefaultClusterId()) {
            listener.onMessage("\n- ERR: Class definition for " + clazz.getName() + " in DB1 is not equals in default cluser id in DB2.");
            ok = false;
        }
        for (OProperty prop : clazz.declaredProperties()) {
            OProperty prop2 = clazz2.getProperty(prop.getName());
            if (prop2 == null) {
                listener.onMessage("\n- ERR: Class definition for " + clazz.getName() + " as missed property " + prop.getName() + "in DB2.");
                ok = false;
                continue;
            }
            if (prop.getType() != prop2.getType()) {
                listener.onMessage("\n- ERR: Class definition for " + clazz.getName() + " as not same type for property " + prop.getName() + "in DB2. ");
                ok = false;
            }
            if (prop.getLinkedType() != prop2.getLinkedType()) {
                listener.onMessage("\n- ERR: Class definition for " + clazz.getName() + " as not same linkedtype for property " + prop.getName() + "in DB2.");
                ok = false;
            }
            if (prop.getMin() != null) {
                if (!prop.getMin().equals(prop2.getMin())) {
                    listener.onMessage("\n- ERR: Class definition for " + clazz.getName() + " as not same min for property " + prop.getName() + "in DB2.");
                    ok = false;
                }
            }
            if (prop.getMax() != null) {
                if (!prop.getMax().equals(prop2.getMax())) {
                    listener.onMessage("\n- ERR: Class definition for " + clazz.getName() + " as not same max for property " + prop.getName() + "in DB2.");
                    ok = false;
                }
            }
            if (prop.getMax() != null) {
                if (!prop.getMax().equals(prop2.getMax())) {
                    listener.onMessage("\n- ERR: Class definition for " + clazz.getName() + " as not same regexp for property " + prop.getName() + "in DB2.");
                    ok = false;
                }
            }
            if (prop.getLinkedClass() != null) {
                if (!prop.getLinkedClass().equals(prop2.getLinkedClass())) {
                    listener.onMessage("\n- ERR: Class definition for " + clazz.getName() + " as not same linked class for property " + prop.getName() + "in DB2.");
                    ok = false;
                }
            }
            if (prop.getLinkedClass() != null) {
                if (!prop.getCustomKeys().equals(prop2.getCustomKeys())) {
                    listener.onMessage("\n- ERR: Class definition for " + clazz.getName() + " as not same custom keys for property " + prop.getName() + "in DB2.");
                    ok = false;
                }
            }
            if (prop.isMandatory() != prop2.isMandatory()) {
                listener.onMessage("\n- ERR: Class definition for " + clazz.getName() + " as not same mandatory flag for property " + prop.getName() + "in DB2.");
                ok = false;
            }
            if (prop.isNotNull() != prop2.isNotNull()) {
                listener.onMessage("\n- ERR: Class definition for " + clazz.getName() + " as not same nut null flag for property " + prop.getName() + "in DB2.");
                ok = false;
            }
            if (prop.isReadonly() != prop2.isReadonly()) {
                listener.onMessage("\n- ERR: Class definition for " + clazz.getName() + " as not same readonly flag setting for property " + prop.getName() + "in DB2.");
                ok = false;
            }
        }
        if (!ok) {
            ++differences;
            ok = true;
        }
    }
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OClass(com.orientechnologies.orient.core.metadata.schema.OClass)

Example 48 with OProperty

use of com.orientechnologies.orient.core.metadata.schema.OProperty 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);
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OType(com.orientechnologies.orient.core.metadata.schema.OType)

Example 49 with OProperty

use of com.orientechnologies.orient.core.metadata.schema.OProperty 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;
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OType(com.orientechnologies.orient.core.metadata.schema.OType) OClass(com.orientechnologies.orient.core.metadata.schema.OClass)

Example 50 with OProperty

use of com.orientechnologies.orient.core.metadata.schema.OProperty in project orientdb by orientechnologies.

the class ORecordSerializerJSON method getValue.

@SuppressWarnings("unchecked")
private Object getValue(final ODocument iRecord, String iFieldName, String iFieldValue, String iFieldValueAsString, OType iType, OType iLinkedType, final Map<String, Character> iFieldTypes, final boolean iNoMap, final String iOptions) {
    if (iFieldValue.equals("null"))
        return null;
    if (iFieldName != null && ODocumentInternal.getImmutableSchemaClass(iRecord) != null) {
        final OProperty p = ODocumentInternal.getImmutableSchemaClass(iRecord).getProperty(iFieldName);
        if (p != null) {
            iType = p.getType();
            iLinkedType = p.getLinkedType();
        }
    }
    if (iType == null && iFieldTypes != null && iFieldTypes.containsKey(iFieldName))
        iType = ORecordSerializerStringAbstract.getType(iFieldValue, iFieldTypes.get(iFieldName));
    if (iFieldValue.startsWith("{") && iFieldValue.endsWith("}")) {
        return getValueAsObjectOrMap(iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
    } else if (iFieldValue.startsWith("[") && iFieldValue.endsWith("]")) {
        return getValueAsCollection(iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
    }
    if (iType == null || iType == OType.ANY)
        // TRY TO DETERMINE THE CONTAINED TYPE from THE FIRST VALUE
        if (iFieldValue.charAt(0) != '\"' && iFieldValue.charAt(0) != '\'') {
            if (iFieldValue.equalsIgnoreCase("false") || iFieldValue.equalsIgnoreCase("true"))
                iType = OType.BOOLEAN;
            else {
                Character c = null;
                if (iFieldTypes != null) {
                    c = iFieldTypes.get(iFieldName);
                    if (c != null)
                        iType = ORecordSerializerStringAbstract.getType(iFieldValue + c);
                }
                if (c == null && !iFieldValue.isEmpty()) {
                    // TRY TO AUTODETERMINE THE BEST TYPE
                    if (ORecordId.isA(iFieldValue))
                        iType = OType.LINK;
                    else if (iFieldValue.matches(".*[\\.Ee].*")) {
                        // DECIMAL FORMAT: DETERMINE IF DOUBLE OR FLOAT
                        final Double v = new Double(OIOUtils.getStringContent(iFieldValue));
                        return v;
                    // REMOVED TRUNK to float
                    // if (canBeTrunkedToFloat(v))
                    // return v.floatValue();
                    // else
                    // return v;
                    } else {
                        final Long v = new Long(OIOUtils.getStringContent(iFieldValue));
                        if (canBeTrunkedToInt(v))
                            return v.intValue();
                        else
                            return v;
                    }
                }
            }
        } else if (iFieldValue.startsWith("{") && iFieldValue.endsWith("}"))
            iType = OType.EMBEDDED;
        else {
            if (ORecordId.isA(iFieldValueAsString))
                iType = OType.LINK;
            if (iFieldTypes != null) {
                Character c = iFieldTypes.get(iFieldName);
                if (c != null)
                    iType = ORecordSerializerStringAbstract.getType(iFieldValueAsString, c);
            }
            if (iType == null)
                iType = OType.STRING;
        }
    if (iType != null)
        switch(iType) {
            case STRING:
                return decodeJSON(iFieldValueAsString);
            case LINK:
                final int pos = iFieldValueAsString.indexOf('@');
                if (pos > -1)
                    // CREATE DOCUMENT
                    return new ODocument(iFieldValueAsString.substring(1, pos), new ORecordId(iFieldValueAsString.substring(pos + 1)));
                else {
                    // CREATE SIMPLE RID
                    return new ORecordId(iFieldValueAsString);
                }
            case EMBEDDED:
                return fromString(iFieldValueAsString);
            case DATE:
                if (iFieldValueAsString == null || iFieldValueAsString.equals(""))
                    return null;
                try {
                    // TRY TO PARSE AS LONG
                    return Long.parseLong(iFieldValueAsString);
                } catch (NumberFormatException e) {
                    try {
                        // TRY TO PARSE AS DATE
                        return ODateHelper.getDateFormatInstance().parseObject(iFieldValueAsString);
                    } catch (ParseException ex) {
                        throw OException.wrapException(new OSerializationException("Unable to unmarshall date (format=" + ODateHelper.getDateFormat() + ") : " + iFieldValueAsString), e);
                    }
                }
            case DATETIME:
                if (iFieldValueAsString == null || iFieldValueAsString.equals(""))
                    return null;
                try {
                    // TRY TO PARSE AS LONG
                    return Long.parseLong(iFieldValueAsString);
                } catch (NumberFormatException e) {
                    try {
                        // TRY TO PARSE AS DATETIME
                        return ODateHelper.getDateTimeFormatInstance().parseObject(iFieldValueAsString);
                    } catch (ParseException ex) {
                        throw OException.wrapException(new OSerializationException("Unable to unmarshall datetime (format=" + ODateHelper.getDateTimeFormat() + ") : " + iFieldValueAsString), e);
                    }
                }
            case BINARY:
                return OStringSerializerHelper.fieldTypeFromStream(iRecord, iType, iFieldValueAsString);
            case CUSTOM:
                {
                    try {
                        ByteArrayInputStream bais = new ByteArrayInputStream(OBase64Utils.decode(iFieldValueAsString));
                        ObjectInputStream input = new ObjectInputStream(bais);
                        return input.readObject();
                    } catch (IOException e) {
                        throw OException.wrapException(new OSerializationException("Error on custom field deserialization"), e);
                    } catch (ClassNotFoundException e) {
                        throw OException.wrapException(new OSerializationException("Error on custom field deserialization"), e);
                    }
                }
            default:
                return OStringSerializerHelper.fieldTypeFromStream(iRecord, iType, iFieldValue);
        }
    return iFieldValueAsString;
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) IOException(java.io.IOException) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseException(java.text.ParseException) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)87 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)69 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)32 Test (org.testng.annotations.Test)30 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)25 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)23 OType (com.orientechnologies.orient.core.metadata.schema.OType)14 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)11 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)9 OIndex (com.orientechnologies.orient.core.index.OIndex)7 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)6 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)6 Map (java.util.Map)6 Set (java.util.Set)6 OIndexDefinition (com.orientechnologies.orient.core.index.OIndexDefinition)5 Collection (java.util.Collection)5 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)4 Field (java.lang.reflect.Field)4 Test (org.junit.Test)4 OException (com.orientechnologies.common.exception.OException)3