示例#1
0
  /**
   * For this method only is the object to be modify. It is necessary to execute obtain previously
   * to recover the proper object.
   *
   * @param Object
   * @return True if the modification was successfull, False in the other case
   */
  public boolean modify(Object object) {
    if (!this.manager.checkOptimisticLock(this, object)) {
      throw new OptimisticLockException();
    }
    boolean value = false;
    try {
      boolean commitValue = this.commit;
      if (this.commit || this.autoCommit) {
        conex.initTransaction();
        this.commit = false;
      }

      ArrayList<String> manys = this.manager.getRef().isMany2Many(object.getClass());
      if (!manys.isEmpty()) {
        if (this.manager.deleteMany2Many(this, object, manys)) {
          this.manager.cleanStack();
          value = this.save(object);
        }
      } else {
        value = this.modifyProcess(object);
      }

      this.commit = commitValue;
      if (this.commit || this.autoCommit) conex.confirmTransaction();
    } catch (SQLException e) {
      this.commit = true;
    }

    return value;
  }
示例#2
0
  /**
   * Modify in the Database the objects in the array received as parameter.
   *
   * @param An Object that implements java.util.Collection with the items
   * @return An ArrayList with the index of each item if collection if from a Reference Collection
   *     or an ArrayList with the number of item that couldnt be updated if this is not a Collection
   *     Reference
   */
  public ArrayList<Integer> modifyAll(Collection array) {
    ArrayList<Integer> results = new ArrayList<Integer>();
    try {
      boolean commitValue = this.commit;
      if (this.commit || this.autoCommit) {
        conex.initTransaction();
        this.commit = false;
      }

      Object objects[] = array.toArray();
      if (!this.collection) {
        for (int i = 0; i < objects.length; i++) {
          if (!this.modify(objects[i])) {
            results.add(i);
            break;
          }
        }
      } else {
        this.collection = false;
        for (Object obj : objects) {
          int index = this.modifyGetIndex(obj);
          results.add(index);
        }
        this.collection = true;
      }

      this.commit = commitValue;
      if (this.commit || this.autoCommit) conex.confirmTransaction();
    } catch (SQLException e) {
      this.commit = true;
    }

    return results;
  }
示例#3
0
  /**
   * This method can receive an object to determine the type of object that is going to be manage
   * and a String with the query. (Faster than 'obtain', this method doesnt has to parse the
   * expression)
   *
   * @param Object, String
   * @return True if the object could be obtained, False in the other case.
   */
  public boolean obtainWhere(Object object, String sql) {
    ArrayList array = this.manager.entity2Array(this, object, EntityManager.OPERATION.OBTAIN);

    try {
      if (this.collectionHasName) {
        this.rs = conex.selectWhere(this.manager.getNameCollection(), sql);
      } else {
        this.rs = conex.selectWhere(((String) array.get(0)), sql);
      }
      this.arrayRs.push(this.rs);

      if (rs.next() && (this.manager.result2Object(this, object, rs) != null)) {
        return true;
      }
    } catch (Exception e) {
      return false;
    } finally {
      if (!this.startObtainAll) {
        this.arrayRs.pop();
        if (!this.arrayRs.isEmpty()) {
          this.rs = this.arrayRs.peek();
        }
      } else {
        this.startObtainAll = false;
      }
    }

    return false;
  }
示例#4
0
  /**
   * Store the object in the Database (create the Table if not exist) and return the value of the
   * PrimaryKey.
   *
   * @param Object
   * @return The value of the PrimaryKey, or -1 if the object couldn't be saved.
   */
  public int saveGetIndex(Object object) {
    int index = -1;
    try {
      boolean commitValue = this.commit;
      if (this.commit || this.autoCommit) {
        conex.initTransaction();
        this.commit = false;
      }

      ArrayList array = this.manager.entity2Array(this, object, EntityManager.OPERATION.SAVE);

      index = this.saveProcess(array, object);
      this.commit = commitValue;
      if (this.commit || this.autoCommit) {
        conex.confirmTransaction();
        if (this.conex.getConnection().getAutoCommit()) {
          this.conex.getConnection().setAutoCommit(false);
        }
      }
    } catch (SQLException e) {
      this.commit = true;
    }

    return index;
  }
