Search in sources :

Example 26 with OProperty

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

the class OCommandExecutorSQLCreatePropertyTest method testCreatePropertyWithLinkedClass.

@Test
public void testCreatePropertyWithLinkedClass() throws Exception {
    final ODatabaseDocumentTx db = new ODatabaseDocumentTx("memory:OCommandExecutorSQLCreatePropertyTest" + System.nanoTime());
    db.create();
    db.command(new OCommandSQL("CREATE class division")).execute();
    db.command(new OCommandSQL("CREATE class company")).execute();
    db.command(new OCommandSQL("CREATE property company.division LINK division")).execute();
    OClass companyClass = db.getMetadata().getSchema().getClass("company");
    OProperty property = companyClass.getProperty(PROP_DIVISION);
    assertEquals(property.getName(), PROP_DIVISION);
    assertEquals(property.getFullName(), PROP_FULL_DIVISION);
    assertEquals(property.getType(), OType.LINK);
    assertEquals(property.getLinkedClass().getName(), "division");
    assertFalse(property.isMandatory());
    assertFalse(property.isNotNull());
    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 27 with OProperty

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

the class OCommandExecutorSQLCreatePropertyTest method testRegex.

@Test
public void testRegex() throws Exception {
    final ODatabaseDocumentTx db = new ODatabaseDocumentTx("memory:OCommandExecutorSQLCreatePropertyTest" + System.nanoTime());
    db.create();
    db.command(new OCommandSQL("CREATE class myClass")).execute();
    db.command(new OCommandSQL("CREATE property myClass.regexp String (REGEX \"[M|F]\", mandatory) unsafe")).execute();
    OClass companyClass = db.getMetadata().getSchema().getClass("myClass");
    OProperty regexp = companyClass.getProperty("regexp");
    assertTrue(regexp.isMandatory());
    assertEquals(regexp.getRegexp(), "[M|F]");
    ODocument doc = db.newInstance("myClass");
    doc.field("regexp", "M");
    doc.save();
    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) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 28 with OProperty

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

the class OCommandExecutorSQLCreatePropertyTest method testCreateMandatoryProperty.

@Test
public void testCreateMandatoryProperty() 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 (MANDATORY)")).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);
    assertTrue(property.isMandatory());
    assertFalse(property.isNotNull());
    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 29 with OProperty

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

the class OObjectEntitySerializer method toStream.

/**
 * Serialize the user POJO to a ORecordDocument instance.
 *
 * @param iPojo User pojo to serialize
 *
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 */
@SuppressWarnings("unchecked")
protected static <T> T toStream(final T iPojo, final Proxy iProxiedPojo, ODatabaseObject db) throws IllegalArgumentException, IllegalAccessException {
    final ODocument iRecord = getDocument(iProxiedPojo);
    final long timer = Orient.instance().getProfiler().startChrono();
    final Integer identityRecord = System.identityHashCode(iPojo);
    if (OObjectSerializationThreadLocal.INSTANCE.get().containsKey(identityRecord))
        return (T) OObjectSerializationThreadLocal.INSTANCE.get().get(identityRecord);
    OObjectSerializationThreadLocal.INSTANCE.get().put(identityRecord, iProxiedPojo);
    OProperty schemaProperty;
    final Class<?> pojoClass = iPojo.getClass();
    final OClass schemaClass = iRecord.getSchemaClass();
    // CHECK FOR ID BINDING
    final Field idField = getIdField(pojoClass);
    if (idField != null) {
        Object id = getFieldValue(idField, iPojo);
        if (id != null) {
            // FOUND
            if (id instanceof ORecordId) {
                ORecordInternal.setIdentity(iRecord, (ORecordId) id);
            } else if (id instanceof Number) {
                // TREATS AS CLUSTER POSITION
                ((ORecordId) iRecord.getIdentity()).setClusterId(schemaClass.getDefaultClusterId());
                ((ORecordId) iRecord.getIdentity()).setClusterPosition(((Number) id).longValue());
            } else if (id instanceof String)
                ((ORecordId) iRecord.getIdentity()).fromString((String) id);
            else if (id.getClass().equals(Object.class))
                ORecordInternal.setIdentity(iRecord, (ORecordId) id);
            else
                OLogManager.instance().warn(OObjectSerializerHelper.class, "@Id field has been declared as %s while the supported are: ORID, Number, String, Object", id.getClass());
        }
        if (iRecord.getIdentity().isValid() && iRecord.getIdentity().isPersistent())
            iRecord.reload();
    }
    // CHECK FOR VERSION BINDING
    final Field vField = getVersionField(pojoClass);
    boolean versionConfigured = false;
    if (vField != null) {
        versionConfigured = true;
        Object ver = getFieldValue(vField, iPojo);
        if (ver != null) {
            // FOUND
            int version = iRecord.getVersion();
            if (ver instanceof Number) {
                // TREATS AS CLUSTER POSITION
                version = ((Number) ver).intValue();
            } else if (ver instanceof String)
                version = Integer.parseInt((String) ver);
            else
                OLogManager.instance().warn(OObjectSerializerHelper.class, "@Version field has been declared as %s while the supported are: Number, String", ver.getClass());
            ORecordInternal.setVersion(iRecord, version);
        }
    }
    if (db.isMVCC() && !versionConfigured && db.getTransaction() instanceof OTransactionOptimistic)
        throw new OTransactionException("Cannot involve an object of class '" + pojoClass + "' in an Optimistic Transaction commit because it does not define @Version or @OVersion and therefore cannot handle MVCC");
    String fieldName;
    Object fieldValue;
    // CALL BEFORE MARSHALLING
    invokeCallback(pojoClass, iPojo, iRecord, OBeforeSerialization.class);
    Class<?> currentClass = pojoClass;
    OObjectEntitySerializedSchema serializedSchema = getCurrentSerializedSchema();
    while (!currentClass.equals(Object.class) && serializedSchema.classes.contains(pojoClass)) {
        for (Field p : getDeclaredFields(currentClass)) {
            if (Modifier.isStatic(p.getModifiers()) || Modifier.isNative(p.getModifiers()) || Modifier.isTransient(p.getModifiers()) || p.getType().isAnonymousClass())
                continue;
            fieldName = p.getName();
            List<String> classTransientFields = serializedSchema.transientFields.get(currentClass);
            if ((idField != null && fieldName.equals(idField.getName()) || (vField != null && fieldName.equals(vField.getName())) || (classTransientFields != null && classTransientFields.contains(fieldName))))
                continue;
            fieldValue = getFieldValue(p, iPojo);
            if (fieldValue != null && fieldValue.getClass().isAnonymousClass())
                continue;
            if (isSerializedType(p))
                fieldValue = serializeFieldValue(p.getType(), fieldValue);
            schemaProperty = schemaClass != null ? schemaClass.getProperty(fieldName) : null;
            OType fieldType = schemaProperty != null ? schemaProperty.getType() : getTypeByClass(currentClass, fieldName);
            if (fieldValue != null) {
                if (isEmbeddedObject(p)) {
                    // AUTO CREATE SCHEMA CLASS
                    if (iRecord.getSchemaClass() == null) {
                        db.getMetadata().getSchema().createClass(iPojo.getClass());
                        iRecord.setClassNameIfExists(iPojo.getClass().getSimpleName());
                    }
                }
            }
            fieldValue = typeToStream(fieldValue, fieldType, db, iRecord);
            iRecord.field(fieldName, fieldValue, fieldType);
        }
        currentClass = currentClass.getSuperclass();
        if (currentClass == null || currentClass.equals(ODocument.class))
            // POJO EXTENDS ODOCUMENT: SPECIAL CASE: AVOID TO CONSIDER
            // ODOCUMENT FIELDS
            currentClass = Object.class;
    }
    // CALL AFTER MARSHALLING
    invokeCallback(pojoClass, iPojo, iRecord, OAfterSerialization.class);
    OObjectSerializationThreadLocal.INSTANCE.get().remove(identityRecord);
    Orient.instance().getProfiler().stopChrono("Object.toStream", "Serialize a POJO", timer);
    return (T) iProxiedPojo;
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OType(com.orientechnologies.orient.core.metadata.schema.OType) ORecordId(com.orientechnologies.orient.core.id.ORecordId) Field(java.lang.reflect.Field) OTransactionOptimistic(com.orientechnologies.orient.core.tx.OTransactionOptimistic) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ProxyObject(javassist.util.proxy.ProxyObject) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) OSchemaProxyObject(com.orientechnologies.orient.object.metadata.schema.OSchemaProxyObject) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 30 with OProperty

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

the class OObjectSerializerHelper method toStream.

/**
 * Serialize the user POJO to a ORecordDocument instance.
 *
 * @param iPojo
 *          User pojo to serialize
 * @param iRecord
 *          Record where to update
 * @param iObj2RecHandler
 */
public static ODocument toStream(final Object iPojo, final ODocument iRecord, final OEntityManager iEntityManager, final OClass schemaClass, final OUserObject2RecordHandler iObj2RecHandler, final ODatabaseObject db, final boolean iSaveOnlyDirty) {
    if (iSaveOnlyDirty && !iRecord.isDirty())
        return iRecord;
    final long timer = Orient.instance().getProfiler().startChrono();
    final Integer identityRecord = System.identityHashCode(iRecord);
    if (OSerializationThreadLocal.INSTANCE.get().contains(identityRecord))
        return iRecord;
    OSerializationThreadLocal.INSTANCE.get().add(identityRecord);
    OProperty schemaProperty;
    final Class<?> pojoClass = iPojo.getClass();
    final List<Field> properties = getClassFields(pojoClass);
    // CHECK FOR ID BINDING
    final Field idField = fieldIds.get(pojoClass);
    if (idField != null) {
        Object id = getFieldValue(iPojo, idField.getName());
        if (id != null) {
            // FOUND
            if (id instanceof ORecordId) {
                ORecordInternal.setIdentity(iRecord, (ORecordId) id);
            } else if (id instanceof Number) {
                // TREATS AS CLUSTER POSITION
                ((ORecordId) iRecord.getIdentity()).setClusterId(schemaClass.getDefaultClusterId());
                ((ORecordId) iRecord.getIdentity()).setClusterPosition(((Number) id).longValue());
            } else if (id instanceof String)
                ((ORecordId) iRecord.getIdentity()).fromString((String) id);
            else if (id.getClass().equals(Object.class))
                ORecordInternal.setIdentity(iRecord, (ORecordId) id);
            else
                OLogManager.instance().warn(OObjectSerializerHelper.class, "@Id field has been declared as %s while the supported are: ORID, Number, String, Object", id.getClass());
        }
    }
    // CHECK FOR VERSION BINDING
    final Field vField = fieldVersions.get(pojoClass);
    boolean versionConfigured = false;
    if (vField != null) {
        versionConfigured = true;
        Object ver = getFieldValue(iPojo, vField.getName());
        final int version = convertVersion(ver);
        ORecordInternal.setVersion(iRecord, version);
    }
    if (db.isMVCC() && !versionConfigured && db.getTransaction() instanceof OTransactionOptimistic)
        throw new OTransactionException("Cannot involve an object of class '" + pojoClass + "' in an Optimistic Transaction commit because it does not define @Version or @OVersion and therefore cannot handle MVCC");
    // SET OBJECT CLASS
    iRecord.setClassName(schemaClass != null ? schemaClass.getName() : null);
    String fieldName;
    Object fieldValue;
    // CALL BEFORE MARSHALLING
    invokeCallback(iPojo, iRecord, OBeforeSerialization.class);
    for (Field p : properties) {
        fieldName = p.getName();
        if (idField != null && fieldName.equals(idField.getName()))
            continue;
        if (vField != null && fieldName.equals(vField.getName()))
            continue;
        fieldValue = serializeFieldValue(getFieldType(iPojo, fieldName), getFieldValue(iPojo, fieldName));
        schemaProperty = schemaClass != null ? schemaClass.getProperty(fieldName) : null;
        if (fieldValue != null) {
            if (isEmbeddedObject(iPojo.getClass(), fieldValue.getClass(), fieldName, iEntityManager)) {
                // AUTO CREATE SCHEMA PROPERTY
                if (schemaClass == null) {
                    db.getMetadata().getSchema().createClass(iPojo.getClass());
                    iRecord.setClassNameIfExists(iPojo.getClass().getSimpleName());
                }
                if (schemaProperty == null) {
                    OType t = OType.getTypeByClass(fieldValue.getClass());
                    if (t == null)
                        t = OType.EMBEDDED;
                    schemaProperty = iRecord.getSchemaClass().createProperty(fieldName, t);
                }
            }
        }
        fieldValue = typeToStream(fieldValue, schemaProperty != null ? schemaProperty.getType() : null, iEntityManager, iObj2RecHandler, db, iRecord, iSaveOnlyDirty);
        iRecord.field(fieldName, fieldValue);
    }
    iObj2RecHandler.registerUserObject(iPojo, iRecord);
    // CALL AFTER MARSHALLING
    invokeCallback(iPojo, iRecord, OAfterSerialization.class);
    OSerializationThreadLocal.INSTANCE.get().remove(identityRecord);
    Orient.instance().getProfiler().stopChrono("Object.toStream", "Serialize object to stream", timer);
    return iRecord;
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OType(com.orientechnologies.orient.core.metadata.schema.OType) ORecordId(com.orientechnologies.orient.core.id.ORecordId) Field(java.lang.reflect.Field) OTransactionOptimistic(com.orientechnologies.orient.core.tx.OTransactionOptimistic) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject)

Aggregations

OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)121 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)84 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)41 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)32 Test (org.testng.annotations.Test)30 Test (org.junit.Test)28 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)25 OType (com.orientechnologies.orient.core.metadata.schema.OType)15 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)11 FilterCriteriaManager (ru.ydn.wicket.wicketorientdb.utils.query.filter.FilterCriteriaManager)10 IFilterCriteriaManager (ru.ydn.wicket.wicketorientdb.utils.query.filter.IFilterCriteriaManager)10 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)8 OIndex (com.orientechnologies.orient.core.index.OIndex)8 Date (java.util.Date)8 Set (java.util.Set)8 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)7 Map (java.util.Map)7 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)6 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)6 Collection (java.util.Collection)6