Search in sources :

Example 71 with OProperty

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

the class OCommandExecutorSQLCreatePropertyTest method testIfNotExists.

@Test
public void testIfNotExists() throws Exception {
    final ODatabaseDocumentTx db = new ODatabaseDocumentTx("memory:OCommandExecutorSQLCreatePropertyTest" + System.nanoTime());
    db.create();
    db.command(new OCommandSQL("CREATE class testIfNotExists")).execute();
    db.command(new OCommandSQL("CREATE property testIfNotExists.name if not exists STRING")).execute();
    OClass companyClass = db.getMetadata().getSchema().getClass("testIfNotExists");
    OProperty property = companyClass.getProperty("name");
    assertEquals(property.getName(), PROP_NAME);
    db.command(new OCommandSQL("CREATE property testIfNotExists.name if not exists STRING")).execute();
    companyClass = db.getMetadata().getSchema().getClass("testIfNotExists");
    property = companyClass.getProperty("name");
    assertEquals(property.getName(), PROP_NAME);
    db.drop();
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) Test(org.testng.annotations.Test)

Example 72 with OProperty

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

the class OCommandExecutorSQLCreatePropertyTest method testCreateReadOnlyFalseProperty.

@Test
public void testCreateReadOnlyFalseProperty() throws Exception {
    final ODatabaseDocumentTx db = new ODatabaseDocumentTx("memory:OCommandExecutorSQLCreatePropertyTest" + System.nanoTime());
    db.create();
    db.command(new OCommandSQL("CREATE class company")).execute();
    db.command(new OCommandSQL("CREATE property company.name STRING (READONLY false)")).execute();
    OClass companyClass = db.getMetadata().getSchema().getClass("company");
    OProperty property = companyClass.getProperty(PROP_NAME);
    assertEquals(property.getName(), PROP_NAME);
    assertEquals(property.getFullName(), PROP_FULL_NAME);
    assertFalse(property.isReadonly());
    db.close();
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) Test(org.testng.annotations.Test)

Example 73 with OProperty

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

the class OCommandExecutorSQLCreatePropertyTest method testComplexCreateProperty.

@Test
public void testComplexCreateProperty() throws Exception {
    final ODatabaseDocumentTx db = new ODatabaseDocumentTx("memory:OCommandExecutorSQLCreatePropertyTest" + System.nanoTime());
    db.create();
    db.command(new OCommandSQL("CREATE Class company")).execute();
    db.command(new OCommandSQL("CREATE Property company.officers EMBEDDEDLIST STRING (MANDATORY, READONLY, NOTNULL) UNSAFE")).execute();
    OClass companyClass = db.getMetadata().getSchema().getClass("company");
    OProperty property = companyClass.getProperty(PROP_OFFICERS);
    assertEquals(property.getName(), PROP_OFFICERS);
    assertEquals(property.getFullName(), PROP_FULL_OFFICERS);
    assertEquals(property.getType(), OType.EMBEDDEDLIST);
    assertEquals(property.getLinkedType(), OType.STRING);
    assertTrue(property.isMandatory());
    assertTrue(property.isNotNull());
    assertTrue(property.isReadonly());
    db.close();
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) Test(org.testng.annotations.Test)

Example 74 with OProperty

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

the class OObjectProxyMethodHandler method setValue.