示例#5
0
  protected int saveProcess(ArrayList array, Object object) {
    int index = -1;
    String table = "";
    int i = 2;
    try {
      if (this.forceTable) {
        table = tableForcedName;
        array.set(0, table);
      } else {
        table = String.valueOf(array.get(0));
      }

      conex.openBlock(table);

      int size = array.size() - 1;
      for (; i < size; i++) {
        Object[] obj = (Object[]) array.get(i);

        String column = (String) obj[0];
        Object value = obj[1];

        conex.insertField(column, value);
      }

      index = conex.closeBlock();
      if (this.collection) {
        this.saveMany2Many(((String) array.get(0)), true, index);
      }
      this.manager.updateCache(object.getClass(), this);
    } catch (SQLException ex) {
      int value = -1;
      try {
        this.conex.getConnection().setAutoCommit(true);
        if (!this.checkTableExist(table)) {
          if (this.createTable(object, array.toArray())) {
            value = this.saveProcess(array, object);
          }
        } else {
          if (this.addColumn(table, object, array, i)) {
            value = this.saveProcess(array, object);
          }
        }
      } catch (Exception e) {
      }
      return value;
    } catch (Exception e) {
      conex.connectMySQL();
      try {
        conex.cancelTransaction();
      } catch (Exception ex) {
        return -1;
      }
    }

    return index;
  }
示例#6
0
  /**
   * Execute the SQL query received as parameter
   *
   * @param object
   * @return
   */
  public boolean executeQuery(String statement) {
    try {
      conex.executeQuery(statement);

      return true;
    } catch (Exception e) {
      return false;
    }
  }
示例#7
0
  /**
   * Delete the object received as parameter from the Database
   *
   * @param Object
   * @return True if the operation complete successfully, False in the other case.
   */
  public boolean delete(Object object) {

    try {
      boolean commitValue = this.commit;
      if (this.commit || this.autoCommit) {
        conex.initTransaction();
        this.commit = false;
      }

      ArrayList array = this.manager.entity2Array(this, object, EntityManager.OPERATION.DELETE);

      conex.deleteRows(((String) array.get(0)), ((String) array.get(array.size() - 1)));

      this.manager.deleteParent(this, object);

      // Obtain the attributes from this object that are collections
      // and delete their relations in the Relational Table.
      ArrayList<String[]> strings = this.manager.getCollectionsTableForDelete(object, this);
      int index = (Integer) ((Object[]) array.get(1))[1];
      for (String[] s : strings) {
        if (this.checkTableExist(s[0])) {
          this.executeQuery("DELETE FROM " + s[0] + " WHERE base=" + index);
        } else if (this.checkTableExist(s[1])) {
          this.executeQuery("DELETE FROM " + s[1] + " WHERE related=" + index);
        }
      }

      this.commit = commitValue;
      if (this.commit || this.autoCommit) conex.confirmTransaction();
    } catch (Exception e) {
      this.commit = true;
      try {
        conex.cancelTransaction();
      } catch (Exception ex) {
        return false;
      }
      return false;
    }

    return true;
  }
示例#8
0
  protected boolean modifyProcess(Object object) {
    ArrayList array = this.manager.entity2Array(this, object, EntityManager.OPERATION.MODIFY);

    try {
      this.rs =
          conex.updateField(((String) array.get(0)), String.valueOf(array.get(array.size() - 1)));
      rs.next();

      do {
        int size = array.size() - 1;
        for (int i = 1; i < size; i++) {
          Object[] obj = (Object[]) array.get(i);

          String column = (String) obj[0];
          Object value = obj[1];

          rs.updateObject(column, value);
          rs.updateRow();
        }
      } while (rs.next());

      int index =
          Integer.parseInt(
              String.valueOf(array.get(array.size() - 1))
                  .substring(String.valueOf(array.get(array.size() - 1)).indexOf("=") + 1));

      if (this.collection) {
        this.saveMany2Many(((String) array.get(0)), false, index);
      }
      this.manager.updateCache(object.getClass(), this);
    } catch (Exception e) {
      try {
        conex.cancelTransaction();
      } catch (Exception ex) {
        return false;
      }
      return false;
    }

    return true;
  }
