Search in sources :

Example 1 with OPropertyImpl

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

the class OServerCommandPostStudio method executeClassProperties.

private void executeClassProperties(final OHttpRequest iRequest, final OHttpResponse iResponse, final ODatabaseDocument db, final String operation, final String rid, final String className, final Map<String, String> fields) throws IOException {
    // GET THE TARGET CLASS
    final OClass cls = db.getMetadata().getSchema().getClass(rid);
    if (cls == null) {
        iResponse.send(OHttpUtils.STATUS_INTERNALERROR_CODE, "Error", OHttpUtils.CONTENT_TEXT_PLAIN, "Error: Class '" + rid + "' not found.", null);
        return;
    }
    if ("add".equals(operation)) {
        iRequest.data.commandInfo = "Studio add property";
        try {
            OType type = OType.valueOf(fields.get("type"));
            OPropertyImpl prop;
            if (type == OType.LINK || type == OType.LINKLIST || type == OType.LINKSET || type == OType.LINKMAP)
                prop = (OPropertyImpl) cls.createProperty(fields.get("name"), type, db.getMetadata().getSchema().getClass(fields.get("linkedClass")));
            else
                prop = (OPropertyImpl) cls.createProperty(fields.get("name"), type);
            if (fields.get("linkedType") != null)
                prop.setLinkedType(OType.valueOf(fields.get("linkedType")));
            if (fields.get("mandatory") != null)
                prop.setMandatory("on".equals(fields.get("mandatory")));
            if (fields.get("readonly") != null)
                prop.setReadonly("on".equals(fields.get("readonly")));
            if (fields.get("notNull") != null)
                prop.setNotNull("on".equals(fields.get("notNull")));
            if (fields.get("min") != null)
                prop.setMin(fields.get("min"));
            if (fields.get("max") != null)
                prop.setMax(fields.get("max"));
            iResponse.send(OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_TEXT_PLAIN, "Property " + fields.get("name") + " created successfully", null);
        } catch (Exception e) {
            iResponse.send(OHttpUtils.STATUS_INTERNALERROR_CODE, "Error on creating a new property in class " + rid + ": " + e, OHttpUtils.CONTENT_TEXT_PLAIN, "Error on creating a new property in class " + rid + ": " + e, null);
        }
    } else if ("del".equals(operation)) {
        iRequest.data.commandInfo = "Studio delete property";
        cls.dropProperty(className);
        iResponse.send(OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_TEXT_PLAIN, "Property " + fields.get("name") + " deleted successfully.", null);
    }
}
Also used : OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OType(com.orientechnologies.orient.core.metadata.schema.OType) OPropertyImpl(com.orientechnologies.orient.core.metadata.schema.OPropertyImpl) IOException(java.io.IOException)

Example 2 with OPropertyImpl

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

the class OCommandExecutorSQLCreateProperty method execute.

/**
   * Execute the CREATE PROPERTY.
   */
