Advertisement
Guest User

DB Controller

a guest
Jun 15th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.17 KB | None | 0 0
  1. package sv.turismoinclusivo;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteOpenHelper;
  8.  
  9. import com.google.android.gms.maps.model.LatLng;
  10.  
  11. import java.util.Date;
  12. import java.util.Vector;
  13.  
  14. public class DBController extends SQLiteOpenHelper {
  15.     public DBController(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
  16.         super(context, name, factory, version);
  17.     }
  18.  
  19.     @Override
  20.     public void onCreate(SQLiteDatabase db) {
  21.         db.execSQL("CREATE TABLE UTILIZADOR(" +
  22.                 "USERNAME TEXT PRIMARY KEY," +
  23.                 "PASSWORD TEXT," +
  24.                 "NOME TEXT," +
  25.                 "MORADA TEXT," +
  26.                 "DATA_NASC TEXT," +
  27.                 "DESABILIDADE TEXT NULL" +
  28.                 ");");
  29.         db.execSQL("CREATE TABLE LOCAL(" +
  30.                 "ID_LOCAL INTEGER PRIMARY KEY AUTOINCREMENT," +
  31.                 "NOME TEXT," +
  32.                 "DESCRICAO TEXT," +
  33.                 "FOTO TEXT," +
  34.                 "LATITUDE TEXT," +
  35.                 "LONGITUDE TEXT," +
  36.                 "CATEGORIA TEXT," +
  37.                 "DESABILIDADE TEXT," +
  38.                 "USERNAME TEXT" +
  39.                 ");");
  40.         db.execSQL("CREATE TABLE FAVORITO(" +
  41.                 "ID_FAVORITO INTEGER PRIMARY KEY AUTOINCREMENT," +
  42.                 "ID_LOCAL INTEGER," +
  43.                 "USERNAME TEXT," +
  44.                 "FOREIGN KEY(USERNAME) REFERENCES UTILIZADOR(USERNAME)" +
  45.                 ");");
  46.         db.execSQL("CREATE TABLE DENUNCIA(" +
  47.                 "ID_DENUNCIA INTEGER PRIMARY KEY AUTOINCREMENT," +
  48.                 "ID_LOCAL INTEGER," +
  49.                 "USERNAME TEXT," +
  50.                 "FOREIGN KEY(USERNAME) REFERENCES UTILIZADOR(USERNAME)," +
  51.                 "FOREIGN KEY(ID_LOCAL) REFERENCES LOCAL(ID_LOCAL)" +
  52.                 ");");
  53.         db.execSQL("CREATE TABLE OPINIAO(" +
  54.                 "ID_OPINIAO INTEGER PRIMARY KEY AUTOINCREMENT," +
  55.                 "COMENTARIO TEXT NULL," +
  56.                 "CLASSIFICACAO INTEGER NULL," +
  57.                 "DATA_OPINIAO TEXT," +
  58.                 "ID_LOCAL INTEGER," +
  59.                 "USERNAME TEXT," +
  60.                 "FOREIGN KEY(USERNAME) REFERENCES UTILIZADOR(USERNAME)," +
  61.                 "FOREIGN KEY(ID_LOCAL) REFERENCES LOCAL(ID_LOCAL)" +
  62.                 ");");
  63.         // TODO criar o resto da BD e atualizar versões
  64.     }
  65.  
  66.     @Override
  67.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  68.         db.execSQL("DROP TABLE IF EXISTS UTILIZADOR");
  69.         db.execSQL("DROP TABLE IF EXISTS LOCAL");
  70.         db.execSQL("DROP TABLE IF EXISTS FAVORITO");
  71.         db.execSQL("DROP TABLE IF EXISTS DENUNCIA");
  72.         db.execSQL("DROP TABLE IF EXISTS OPINIAO");
  73.         onCreate(db);
  74.     }
  75.  
  76.     public long insertPlace(String nome, String descricao, String foto, String lat, String lon,
  77.                             String categoria, String desabilidade, String username){
  78.         ContentValues contentValues = new ContentValues();
  79.         contentValues.put("NOME", nome);
  80.         contentValues.put("DESCRICAO", descricao);
  81.         contentValues.put("FOTO", foto);
  82.         contentValues.put("LATITUDE", lat);
  83.         contentValues.put("LONGITUDE", lon);
  84.         contentValues.put("CATEGORIA", categoria);
  85.         contentValues.put("DESABILIDADE", desabilidade);
  86.         contentValues.put("USERNAME", username);
  87.         return getWritableDatabase().insert("LOCAL", "", contentValues);
  88.     }
  89.  
  90.     public void insertFavorite(int idLocal, String username){
  91.         ContentValues contentValues = new ContentValues();
  92.         contentValues.put("ID_LOCAL", idLocal);
  93.         contentValues.put("USERNAME", username);
  94.         getWritableDatabase().insert("FAVORITO", "", contentValues);
  95.     }
  96.  
  97.     public void insertComplain(int idLocal, String username){
  98.         // Não pode fazer denuncia se está nos favoritos
  99.         if(isAlreadyFavorite(idLocal, username))
  100.             removeFavorite(idLocal, username);
  101.  
  102.         ContentValues contentValues = new ContentValues();
  103.         contentValues.put("ID_LOCAL", idLocal);
  104.         contentValues.put("USERNAME", username);
  105.         getWritableDatabase().insert("DENUNCIA", "", contentValues);
  106.         if(getComplaintCount(idLocal) >= PlaceActivity.COMPLAINT_LIMIT)
  107.             removePlace(idLocal);
  108.     }
  109.  
  110.     public void insertOpinion(String comentario, int classificacao, String data, int idLocal, String username){
  111.         ContentValues contentValues = new ContentValues();
  112.         contentValues.put("COMENTARIO", comentario);
  113.         contentValues.put("CLASSIFICACAO", classificacao);
  114.         contentValues.put("DATA_OPINIAO", data);
  115.         contentValues.put("ID_LOCAL", idLocal);
  116.         contentValues.put("USERNAME", username);
  117.         getWritableDatabase().insert("OPINIAO", "", contentValues);
  118.     }
  119.  
  120.     public void updateOpinion(String comentario, int classificacao, String data, int idLocal, String username){
  121.         ContentValues contentValues = new ContentValues();
  122.         contentValues.put("COMENTARIO", comentario);
  123.         contentValues.put("CLASSIFICACAO", classificacao);
  124.         contentValues.put("DATA_OPINIAO", data);
  125.         contentValues.put("ID_LOCAL", idLocal);
  126.         contentValues.put("USERNAME", username);
  127.         getWritableDatabase().update("OPINIAO", contentValues, "USERNAME = ? AND ID_LOCAL = ?",
  128.                 new String[]{username, String.valueOf(idLocal)});
  129.     }
  130.  
  131.     public void insertOrUpdateOpinion(String comentario, int classificacao, String data, int idLocal, String username){
  132.         Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM OPINIAO WHERE ID_LOCAL = " +
  133.                 idLocal + " AND USERNAME = '" + username + "'", null);
  134.         if(cursor.getCount() == 0)
  135.             insertOpinion(comentario, classificacao, data, idLocal, username);
  136.         else
  137.             updateOpinion(comentario, classificacao, data, idLocal, username);
  138.     }
  139.  
  140.     public boolean existsComplaint(int idLocal, String username){
  141.         Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM DENUNCIA WHERE ID_LOCAL = " +
  142.                 idLocal + " AND USERNAME = '" + username + "'", null);
  143.         return cursor.getCount() > 0;
  144.     }
  145.  
  146.     public int getComplaintCount(int idLocal){
  147.         Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM DENUNCIA WHERE ID_LOCAL = " +
  148.                 idLocal, null);
  149.         return cursor.getCount();
  150.     }
  151.  
  152.     public boolean isAlreadyFavorite(int idLocal, String username){
  153.         Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM FAVORITO WHERE USERNAME = '" +
  154.                 username + "' AND ID_LOCAL = " + idLocal, null);
  155.         return cursor.getCount() != 0;
  156.     }
  157.  
  158.     public boolean isComplaint(int idLocal, String username){
  159.         Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM DENUNCIA WHERE USERNAME = '" +
  160.                 username + "' AND ID_LOCAL = " + idLocal, null);
  161.         return cursor.getCount() != 0;
  162.     }
  163.  
  164.     public void insertUser(String username, String password, String nome, String morada, String data, String desabilidade){
  165.         ContentValues contentValues = new ContentValues();
  166.         contentValues.put("USERNAME", username);
  167.         contentValues.put("PASSWORD", password);
  168.         contentValues.put("NOME", nome);
  169.         contentValues.put("MORADA", morada);
  170.         contentValues.put("DATA_NASC", data.replace("-",""));
  171.         if(desabilidade != null)
  172.             contentValues.put("DESABILIDADE", desabilidade);
  173.         getWritableDatabase().insert("UTILIZADOR", "", contentValues);
  174.     }
  175.  
  176.     public boolean userExists(String username){
  177.         Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM UTILIZADOR WHERE USERNAME = '" + username + "'", null);
  178.         return cursor.getCount() != 0;
  179.     }
  180.  
  181.     public void updateUser(String username, String nome, String morada, String data, String desabilidade){
  182.         ContentValues contentValues = new ContentValues();
  183.         contentValues.put("NOME", nome);
  184.         contentValues.put("MORADA", morada);
  185.         contentValues.put("DATA_NASC", data);
  186.         contentValues.put("DESABILIDADE", desabilidade);
  187.         getWritableDatabase().update("UTILIZADOR", contentValues, "USERNAME = ?", new String[]{username});
  188.     }
  189.  
  190.     public void setUserDisabilities(String username, String desabilities){
  191.         ContentValues contentValues = new ContentValues();
  192.         contentValues.put("DESABILIDADE", desabilities);
  193.         getWritableDatabase().update("UTILIZADOR", contentValues, "USERNAME = ?", new String[]{username});
  194.     }
  195.  
  196.     public boolean credentialsMatch(String username, String password){
  197.         Cursor cursor = getReadableDatabase().rawQuery("SELECT USERNAME, PASSWORD FROM UTILIZADOR WHERE " +
  198.                 "USERNAME = '" + username + "' AND PASSWORD = '" + password + "'", null);
  199.         return cursor.getCount() != 0;
  200.     }
  201.  
  202.     public String[] getUserInfo(String username){
  203.         Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM UTILIZADOR WHERE USERNAME = '" + username + "'", null);
  204.         cursor.moveToFirst();
  205.         String[] utilizador = new String[cursor.getColumnCount()];
  206.         for (int i = 0; i < cursor.getColumnCount(); i++)
  207.             utilizador[i] = cursor.getString(i);
  208.         return utilizador;
  209.     }
  210.  
  211.     public float getPlaceAverageScore(int idLocal){
  212.         Cursor cursor = getReadableDatabase().rawQuery("SELECT CLASSIFICACAO FROM OPINIAO WHERE " +
  213.                 "ID_LOCAL = " + idLocal, null);
  214.         int sum = 0;
  215.         while(cursor.moveToNext())
  216.             if(cursor.getInt(0) > 0)
  217.                 sum += cursor.getInt(0);
  218.         return (float) sum / cursor.getCount();
  219.     }
  220.  
  221.     public String[] getPlaceComments(int idLocal){
  222.         Cursor cursor = getReadableDatabase().rawQuery("SELECT OPINIAO.COMENTARIO, UTILIZADOR.NOME " +
  223.                 "FROM OPINIAO, UTILIZADOR WHERE OPINIAO.ID_LOCAL = " + idLocal +
  224.                 " AND OPINIAO.USERNAME = UTILIZADOR.USERNAME ORDER BY DATA_OPINIAO ASC", null);
  225.         String[] str = new String[cursor.getCount()];
  226.         while(cursor.moveToNext())
  227.             if(cursor.getString(0).length() > 0)
  228.                 str[cursor.getPosition()] = cursor.getString(1) + ": " + cursor.getString(0);
  229.         return str;
  230.     }
  231.  
  232.     public String getUserDisabilities(String username){
  233.         Cursor cursor = getReadableDatabase().rawQuery("SELECT DESABILITIES FROM UTILIZADOR WHERE " +
  234.                 "USERNAME = '" + username + "'", null);
  235.         cursor.moveToFirst();
  236.         return cursor.getString(0);
  237.     }
  238.  
  239.     public String[] getPlaceInfo(int id){
  240.         Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM LOCAL WHERE ID_LOCAL = " + id, null);
  241.         cursor.moveToFirst();
  242.         String[] local = new String[cursor.getColumnCount()];
  243.         for (int i = 0; i < cursor.getColumnCount(); i++)
  244.             local[i] = cursor.getString(i);
  245.         return local;
  246.     }
  247.  
  248.     public String[] getPlaceNames(){
  249.         Cursor cursor = getReadableDatabase().rawQuery("SELECT NOME FROM LOCAL", null);
  250.         String[] local = new String[cursor.getCount()];
  251.         while(cursor.moveToNext())
  252.             local[cursor.getPosition()] = cursor.getString(0);
  253.         return local;
  254.     }
  255.  
  256.     public String[] getPlaceNames(String nome, String categoria, String desabilidade){
  257.         /*String query = "SELECT LOCAL.ID_LOCAL, LOCAL.NOME FROM LOCAL, DENUNCIA WHERE " +
  258.                 "DENUNCIA.ID_LOCAL != LOCAL.ID_LOCAL AND DENUNCIA.USERNAME != '" + UserTemp.getCurrentUser() + "'";*/
  259.         /*String query = "SELECT LOCAL.ID_LOCAL, LOCAL.NOME FROM LOCAL, DENUNCIA WHERE " +
  260.                 "LOCAL.ID_LOCAL NOT IN (SELECT DENUNCIA.ID_LOCAL FROM DENUNCIA WHERE " +
  261.                 "DENUNCIA.USERNAME = '" + UserTemp.getCurrentUser() + "')";*/
  262.         String query = "SELECT LOCAL.ID_LOCAL, LOCAL.NOME FROM LOCAL";
  263.         if(categoria.charAt(0) == '<')
  264.             categoria = "";
  265.         if(desabilidade.charAt(0) == '<')
  266.             desabilidade = "";
  267.  
  268.         if(nome.isEmpty() && categoria.isEmpty() && !desabilidade.isEmpty())
  269.             query += " WHERE DESABILIDADE LIKE '%" + desabilidade + "%'";
  270.         else if(nome.isEmpty() && !categoria.isEmpty() && desabilidade.isEmpty())
  271.             query += " WHERE CATEGORIA LIKE '%" + categoria + "%'";
  272.         else if(!nome.isEmpty() && categoria.isEmpty() && desabilidade.isEmpty())
  273.             query += " WHERE NOME LIKE '%" + nome + "%'";
  274.         else if(nome.isEmpty() && !categoria.isEmpty() && !desabilidade.isEmpty())
  275.             query += " WHERE CATEGORIA LIKE '%" + categoria + "%' AND DESABILIDADE LIKE '%" + desabilidade + "%'";
  276.         else if(!nome.isEmpty() && !categoria.isEmpty() && desabilidade.isEmpty())
  277.             query += " WHERE CATEGORIA LIKE '%" + categoria + "%' AND NOME LIKE '%" + nome + "%'";
  278.         else if(!nome.isEmpty() && categoria.isEmpty() && !desabilidade.isEmpty())
  279.             query += " WHERE DESABILIDADE LIKE '%" + desabilidade +"%' AND NOME LIKE '%" + nome + "%'";
  280.         else if(!nome.isEmpty() && !categoria.isEmpty() && !desabilidade.isEmpty()) {
  281.             query += " WHERE DESABILIDADE LIKE '%" + desabilidade + "%' AND NOME LIKE '%" + nome + "%' " +
  282.                     "AND CATEGORIA LIKE '%" + categoria + "%'";
  283.         }
  284.  
  285.         query += " EXCEPT " +
  286.                 "SELECT LOCAL.ID_LOCAL, LOCAL.NOME FROM LOCAL,DENUNCIA WHERE " +
  287.                 "DENUNCIA.ID_LOCAL = LOCAL.ID_LOCAL " +
  288.                 "AND DENUNCIA.USERNAME = '" + UserTemp.getCurrentUser() + "'";
  289.  
  290.         Cursor cursor = getReadableDatabase().rawQuery(query, null);
  291.         String[] local = new String[cursor.getCount()];
  292.         while(cursor.moveToNext())
  293.             local[cursor.getPosition()] = cursor.getString(0) + ":" + cursor.getString(1);
  294.         return local;
  295.     }
  296.  
  297.     public LatLng[] getLocations(){
  298.         Cursor cursor = getReadableDatabase().rawQuery("SELECT LATITUDE, LONGITUDE FROM LOCAL ", null);
  299.         LatLng[] local = new LatLng[cursor.getCount()];
  300.         while(cursor.moveToNext())
  301.             local[cursor.getPosition()] = new LatLng(Double.parseDouble(cursor.getString(0)), Double.parseDouble(cursor.getString(1)));
  302.         return local;
  303.     }
  304.  
  305.     public LatLng getLatLngLocal(int idLocal){
  306.         Cursor cursor = getReadableDatabase().rawQuery("SELECT LATITUDE, LONGITUDE FROM LOCAL " +
  307.                 "WHERE ID_LOCAL = " + idLocal, null);
  308.         cursor.moveToFirst();
  309.         return new LatLng(Double.parseDouble(cursor.getString(0)), Double.parseDouble(cursor.getString(1)));
  310.     }
  311.  
  312.     public String[] getFavoritePlaceNames(String username){
  313.         Cursor cursor = getReadableDatabase().rawQuery("SELECT LOCAL.ID_LOCAL, LOCAL.NOME FROM LOCAL, FAVORITO " +
  314.                 "WHERE LOCAL.ID_LOCAL = FAVORITO.ID_LOCAL " +
  315.                 "AND FAVORITO.USERNAME = '" + username + "'", null);
  316.         String[] local = new String[cursor.getCount()];
  317.         while(cursor.moveToNext())
  318.             local[cursor.getPosition()] = cursor.getString(0) + ":" + cursor.getString(1);
  319.         return local;
  320.     }
  321.  
  322.     public void removePlace(int idLocal){
  323.         SQLiteDatabase db = getReadableDatabase();
  324.         db.delete("DENUNCIA", "ID_LOCAL = " + idLocal, null);
  325.         db.delete("OPINIAO", "ID_LOCAL = " + idLocal, null);
  326.         db.delete("FAVORITO", "ID_LOCAL = " + idLocal, null);
  327.         db.delete("LOCAL", "ID_LOCAL = " + idLocal, null);
  328.     }
  329.  
  330.     public void removeFavorite(int idLocal, String username){
  331.         SQLiteDatabase db = getReadableDatabase();
  332.         db.delete("FAVORITO", "ID_LOCAL = ? AND USERNAME = ?",
  333.                 new String[]{String.valueOf(idLocal), username});
  334.     }
  335.  
  336.     public void removePlaceComments(int idLocal){
  337.         SQLiteDatabase db = getReadableDatabase();
  338.         db.delete("OPINIAO", "ID_LOCAL = ?",
  339.                 new String[]{String.valueOf(idLocal)});
  340.     }
  341.  
  342.     public void deleteAllPlaces(){
  343.         getWritableDatabase().delete("LOCAL", null, null);
  344.     }
  345.  
  346.     public String getPath(){
  347.         return getReadableDatabase().getPath();
  348.     }
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement