gt:             throw new IOException(e.getLocalizedMessage());
context: /*
   * Geopaparazzi - Digital field mapping on Android based devices
   * Copyright (C) 2016  HydroloGIS (www.hydrologis.com)
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   */
  package eu.geopaparazzi.core.database;
  
  import android.content.ContentValues;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteStatement;
  import android.graphics.drawable.Drawable;
  import android.util.Log;
  
  import org.mapsforge.android.maps.overlay.OverlayItem;
  import org.mapsforge.core.model.GeoPoint;
  
  import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;
  
  import eu.geopaparazzi.library.database.GPLog;
  import eu.geopaparazzi.library.database.IImagesDbHelper;
  import eu.geopaparazzi.library.database.Image;
  import eu.geopaparazzi.library.images.ImageUtilities;
  import eu.geopaparazzi.core.GeopaparazziApplication;
  
  import static eu.geopaparazzi.core.database.TableDescriptions.ImageDataTableFields;
  import static eu.geopaparazzi.core.database.TableDescriptions.ImageTableFields;
  import static eu.geopaparazzi.core.database.TableDescriptions.TABLE_IMAGES;
  import static eu.geopaparazzi.core.database.TableDescriptions.TABLE_IMAGE_DATA;
  
  /**
   * Data access object for images.
   *
   * @author Andrea Antonello (www.hydrologis.com)
   */
  @SuppressWarnings("nls")
  public class DaoImages implements IImagesDbHelper {
  
      /**
       * Create the image tables.
       *
       * @throws IOException if something goes wrong.
       */
      public static void createTables() throws IOException {
          StringBuilder sB = new StringBuilder();
          sB.append("CREATE TABLE ");
          sB.append(TABLE_IMAGES);
          sB.append(" (");
          sB.append(ImageTableFields.COLUMN_ID.getFieldName());
          sB.append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
          sB.append(ImageTableFields.COLUMN_LON.getFieldName()).append(" REAL NOT NULL, ");
          sB.append(ImageTableFields.COLUMN_LAT.getFieldName()).append(" REAL NOT NULL,");
          sB.append(ImageTableFields.COLUMN_ALTIM.getFieldName()).append(" REAL NOT NULL,");
          sB.append(ImageTableFields.COLUMN_AZIM.getFieldName()).append(" REAL NOT NULL,");
          sB.append(ImageTableFields.COLUMN_IMAGEDATA_ID.getFieldName());
          sB.append(" INTEGER NOT NULL ");
          sB.append("CONSTRAINT " + ImageTableFields.COLUMN_IMAGEDATA_ID.getFieldName() + " REFERENCES ");
          sB.append(TABLE_IMAGE_DATA);
          sB.append("(");
          sB.append(ImageDataTableFields.COLUMN_ID);
          sB.append(") ON DELETE CASCADE,");
          sB.append(ImageTableFields.COLUMN_TS.getFieldName()).append(" DATE NOT NULL,");
          sB.append(ImageTableFields.COLUMN_TEXT.getFieldName()).append(" TEXT NOT NULL,");
          sB.append(ImageTableFields.COLUMN_NOTE_ID.getFieldName()).append(" INTEGER,");
          sB.append(ImageTableFields.COLUMN_ISDIRTY.getFieldName()).append(" INTEGER NOT NULL");
          sB.append(");");
          String CREATE_TABLE_IMAGES = sB.toString();
  
          sB = new StringBuilder();
          sB.append("CREATE INDEX images_ts_idx ON ");
          sB.append(TABLE_IMAGES);
          sB.append(" ( ");
          sB.append(ImageTableFields.COLUMN_TS.getFieldName());
          sB.append(" );");
          String CREATE_INDEX_IMAGES_TS = sB.toString();
  
          sB = new StringBuilder();
          sB.append("CREATE INDEX images_x_by_y_idx ON ");
          sB.append(TABLE_IMAGES);
          sB.append(" ( ");
          sB.append(ImageTableFields.COLUMN_LON.getFieldName());
          sB.append(", ");
          sB.append(ImageTableFields.COLUMN_LAT.getFieldName());
          sB.append(" );");
          String CREATE_INDEX_IMAGES_X_BY_Y = sB.toString();
  
          sB = new StringBuilder();
          sB.append("CREATE INDEX images_noteid_idx ON ");
          sB.append(TABLE_IMAGES);
          sB.append(" ( ");
          sB.append(ImageTableFields.COLUMN_NOTE_ID.getFieldName());
          sB.append(" );");
          String CREATE_INDEX_IMAGES_NOTEID = sB.toString();
  
          sB = new StringBuilder();
          sB.append("CREATE INDEX images_isdirty_idx ON ");
          sB.append(TABLE_IMAGES);
          sB.append(" ( ");
          sB.append(ImageTableFields.COLUMN_ISDIRTY.getFieldName());
          sB.append(" );");
          String CREATE_INDEX_IMAGES_ISDIRTY = sB.toString();
  
          sB = new StringBuilder();
          sB.append("CREATE TABLE ");
          sB.append(TABLE_IMAGE_DATA);
          sB.append(" (");
          sB.append(ImageDataTableFields.COLUMN_ID.getFieldName());
          sB.append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
          sB.append(ImageDataTableFields.COLUMN_IMAGE.getFieldName()).append(" BLOB NOT NULL,");
          sB.append(ImageDataTableFields.COLUMN_THUMBNAIL.getFieldName()).append(" BLOB NOT NULL");
          sB.append(");");
          String CREATE_TABLE_IMAGEDATA = sB.toString();
  
          SQLiteDatabase sqliteDatabase = GeopaparazziApplication.getInstance().getDatabase();
          if (GPLog.LOG_HEAVY)
              Log.i("DAOIMAGES", "Create the images tables.");
  
          sqliteDatabase.beginTransaction();
          try {
              sqliteDatabase.execSQL(CREATE_TABLE_IMAGES);
              sqliteDatabase.execSQL(CREATE_INDEX_IMAGES_TS);
              sqliteDatabase.execSQL(CREATE_INDEX_IMAGES_X_BY_Y);
              sqliteDatabase.execSQL(CREATE_INDEX_IMAGES_NOTEID);
              sqliteDatabase.execSQL(CREATE_INDEX_IMAGES_ISDIRTY);
  
              sqliteDatabase.execSQL(CREATE_TABLE_IMAGEDATA);
  
              sqliteDatabase.setTransactionSuccessful();
          } catch (Exception e) {
              Log.e("DAOIMAGES", e.getLocalizedMessage(), e);
              throw new IOException(e);
          } finally {
              sqliteDatabase.endTransaction();
          }
      }
  
  
      public long addImage(double lon, double lat, double altim, double azim, long timestamp, String text, byte[] image, byte[] thumb, long noteId)
              throws IOException {
          SQLiteDatabase sqliteDatabase = GeopaparazziApplication.getInstance().getDatabase();
          sqliteDatabase.beginTransaction();
          try {
              // first insert image data
              ContentValues imageDataValues = new ContentValues();
              imageDataValues.put(ImageDataTableFields.COLUMN_IMAGE.getFieldName(), image);
              imageDataValues.put(ImageDataTableFields.COLUMN_THUMBNAIL.getFieldName(), thumb);
              long imageDataId = sqliteDatabase.insertOrThrow(TABLE_IMAGE_DATA, null, imageDataValues);
  
              // then insert the image properties and reference to the image itself
              ContentValues values = new ContentValues();
              values.put(ImageTableFields.COLUMN_LON.getFieldName(), lon);
              values.put(ImageTableFields.COLUMN_LAT.getFieldName(), lat);
              values.put(ImageTableFields.COLUMN_ALTIM.getFieldName(), altim);
              values.put(ImageTableFields.COLUMN_TS.getFieldName(), timestamp);
              values.put(ImageTableFields.COLUMN_TEXT.getFieldName(), text);
              values.put(ImageTableFields.COLUMN_IMAGEDATA_ID.getFieldName(), imageDataId);
              values.put(ImageTableFields.COLUMN_AZIM.getFieldName(), azim);
              values.put(ImageTableFields.COLUMN_ISDIRTY.getFieldName(), 1);
              values.put(ImageTableFields.COLUMN_NOTE_ID.getFieldName(), noteId);
              long imageId = sqliteDatabase.insertOrThrow(TABLE_IMAGES, null, values);
  
              sqliteDatabase.setTransactionSuccessful();
  
              return imageId;
          } catch (Exception e) {
              GPLog.error("DAOIMAGES", e.getLocalizedMessage(), e);
              throw new IOException(e.getLocalizedMessage());
          } finally {
              sqliteDatabase.endTransaction();
          }
      }
  
      /**
       * Deletes images from the db.
       *
       * @param ids the ids of the images to remove.
       * @throws IOException if something goes wrong.
       */
      public static void deleteImages(long... ids) throws IOException {
          SQLiteDatabase sqliteDatabase = GeopaparazziApplication.getInstance().getDatabase();
          sqliteDatabase.beginTransaction();
          try {
              String asColumnsToReturn[] = {ImageTableFields.COLUMN_IMAGEDATA_ID.getFieldName()};
              String imageIdsWhereStr = "";
              int count = 0;
              for (long id : ids) {
                  if (count > 0) {
                      imageIdsWhereStr = imageIdsWhereStr + " || ";
                  }
                  imageIdsWhereStr = imageIdsWhereStr + ImageTableFields.COLUMN_ID.getFieldName() + " = " + id;
                  count++;
              }
              Cursor c = sqliteDatabase.query(TABLE_IMAGES, asColumnsToReturn, imageIdsWhereStr, null, null, null, null);
              c.moveToFirst();
              String imageDataIdsWhereStr = "";
              count = 0;
              while (!c.isAfterLast()) {
                  long imageDataId = c.getLong(0);
                  c.moveToNext();
                  if (count > 0) {
                      imageDataIdsWhereStr = imageDataIdsWhereStr + " || ";
                  }
                  imageDataIdsWhereStr = imageDataIdsWhereStr + ImageDataTableFields.COLUMN_ID.getFieldName() + " = " + imageDataId;
                  count++;
  
              }
              c.close();
  
  
              // delete images
              String query = "delete from " + TABLE_IMAGES + " where " + imageIdsWhereStr;
              SQLiteStatement deleteStmt = sqliteDatabase.compileStatement(query);
              deleteStmt.execute();
  
              // delete images data
              query = "delete from " + TABLE_IMAGE_DATA + " where " + imageDataIdsWhereStr;
              deleteStmt = sqliteDatabase.compileStatement(query);
              deleteStmt.execute();
  
              sqliteDatabase.setTransactionSuccessful();
          } catch (Exception e) {
              GPLog.error("DAOIMAGES", e.getLocalizedMessage(), e);
  