@SuppressWarnings("rawtypes")
protected Object setValue(final Object self, final String fieldName, Object valueToSet) {
    if (valueToSet == null) {
        Object oldValue = doc.field(fieldName);
        if (OObjectEntitySerializer.isCascadeDeleteField(self.getClass(), fieldName) && oldValue instanceof OIdentifiable)
            orphans.add(((OIdentifiable) oldValue).getIdentity());
        setDocFieldValue(fieldName, valueToSet, OObjectEntitySerializer.getTypeByClass(self.getClass(), fieldName));
    } else if (!valueToSet.getClass().isAnonymousClass()) {
        if (valueToSet instanceof Proxy) {
            ODocument docToSet = OObjectEntitySerializer.getDocument((Proxy) valueToSet);
            if (OObjectEntitySerializer.isEmbeddedField(self.getClass(), fieldName))
                ODocumentInternal.addOwner(docToSet, doc);
            Object oldValue = doc.field(fieldName);
            if (OObjectEntitySerializer.isCascadeDeleteField(self.getClass(), fieldName) && oldValue instanceof OIdentifiable)
                orphans.add(((OIdentifiable) oldValue).getIdentity());
            setDocFieldValue(fieldName, docToSet, OObjectEntitySerializer.getTypeByClass(self.getClass(), fieldName));
        } else if (valueToSet instanceof OIdentifiable) {
            if (valueToSet instanceof ODocument && OObjectEntitySerializer.isEmbeddedField(self.getClass(), fieldName))
                ODocumentInternal.addOwner((ODocument) valueToSet, doc);
            Object oldValue = doc.field(fieldName);
            if (OObjectEntitySerializer.isCascadeDeleteField(self.getClass(), fieldName) && oldValue instanceof OIdentifiable)
                orphans.add(((OIdentifiable) oldValue).getIdentity());
            setDocFieldValue(fieldName, valueToSet, OObjectEntitySerializer.getTypeByClass(self.getClass(), fieldName));
        } else if (((valueToSet instanceof Collection<?> || valueToSet instanceof Map<?, ?>)) || valueToSet.getClass().isArray()) {
            Class<?> genericMultiValueType = OReflectionHelper.getGenericMultivalueType(OObjectEntitySerializer.getField(fieldName, self.getClass()));
            if (genericMultiValueType != null && !OReflectionHelper.isJavaType(genericMultiValueType)) {
                if (!(valueToSet instanceof OObjectLazyMultivalueElement)) {
                    if (valueToSet instanceof Collection<?>) {
                        boolean customSerialization = false;
                        final Field f = OObjectEntitySerializer.getField(fieldName, self.getClass());
                        if (OObjectEntitySerializer.isSerializedType(f)) {
                            customSerialization = true;
                        }
                        valueToSet = manageCollectionSave(self, f, (Collection<?>) valueToSet, customSerialization, true);
                    } else if (valueToSet instanceof Map<?, ?>) {
                        boolean customSerialization = false;
                        final Field f = OObjectEntitySerializer.getField(fieldName, self.getClass());
                        if (OObjectEntitySerializer.isSerializedType(f)) {
                            customSerialization = true;
                        }
                        valueToSet = manageMapSave(self, f, (Map<?, ?>) valueToSet, customSerialization);
                    } else if (valueToSet.getClass().isArray()) {
                        valueToSet = manageArraySave(fieldName, (Object[]) valueToSet);
                    }
                }
            } else {
                if (OObjectEntitySerializer.isToSerialize(valueToSet.getClass())) {
                    setDocFieldValue(fieldName, OObjectEntitySerializer.serializeFieldValue(OObjectEntitySerializer.getField(fieldName, self.getClass()).getType(), valueToSet), OObjectEntitySerializer.getTypeByClass(self.getClass(), fieldName));
                } else {
                    if (valueToSet.getClass().isArray()) {
                        final OClass schemaClass = doc.getSchemaClass();
                        OProperty schemaProperty = null;
                        if (schemaClass != null)
                            schemaProperty = schemaClass.getProperty(fieldName);
                        setDocFieldValue(fieldName, OObjectEntitySerializer.typeToStream(valueToSet, schemaProperty != null ? schemaProperty.getType() : null, getDatabase(), doc), OObjectEntitySerializer.getTypeByClass(self.getClass(), fieldName));
                    } else
                        setDocFieldValue(fieldName, valueToSet, OObjectEntitySerializer.getTypeByClass(self.getClass(), fieldName));
                }
            }
        } else if (valueToSet.getClass().isEnum()) {
            setDocFieldValue(fieldName, ((Enum) valueToSet).name(), OObjectEntitySerializer.getTypeByClass(self.getClass(), fieldName));
        } else {
            if (OObjectEntitySerializer.isToSerialize(valueToSet.getClass())) {
                setDocFieldValue(fieldName, OObjectEntitySerializer.serializeFieldValue(OObjectEntitySerializer.getField(fieldName, self.getClass()).getType(), valueToSet), OObjectEntitySerializer.getTypeByClass(self.getClass(), fieldName));
            } else if (getDatabase().getEntityManager().getEntityClass(valueToSet.getClass().getSimpleName()) != null && !valueToSet.getClass().isEnum()) {
                valueToSet = OObjectEntitySerializer.serializeObject(valueToSet, getDatabase());
                final ODocument docToSet = OObjectEntitySerializer.getDocument((Proxy) valueToSet);
                if (OObjectEntitySerializer.isEmbeddedField(self.getClass(), fieldName))
                    ODocumentInternal.addOwner((ODocument) docToSet, doc);
                setDocFieldValue(fieldName, docToSet, OObjectEntitySerializer.getTypeByClass(self.getClass(), fieldName));
            } else {
                setDocFieldValue(fieldName, valueToSet, OObjectEntitySerializer.getTypeByClass(self.getClass(), fieldName));
            }
        }
        loadedFields.put(fieldName, doc.getVersion());
        setDirty();
    } else {
        OLogManager.instance().warn(this, "Setting property '%s' in proxied class '%s' with an anonymous class '%s'. The document won't have this property.", fieldName, self.getClass().getName(), valueToSet.getClass().getName());
    }
    return valueToSet;
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OObjectLazyMultivalueElement(com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) Field(java.lang.reflect.Field) Proxy(javassist.util.proxy.Proxy) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ProxyObject(javassist.util.proxy.ProxyObject) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) OObjectCustomSerializerMap(com.orientechnologies.orient.object.serialization.OObjectCustomSerializerMap) HashMap(java.util.HashMap) OObjectEnumLazyMap(com.orientechnologies.orient.object.enumerations.OObjectEnumLazyMap) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Map(java.util.Map) OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 75 with OProperty

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

