コード例 #1
0
  /**
   * Saves this entity to the database, inserts or updates as needed.
   *
   * @return number of rows affected on success, -1 on failure
   * @throws ActiveRecordException
   */
  public long save() throws ActiveRecordException {
    long r = -1;

    if (m_Database == null) throw new ActiveRecordException("Set database first");

    if (null == findByID(this.getClass(), _id)) {
      if (insert() > 0) {
        r = 1;
      }
    } else {
      r = update();
    }
    for (Field column : getColumnFields()) {
      if (column.getType() == List.class) {
        try {
          List<ActiveRecordBase> listObjects = (List<ActiveRecordBase>) column.get(this);
          Field itemOwner = null;
          if (listObjects != null && listObjects.size() > 0) {
            for (Field childColumn : listObjects.get(0).getColumnFields()) {
              // Try to find the parent column and set it
              // automatically to save time from those setters
              // and getters
              if (childColumn.getType() == this.getClass()) {
                itemOwner = childColumn;
                break;
              }
            }
            for (ActiveRecordBase item : listObjects) {
              if (item.getDatabase() == null) {
                item.setDatabase(getDatabase());
              }
              if (itemOwner != null) {
                itemOwner.set(item, this);
              }
              item.save();
            }
          }
        } catch (IllegalAccessException e) {
          e.printStackTrace();
        } catch (IllegalArgumentException e) {
          e.printStackTrace();
        }
      }
    }
    s_EntitiesMap.set(this);

    return r;
  }
コード例 #2
0
 /** Creates the ContentValues to be used for data insertion */
 public ContentValues createContentValues() throws ActiveRecordException {
   List<Field> columns = _id > 0 ? getColumnFields() : getColumnFieldsWithoutID();
   ContentValues values = new ContentValues(columns.size());
   for (Field column : columns) {
     Annotation ignore = column.getAnnotation(ActiveRecordIgnoreAttribute.class);
     Annotation manyToMany = column.getAnnotation(ManyToManyRelation.class);
     if (manyToMany != null) {
       // TODO: Fixme
       continue;
     }
     try {
       if (column.getType().getSuperclass() == ActiveRecordBase.class) {
         ActiveRecordBase col = (ActiveRecordBase) column.get(this);
         // This is another entry and if it doesn't already have an ID
         // it means we should persist *it* before we try to grab it's
         // value
         if (col != null && col.getID() < 0) {
           if (col.getDatabase() == null) {
             col.setDatabase(getDatabase());
           }
           col.save();
         }
         String columnName = CamelNotationHelper.toSQLName(column.getName());
         String value = String.valueOf(col != null ? col.getID() : 0);
         values.put(columnName, value);
       } else if (column.getType() == List.class) {
       } else if (ignore != null) {
         continue;
       } else {
         values.put(
             CamelNotationHelper.toSQLName(column.getName()), String.valueOf(column.get(this)));
       }
     } catch (IllegalArgumentException e) {
       throw new ActiveRecordException("No column " + column.getName());
     } catch (IllegalAccessException e) {
       throw new ActiveRecordException("No column " + column.getName());
     }
   }
   return values;
 }
コード例 #3
0
 /**
  * Creates and opens new ActiveRecord object instance and underlying database. Returned
  * ActiveRecord object is fully ready for use.
  *
  * @param ctx
  * @param dbName
  * @return
  * @throws ActiveRecordException
  */
 public static ActiveRecordBase open(Context ctx, String dbName, int dbVersion)
     throws ActiveRecordException {
   Database db = Database.createInstance(ctx, dbName, dbVersion);
   db.open();
   return ActiveRecordBase.createInstance(db);
 }