public Object execute(final Map<Object, Object> iArgs) {
    if (type == null)
        throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
    final ODatabaseDocument database = getDatabase();
    final OClassImpl sourceClass = (OClassImpl) database.getMetadata().getSchema().getClass(className);
    if (sourceClass == null)
        throw new OCommandExecutionException("Source class '" + className + "' not found");
    OPropertyImpl prop = (OPropertyImpl) sourceClass.getProperty(fieldName);
    if (prop != null) {
        if (ifNotExists) {
            return sourceClass.properties().size();
        }
        throw new OCommandExecutionException("Property '" + className + "." + fieldName + "' already exists. Remove it before to retry.");
    }
    // CREATE THE PROPERTY
    OClass linkedClass = null;
    OType linkedType = null;
    if (linked != null) {
        // FIRST SEARCH BETWEEN CLASSES
        linkedClass = database.getMetadata().getSchema().getClass(linked);
        if (linkedClass == null)
            // NOT FOUND: SEARCH BETWEEN TYPES
            linkedType = OType.valueOf(linked.toUpperCase(Locale.ENGLISH));
    }
    // CREATE IT LOCALLY
    OPropertyImpl internalProp = sourceClass.addPropertyInternal(fieldName, type, linkedType, linkedClass, unsafe);
    boolean toSave = false;
    if (readonly) {
        internalProp.setReadonly(true);
        toSave = true;
    }
    if (mandatory) {
        internalProp.setMandatory(true);
        toSave = true;
    }
    if (notnull) {
        internalProp.setNotNull(true);
        toSave = true;
    }
    if (max != null) {
        internalProp.setMax(max);
        toSave = true;
    }
    if (min != null) {
        internalProp.setMin(min);
        toSave = true;
    }
    if (defaultValue != null) {
        internalProp.setDefaultValue(defaultValue);
        toSave = true;
    }
    if (collate != null) {
        internalProp.setCollate(collate);
        toSave = true;
    }
    if (regex != null) {
        internalProp.setRegexp(regex);
        toSave = true;
    }
    if (toSave) {
        internalProp.save();
    }
    return sourceClass.properties().size();
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OType(com.orientechnologies.orient.core.metadata.schema.OType) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OClassImpl(com.orientechnologies.orient.core.metadata.schema.OClassImpl) OPropertyImpl(com.orientechnologies.orient.core.metadata.schema.OPropertyImpl)

Example 3 with OPropertyImpl

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

the class OCommandExecutorSQLAlterProperty method execute.

/**
   * Execute the ALTER PROPERTY.
   */
public Object execute(final Map<Object, Object> iArgs) {
    if (attribute == null)
        throw new OCommandExecutionException("Cannot execute the command because it has not yet been parsed");
    final OClassImpl sourceClass = (OClassImpl) getDatabase().getMetadata().getSchema().getClass(className);
    if (sourceClass == null)
        throw new OCommandExecutionException("Source class '" + className + "' not found");
    final OPropertyImpl prop = (OPropertyImpl) sourceClass.getProperty(fieldName);
    if (prop == null)
        throw new OCommandExecutionException("Property '" + className + "." + fieldName + "' not exists");
    if ("null".equalsIgnoreCase(value))
        prop.set(attribute, null);
    else
        prop.set(attribute, value);
    return null;
}
Also used : OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OClassImpl(com.orientechnologies.orient.core.metadata.schema.OClassImpl) OPropertyImpl(com.orientechnologies.orient.core.metadata.schema.OPropertyImpl)

Example 4 with OPropertyImpl

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

the class OServerCommandGetDatabase method exportClass.

public static void exportClass(final ODatabaseDocument db, final OJSONWriter json, final OClass cls) throws IOException {
    json.beginObject();
    json.writeAttribute("name", cls.getName());
    json.writeAttribute("superClass", cls.getSuperClass() != null ? cls.getSuperClass().getName() : "");
    json.beginCollection("superClasses");
    int i = 0;
    for (OClass oClass : cls.getSuperClasses()) {
        json.write((i > 0 ? "," : "") + "\"" + oClass.getName() + "\"");
        i++;
    }
    json.endCollection();
    json.writeAttribute("alias", cls.getShortName());
    json.writeAttribute("abstract", cls.isAbstract());
    json.writeAttribute("strictmode", cls.isStrictMode());
    json.writeAttribute("clusters", cls.getClusterIds());
    json.writeAttribute("defaultCluster", cls.getDefaultClusterId());
    json.writeAttribute("clusterSelection", cls.getClusterSelection().getName());
    if (cls instanceof OClassImpl) {
        final Map<String, String> custom = ((OClassImpl) cls).getCustomInternal();
        if (custom != null && !custom.isEmpty()) {
            json.writeAttribute("custom", custom);
        }
    }
    try {
        json.writeAttribute("records", db.countClass(cls.getName()));
    } catch (OSecurityAccessException e) {
        json.writeAttribute("records", "? (Unauthorized)");
    } catch (Exception e) {
        json.writeAttribute("records", "? (Error)");
    }
    if (cls.properties() != null && cls.properties().size() > 0) {
        json.beginCollection("properties");
        for (final OProperty prop : cls.properties()) {
            json.beginObject();
            json.writeAttribute("name", prop.getName());
            if (prop.getLinkedClass() != null)
                json.writeAttribute("linkedClass", prop.getLinkedClass().getName());
            if (prop.getLinkedType() != null)
                json.writeAttribute("linkedType", prop.getLinkedType().toString());
            json.writeAttribute("type", prop.getType().toString());
            json.writeAttribute("mandatory", prop.isMandatory());
            json.writeAttribute("readonly", prop.isReadonly());
            json.writeAttribute("notNull", prop.isNotNull());
            json.writeAttribute("min", prop.getMin());
            json.writeAttribute("max", prop.getMax());
            json.writeAttribute("regexp", prop.getRegexp());
            json.writeAttribute("collate", prop.getCollate() != null ? prop.getCollate().getName() : "default");
            json.writeAttribute("defaultValue", prop.getDefaultValue());
            if (prop instanceof OPropertyImpl) {
                final Map<String, String> custom = ((OPropertyImpl) prop).getCustomInternal();
                if (custom != null && !custom.isEmpty()) {
                    json.writeAttribute("custom", custom);
                }
            }
            json.endObject();
        }
        json.endCollection();
    }
    final Set<OIndex<?>> indexes = cls.getIndexes();
    if (!indexes.isEmpty()) {
        json.beginCollection("indexes");
        for (final OIndex<?> index : indexes) {
            json.beginObject();
            json.writeAttribute("name", index.getName());
            json.writeAttribute("type", index.getType());
            final OIndexDefinition indexDefinition = index.getDefinition();
            if (indexDefinition != null && !indexDefinition.getFields().isEmpty())
                json.writeAttribute("fields", indexDefinition.getFields());
            json.endObject();
        }
        json.endCollection();
    }
    json.endObject();
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition) OIndex(com.orientechnologies.orient.core.index.OIndex) OClassImpl(com.orientechnologies.orient.core.metadata.schema.OClassImpl) OPropertyImpl(com.orientechnologies.orient.core.metadata.schema.OPropertyImpl) IOException(java.io.IOException) OSecurityAccessException(com.orientechnologies.orient.core.exception.OSecurityAccessException) OSecurityAccessException(com.orientechnologies.orient.core.exception.OSecurityAccessException) OClass(com.orientechnologies.orient.core.metadata.schema.OClass)

Aggregations

OPropertyImpl (com.orientechnologies.orient.core.metadata.schema.OPropertyImpl)4 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)3 OClassImpl (com.orientechnologies.orient.core.metadata.schema.OClassImpl)3 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)2 OType (com.orientechnologies.orient.core.metadata.schema.OType)2 IOException (java.io.IOException)2 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 OSecurityAccessException (com.orientechnologies.orient.core.exception.OSecurityAccessException)1 OIndex (com.orientechnologies.orient.core.index.OIndex)1 OIndexDefinition (com.orientechnologies.orient.core.index.OIndexDefinition)1 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)1