the class OObjectProxyMethodHandler method getValue.

@SuppressWarnings({ "rawtypes", "unchecked" })
protected Object getValue(final Object self, final String fieldName, final boolean idOrVersionField, Object value, final boolean iIgnoreLoadedFields) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    if (!idOrVersionField) {
        if (value == null) {
            if (!iIgnoreLoadedFields && loadedFields.containsKey(fieldName) && loadedFields.get(fieldName).compareTo(doc.getVersion()) == 0) {
                return null;
            } else {
                final Object docValue = getDocFieldValue(self, fieldName);
                if (docValue != null) {
                    value = lazyLoadField(self, fieldName, docValue, value);
                }
            }
        } else {
            if (((value instanceof Collection<?> || value instanceof Map<?, ?>) && !(value instanceof OObjectLazyMultivalueElement)) || value.getClass().isArray()) {
                final Class<?> genericMultiValueType = OReflectionHelper.getGenericMultivalueType(OObjectEntitySerializer.getField(fieldName, self.getClass()));
                if (genericMultiValueType == null || !OReflectionHelper.isJavaType(genericMultiValueType)) {
                    final Field f = OObjectEntitySerializer.getField(fieldName, self.getClass());
                    if (OObjectEntitySerializer.isSerializedType(f) && !(value instanceof OObjectLazyCustomSerializer)) {
                        value = manageSerializedCollections(self, fieldName, value);
                    } else if (genericMultiValueType != null && genericMultiValueType.isEnum() && !(value instanceof OObjectLazyEnumSerializer)) {
                        value = manageEnumCollections(self, f.getName(), genericMultiValueType, value);
                    } else {
                        value = manageObjectCollections(self, fieldName, value);
                    }
                } else {
                    final Object docValue = getDocFieldValue(self, fieldName);
                    if (docValue == null) {
                        if (value.getClass().isArray()) {
                            OClass schemaClass = doc.getSchemaClass();
                            OProperty schemaProperty = null;
                            if (schemaClass != null)
                                schemaProperty = schemaClass.getProperty(fieldName);
                            doc.field(fieldName, OObjectEntitySerializer.typeToStream(value, schemaProperty != null ? schemaProperty.getType() : null, getDatabase(), doc));
                        } else
                            doc.field(fieldName, value);
                    } else if (!loadedFields.containsKey(fieldName)) {
                        value = manageArrayFieldObject(OObjectEntitySerializer.getField(fieldName, self.getClass()), self, docValue);
                        Method setMethod = getSetMethod(self.getClass().getSuperclass(), getSetterFieldName(fieldName), value);
                        setMethod.invoke(self, value);
                    } else if ((value instanceof Set || value instanceof Map) && loadedFields.get(fieldName).compareTo(doc.getVersion()) < 0 && !OReflectionHelper.isJavaType(genericMultiValueType)) {
                        if (value instanceof Set)
                            value = new OObjectLazySet(self, (Set<OIdentifiable>) docValue, OObjectEntitySerializer.isCascadeDeleteField(self.getClass(), fieldName));
                        else
                            value = new OObjectLazyMap(self, (Map<Object, OIdentifiable>) docValue, OObjectEntitySerializer.isCascadeDeleteField(self.getClass(), fieldName));
                        final Method setMethod = getSetMethod(self.getClass().getSuperclass(), getSetterFieldName(fieldName), value);
                        setMethod.invoke(self, value);
                    }
                }
            } else if (!loadedFields.containsKey(fieldName) || loadedFields.get(fieldName).compareTo(doc.getVersion()) < 0) {
                final Object docValue = getDocFieldValue(self, fieldName);
                if (docValue != null && !docValue.equals(value)) {
                    value = lazyLoadField(self, fieldName, docValue, value);
                }
            }
        }
    }
    return value;
}
Also used : OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap) OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OObjectCustomSerializerSet(com.orientechnologies.orient.object.serialization.OObjectCustomSerializerSet) HashSet(java.util.HashSet) OObjectLazySet(com.orientechnologies.orient.object.db.OObjectLazySet) OObjectEnumLazySet(com.orientechnologies.orient.object.enumerations.OObjectEnumLazySet) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) Set(java.util.Set) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OObjectLazyCustomSerializer(com.orientechnologies.orient.object.serialization.OObjectLazyCustomSerializer) OObjectLazyMultivalueElement(com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement) Method(java.lang.reflect.Method) OObjectLazyEnumSerializer(com.orientechnologies.orient.object.enumerations.OObjectLazyEnumSerializer) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) Field(java.lang.reflect.Field) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ProxyObject(javassist.util.proxy.ProxyObject) OObjectLazySet(com.orientechnologies.orient.object.db.OObjectLazySet) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) OObjectCustomSerializerMap(com.orientechnologies.orient.object.serialization.OObjectCustomSerializerMap) HashMap(java.util.HashMap) OObjectEnumLazyMap(com.orientechnologies.orient.object.enumerations.OObjectEnumLazyMap) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Map(java.util.Map) OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap)

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