示例#9
0
  /**
   * Save all the items in the Collection in the Database
   *
   * @param An Object that implement java.util.Collection
   * @return An ArrayList with the number of elements inserted or an ArrayList with each item index
   *     if the array received represent a Collection Reference.
   */
  public ArrayList<Integer> saveAll(Collection array) {
    ArrayList<Integer> results = new ArrayList<Integer>();
    try {
      boolean commitValue = this.commit;
      if (this.commit || this.autoCommit) {
        conex.initTransaction();
        this.commit = false;
      }

      Object objects[] = array.toArray();
      if (!this.collection) {
        for (int i = 0; i < objects.length; i++) {
          if (!this.save(objects[i])) {
            results.add(i); // throw exception
            break;
          }
        }
      } else {
        this.collection = false;
        for (Object obj : objects) {
          results.add(this.saveGetIndex(obj));
        }
        this.collection = true;
      }

      this.commit = commitValue;
      if (this.commit || this.autoCommit) {
        conex.confirmTransaction();
        if (this.conex.getConnection().getAutoCommit()) {
          this.conex.getConnection().setAutoCommit(false);
        }
      }
    } catch (SQLException e) {
      this.commit = true;
    }

    return results;
  }
示例#10
0
  /**
   * This Method return an Object[] containing String[] in each item, that represent the result of
   * the specified query as a Matrix of Strings
   *
   * @param SQL Query [String]
   * @param Number of Columns involved [int]
   * @return Object[] or null
   */
  public Object[] obtainTable(Object... prop) {
    try {
      this.rs = conex.select((String) prop[0]);
      rs.next();
      int cols = 0;
      String[] names = new String[0];
      boolean hash = false;
      if (prop.length > 1 && prop[1].getClass().getSimpleName().equalsIgnoreCase("Integer")) {
        cols = (Integer) prop[1];
      } else if (prop.length > 1
          && prop[1].getClass().getSimpleName().equalsIgnoreCase("Boolean")) {
        if ((Boolean) prop[1]) {
          String sql = ((String) prop[0]).toLowerCase();
          names = sql.substring(sql.indexOf("select ") + 7, sql.indexOf(" from")).split(",");
          cols = names.length;
          hash = true;
        } else {
          String sql = ((String) prop[0]).toLowerCase();
          cols = sql.substring(sql.indexOf("select ") + 7, sql.indexOf(" from")).split(",").length;
        }
      } else {
        throw new InvalidParameterException();
      }

      if (hash) {
        ArrayList<Hashtable<String, Object>> array = new ArrayList<Hashtable<String, Object>>();
        do {
          Hashtable<String, Object> table = new Hashtable<String, Object>(cols);
          for (int i = 1; i < cols + 1; i++) {
            table.put(names[i - 1].trim(), rs.getObject(i));
          }
          array.add(table);
        } while (rs.next());
        return array.toArray();
      } else {
        ArrayList<String[]> array = new ArrayList<String[]>();
        do {
          String[] objs = new String[cols];
          for (int i = 1; i < cols + 1; i++) {
            objs[i - 1] = rs.getObject(i).toString();
          }
          array.add(objs);
        } while (rs.next());
        return array.toArray();
      }

    } catch (Exception e) {
      return null;
    }
  }
示例#11
0
  /**
   * Return the Object from the Database that represent the [completely] specified query
   *
   * @param Object
   * @param SQL Query [String]
   * @return True if the object could be restored, False in the other case.
   */
  public boolean obtainSelect(Object object, String sql) {
    try {
      this.rs = conex.select(sql);
      this.arrayRs.push(this.rs);

      if (rs.next() && (this.manager.result2Object(this, object, rs) != null)) {
        return true;
      }
    } catch (Exception e) {
      return false;
    } finally {
      if (!this.startObtainAll) {
        this.arrayRs.pop();
        if (!this.arrayRs.isEmpty()) {
          this.rs = this.arrayRs.peek();
        }
      } else {
        this.startObtainAll = false;
      }
    }

    return false;
  }