public boolean checkMissingTomesIcons(Handler handler) {
    Tome tome;

    Log.d(Global.getLogTag(MyDbAdaptor.class), "checkMissingTomesIcons");
    Cursor cursor =
        database.rawQuery(MySQLiteOpenHelper.SELECT_ALL_MISSING_WITHOUT_ICON_RQST, null);
    if (cursor.moveToFirst()) {
      while (!cursor.isAfterLast()) {
        tome = new Tome();
        tome.isMissingTome = true;
        tome.setId(cursor.getInt(cursor.getColumnIndex(MySQLiteOpenHelper.COL_MISSING_ID)));
        tome.setNumber(cursor.getInt(cursor.getColumnIndex(MySQLiteOpenHelper.COL_MISSING_NUMBER)));
        tome.setSerieId(
            cursor.getInt(cursor.getColumnIndex(MySQLiteOpenHelper.COL_MISSING_SERIE_ID)));
        tome.setTomePageUrl(
            cursor.getString(cursor.getColumnIndex(MySQLiteOpenHelper.COL_MISSING_PAGEURL)));
        tome.setIconUrl(
            cursor.getString(cursor.getColumnIndex(MySQLiteOpenHelper.COL_MISSING_ICONURL)));
        tome.setIcon(cursor.getBlob(cursor.getColumnIndex(MySQLiteOpenHelper.COL_MISSING_ICON)));
        ServerConnector.getMissingTomeIcon(handler, tome);
        cursor.moveToNext();
      }
    }
    cursor.close();
    return true;
  }
 private void checkTomeIcons(Handler handler, int sid) {
   Cursor cursor =
       database.rawQuery(
           MySQLiteOpenHelper.SELECT_UNICON_TOME_FROM_SERIE_ID_RQST,
           new String[] {Integer.toString(sid)});
   if (cursor.moveToFirst()) {
     Tome tome;
     while (!cursor.isAfterLast()) {
       tome = new Tome();
       tome.setId(cursor.getInt(cursor.getColumnIndex(MySQLiteOpenHelper.COL_TOME_ID)));
       tome.setNumber(cursor.getInt(cursor.getColumnIndex(MySQLiteOpenHelper.COL_TOME_NUMBER)));
       tome.setEditionId(
           cursor.getInt(cursor.getColumnIndex(MySQLiteOpenHelper.COL_TOME_EDITION_ID)));
       tome.setIconUrl(
           cursor.getString(cursor.getColumnIndex(MySQLiteOpenHelper.COL_TOME_ICONURL)));
       tome.setSerieId(sid);
       ServerConnector.getTomeIcon(handler, tome);
       cursor.moveToNext();
     }
   }
   cursor.close();
 }
  public boolean insertSerie(Handler handler, Serie serie) {
    if (serie == null) return false;
    Hashtable<String, String> editions = serie.getEditions();
    Log.d(
        Global.getLogTag(MyDbAdaptor.class),
        "insertSerie " + serie.getName() + "(" + serie.getId() + ") - " + serie.getTomeCount());
    Enumeration<String> e = editions.keys();
    while (e.hasMoreElements()) {
      String key = e.nextElement();
      String value = editions.get(key);
      Log.d(Global.getLogTag(MyDbAdaptor.class), "id_edition = " + key + " count=" + value);
    }

    boolean result = false;
    ContentValues values = new ContentValues();
    values.put(MySQLiteOpenHelper.COL_SERIES_ID, serie.getId());
    values.put(MySQLiteOpenHelper.COL_SERIES_NAME, serie.getName());
    values.put(MySQLiteOpenHelper.COL_SERIES_STATUS, serie.getStatus().getValue());

    // Find row
    Cursor cursor =
        database.query(
            MySQLiteOpenHelper.TABLE_SERIES,
            null,
            new StringBuilder().append(MySQLiteOpenHelper.COL_SERIES_ID).append("=?").toString(),
            new String[] {Integer.toString(serie.getId())},
            null,
            null,
            null);

    Enumeration<String> key = serie.getEditions().keys();
    int tome_count;
    String id_edition;
    if (cursor.getCount() == 0) {
      result = (database.insert(MySQLiteOpenHelper.TABLE_SERIES, null, values) >= 0);
      while (key.hasMoreElements()) {
        id_edition = key.nextElement();
        tome_count = Integer.parseInt(serie.getEditions().get(id_edition));
        if (tome_count != getTomesCountFromEditionId(id_edition)) {
          // sync editions tomes
          ServerConnector.syncEdition(handler, id_edition, serie.getId());
        }
      }
    } else {
      cursor.moveToFirst();
      int tomeCount = 0;
      if (serie.getTomeCount() != (tomeCount = getTomesCountFromSerieId(serie.getId()))) {
        while (key.hasMoreElements()) {
          id_edition = key.nextElement();
          tome_count = Integer.parseInt(serie.getEditions().get(id_edition));
          if (tome_count != getTomesCountFromEditionId(id_edition)) {
            // sync editions tomes
            ServerConnector.syncEdition(handler, id_edition, serie.getId());
          }
        }
      } else if (tomeCount > 0) checkTomeIcons(handler, serie.getId());

      Serie saved = loadSerieFromCursor(cursor);
      if (!(result = saved.equals(serie)))
        result =
            (database.update(
                    MySQLiteOpenHelper.TABLE_SERIES,
                    values,
                    new StringBuilder()
                        .append(MySQLiteOpenHelper.COL_SERIES_ID)
                        .append("=?")
                        .toString(),
                    new String[] {Integer.toString(serie.getId())})
                >= 0);
    }
    cursor.close();
    return result;
  }
  public boolean insertTome(Handler handler, Tome tome) {
    if (tome == null) return false;
    Log.d(
        Global.getLogTag(MyDbAdaptor.class),
        "insertTome "
            + tome.getNumber()
            + "("
            + tome.getId()
            + ") - "
            + tome.getSerieId()
            + " url="
            + tome.getIconUrl()
            + " icon="
            + tome.getIcon());

    boolean result = false;
    ContentValues values = new ContentValues();
    if (tome.isMissingTome) {
      values.put(MySQLiteOpenHelper.COL_MISSING_SERIE_ID, tome.getSerieId());
      values.put(MySQLiteOpenHelper.COL_MISSING_NUMBER, tome.getNumber());
      values.put(MySQLiteOpenHelper.COL_MISSING_ICONURL, tome.getIconUrl());
      if (tome.getIconUrl() == null) values.putNull(MySQLiteOpenHelper.COL_MISSING_ICON);
    } else {
      values.put(MySQLiteOpenHelper.COL_TOME_ID, tome.getId());
      values.put(MySQLiteOpenHelper.COL_TOME_SERIE_ID, tome.getSerieId());
      values.put(MySQLiteOpenHelper.COL_TOME_EDITION_ID, tome.getEditionId());
      values.put(MySQLiteOpenHelper.COL_TOME_NUMBER, tome.getNumber());
      values.put(MySQLiteOpenHelper.COL_TOME_PAGEURL, tome.getTomePageUrl());
      values.put(MySQLiteOpenHelper.COL_TOME_ICONURL, tome.getIconUrl());
      if (tome.getIconUrl() == null) values.putNull(MySQLiteOpenHelper.COL_TOME_ICON);
    }
    // Find row
    Cursor cursor = null;
    if (tome.isMissingTome)
      cursor =
          database.query(
              MySQLiteOpenHelper.TABLE_MISSING,
              null,
              new StringBuilder().append(MySQLiteOpenHelper.COL_MISSING_ID).append("=?").toString(),
              new String[] {Integer.toString(tome.getId())},
              null,
              null,
              null);
    else
      cursor =
          database.query(
              MySQLiteOpenHelper.TABLE_TOMES,
              null,
              new StringBuilder().append(MySQLiteOpenHelper.COL_TOME_ID).append("=?").toString(),
              new String[] {Integer.toString(tome.getId())},
              null,
              null,
              null);

    if (cursor.getCount() == 0) {
      if (tome.isMissingTome)
        result = (database.insert(MySQLiteOpenHelper.TABLE_MISSING, null, values) >= 0);
      else result = (database.insert(MySQLiteOpenHelper.TABLE_TOMES, null, values) >= 0);
      if (tome.getIconUrl() != null) ServerConnector.getTomeIcon(handler, tome);
    } else {
      cursor.moveToFirst();
      Tome saved = loadTomeFromCursor(cursor);
      Log.d(
          Global.getLogTag(MyDbAdaptor.class),
          "saved url=" + saved.getIconUrl() + " icon=" + saved.getIcon());
      if (tome.getIconUrl() != null) {
        if (saved.getIconUrl() != null
            && tome.getIconUrl().equals(saved.getIconUrl())
            && saved.getIcon() != null) {
          if (tome.isMissingTome) values.remove(MySQLiteOpenHelper.COL_MISSING_ICON);
          else values.remove(MySQLiteOpenHelper.COL_TOME_ICON);
        } else {
          ServerConnector.getTomeIcon(handler, tome);
        }
      }

      if (!tome.equals(saved)) {
        if (tome.isMissingTome)
          result =
              (database.update(
                      MySQLiteOpenHelper.TABLE_MISSING,
                      values,
                      new StringBuilder()
                          .append(MySQLiteOpenHelper.COL_MISSING_ID)
                          .append("=?")
                          .toString(),
                      new String[] {Integer.toString(tome.getId())})
                  >= 0);
        else
          result =
              (database.update(
                      MySQLiteOpenHelper.TABLE_TOMES,
                      values,
                      new StringBuilder()
                          .append(MySQLiteOpenHelper.COL_TOME_ID)
                          .append("=?")
                          .toString(),
                      new String[] {Integer.toString(tome.getId())})
                  >= 0);
      }
    }
    cursor.close();
    return result;
  }