/** Return all the instances of an entity who are owned by this instance */
 public <T extends ActiveRecordBase> List<T> getHasMany(Class<T> type)
     throws ActiveRecordException {
   return findByColumn(
       type,
       CamelNotationHelper.toSQLName(this.getClass().getSimpleName()),
       Long.toString(getID()));
 }
 public <T extends ActiveRecordBase> int getCount(Class<T> type) throws ActiveRecordException {
   T entity = newEntity(type);
   // We don't need to perform a full projection on the database because we only need to
   // get enough data for us to count
   String[] projection = new String[] {CamelNotationHelper.toSQLName(entity.getColumns()[0])};
   Cursor c = m_Database.query(entity.getTableName(), projection, null, null);
   int count = c.getCount();
   c.close();
   return count;
 }
 /** 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;
 }
  /**
   * Inflate this entity using the current row from the given cursor.
   *
   * @param cursor The cursor to get object data from.
   * @throws IllegalArgumentException
   * @throws IllegalAccessException
   * @throws InstantiationException
   */
  @SuppressWarnings("unchecked")
  void inflate(Cursor cursor) throws ActiveRecordException {
    HashMap<Field, Long> entities = new HashMap<Field, Long>();
    for (Field field : getColumnFields()) {
      try {
        String typeString = field.getType().getName();
        String colName = CamelNotationHelper.toSQLName(field.getName());
        if (typeString.equals("long")) {
          field.setLong(this, cursor.getLong(cursor.getColumnIndex(colName)));
        } else if (typeString.equals("java.lang.String")) {
          String val = cursor.getString(cursor.getColumnIndex(colName));
          field.set(this, val.equals("null") ? null : val);
        } else if (typeString.equals("double")) {
          field.setDouble(this, cursor.getDouble(cursor.getColumnIndex(colName)));
        } else if (typeString.equals("boolean")) {
          field.setBoolean(this, cursor.getString(cursor.getColumnIndex(colName)).equals("true"));
        } else if (typeString.equals("[B")) {
          field.set(this, cursor.getBlob(cursor.getColumnIndex(colName)));
        } else if (typeString.equals("int")) {
          field.setInt(this, cursor.getInt(cursor.getColumnIndex(colName)));
        } else if (typeString.equals("float")) {
          field.setFloat(this, cursor.getFloat(cursor.getColumnIndex(colName)));
        } else if (typeString.equals("short")) {
          field.setShort(this, cursor.getShort(cursor.getColumnIndex(colName)));
        } else if (typeString.equals("java.sql.Timestamp")) {
          long l = cursor.getLong(cursor.getColumnIndex(colName));
          field.set(this, new Timestamp(l));
        } else if (field.getType().getSuperclass() == ActiveRecordBase.class) {
          long id = cursor.getLong(cursor.getColumnIndex(colName));
          if (id > 0) entities.put(field, id);
          else field.set(this, null);
        } else if (field.getType() == List.class) {
          String classKey = field.getName();
          // hack
          classKey =
              classKey.replaceFirst(
                  "" + classKey.charAt(0), "" + Character.toUpperCase(classKey.charAt(0)));
          if (field.getName().endsWith("s")) {
            classKey = classKey.substring(0, classKey.length() - 1);
          }
          DatabaseBuilder ownBuilder = getDatabase().getOwnBuilder();
          Class fieldClass = ownBuilder.getClassForName(classKey);
          if (fieldClass != null) {
            List<ActiveRecordBase> ownObjects =
                (List<ActiveRecordBase>)
                    findByColumn(
                        fieldClass,
                        CamelNotationHelper.toSQLName(getTableName()),
                        Long.toString(getID()));
            field.set(this, ownObjects);
          }
        } else if (field.getAnnotation(ActiveRecordIgnoreAttribute.class) != null) {
          continue;
        } else
          throw new ActiveRecordException(
              field.getName()
                  + " of type "
                  + field.getType()
                  + " cannot be read from Sqlite3 database.");
      } catch (IllegalArgumentException e) {
        throw new ActiveRecordException(e.getLocalizedMessage());
      } catch (IllegalAccessException e) {
        throw new ActiveRecordException(e.getLocalizedMessage());
      }
    }

    s_EntitiesMap.set(this);
    for (Field f : entities.keySet()) {
      try {
        f.set(
            this, this.findByID((Class<? extends ActiveRecordBase>) f.getType(), entities.get(f)));
      } catch (SQLiteException e) {
        throw new ActiveRecordException(e.getLocalizedMessage());
      } catch (IllegalArgumentException e) {
        throw new ActiveRecordException(e.getLocalizedMessage());
      } catch (IllegalAccessException e) {
        throw new ActiveRecordException(e.getLocalizedMessage());
      }
    }
  }
 /**
  * Get the table name for this class.
  *
  * @return The table name for this class.
  */
 protected String getTableName() {
   return CamelNotationHelper.toSQLName(getClass().getSimpleName());
 }