Advertisement
Guest User

DB Controller

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