Search in sources :

Example 36 with OProperty

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

the class OServerCommandPostProperty method addSingleProperty.

@SuppressWarnings("unused")
protected boolean addSingleProperty(final OHttpRequest iRequest, final OHttpResponse iResponse, final ODatabaseDocument db) throws InterruptedException, IOException {
    String[] urlParts = checkSyntax(iRequest.url, 4, "Syntax error: property/<database>/<class-name>/<property-name>/[<property-type>]/[<link-type>]");
    iRequest.data.commandInfo = "Create property";
    iRequest.data.commandDetail = urlParts[2] + "." + urlParts[3];
    if (db.getMetadata().getSchema().getClass(urlParts[2]) == null)
        throw new IllegalArgumentException("Invalid class '" + urlParts[2] + "'");
    final OClass cls = db.getMetadata().getSchema().getClass(urlParts[2]);
    final String propertyName = urlParts[3];
    final OType propertyType = urlParts.length > 4 ? OType.valueOf(urlParts[4]) : OType.STRING;
    switch(propertyType) {
        case LINKLIST:
        case LINKMAP:
        case LINKSET:
        case LINK:
            {
                /* try link as OType */
                OType linkType = null;
                OClass linkClass = null;
                if (urlParts.length >= 6) {
                    try {
                        linkType = OType.valueOf(urlParts[5]);
                    } catch (IllegalArgumentException ex) {
                    }
                    if (linkType == null) {
                        linkClass = db.getMetadata().getSchema().getClass(urlParts[5]);
                        if (linkClass == null) {
                            throw new IllegalArgumentException("linked type declared as " + urlParts[5] + " can be either a Type or a Class, use the JSON document usage instead. See 'http://code.google.com/p/orient/w/edit/OrientDB_REST'");
                        }
                    }
                }
                if (linkType != null) {
                    final OProperty prop = cls.createProperty(propertyName, propertyType, linkType);
                } else if (linkClass != null) {
                    final OProperty prop = cls.createProperty(propertyName, propertyType, linkClass);
                } else {
                    final OProperty prop = cls.createProperty(propertyName, propertyType);
                }
            }
            break;
        default:
            final OProperty prop = cls.createProperty(propertyName, propertyType);
            break;
    }
    iResponse.send(OHttpUtils.STATUS_CREATED_CODE, OHttpUtils.STATUS_CREATED_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, cls.properties().size(), null);
    return false;
}
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 37 with OProperty

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

the class OServerCommandPostProperty method addMultipreProperties.

@SuppressWarnings({ "unchecked", "unused" })
protected boolean addMultipreProperties(final OHttpRequest iRequest, final OHttpResponse iResponse, final ODatabaseDocument db) throws InterruptedException, IOException {
    String[] urlParts = checkSyntax(iRequest.url, 3, "Syntax error: property/<database>/<class-name>");
    iRequest.data.commandInfo = "Create property";
    iRequest.data.commandDetail = urlParts[2];
    if (db.getMetadata().getSchema().getClass(urlParts[2]) == null)
        throw new IllegalArgumentException("Invalid class '" + urlParts[2] + "'");
    final OClass cls = db.getMetadata().getSchema().getClass(urlParts[2]);
    final ODocument propertiesDoc = new ODocument().fromJSON(iRequest.content);
    for (String propertyName : propertiesDoc.fieldNames()) {
        final Map<String, String> doc = (Map<String, String>) propertiesDoc.field(propertyName);
        final OType propertyType = OType.valueOf(doc.get(PROPERTY_TYPE_JSON_FIELD));
        switch(propertyType) {
            case LINKLIST:
            case LINKMAP:
            case LINKSET:
                {
                    final String linkType = doc.get(LINKED_TYPE_JSON_FIELD);
                    final String linkClass = doc.get(LINKED_CLASS_JSON_FIELD);
                    if (linkType != null) {
                        final OProperty prop = cls.createProperty(propertyName, propertyType, OType.valueOf(linkType));
                    } else if (linkClass != null) {
                        final OProperty prop = cls.createProperty(propertyName, propertyType, db.getMetadata().getSchema().getClass(linkClass));
                    } else {
                        throw new IllegalArgumentException("property named " + propertyName + " is declared as " + propertyType + " but linked type is not declared");
                    }
                }
                break;
            case LINK:
                {
                    final String linkClass = doc.get(LINKED_CLASS_JSON_FIELD);
                    if (linkClass != null) {
                        final OProperty prop = cls.createProperty(propertyName, propertyType, db.getMetadata().getSchema().getClass(linkClass));
                    } else {
                        throw new IllegalArgumentException("property named " + propertyName + " is declared as " + propertyType + " but linked Class is not declared");
                    }
                }
                break;
            default:
                final OProperty prop = cls.createProperty(propertyName, propertyType);
                break;
        }
    }
    iResponse.send(OHttpUtils.STATUS_CREATED_CODE, OHttpUtils.STATUS_CREATED_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, cls.properties().size(), null);
    return false;
}
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) Map(java.util.Map) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 38 with OProperty

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

