@Override
  protected Cursor loadCursor() throws Exception {
    // Log.i("RiaService", "chats base loads cursor");
    String messages = Cache.getTableInfo(MessageContainer.class).getTableName();
    String req =
        "SELECT "
            + BaseColumns._ID
            + ","
            + DbColumns.ThreadIdCol
            + ","
            + DbColumns.MsgBodyCol
            + ","
            + DbColumns.FromJidCol
            + ","
            + "MAX("
            + DbColumns.CreatedCol
            + ") AS "
            + DbColumns.CreatedCol
            + " FROM "
            + messages
            + " WHERE "
            + DbColumns.ChatTypeCol
            + "="
            + MessageContainer.CHAT_SIMPLE
            + " GROUP BY "
            + DbColumns.ThreadIdCol
            + " ORDER BY "
            + DbColumns.CreatedCol
            + " DESC ";
    /*boolean isMoved = resultCursor.moveToPosition(0);
    int count = resultCursor.getCount();*/

    return Cache.openDatabase().rawQuery(req, null);
  }
  Cursor retrieveDataFromRoster() {

    String resultRecords;
    Cursor resultCursor = null;

    BaseTabFragment.FragIds fragIds = BaseTabFragment.FragIds.values()[tabIdloader];

    switch (fragIds) {
      case CONTACTS_FRAGMENT:
        {
          String groupToExcludeRequest =
              new Select()
                  .from(RosterGroupModel.class)
                  .where(DbColumns.NameCol + "='" + SmackRosterManager.FIRST_SORTED_GROUP + "'")
                  .toSql();
          Cursor groupCursor = Cache.openDatabase().rawQuery(groupToExcludeRequest, null);
          List<RosterGroupModel> groupModelList =
              SQLiteUtils.processCursor(RosterGroupModel.class, groupCursor);
          groupCursor.close();
          long groupToExcludeId = 0;
          if (groupModelList != null && groupModelList.size() > 0) {
            groupToExcludeId = groupModelList.get(0).getId();
          }
          From from = new Select().from(RosterEntryModel.class);

          String sqlReqText = "";
          if (groupToExcludeId != 0) {
            sqlReqText += " RosterGroupModel != " + groupToExcludeId;
          }

          if (!TextUtils.isEmpty(title_to_search)) {
            if (!TextUtils.isEmpty(sqlReqText)) {
              sqlReqText += " and ";
            }
            sqlReqText += "Name LIKE '%" + title_to_search.toLowerCase(Locale.getDefault()) + "%'";
          }

          if (jidToExcludeList != null && jidToExcludeList.size() > 0) {
            for (String jid : jidToExcludeList) {
              sqlReqText += " and " + DbColumns.FromJidCol + " != '" + jid + "'";
            }
          }

          if (!TextUtils.isEmpty(sqlReqText)) {
            from.where(sqlReqText);
          }

          String queryResults = from.orderBy("Name ASC").toSql();
          resultCursor = Cache.openDatabase().rawQuery(queryResults, null);

          // listCursor = getContactsList(queryResults);
          // listCursor = fetchResultCursor(RosterEntryModel.class);
          // SQLiteUtils.processCursor(RosterEntryModel.class, listCursor);
        }
        break;
      case ROBOTS_FRAGMENT:
        {
          //  List<RosterEntry> rosterEntries =
          /*RosterGroupModel rosterGroupModel = new Select().from(RosterGroupModel.class)//.where("name = ?", getContext().getString(R.string.robots))
                  .orderBy("name ASC")
                  .executeSingle();
          if (rosterGroupModel != null) {
              listCursor = rosterGroupModel.items();
          }*/
          String groupTableName = Cache.getTableInfo(RosterGroupModel.class).getTableName();
          resultRecords =
              new Select()
                  .from(RosterEntryModel.class)
                  .where(
                      "RosterEntryModels.RosterGroupModel IN (SELECT _id"
                          + " FROM "
                          + groupTableName
                          + " WHERE name ='"
                          + SmackRosterManager.FIRST_SORTED_GROUP
                          + "')")
                  .orderBy("name ASC")
                  .toSql();

          //  tableName = Cache.getTableInfo(RosterGroupModel.class).getTableName();
          //  resultRecords = new Select().from(RosterGroupModel.class).where("name = ?",
          // getContext().getString(R.string.robots)).toSql();
          resultCursor = Cache.openDatabase().rawQuery(resultRecords, null);
        }
        break;
      case GROUPS_FRAGMENT:
        {
          /*List<RosterGroupModel> queryResults = new Select().from(RosterGroupModel.class)
                  .orderBy("name ASC").execute();

          for (RosterGroupModel group : queryResults) {
              group.items()
              Collections.sort(group.getEntries(), new EntrySortBasedOnName());
          }*/
          String tableName = Cache.getTableInfo(RosterGroupModel.class).getTableName();
          resultRecords = new Select("*").from(RosterGroupModel.class).toSql();
          resultCursor = Cache.openDatabase().rawQuery(resultRecords, null);
        }
        break;
    }

    return resultCursor;
  }
  @SuppressWarnings("unchecked")
  public static String createColumnDefinition(TableInfo tableInfo, Field field) {
    StringBuilder definition = new StringBuilder();

    Class<?> type = field.getType();
    final String name = tableInfo.getColumnName(field);
    final TypeSerializer typeSerializer = Cache.getParserForType(field.getType());
    final Column column = field.getAnnotation(Column.class);

    if (typeSerializer != null) {
      type = typeSerializer.getSerializedType();
    }

    if (TYPE_MAP.containsKey(type)) {
      definition.append(name);
      definition.append(" ");
      definition.append(TYPE_MAP.get(type).toString());
    } else if (ReflectionUtils.isModel(type)) {
      definition.append(name);
      definition.append(" ");
      definition.append(SQLiteType.INTEGER.toString());
    } else if (ReflectionUtils.isSubclassOf(type, Enum.class)) {
      definition.append(name);
      definition.append(" ");
      definition.append(SQLiteType.TEXT.toString());
    }

    if (!TextUtils.isEmpty(definition)) {
      if (column.length() > -1) {
        definition.append("(");
        definition.append(column.length());
        definition.append(")");
      }

      if (name.equals("Id")) {
        definition.append(" PRIMARY KEY AUTOINCREMENT");
      }

      if (column.notNull()) {
        definition.append(" NOT NULL ON CONFLICT ");
        definition.append(column.onNullConflict().toString());
      }

      if (column.unique()) {
        definition.append(" UNIQUE ON CONFLICT ");
        definition.append(column.onUniqueConflict().toString());
      }

      if (FOREIGN_KEYS_SUPPORTED && ReflectionUtils.isModel(type)) {
        definition.append(" REFERENCES ");
        definition.append(Cache.getTableInfo((Class<? extends Model>) type).getTableName());
        definition.append("(Id)");
        definition.append(" ON DELETE ");
        definition.append(column.onDelete().toString().replace("_", " "));
        definition.append(" ON UPDATE ");
        definition.append(column.onUpdate().toString().replace("_", " "));
      }
    } else {
      Log.e("No type mapping for: " + type.toString());
    }

    return definition.toString();
  }