/**
   * Add a form to to the rapidandroid_form table, inserting new fields as well. <br>
   * <br>
   * Upon form insert, the formdata_[prefix] table will be generated.
   *
   * @param f
   * @param fields
   * @param crform
   */
  public static void addFormToDatabase(Form f) {
    boolean newFormInserted;
    ContentValues typecv = new ContentValues();

    if (f.getFormId() != -1) {
      typecv.put(BaseColumns._ID, f.getFormId());
    }
    typecv.put(RapidSmsDBConstants.Form.FORMNAME, f.getFormName());
    typecv.put(RapidSmsDBConstants.Form.PARSEMETHOD, "simpleregex"); // eww,
    // hacky
    // magic
    // string
    typecv.put(RapidSmsDBConstants.Form.PREFIX, f.getPrefix());
    typecv.put(RapidSmsDBConstants.Form.DESCRIPTION, f.getDescription());

    Uri insertedFormUri =
        mContext.getContentResolver().insert(RapidSmsDBConstants.Form.CONTENT_URI, typecv);
    Log.d("dimagi", "****** Inserted form into db: " + insertedFormUri);

    int newFormId = Integer.valueOf(insertedFormUri.getPathSegments().get(1)).intValue();
    f.setFormId(newFormId);

    Field[] fields = f.getFields();
    Log.d("dimagi", "****** Begin fields loop: " + fields.length);
    for (int j = 0; j < fields.length; j++) {
      Field thefield = fields[j];
      Log.d("dimagi", "******** Iterating through fields: " + thefield.getName());
      Uri fieldUri = RapidSmsDBConstants.Field.CONTENT_URI;
      StringBuilder where = new StringBuilder();
      where.append("name='" + thefield.getName() + "' AND ");
      where.append("form_id=" + newFormId);

      Log.i("ModelTranslator", "where clause: " + where.toString());

      Cursor crfield =
          mContext.getContentResolver().query(fieldUri, null, where.toString(), null, null);
      if (crfield.getCount() == 0) {

        Log.i("ModelTranslator", "building field cvs");

        ContentValues fieldcv = new ContentValues();

        if (thefield.getFieldId() != -1) {
          fieldcv.put(BaseColumns._ID, thefield.getFieldId());
        }
        Log.i("ModelTranslator", "name: " + thefield.getName());
        fieldcv.put(RapidSmsDBConstants.Field.NAME, thefield.getName());

        Log.i("ModelTranslator", "form: " + f.getFormId());
        fieldcv.put(RapidSmsDBConstants.Field.FORM, f.getFormId());

        Log.i("ModelTranslator", "prompt: " + thefield.getDescription());
        fieldcv.put(RapidSmsDBConstants.Field.PROMPT, thefield.getDescription());

        Log.i("ModelTranslator", "sequence: " + thefield.getSequenceId());
        fieldcv.put(RapidSmsDBConstants.Field.SEQUENCE, thefield.getSequenceId());

        Log.i("ModelTranslator", "fieldtype: " + ((thefield.getFieldType())));
        Log.i(
            "ModelTranslator",
            "fieldtype: " + ((SimpleFieldType) (thefield.getFieldType())).getId());
        fieldcv.put(
            RapidSmsDBConstants.Field.FIELDTYPE,
            ((SimpleFieldType) (thefield.getFieldType())).getId());

        Log.i("ModelTranslator", "inserting field");
        Uri insertedFieldUri =
            mContext.getContentResolver().insert(RapidSmsDBConstants.Field.CONTENT_URI, fieldcv);
        Log.d("dimagi", "********** Inserted Field into db: " + insertedFieldUri);
      }
      crfield.close();
    }

    // ok, so form and fields have been inserted. Now we need to generate
    // the form table if it doesn't exist yet.
    generateFormTable(f);

    SmsParseReceiver.initFormCache();
  }