Package com.orientechnologies.orient.core.metadata.schema

Examples of com.orientechnologies.orient.core.metadata.schema.OClass


      throw new OCommandSQLParsingException("This command supports only the database type ODatabaseDocumentTx and type '"
          + database.getClass() + "' was found");

    final ODatabaseDocumentTx db = (ODatabaseDocumentTx) database;

    OClass sourceClass = database.getMetadata().getSchema().getClass(sourceClassName);
    if (sourceClass == null)
      throw new OCommandExecutionException("Source class '" + sourceClassName + "' not found");

    OClass destClass = database.getMetadata().getSchema().getClass(destClassName);
    if (destClass == null)
      throw new OCommandExecutionException("Destination class '" + destClassName + "' not found");

    Object value;
    String cmd = "select from " + destClassName + " where " + destField + " = ";
    List<ODocument> result;
    ODocument target;
    Object oldValue;
    long total = 0;

    if (linkName == null)
      // NO LINK NAME EXPRESSED: OVERWRITE THE SOURCE FIELD
      linkName = sourceField;

    boolean inverse = linkType != null && linkType.equalsIgnoreCase("inverse");
    boolean multipleRelationship = false;

    long totRecords = db.countClass(sourceClass.getName());
    long currRecord = 0;

    if (progressListener != null)
      progressListener.onBegin(this, totRecords);

    try {
      // BROWSE ALL THE RECORDS OF THE SOURCE CLASS
      for (ODocument doc : db.browseClass(sourceClass.getName())) {
        value = doc.field(sourceField);

        if (value != null) {
          if (value instanceof ODocument || value instanceof ORID) {
            // ALREADY CONVERTED
          } else if (value instanceof Collection<?>) {
            // TODO
          } else {
            // SEARCH THE DESTINATION RECORD
            if (value instanceof String) {
              target = null;

              if (((String) value).length() == 0)
                value = null;
              else {
                value = "'" + value + "'";
                result = database.<OCommandRequest> command(new OSQLSynchQuery<ODocument>(cmd + value)).execute();

                if (result == null || result.size() == 0)
                  // throw new
                  // OCommandExecutionException("Can't create link because the destination record was not found in class '"
                  // + destClass.getName() + "' and with the field '" + destField + "' equals to " + value);
                  value = null;
                else if (result.size() > 1)
                  throw new OCommandExecutionException("Can't create link because multiple records was found in class '"
                      + destClass.getName() + "' with value " + value + " in field '" + destField + "'");
                else {
                  target = result.get(0);
                  value = target;
                }
              }

              if (target != null && inverse) {
                // INVERSE RELATIONSHIP
                oldValue = target.field(linkName);

                if (oldValue != null) {
                  if (!multipleRelationship)
                    multipleRelationship = true;

                  Collection<ODocument> coll;
                  if (oldValue instanceof Collection) {
                    // ADD IT IN THE EXISTENT COLLECTION
                    coll = (Collection<ODocument>) oldValue;
                    target.setDirty();
                  } else {
                    // CREATE A NEW COLLECTION FOR BOTH
                    coll = new ArrayList<ODocument>(2);
                    target.field(linkName, coll);
                    coll.add((ODocument) oldValue);
                  }
                  coll.add(doc);
                } else {
                  target.field(linkName, doc);
                }
                target.save();

              } else {
                // SET THE REFERENCE
                doc.field(linkName, value);
                doc.save();
              }

              total++;
            }
          }
        }

        if (progressListener != null)
          progressListener.onProgress(this, currRecord, currRecord * 100f / totRecords);
      }

      if (total > 0) {
        if (inverse) {
          // REMOVE THE OLD PROPERTY IF ANY
          OProperty prop = destClass.getProperty(linkName);
          if (prop != null)
            destClass.dropProperty(linkName);

          // CREATE THE PROPERTY
          destClass.createProperty(linkName, multipleRelationship ? OType.LINKLIST : OType.LINK, sourceClass);

        } else {

          // REMOVE THE OLD PROPERTY IF ANY
          OProperty prop = sourceClass.getProperty(linkName);
View Full Code Here


      // int defCluster = fields.get("defaultCluster") != null ? Integer.parseInt(fields.get("defaultCluster")) : db
      // .getDefaultClusterId();

      try {
        final OClass cls = db.getMetadata().getSchema().createClass(fields.get("name"));
        db.getMetadata().getSchema().save();

        sendTextContent(iRequest, OHttpUtils.STATUS_OK_CODE, "OK", null, OHttpUtils.CONTENT_TEXT_PLAIN, "Class '" + rid
            + "' created successfully with id=" + cls.getId());

      } catch (Exception e) {
        sendTextContent(iRequest, OHttpUtils.STATUS_INTERNALERROR, "Error on creating the new class '" + rid + "': " + e, null,
            OHttpUtils.CONTENT_TEXT_PLAIN, "Error on creating the new class '" + rid + "': " + e);
      }
View Full Code Here

      // int defCluster = fields.get("defaultCluster") != null ? Integer.parseInt(fields.get("defaultCluster")) : db
      // .getDefaultClusterId();

      try {
        final OClass cls = db.getMetadata().getSchema().createClass(fields.get("name"));
        db.getMetadata().getSchema().save();

        sendTextContent(iRequest, OHttpUtils.STATUS_OK_CODE, "OK", null, OHttpUtils.CONTENT_TEXT_PLAIN, "Class '" + rid
            + "' created successfully with id=" + cls.getId());

      } catch (Exception e) {
        sendTextContent(iRequest, OHttpUtils.STATUS_INTERNALERROR, "Error on creating the new class '" + rid + "': " + e, null,
            OHttpUtils.CONTENT_TEXT_PLAIN, "Error on creating the new class '" + rid + "': " + e);
      }
View Full Code Here

    }
  }

  public boolean evaluate(final ODatabaseRecord iDatabase, final ORecordSchemaAware<?> iRecord) {
    if (targetClasses != null) {
      final OClass cls = targetClasses.keySet().iterator().next();
      // CHECK IF IT'S PART OF THE REQUESTED CLASS
      if (iRecord.getSchemaClass() == null || !iRecord.getSchemaClass().isSubClassOf(cls))
        // EXCLUDE IT
        return false;
    }
View Full Code Here

          // REGISTER AS CLASS
          if (targetClasses == null)
            targetClasses = new HashMap<OClass, String>();

          OClass cls = database.getMetadata().getSchema().getClass(subjectName);
          if (cls == null)
            throw new OCommandExecutionException("Class '" + subjectName + "' was not found");

          targetClasses.put(cls, alias);
        }
View Full Code Here

      if (o == null)
        continue;

      ODocument document = null;
      final OClass linkedClass;
      if (!(o instanceof ORecord<?>)) {
        final String fieldBound = OObjectSerializerHelper.getDocumentBoundField(o.getClass());
        if (fieldBound != null) {
          OObjectSerializerHelper.invokeCallback(o, null, OBeforeSerialization.class);
          document = (ODocument) OObjectSerializerHelper.getFieldValue(o, fieldBound);
View Full Code Here

        if (iLinkedRecord.getDatabase() == null && iParentRecord != null)
          // OVERWRITE THE DATABASE TO THE SAME OF THE PARENT ONE
          iLinkedRecord.setDatabase(iParentRecord.getDatabase());

        if (iLinkedRecord instanceof ODocument) {
          final OClass schemaClass = ((ODocument) iLinkedRecord).getSchemaClass();
          iLinkedRecord.getDatabase().save(iLinkedRecord,
              schemaClass != null ? iLinkedRecord.getDatabase().getClusterNameById(schemaClass.getDefaultClusterId()) : null);
        } else
          // STORE THE TRAVERSED OBJECT TO KNOW THE RECORD ID. CALL THIS VERSION TO AVOID CLEAR OF STACK IN THREAD-LOCAL
          iLinkedRecord.getDatabase().save(iLinkedRecord);

        iLinkedRecord.getDatabase().registerPojo(iLinkedRecord.getDatabase().getUserObjectByRecord(iLinkedRecord, null),
View Full Code Here

    if (prop != null)
      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);
View Full Code Here

      db = getProfiledDatabaseInstance(iRequest);

      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 OProperty prop = cls.createProperty(urlParts[3], OType.STRING);

      db.getMetadata().getSchema().save();

      sendTextContent(iRequest, OHttpUtils.STATUS_CREATED_CODE, OHttpUtils.STATUS_CREATED_DESCRIPTION, null,
          OHttpUtils.CONTENT_TEXT_PLAIN, prop.getId());
View Full Code Here

      System.out.println("id=" + o.field("id") + "\tname=" + o.field("name"));
    }
  }

  public static void create() {
    OClass account = database.getMetadata().getSchema()
        .createClass("Account", database.getStorage().addCluster("account", OStorage.CLUSTER_TYPE.PHYSICAL));
    account.createProperty("id", OType.LONG);
    account.createProperty("val1", OType.DOUBLE);
    account.createProperty("val2", OType.DOUBLE);
    account.createProperty("val3", OType.FLOAT);
    account.createProperty("val4", OType.SHORT);
    account.createProperty("val5", OType.STRING);
    account.createProperty("name", OType.STRING);

    database.getMetadata().getSchema().save();
  }
View Full Code Here

TOP

Related Classes of com.orientechnologies.orient.core.metadata.schema.OClass

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.