Advertisement
yalik97

Untitled

Apr 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.87 KB | None | 0 0
  1. package com.effectivesoft.caloriesmanager.repository;
  2.  
  3. import android.content.ContentValues;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.util.Log;
  7.  
  8. import com.effectivesoft.caloriesmanager.CaloriesManagerApplication;
  9. import com.effectivesoft.caloriesmanager.data.model.Dish;
  10. import com.effectivesoft.caloriesmanager.data.model.Meal;
  11. import com.effectivesoft.caloriesmanager.data.model.PhysicalExercise;
  12. import com.effectivesoft.caloriesmanager.data.model.PhysicalTraining;
  13. import com.effectivesoft.caloriesmanager.data.model.Record;
  14. import com.effectivesoft.caloriesmanager.data.model.Water;
  15. import com.effectivesoft.caloriesmanager.database.DbHelper;
  16. import com.effectivesoft.caloriesmanager.util.DateUtils;
  17.  
  18. import java.util.ArrayList;
  19. import java.util.Calendar;
  20. import java.util.List;
  21.  
  22. public class Repository {
  23.  
  24.     private final static String LOG_TAG = "Repository";
  25.  
  26.     private static volatile Repository instance;
  27.  
  28.     private SQLiteDatabase database;
  29.  
  30.     private Repository() {
  31.         DbHelper dbHelper = new DbHelper(CaloriesManagerApplication.getAppContext());
  32.         database = dbHelper.getWritableDatabase();
  33.     }
  34.  
  35.     public static Repository getInstance() {
  36.         if (instance == null) {
  37.             synchronized (Repository.class) {
  38.                 if (instance == null) {
  39.                     instance = new Repository();
  40.                 }
  41.             }
  42.         }
  43.         return instance;
  44.     }
  45.  
  46.     public void writeRecordList(List<Record> records, boolean writeOnlyUniqueRecords) {
  47.         for (Record record : records) {
  48.             writeRecord(record, writeOnlyUniqueRecords);
  49.         }
  50.     }
  51.  
  52.     public void writeRecord(Record record, boolean writeOnlyUniqueRecords) {
  53.         if (record instanceof Meal) {
  54.             writeMeal((Meal) record, writeOnlyUniqueRecords);
  55.         } else if (record instanceof PhysicalTraining) {
  56.             writeTraining((PhysicalTraining) record, writeOnlyUniqueRecords);
  57.         } else if (record instanceof Water) {
  58.             writeWater((Water) record, writeOnlyUniqueRecords);
  59.         }
  60.     }
  61.  
  62.     public void deleteRecord(Record record) {
  63.         String tableName = "";
  64.         if (record instanceof Meal) {
  65.             tableName = DbHelper.TABLE_MEALS_NAME;
  66.         } else if (record instanceof PhysicalTraining) {
  67.             tableName = DbHelper.TABLE_TRAININGS_NAME;
  68.         } else if (record instanceof Water) {
  69.             tableName = DbHelper.TABLE_WATER_NAME;
  70.         }
  71.  
  72.         database.delete(tableName, DbHelper.COLUMN_NAME + " = ? AND " + DbHelper.COLUMN_DATE + " = ?",
  73.                 new String[]{record.getName(), record.getDate()});
  74.     }
  75.  
  76.     private void writeMeal(final Meal meal, boolean writeOnlyUniqueMeals) {
  77.         if (!writeOnlyUniqueMeals || isUniqueMeal(meal)) {
  78.             long rowId = database.insert(DbHelper.TABLE_MEALS_NAME,
  79.                     null, RepositoryConverter.mealToContentValues(meal));
  80.             logWriteIntoDatabase(DbHelper.TABLE_MEALS_NAME, rowId);
  81.         }
  82.     }
  83.  
  84.     private boolean isUniqueMeal(Meal meal) {
  85.         Cursor cursor = database.query(DbHelper.TABLE_MEALS_NAME, DbHelper.MEALS_COLUMN_ARRAY,
  86.                 DbHelper.COLUMN_NAME + " = ? AND " + DbHelper.COLUMN_DATE + " = ? ",
  87.                 new String[]{meal.getName(), meal.getDate()}, null, null, null);
  88.         boolean isUnique = !cursor.moveToFirst();
  89.         cursor.close();
  90.         return isUnique;
  91.     }
  92.  
  93.     private int getMealId(Meal meal) {
  94.         Cursor cursor = database.query(DbHelper.TABLE_MEALS_NAME, new String[]{DbHelper.COLUMN_MEALS_ID},
  95.                 DbHelper.COLUMN_DATE + " = ? AND " + DbHelper.COLUMN_NAME + " = ?",
  96.                 new String[]{meal.getDate(), meal.getName()}, null, null, null);
  97.  
  98.         if (cursor.moveToFirst()) {
  99.             int mealId = Integer.parseInt(cursor.getString(cursor.getColumnIndex(DbHelper.COLUMN_MEALS_ID)));
  100.             cursor.close();
  101.             return mealId;
  102.         } else {
  103.             cursor.close();
  104.             return -1;
  105.         }
  106.     }
  107.  
  108.     public List<Dish> findDishesByMeal(Meal meal) {
  109.         int mealId = getMealId(meal);
  110.  
  111.         String JOIN_QUERY =
  112.                 DbHelper.TABLE_MEALS_NAME + " INNER JOIN " + DbHelper.TABLE_MEALS_DISHES_NAME + " ON "
  113.                         + DbHelper.TABLE_MEALS_DISHES_NAME + "." + DbHelper.COLUMN_MEALS_ID + " =  "
  114.                         + DbHelper.TABLE_MEALS_NAME + "." + DbHelper.COLUMN_MEALS_ID
  115.                         + " INNER JOIN " + DbHelper.TABLE_DISHES_NAME + " ON " + DbHelper.TABLE_DISHES_NAME +
  116.                         "." + DbHelper.COLUMN_DISHES_ID + " = " + DbHelper.TABLE_MEALS_DISHES_NAME +
  117.                         "." + DbHelper.COLUMN_DISHES_ID;
  118.  
  119.         Cursor cursor = database.query(JOIN_QUERY, DbHelper.FIND_DISHES_BY_MEAL_COLUMN_ARRAY,
  120.                 DbHelper.TABLE_MEALS_NAME + "." + DbHelper.COLUMN_MEALS_ID + " = ?",
  121.                 new String[]{String.valueOf(mealId)}, null, null, null);
  122.  
  123.         List<Dish> dishes = new ArrayList<>();
  124.         while (cursor.moveToNext()) {
  125.             dishes.add(RepositoryConverter.cursorToDishWithWeight(cursor));
  126.         }
  127.         cursor.close();
  128.  
  129.         return dishes;
  130.     }
  131.  
  132.     public List<PhysicalExercise> findExercisesByTraining(PhysicalTraining physicalTraining) {
  133.         int physicalTrainingId = getTrainingId(physicalTraining);
  134.  
  135.         String JOIN_QUERY =
  136.                 DbHelper.TABLE_TRAININGS_NAME
  137.                         + " INNER JOIN " + DbHelper.TABLE_TRAININGS_EXERCISES_NAME + " ON "
  138.                         + DbHelper.TABLE_TRAININGS_EXERCISES_NAME + "." + DbHelper.COLUMN_TRAININGS_ID
  139.                         + " =  " + DbHelper.TABLE_TRAININGS_NAME + "." + DbHelper.COLUMN_TRAININGS_ID
  140.                         + " INNER JOIN " + DbHelper.TABLE_EXERCISES_NAME + " ON "
  141.                         + DbHelper.TABLE_EXERCISES_NAME + "." + DbHelper.COLUMN_EXERCISES_ID
  142.                         + " = " + DbHelper.TABLE_TRAININGS_EXERCISES_NAME +
  143.                         "." + DbHelper.COLUMN_EXERCISES_ID;
  144.  
  145.         Cursor cursor = database.query(JOIN_QUERY, DbHelper.FIND_EXERCISES_BY_TRAINING_COLUMN_ARRAY,
  146.                 DbHelper.TABLE_TRAININGS_NAME + "." + DbHelper.COLUMN_TRAININGS_ID + " = ?",
  147.                 new String[]{String.valueOf(physicalTrainingId)}, null, null, null);
  148.  
  149.         List<PhysicalExercise> exercises = new ArrayList<>();
  150.         while (cursor.moveToNext()) {
  151.             exercises.add(RepositoryConverter.cursorToExerciseWithTime(cursor));
  152.         }
  153.         cursor.close();
  154.  
  155.         return exercises;
  156.     }
  157.  
  158.     public void writeDish(final Dish dish) {
  159.         long rowId = database.insert(DbHelper.TABLE_DISHES_NAME,
  160.                 null, RepositoryConverter.dishToContentValues(dish));
  161.         logWriteIntoDatabase(DbHelper.TABLE_DISHES_NAME, rowId);
  162.     }
  163.  
  164.     private int getDishId(Dish dish) {
  165.         Cursor cursor = database.query(DbHelper.TABLE_DISHES_NAME, new String[]{DbHelper.COLUMN_DISHES_ID},
  166.                 DbHelper.COLUMN_NAME + " = ? AND " + DbHelper.COLUMN_CALORIES + " = ? AND " +
  167.                         DbHelper.COLUMN_PROTEIN + " = ? AND " + DbHelper.COLUMN_FAT + " = ? AND " +
  168.                         DbHelper.COLUMN_CARBOHYDRATES + " = ?",
  169.                 new String[]{dish.getName(), String.valueOf(dish.getCalories()),
  170.                         String.valueOf(dish.getProtein()), String.valueOf(dish.getFat()),
  171.                         String.valueOf(dish.getCarbohydrates())}, null, null, null);
  172.  
  173.         if (cursor.moveToFirst()) {
  174.             int dishId = Integer.parseInt(cursor.getString(cursor.getColumnIndex(DbHelper.COLUMN_DISHES_ID)));
  175.             cursor.close();
  176.             return dishId;
  177.         } else {
  178.             cursor.close();
  179.             return -1;
  180.         }
  181.     }
  182.  
  183.     public boolean isDishesByDayExist(Calendar calendar) {
  184.         for (Meal meal : findMealsByDate(calendar)) {
  185.             if (!findDishesByMeal(meal).isEmpty()) {
  186.                 return true;
  187.             }
  188.         }
  189.         return false;
  190.     }
  191.  
  192.     public void writeMealDish(Meal meal, Dish dish) {
  193.         ContentValues contentValues = new ContentValues();
  194.         contentValues.put(DbHelper.COLUMN_MEALS_ID, getMealId(meal));
  195.         contentValues.put(DbHelper.COLUMN_DISHES_ID, getDishId(dish));
  196.         contentValues.put(DbHelper.COLUMN_WEIGHT, dish.getWeight());
  197.         database.insert(DbHelper.TABLE_MEALS_DISHES_NAME, null, contentValues);
  198.     }
  199.  
  200.     public void writeTrainingExercise(PhysicalTraining training, PhysicalExercise exercise) {
  201.         ContentValues contentValues = new ContentValues();
  202.         contentValues.put(DbHelper.COLUMN_TRAININGS_ID, getTrainingId(training));
  203.         contentValues.put(DbHelper.COLUMN_EXERCISES_ID, getExerciseId(exercise));
  204.         contentValues.put(DbHelper.COLUMN_TIME, exercise.getTime());
  205.         database.insert(DbHelper.TABLE_TRAININGS_EXERCISES_NAME, null, contentValues);
  206.     }
  207.  
  208.     private int getTrainingId(PhysicalTraining training) {
  209.         Cursor cursor = database.query(DbHelper.TABLE_TRAININGS_NAME, new String[]{DbHelper.COLUMN_TRAININGS_ID},
  210.                 DbHelper.COLUMN_DATE + " = ? AND " + DbHelper.COLUMN_NAME + " = ?",
  211.                 new String[]{training.getDate(), training.getName()}, null, null, null);
  212.  
  213.         if (cursor.moveToFirst()) {
  214.             int mealId = Integer.parseInt(cursor.getString(cursor.getColumnIndex(DbHelper.COLUMN_TRAININGS_ID)));
  215.             cursor.close();
  216.             return mealId;
  217.         } else {
  218.             cursor.close();
  219.             return -1;
  220.         }
  221.     }
  222.  
  223.     private int getExerciseId(PhysicalExercise exercise) {
  224.         Cursor cursor = database.query(DbHelper.TABLE_EXERCISES_NAME, new String[]{DbHelper.COLUMN_EXERCISES_ID},
  225.                 DbHelper.COLUMN_NAME + " = ? AND " + DbHelper.COLUMN_CALORIES + " = ?",
  226.                 new String[]{exercise.getName(), String.valueOf(exercise.getCalories())}, null, null, null);
  227.  
  228.         if (cursor.moveToFirst()) {
  229.             int dishId = Integer.parseInt(cursor.getString(cursor.getColumnIndex(DbHelper.COLUMN_EXERCISES_ID)));
  230.             cursor.close();
  231.             return dishId;
  232.         } else {
  233.             cursor.close();
  234.             return -1;
  235.         }
  236.     }
  237.  
  238.     public List<Meal> findMealsByDate(Calendar calendar) {
  239.         Cursor cursor = database.query(DbHelper.TABLE_MEALS_NAME, DbHelper.MEALS_COLUMN_ARRAY,
  240.                 DbHelper.COLUMN_DATE + " = ?", new String[]{DateUtils.calendarToString(calendar)},
  241.                 null, null, null);
  242.  
  243.         List<Meal> meals = new ArrayList<>();
  244.         while (cursor.moveToNext()) {
  245.             meals.add(RepositoryConverter.cursorToMeal(cursor));
  246.         }
  247.         cursor.close();
  248.  
  249.         return meals;
  250.     }
  251.  
  252.     private void writeTraining(PhysicalTraining training, boolean writeOnlyUniqueTrainings) {
  253.         if (!writeOnlyUniqueTrainings || isUniqueTraining(training)) {
  254.             long rowId = database.insert(DbHelper.TABLE_TRAININGS_NAME,
  255.                     null, RepositoryConverter.trainingToContentValues(training));
  256.             logWriteIntoDatabase(DbHelper.TABLE_TRAININGS_NAME, rowId);
  257.         }
  258.     }
  259.  
  260.     private boolean isUniqueTraining(PhysicalTraining training) {
  261.         Cursor cursor = database.query(DbHelper.TABLE_TRAININGS_NAME, DbHelper.TRAININGS_COLUMN_ARRAY,
  262.                 DbHelper.COLUMN_NAME + " = ? AND " + DbHelper.COLUMN_DATE + " = ? ",
  263.                 new String[]{training.getName(), training.getDate()}, null, null, null);
  264.         boolean isUnique = !cursor.moveToFirst();
  265.         cursor.close();
  266.         return isUnique;
  267.     }
  268.  
  269.     public List<PhysicalTraining> findTrainingsByDate(Calendar calendar) {
  270.         Cursor cursor = database.query(DbHelper.TABLE_TRAININGS_NAME, DbHelper.TRAININGS_COLUMN_ARRAY,
  271.                 DbHelper.COLUMN_DATE + " = ?", new String[]{DateUtils.calendarToString(calendar)},
  272.                 null, null, null);
  273.  
  274.         List<PhysicalTraining> trainings = new ArrayList<>();
  275.         while (cursor.moveToNext()) {
  276.             trainings.add(RepositoryConverter.cursorToTraining(cursor));
  277.         }
  278.         cursor.close();
  279.  
  280.         return trainings;
  281.     }
  282.  
  283.     public void addDishesFromCsv(List<String[]> lineList) {
  284.         if (lineList != null) {
  285.             database.beginTransaction();
  286.             for (String[] dishValuesArray : lineList) {
  287.                 ContentValues dishContentValues =
  288.                         RepositoryConverter.dishValuesArrayToContentValues(dishValuesArray);
  289.                 if (dishContentValues != null) {
  290.                     database.insert(DbHelper.TABLE_DISHES_NAME, null, dishContentValues);
  291.                 }
  292.             }
  293.             database.setTransactionSuccessful();
  294.             database.endTransaction();
  295.         }
  296.     }
  297.  
  298.     public void addPhysicalExercisesFromCsv(List<String[]> lineList) {
  299.         if (lineList != null) {
  300.             database.beginTransaction();
  301.             for (String[] exercisesValuesArray : lineList) {
  302.                 ContentValues exercisesContentValues =
  303.                         RepositoryConverter.exerciseValuesArrayToContentValues(exercisesValuesArray);
  304.                 if (exercisesContentValues != null) {
  305.                     database.insert(DbHelper.TABLE_EXERCISES_NAME, null, exercisesContentValues);
  306.                 }
  307.             }
  308.             database.setTransactionSuccessful();
  309.             database.endTransaction();
  310.         }
  311.     }
  312.  
  313.     private void writeWater(Water water, boolean writeOnlyUniqueWater) {
  314.         if (!writeOnlyUniqueWater || isUniqueWater(water)) {
  315.             long rowId = database.insert(DbHelper.TABLE_WATER_NAME,
  316.                     null, RepositoryConverter.waterToContentValues(water));
  317.             logWriteIntoDatabase(DbHelper.TABLE_WATER_NAME, rowId);
  318.         }
  319.     }
  320.  
  321.     public Water findWaterByDate(Calendar calendar) {
  322.         Cursor cursor = database.query(DbHelper.TABLE_WATER_NAME, DbHelper.WATER_COLUMN_ARRAY,
  323.                 DbHelper.COLUMN_DATE + " = ?", new String[]{DateUtils.calendarToString(calendar)},
  324.                 null, null, null);
  325.  
  326.         Water water = null;
  327.         if (cursor.moveToFirst()) {
  328.             water = RepositoryConverter.cursorToWater(cursor);
  329.         }
  330.         cursor.close();
  331.         return water;
  332.     }
  333.  
  334.     private boolean isUniqueWater(Water water) {
  335.         Cursor cursor = database.query(DbHelper.TABLE_WATER_NAME, new String[]{DbHelper.COLUMN_DATE},
  336.                 DbHelper.COLUMN_NAME + " = ? AND " + DbHelper.COLUMN_DATE + " = ? ",
  337.                 new String[]{water.getName(), water.getDate()}, null, null, null);
  338.         boolean isUnique = !cursor.moveToFirst();
  339.         cursor.close();
  340.         return isUnique;
  341.     }
  342.  
  343.     private void logWriteIntoDatabase(String tableName, long rowId) {
  344.         Log.d(LOG_TAG, tableName + " : row inserted, ID = " + rowId);
  345.     }
  346.  
  347.     public List<Dish> findDishByNameInFavorite(String findString) {
  348.         Cursor findCursor = database.query(DbHelper.TABLE_DISHES_NAME, DbHelper.DISHES_COLUMN_ARRAY,
  349.                 DbHelper.COLUMN_NAME + " LIKE ? and " + DbHelper.COLUMN_FAVORITE + "=?",
  350.                 new String[]{"%" + findString + "%", "1"}, null, null, DbHelper.COLUMN_CUSTOM + " DESC");
  351.         return RepositoryConverter.cursorToDishArrayList(findCursor);
  352.     }
  353.  
  354.     public List<Dish> findDishByNameInAllDatabase(String findString) {
  355.         Cursor findCursor = database.query(DbHelper.TABLE_DISHES_NAME, DbHelper.DISHES_COLUMN_ARRAY,
  356.                 DbHelper.COLUMN_NAME + " LIKE ?",
  357.                 new String[]{"%" + findString + "%"},
  358.                 null, null, DbHelper.COLUMN_FAVORITE + " DESC" + "," + DbHelper.COLUMN_CUSTOM + " DESC");
  359.         return RepositoryConverter.cursorToDishArrayList(findCursor);
  360.     }
  361.  
  362.     public List<Dish> findDishByNameInCustom(String findString) {
  363.         Cursor findCursor = database.query(DbHelper.TABLE_DISHES_NAME, DbHelper.DISHES_COLUMN_ARRAY,
  364.                 DbHelper.COLUMN_NAME + " LIKE ? and " + DbHelper.COLUMN_CUSTOM + "=?",
  365.                 new String[]{"%" + findString + "%", "1"},
  366.                 null, null, DbHelper.COLUMN_FAVORITE + " DESC");
  367.         return RepositoryConverter.cursorToDishArrayList(findCursor);
  368.     }
  369.  
  370.     public int updateFavorite(Dish dish) {
  371.         ContentValues cv = new ContentValues();
  372.         cv.put(DbHelper.COLUMN_FAVORITE, dish.isFavorite());
  373.         return database.update(DbHelper.TABLE_DISHES_NAME, cv, DbHelper.COLUMN_DISHES_ID + " = ?",
  374.                 new String[]{String.valueOf(dish.getId())});
  375.     }
  376.  
  377.     public List<PhysicalExercise> findExercisesByName(String exerciseName) {
  378.         Cursor findCursor = database.query(DbHelper.TABLE_EXERCISES_NAME, DbHelper.EXERCISES_COLUMN_ARRAY,
  379.                 DbHelper.COLUMN_NAME + " LIKE ?",
  380.                 new String[]{"%" + exerciseName + "%"},
  381.                 null, null, DbHelper.COLUMN_NAME);
  382.         return RepositoryConverter.cursorToExerciseArrayList(findCursor);
  383.     }
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement