/** * Save the list of pistas into local database. * * <p>To be used on result of a remote search; this method 'mark' the object as remote. * * <p>The object is updated only if it exists in local database and was not locally changed. And * it is inserted if it not exist in local database. * * @param pistas */ public long bulkSave(List<Pista> pistas) { SQLiteDatabase db = dbHelper.getWritableDatabase(); try { long count = 0; for (Pista pista : pistas) { pista.setRemote(true); long rowID = -1; if (existsPista(db, pista.getUuid())) { if (!wasLocallyChanged(db, pista.getUuid())) { rowID = update(db, pista); } } else { rowID = insert(db, pista); } if (rowID >= 0) { count++; } else { Log.e(TAG, "Error inserting pista: " + pista.getNome()); } } return count; } catch (Exception ex) { // must not happening Log.e(TAG, "Error on bulk save of pistas.", ex); } finally { dbHelper.close(db); } return -1; }
private long update(SQLiteDatabase db, Pista pista) { db.beginTransaction(); try { // save pista int row = db.update( TABLE, getContentValues(pista), Columns._UUID.getColumnName() + " = ? ", new String[] {pista.getUuid()}); // save local pista.getLocal().setUuidLocalizavel(pista.getUuid()); localDAO.save(db, pista.getLocal()); // save horario pista.getHorario().setUuidLocalizavel(pista.getUuid()); horarioDAO.save(db, pista.getHorario()); // save esportes localizavelEsporteDAO.save(db, pista.getUuid(), pista.getEsportes()); db.setTransactionSuccessful(); return row; } catch (Exception ex) { Log.e(TAG, "Error updating pista.", ex); } finally { db.endTransaction(); } return -1; }
/** * Merges a remote pista into local database, overridign the local pista. * * <p>WARNING: To be used on result of a remote insert/update; this method 'mark' the object as * remote. * * @param pista The local pista to be override. * @param pistaRemote The remote pista to be saved. * @return The saved pista or null if something goes wrong. */ public Pista merge(Pista pista, Pista pistaRemote) { SQLiteDatabase db = dbHelper.getWritableDatabase(); try { // delete local pista delete(db, pista.getUuid()); // insert remote pista pistaRemote.setRemote(true); pistaRemote.setChanged(false); insert(db, pistaRemote); return getPista(pistaRemote.getUuid()); } catch (Exception ex) { Log.e(TAG, "Error merging pistas.", ex); } finally { dbHelper.close(db); } return null; }
/** * Save a pista into local database. * * @param pista The pista to be saved. * @return The saved pista or null if something goes wrong. */ public Pista save(Pista pista) { SQLiteDatabase db = dbHelper.getWritableDatabase(); try { if (pista.getUuid() == null) { pista.setUuid(UUID.randomUUID().toString()); pista.setRemote(false); insert(db, pista); } else { update(db, pista); } return getPista(pista.getUuid()); } catch (Exception ex) { Log.e(TAG, "Error saving pista.", ex); } finally { dbHelper.close(db); } return null; }
private long insert(SQLiteDatabase db, Pista pista) { db.beginTransaction(); try { // save pista long rowID = db.insertOrThrow(TABLE, null, getContentValues(pista)); // save local pista.getLocal().setUuidLocalizavel(pista.getUuid()); localDAO.save(db, pista.getLocal()); // save horario pista.getHorario().setUuidLocalizavel(pista.getUuid()); horarioDAO.save(db, pista.getHorario()); // save esportes localizavelEsporteDAO.save(db, pista.getUuid(), pista.getEsportes()); db.setTransactionSuccessful(); return rowID; } catch (Exception ex) { Log.e(TAG, "Error inserting pista.", ex); } finally { db.endTransaction(); } return -1; }
private ContentValues getContentValues(Pista pista) { ContentValues values = new ContentValues(); values.put(Columns._UUID.getColumnName(), pista.getUuid()); values.put(Columns._NOME.getColumnName(), pista.getNome()); values.put(Columns._DESCRICAO.getColumnName(), pista.getDescricao()); values.put(Columns._WEBSITE.getColumnName(), pista.getWebsite()); values.put(Columns._FACEBOOK.getColumnName(), pista.getFacebook()); values.put(Columns._VIDEO.getColumnName(), pista.getVideo()); values.put(Columns._FOTO.getColumnName(), pista.getFoto()); values.put(Columns._FUNDO.getColumnName(), pista.getFundo()); values.put(Columns._RESPONSAVEL_UUID.getColumnName(), pista.getResponsavel()); values.put(Columns._REMOTE.getColumnName(), pista.isRemote()); values.put(Columns._CHANGED.getColumnName(), pista.isChanged()); return values; }
private List<Pista> getPistas( String uuid, String name, String estado, String esporte, String responsavelUuid) { SQLiteDatabase db = dbHelper.getReadableDatabase(); List<Pista> pistas = new ArrayList<>(); try { SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); queryBuilder.setTables( TABLE + " INNER JOIN " + LocalDAO.TABLE + " ON " + Columns._UUID.getColumnNameWithTable() + " = " + LocalDAO.Columns._UUID.getColumnNameWithTable() + " LEFT OUTER JOIN " + HorarioDAO.TABLE + " ON " + Columns._UUID.getColumnNameWithTable() + " = " + HorarioDAO.Columns._UUID.getColumnNameWithTable() + " LEFT OUTER JOIN " + LocalizavelEsporteDAO.TABLE + " ON " + Columns._UUID.getColumnNameWithTable() + " = " + LocalizavelEsporteDAO.Columns._UUID.getColumnNameWithTable() + " INNER JOIN " + EsporteDAO.TABLE + " ON " + LocalizavelEsporteDAO.Columns._ESPORTE_ID.getColumnNameWithTable() + " = " + EsporteDAO.Columns._ID.getColumnNameWithTable()); StringBuilder selection = new StringBuilder("1 = 1"); List<String> selectionArgs = new ArrayList<>(); if (uuid != null && !uuid.isEmpty()) { selection.append(" AND ").append(Columns._UUID.getColumnNameWithTable()).append(" = ? "); selectionArgs.add(uuid); } if (name != null && !name.isEmpty()) { selection.append(" AND ").append(Columns._NOME.getColumnNameWithTable()).append(" LIKE ? "); selectionArgs.add(name + "%"); } if (estado != null && !estado.isEmpty()) { selection .append(" AND ") .append(LocalDAO.Columns._ESTADO.getColumnNameWithTable()) .append(" = ? "); selectionArgs.add(estado); } if (esporte != null && !esporte.isEmpty()) { selection .append(" AND ") .append(EsporteDAO.Columns._CATEGORIA.getColumnNameWithTable()) .append(" = ? "); selectionArgs.add(esporte); } if (responsavelUuid != null && !responsavelUuid.isEmpty()) { selection .append(" AND ") .append(Columns._RESPONSAVEL_UUID.getColumnNameWithTable()) .append(" = ? "); selectionArgs.add(responsavelUuid); } Cursor cursor = queryBuilder.query( db, getColumnsProjectionWithAlias( Columns.class, LocalDAO.Columns.class, HorarioDAO.Columns.class), selection.toString(), selectionArgs.toArray(new String[selectionArgs.size()]), getGroupBy(Columns.class, LocalDAO.Columns.class, HorarioDAO.Columns.class), null, null); if (cursor.moveToFirst()) { do { Pista pista = getPista(cursor, true); pista.setLocal(localDAO.getLocal(cursor, true)); pista.setHorario(horarioDAO.getHorario(cursor, true)); pista.setEsportes(localizavelEsporteDAO.getEsportes(db, pista.getUuid())); pistas.add(pista); } while (cursor.moveToNext()); } cursor.close(); } catch (Exception ex) { Log.e(TAG, "Error getting pistas.", ex); } finally { dbHelper.close(db); } return pistas; }