the class OServerCommandGetFileDownload method execute.

@Override
public boolean execute(OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    String[] urlParts = checkSyntax(iRequest.url, 3, "Syntax error: fileDownload/<database>/rid/[/<fileName>][/<fileType>].");
    final String fileName = urlParts.length > 3 ? encodeResponseText(urlParts[3]) : "unknown";
    final String fileType = urlParts.length > 5 ? encodeResponseText(urlParts[4]) + '/' + encodeResponseText(urlParts[5]) : (urlParts.length > 4 ? encodeResponseText(urlParts[4]) : "");
    final String rid = urlParts[2];
    iRequest.data.commandInfo = "Download";
    iRequest.data.commandDetail = rid;
    final ORecordAbstract response;
    ODatabaseDocument db = getProfiledDatabaseInstance(iRequest);
    try {
        response = db.load(new ORecordId(rid));
        if (response != null) {
            if (response instanceof OBlob) {
                sendORecordBinaryFileContent(iRequest, iResponse, OHttpUtils.STATUS_OK_CODE, OHttpUtils.STATUS_OK_DESCRIPTION, fileType, (OBlob) response, fileName);
            } else if (response instanceof ODocument) {
                for (OProperty prop : ODocumentInternal.getImmutableSchemaClass(((ODocument) response)).properties()) {
                    if (prop.getType().equals(OType.BINARY))
                        sendBinaryFieldFileContent(iRequest, iResponse, OHttpUtils.STATUS_OK_CODE, OHttpUtils.STATUS_OK_DESCRIPTION, fileType, (byte[]) ((ODocument) response).field(prop.getName()), fileName);
                }
            } else {
                iResponse.send(OHttpUtils.STATUS_INVALIDMETHOD_CODE, "Record requested is not a file nor has a readable schema", OHttpUtils.CONTENT_TEXT_PLAIN, "Record requested is not a file nor has a readable schema", null);
            }
        } else {
            iResponse.send(OHttpUtils.STATUS_INVALIDMETHOD_CODE, "Record requested not exists", OHttpUtils.CONTENT_TEXT_PLAIN, "Record requestes not exists", null);
        }
    } catch (Exception e) {
        iResponse.send(OHttpUtils.STATUS_INTERNALERROR_CODE, OHttpUtils.STATUS_INTERNALERROR_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, e.getMessage(), null);
    } finally {
        if (db != null)
            db.close();
    }
    return false;
}
Also used : ORecordAbstract(com.orientechnologies.orient.core.record.ORecordAbstract) OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OBlob(com.orientechnologies.orient.core.record.impl.OBlob) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ORecordId(com.orientechnologies.orient.core.id.ORecordId) IOException(java.io.IOException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 39 with OProperty

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

the class ClassTest method testOClassAndOPropertyDescription.

@Test
public void testOClassAndOPropertyDescription() {
    final OSchema oSchema = db.getMetadata().getSchema();
    OClass oClass = oSchema.createClass("DescriptionTest");
    OProperty oProperty = oClass.createProperty("property", OType.STRING);
    oClass.setDescription("DescriptionTest-class-description");
    oProperty.setDescription("DescriptionTest-property-description");
    assertEquals(oClass.getDescription(), "DescriptionTest-class-description");
    assertEquals(oProperty.getDescription(), "DescriptionTest-property-description");
    oSchema.reload();
    oClass = oSchema.getClass("DescriptionTest");
    oProperty = oClass.getProperty("property");
    assertEquals(oClass.getDescription(), "DescriptionTest-class-description");
    assertEquals(oProperty.getDescription(), "DescriptionTest-property-description");
    oClass = db.getMetadata().getImmutableSchemaSnapshot().getClass("DescriptionTest");
    oProperty = oClass.getProperty("property");
    assertEquals(oClass.getDescription(), "DescriptionTest-class-description");
    assertEquals(oProperty.getDescription(), "DescriptionTest-property-description");
}
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) Test(org.testng.annotations.Test)

Example 40 with OProperty

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

the class ODocumentTest method testRemovingReadonlyField.

@Test
public void testRemovingReadonlyField() {
    ODatabaseDocumentTx db = new ODatabaseDocumentTx("memory:" + ODocumentTest.class.getSimpleName());
    db.create();
    try {
        OSchema schema = db.getMetadata().getSchema();
        OClass classA = schema.createClass("TestRemovingField2");
        classA.createProperty("name", OType.STRING);
        OProperty property = classA.createProperty("property", OType.STRING);
        property.setReadonly(true);
        ODocument doc = new ODocument(classA);
        doc.field("name", "My Name");
        doc.field("property", "value1");
        doc.save();
        doc.field("name", "My Name 2");
        doc.field("property", "value2");
        // we decided undo everything
        doc.undo();
        // change something
        doc.field("name", "My Name 3");
        doc.save();
        doc.field("name", "My Name 4");
        doc.field("property", "value4");
        // we decided undo readonly field
        doc.undo("property");
        doc.save();
    } finally {
        db.drop();
    }
}
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) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) Test(org.testng.annotations.Test)

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