Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sv.turismoinclusivo;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import com.google.android.gms.maps.model.LatLng;
- import java.util.Date;
- import java.util.Vector;
- /**
- * Created by Samuel Vasques on 11-02-2017.
- */
- public class DBController extends SQLiteOpenHelper {
- public DBController(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
- super(context, name, factory, version);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL("CREATE TABLE UTILIZADOR(" +
- "USERNAME TEXT PRIMARY KEY," +
- "PASSWORD TEXT," +
- "NOME TEXT," +
- "MORADA TEXT," +
- "DATA_NASC TEXT," +
- "DESABILIDADE TEXT NULL" +
- ");");
- db.execSQL("CREATE TABLE LOCAL(" +
- "ID_LOCAL INTEGER PRIMARY KEY AUTOINCREMENT," +
- "NOME TEXT," +
- "DESCRICAO TEXT," +
- "FOTO TEXT," +
- "LATITUDE TEXT," +
- "LONGITUDE TEXT," +
- "CATEGORIA TEXT," +
- "DESABILIDADE TEXT," +
- "USERNAME TEXT" +
- ");");
- db.execSQL("CREATE TABLE FAVORITO(" +
- "ID_FAVORITO INTEGER PRIMARY KEY AUTOINCREMENT," +
- "ID_LOCAL INTEGER," +
- "USERNAME TEXT," +
- "FOREIGN KEY(USERNAME) REFERENCES UTILIZADOR(USERNAME)" +
- ");");
- db.execSQL("CREATE TABLE DENUNCIA(" +
- "ID_DENUNCIA INTEGER PRIMARY KEY AUTOINCREMENT," +
- "ID_LOCAL INTEGER," +
- "USERNAME TEXT," +
- "FOREIGN KEY(USERNAME) REFERENCES UTILIZADOR(USERNAME)," +
- "FOREIGN KEY(ID_LOCAL) REFERENCES LOCAL(ID_LOCAL)" +
- ");");
- db.execSQL("CREATE TABLE OPINIAO(" +
- "ID_OPINIAO INTEGER PRIMARY KEY AUTOINCREMENT," +
- "COMENTARIO TEXT NULL," +
- "CLASSIFICACAO INTEGER NULL," +
- "DATA_OPINIAO TEXT," +
- "ID_LOCAL INTEGER," +
- "USERNAME TEXT," +
- "FOREIGN KEY(USERNAME) REFERENCES UTILIZADOR(USERNAME)," +
- "FOREIGN KEY(ID_LOCAL) REFERENCES LOCAL(ID_LOCAL)" +
- ");");
- // TODO criar o resto da BD e atualizar versões
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("DROP TABLE IF EXISTS UTILIZADOR");
- db.execSQL("DROP TABLE IF EXISTS LOCAL");
- db.execSQL("DROP TABLE IF EXISTS FAVORITO");
- db.execSQL("DROP TABLE IF EXISTS DENUNCIA");
- db.execSQL("DROP TABLE IF EXISTS OPINIAO");
- onCreate(db);
- }
- public long insertPlace(String nome, String descricao, String foto, String lat, String lon,
- String categoria, String desabilidade, String username){
- ContentValues contentValues = new ContentValues();
- contentValues.put("NOME", nome);
- contentValues.put("DESCRICAO", descricao);
- contentValues.put("FOTO", foto);
- contentValues.put("LATITUDE", lat);
- contentValues.put("LONGITUDE", lon);
- contentValues.put("CATEGORIA", categoria);
- contentValues.put("DESABILIDADE", desabilidade);
- contentValues.put("USERNAME", username);
- return getWritableDatabase().insert("LOCAL", "", contentValues);
- }
- public void insertFavorite(int idLocal, String username){
- ContentValues contentValues = new ContentValues();
- contentValues.put("ID_LOCAL", idLocal);
- contentValues.put("USERNAME", username);
- getWritableDatabase().insert("FAVORITO", "", contentValues);
- }
- public void insertComplain(int idLocal, String username){
- // Não pode fazer denuncia se está nos favoritos
- if(isAlreadyFavorite(idLocal, username))
- removeFavorite(idLocal, username);
- ContentValues contentValues = new ContentValues();
- contentValues.put("ID_LOCAL", idLocal);
- contentValues.put("USERNAME", username);
- getWritableDatabase().insert("DENUNCIA", "", contentValues);
- if(getComplaintCount(idLocal) >= PlaceActivity.COMPLAINT_LIMIT)
- removePlace(idLocal);
- }
- public void insertOpinion(String comentario, int classificacao, String data, int idLocal, String username){
- ContentValues contentValues = new ContentValues();
- contentValues.put("COMENTARIO", comentario);
- contentValues.put("CLASSIFICACAO", classificacao);
- contentValues.put("DATA_OPINIAO", data);
- contentValues.put("ID_LOCAL", idLocal);
- contentValues.put("USERNAME", username);
- getWritableDatabase().insert("OPINIAO", "", contentValues);
- }
- public void updateOpinion(String comentario, int classificacao, String data, int idLocal, String username){
- ContentValues contentValues = new ContentValues();
- contentValues.put("COMENTARIO", comentario);
- contentValues.put("CLASSIFICACAO", classificacao);
- contentValues.put("DATA_OPINIAO", data);
- contentValues.put("ID_LOCAL", idLocal);
- contentValues.put("USERNAME", username);
- getWritableDatabase().update("OPINIAO", contentValues, "USERNAME = ? AND ID_LOCAL = ?",
- new String[]{username, String.valueOf(idLocal)});
- }
- public void insertOrUpdateOpinion(String comentario, int classificacao, String data, int idLocal, String username){
- Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM OPINIAO WHERE ID_LOCAL = " +
- idLocal + " AND USERNAME = '" + username + "'", null);
- if(cursor.getCount() == 0)
- insertOpinion(comentario, classificacao, data, idLocal, username);
- else
- updateOpinion(comentario, classificacao, data, idLocal, username);
- }
- public boolean existsComplaint(int idLocal, String username){
- Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM DENUNCIA WHERE ID_LOCAL = " +
- idLocal + " AND USERNAME = '" + username + "'", null);
- return cursor.getCount() > 0;
- }
- public int getComplaintCount(int idLocal){
- Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM DENUNCIA WHERE ID_LOCAL = " +
- idLocal, null);
- return cursor.getCount();
- }
- public boolean isAlreadyFavorite(int idLocal, String username){
- Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM FAVORITO WHERE USERNAME = '" +
- username + "' AND ID_LOCAL = " + idLocal, null);
- return cursor.getCount() != 0;
- }
- public boolean isComplaint(int idLocal, String username){
- Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM DENUNCIA WHERE USERNAME = '" +
- username + "' AND ID_LOCAL = " + idLocal, null);
- return cursor.getCount() != 0;
- }
- public void insertUser(String username, String password, String nome, String morada, String data, String desabilidade){
- ContentValues contentValues = new ContentValues();
- contentValues.put("USERNAME", username);
- contentValues.put("PASSWORD", password);
- contentValues.put("NOME", nome);
- contentValues.put("MORADA", morada);
- contentValues.put("DATA_NASC", data.replace("-",""));
- if(desabilidade != null)
- contentValues.put("DESABILIDADE", desabilidade);
- getWritableDatabase().insert("UTILIZADOR", "", contentValues);
- }
- public boolean userExists(String username){
- Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM UTILIZADOR WHERE USERNAME = '" + username + "'", null);
- return cursor.getCount() != 0;
- }
- public void updateUser(String username, String nome, String morada, String data, String desabilidade){
- ContentValues contentValues = new ContentValues();
- contentValues.put("NOME", nome);
- contentValues.put("MORADA", morada);
- contentValues.put("DATA_NASC", data);
- contentValues.put("DESABILIDADE", desabilidade);
- getWritableDatabase().update("UTILIZADOR", contentValues, "USERNAME = ?", new String[]{username});
- }
- public void setUserDisabilities(String username, String desabilities){
- ContentValues contentValues = new ContentValues();
- contentValues.put("DESABILIDADE", desabilities);
- getWritableDatabase().update("UTILIZADOR", contentValues, "USERNAME = ?", new String[]{username});
- }
- public boolean credentialsMatch(String username, String password){
- Cursor cursor = getReadableDatabase().rawQuery("SELECT USERNAME, PASSWORD FROM UTILIZADOR WHERE " +
- "USERNAME = '" + username + "' AND PASSWORD = '" + password + "'", null);
- return cursor.getCount() != 0;
- }
- public String[] getUserInfo(String username){
- Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM UTILIZADOR WHERE USERNAME = '" + username + "'", null);
- cursor.moveToFirst();
- String[] utilizador = new String[cursor.getColumnCount()];
- for (int i = 0; i < cursor.getColumnCount(); i++)
- utilizador[i] = cursor.getString(i);
- return utilizador;
- }
- public float getPlaceAverageScore(int idLocal){
- Cursor cursor = getReadableDatabase().rawQuery("SELECT CLASSIFICACAO FROM OPINIAO WHERE " +
- "ID_LOCAL = " + idLocal, null);
- int sum = 0;
- while(cursor.moveToNext())
- if(cursor.getInt(0) > 0)
- sum += cursor.getInt(0);
- return (float) sum / cursor.getCount();
- }
- public String[] getPlaceComments(int idLocal){
- Cursor cursor = getReadableDatabase().rawQuery("SELECT OPINIAO.COMENTARIO, UTILIZADOR.NOME " +
- "FROM OPINIAO, UTILIZADOR WHERE OPINIAO.ID_LOCAL = " + idLocal +
- " AND OPINIAO.USERNAME = UTILIZADOR.USERNAME ORDER BY DATA_OPINIAO ASC", null);
- String[] str = new String[cursor.getCount()];
- while(cursor.moveToNext())
- if(cursor.getString(0).length() > 0)
- str[cursor.getPosition()] = cursor.getString(1) + ": " + cursor.getString(0);
- return str;
- }
- public String getUserDisabilities(String username){
- Cursor cursor = getReadableDatabase().rawQuery("SELECT DESABILITIES FROM UTILIZADOR WHERE " +
- "USERNAME = '" + username + "'", null);
- cursor.moveToFirst();
- return cursor.getString(0);
- }
- public String[] getPlaceInfo(int id){
- Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM LOCAL WHERE ID_LOCAL = " + id, null);
- cursor.moveToFirst();
- String[] local = new String[cursor.getColumnCount()];
- for (int i = 0; i < cursor.getColumnCount(); i++)
- local[i] = cursor.getString(i);
- return local;
- }
- public String[] getPlaceNames(){
- Cursor cursor = getReadableDatabase().rawQuery("SELECT NOME FROM LOCAL", null);
- String[] local = new String[cursor.getCount()];
- while(cursor.moveToNext())
- local[cursor.getPosition()] = cursor.getString(0);
- return local;
- }
- public String[] getPlaceNames(String nome, String categoria, String desabilidade){
- /*String query = "SELECT LOCAL.ID_LOCAL, LOCAL.NOME FROM LOCAL, DENUNCIA WHERE " +
- "DENUNCIA.ID_LOCAL != LOCAL.ID_LOCAL AND DENUNCIA.USERNAME != '" + UserTemp.getCurrentUser() + "'";*/
- /*String query = "SELECT LOCAL.ID_LOCAL, LOCAL.NOME FROM LOCAL, DENUNCIA WHERE " +
- "LOCAL.ID_LOCAL NOT IN (SELECT DENUNCIA.ID_LOCAL FROM DENUNCIA WHERE " +
- "DENUNCIA.USERNAME = '" + UserTemp.getCurrentUser() + "')";*/
- String query = "SELECT LOCAL.ID_LOCAL, LOCAL.NOME FROM LOCAL";
- if(categoria.charAt(0) == '<')
- categoria = "";
- if(desabilidade.charAt(0) == '<')
- desabilidade = "";
- if(nome.isEmpty() && categoria.isEmpty() && !desabilidade.isEmpty())
- query += " WHERE DESABILIDADE LIKE '%" + desabilidade + "%'";
- else if(nome.isEmpty() && !categoria.isEmpty() && desabilidade.isEmpty())
- query += " WHERE CATEGORIA LIKE '%" + categoria + "%'";
- else if(!nome.isEmpty() && categoria.isEmpty() && desabilidade.isEmpty())
- query += " WHERE NOME LIKE '%" + nome + "%'";
- else if(nome.isEmpty() && !categoria.isEmpty() && !desabilidade.isEmpty())
- query += " WHERE CATEGORIA LIKE '%" + categoria + "%' AND DESABILIDADE LIKE '%" + desabilidade + "%'";
- else if(!nome.isEmpty() && !categoria.isEmpty() && desabilidade.isEmpty())
- query += " WHERE CATEGORIA LIKE '%" + categoria + "%' AND NOME LIKE '%" + nome + "%'";
- else if(!nome.isEmpty() && categoria.isEmpty() && !desabilidade.isEmpty())
- query += " WHERE DESABILIDADE LIKE '%" + desabilidade +"%' AND NOME LIKE '%" + nome + "%'";
- else if(!nome.isEmpty() && !categoria.isEmpty() && !desabilidade.isEmpty()) {
- query += " WHERE DESABILIDADE LIKE '%" + desabilidade + "%' AND NOME LIKE '%" + nome + "%' " +
- "AND CATEGORIA LIKE '%" + categoria + "%'";
- }
- query += " EXCEPT " +
- "SELECT LOCAL.ID_LOCAL, LOCAL.NOME FROM LOCAL,DENUNCIA WHERE " +
- "DENUNCIA.ID_LOCAL = LOCAL.ID_LOCAL " +
- "AND DENUNCIA.USERNAME = '" + UserTemp.getCurrentUser() + "'";
- Cursor cursor = getReadableDatabase().rawQuery(query, null);
- String[] local = new String[cursor.getCount()];
- while(cursor.moveToNext())
- local[cursor.getPosition()] = cursor.getString(0) + ":" + cursor.getString(1);
- return local;
- }
- public LatLng[] getLocations(){
- Cursor cursor = getReadableDatabase().rawQuery("SELECT LATITUDE, LONGITUDE FROM LOCAL ", null);
- LatLng[] local = new LatLng[cursor.getCount()];
- while(cursor.moveToNext())
- local[cursor.getPosition()] = new LatLng(Double.parseDouble(cursor.getString(0)), Double.parseDouble(cursor.getString(1)));
- return local;
- }
- public LatLng getLatLngLocal(int idLocal){
- Cursor cursor = getReadableDatabase().rawQuery("SELECT LATITUDE, LONGITUDE FROM LOCAL " +
- "WHERE ID_LOCAL = " + idLocal, null);
- cursor.moveToFirst();
- return new LatLng(Double.parseDouble(cursor.getString(0)), Double.parseDouble(cursor.getString(1)));
- }
- public String[] getFavoritePlaceNames(String username){
- Cursor cursor = getReadableDatabase().rawQuery("SELECT LOCAL.ID_LOCAL, LOCAL.NOME FROM LOCAL, FAVORITO " +
- "WHERE LOCAL.ID_LOCAL = FAVORITO.ID_LOCAL " +
- "AND FAVORITO.USERNAME = '" + username + "'", null);
- String[] local = new String[cursor.getCount()];
- while(cursor.moveToNext())
- local[cursor.getPosition()] = cursor.getString(0) + ":" + cursor.getString(1);
- return local;
- }
- public void removePlace(int idLocal){
- SQLiteDatabase db = getReadableDatabase();
- db.delete("DENUNCIA", "ID_LOCAL = " + idLocal, null);
- db.delete("OPINIAO", "ID_LOCAL = " + idLocal, null);
- db.delete("FAVORITO", "ID_LOCAL = " + idLocal, null);
- db.delete("LOCAL", "ID_LOCAL = " + idLocal, null);
- }
- public void removeFavorite(int idLocal, String username){
- SQLiteDatabase db = getReadableDatabase();
- db.delete("FAVORITO", "ID_LOCAL = ? AND USERNAME = ?",
- new String[]{String.valueOf(idLocal), username});
- }
- public void removePlaceComments(int idLocal){
- SQLiteDatabase db = getReadableDatabase();
- db.delete("OPINIAO", "ID_LOCAL = ?",
- new String[]{String.valueOf(idLocal)});
- }
- public void deleteAllPlaces(){
- getWritableDatabase().delete("LOCAL", null, null);
- }
- public String getPath(){
- return getReadableDatabase().getPath();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement