PageRenderTime 58ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/fkf/resturent/database/dbprovider/ContentProviderAccessor.java

https://bitbucket.org/kavi707/fkf_resturent_app
Java | 333 lines | 214 code | 41 blank | 78 comment | 45 complexity | 70ade5dd0fd38e9ac935143ba3d2f009 MD5 | raw file
  1. package com.fkf.resturent.database.dbprovider;
  2. import android.app.Activity;
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.net.Uri;
  7. import android.util.Log;
  8. import com.fkf.resturent.database.LocalDatabaseSQLiteOpenHelper;
  9. import com.fkf.resturent.database.PopularOrLatestRecipe;
  10. import com.fkf.resturent.database.Recipe;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14. /**
  15. * Methods handler for content provider cases
  16. * Created by kavi on 10/31/13.
  17. * @author Kavimal Wijewardana <kavi707@gmail.com>
  18. */
  19. public class ContentProviderAccessor {
  20. /********************************************************/
  21. /********* Modified time stamp table methods ************/
  22. /********************************************************/
  23. /**
  24. * save new updated time stamp in database
  25. * @param newModifiedTimeStamp
  26. * @param context
  27. */
  28. public void saveLastModificationTimeStamp(String newModifiedTimeStamp, Context context) {
  29. ContentValues values = new ContentValues();
  30. values.put(LocalDatabaseSQLiteOpenHelper.MODIFIED_TIME_STAMP, newModifiedTimeStamp);
  31. Uri contextUri = Uri.withAppendedPath(DbContentProvider.CONTENT_URI, LocalDatabaseSQLiteOpenHelper.LAST_MODIFIED_DETAILS_TABLE_NAME);
  32. if(contextUri != null) {
  33. Uri resultUri = context.getContentResolver().insert(contextUri, values);
  34. }
  35. }
  36. /**
  37. * delete existing last time stamp from database
  38. * @param context
  39. */
  40. public void deleteLastModificationTimeStamp(Context context) {
  41. Uri contextUri = Uri.withAppendedPath(DbContentProvider.CONTENT_URI, LocalDatabaseSQLiteOpenHelper.LAST_MODIFIED_DETAILS_TABLE_NAME);
  42. if(contextUri != null) {
  43. context.getContentResolver().delete(contextUri, null, null);
  44. }
  45. }
  46. /**
  47. * return the last modification time stamp
  48. * @param context
  49. * @return
  50. */
  51. public String getLastModificationTimeStamp(Context context) {
  52. String lastTimeStamp = "";
  53. Uri contextUri = Uri.withAppendedPath(DbContentProvider.CONTENT_URI, LocalDatabaseSQLiteOpenHelper.LAST_MODIFIED_DETAILS_TABLE_NAME);
  54. if(contextUri != null) {
  55. String[] selections = {
  56. LocalDatabaseSQLiteOpenHelper.MODIFICATION_ID,
  57. LocalDatabaseSQLiteOpenHelper.MODIFIED_TIME_STAMP
  58. };
  59. Cursor lastModificationDetailsCursor = context.getContentResolver().query(contextUri,selections, null, null, null);
  60. if(lastModificationDetailsCursor != null) {
  61. lastModificationDetailsCursor.moveToFirst();
  62. if(!lastModificationDetailsCursor.isAfterLast()) {
  63. do {
  64. lastTimeStamp = lastModificationDetailsCursor.getString(1);
  65. } while (lastModificationDetailsCursor.moveToNext());
  66. }
  67. }
  68. }
  69. return lastTimeStamp;
  70. }
  71. /**************************************************/
  72. /********* Login details table methods ************/
  73. /**************************************************/
  74. /**
  75. * retrieve saved login details in application
  76. * @param context
  77. * @return
  78. */
  79. public Map<String, String> getLoginDetails(Context context) {
  80. Map<String, String> loginDetails = new HashMap<String, String>();
  81. Uri contextUri = Uri.withAppendedPath(DbContentProvider.CONTENT_URI, LocalDatabaseSQLiteOpenHelper.LOGIN_DETAIL_TABLE_NAME);
  82. if(contextUri != null) {
  83. String[] selections = {
  84. LocalDatabaseSQLiteOpenHelper.LOGIN_ID,
  85. LocalDatabaseSQLiteOpenHelper.LOGIN_USER_ID,
  86. LocalDatabaseSQLiteOpenHelper.LOGIN_STATUS,
  87. LocalDatabaseSQLiteOpenHelper.LAST_LOGIN_USERNAME,
  88. LocalDatabaseSQLiteOpenHelper.LAST_LOGIN_PASSWORD,
  89. LocalDatabaseSQLiteOpenHelper.LAST_LOGIN_NAME
  90. };
  91. String where = LocalDatabaseSQLiteOpenHelper.LOGIN_ID + " = 1";
  92. Cursor loginDetailsCursor = context.getContentResolver().query(contextUri,selections, where, null, null);
  93. if (loginDetailsCursor != null) {
  94. loginDetailsCursor.moveToFirst();
  95. if(!loginDetailsCursor.isAfterLast()) {
  96. do {
  97. loginDetails.put("loginStatus", loginDetailsCursor.getString(1));
  98. loginDetails.put("userId", loginDetailsCursor.getString(2));
  99. loginDetails.put("username", loginDetailsCursor.getString(3));
  100. loginDetails.put("password", loginDetailsCursor.getString(4));
  101. loginDetails.put("fName", loginDetailsCursor.getString(5));
  102. } while (loginDetailsCursor.moveToNext());
  103. }
  104. loginDetailsCursor.close();
  105. }
  106. }
  107. return loginDetails;
  108. }
  109. /****************************************************/
  110. /********* Recipe Category table methods ************/
  111. /****************************************************/
  112. /**
  113. * save new category
  114. * @param categoryProductId
  115. * @param categoryName
  116. * @param context
  117. */
  118. public void saveNewCategory(int categoryProductId, String categoryName, Context context) {
  119. ContentValues values = new ContentValues();
  120. values.put(LocalDatabaseSQLiteOpenHelper.CATEGORY_PRODUCT_ID, categoryProductId);
  121. values.put(LocalDatabaseSQLiteOpenHelper.CATEGORY_NAME, categoryName);
  122. Uri contextUri = Uri.withAppendedPath(DbContentProvider.CONTENT_URI, LocalDatabaseSQLiteOpenHelper.CATEGORY_TABLE_NAME);
  123. if(contextUri != null) {
  124. Uri resultUri = context.getContentResolver().insert(contextUri, values);
  125. }
  126. }
  127. /**
  128. * delete existing all categories from database
  129. * @param context
  130. */
  131. public void deleteAllCategories(Context context) {
  132. Uri contextUri = Uri.withAppendedPath(DbContentProvider.CONTENT_URI, LocalDatabaseSQLiteOpenHelper.CATEGORY_TABLE_NAME);
  133. if(contextUri != null) {
  134. context.getContentResolver().delete(contextUri, null, null);
  135. }
  136. }
  137. /********************************************/
  138. /********* Recipes table methods ************/
  139. /********************************************/
  140. /**
  141. * save new recipe
  142. * @param recipe
  143. * @param context
  144. */
  145. public void saveNewRecipe(Recipe recipe, Context context) {
  146. ContentValues values = new ContentValues();
  147. values.put(LocalDatabaseSQLiteOpenHelper.PRODUCT_ID, recipe.getProductId());
  148. values.put(LocalDatabaseSQLiteOpenHelper.RECIPE_NAME, recipe.getName());
  149. values.put(LocalDatabaseSQLiteOpenHelper.RECIPE_DESCRIPTION, recipe.getDescription());
  150. values.put(LocalDatabaseSQLiteOpenHelper.INGREDIENTS, recipe.getIngredients());
  151. values.put(LocalDatabaseSQLiteOpenHelper.INSTRUCTIONS, recipe.getInstructions());
  152. values.put(LocalDatabaseSQLiteOpenHelper.CATEGORY_ID, recipe.getCategoryId());
  153. values.put(LocalDatabaseSQLiteOpenHelper.ADDED_DATE, "26-06-2013");
  154. values.put(LocalDatabaseSQLiteOpenHelper.RATINGS, recipe.getRatings());
  155. values.put(LocalDatabaseSQLiteOpenHelper.IMAGE_URL, recipe.getImageUrl());
  156. values.put(LocalDatabaseSQLiteOpenHelper.IMAGE_URL_XS, recipe.getImageUrl_xs());
  157. values.put(LocalDatabaseSQLiteOpenHelper.IMAGE_URL_S, recipe.getImageUrl_s());
  158. values.put(LocalDatabaseSQLiteOpenHelper.IMAGE_URL_M, recipe.getImageUrl_m());
  159. values.put(LocalDatabaseSQLiteOpenHelper.IMAGE_URL_L, recipe.getImageUrl_l());
  160. values.put(LocalDatabaseSQLiteOpenHelper.LINKED_IMAGES, recipe.getLinkImages());
  161. values.put(LocalDatabaseSQLiteOpenHelper.LINKED_RECIPE_IDS, recipe.getLinkRecipeIds());
  162. values.put(LocalDatabaseSQLiteOpenHelper.LEGACY, recipe.getLegacy());
  163. values.put(LocalDatabaseSQLiteOpenHelper.BODY, recipe.getBody());
  164. values.put(LocalDatabaseSQLiteOpenHelper.IMAGE_URL_T, recipe.getImageUrlT());
  165. //using content provider database access
  166. Uri contextUri = Uri.withAppendedPath(DbContentProvider.CONTENT_URI, LocalDatabaseSQLiteOpenHelper.RECIPES_TABLE_NAME);
  167. if(contextUri != null) {
  168. context.getContentResolver().insert(contextUri, values);
  169. }
  170. }
  171. public boolean isRecipeExist(String recipeProductId, Context context) {
  172. boolean status = false;
  173. Uri contextUri = Uri.withAppendedPath(DbContentProvider.CONTENT_URI, LocalDatabaseSQLiteOpenHelper.RECIPES_TABLE_NAME);
  174. if(contextUri != null) {
  175. String[] selections = {
  176. LocalDatabaseSQLiteOpenHelper.RECIPE_ID,
  177. LocalDatabaseSQLiteOpenHelper.PRODUCT_ID,
  178. };
  179. String where = LocalDatabaseSQLiteOpenHelper.PRODUCT_ID + " = " + recipeProductId;
  180. Cursor recipeDetailsCursor = context.getContentResolver().query(contextUri,selections, where, null, null);
  181. if(recipeDetailsCursor != null) {
  182. int existCount = 0;
  183. recipeDetailsCursor.moveToFirst();
  184. if(!recipeDetailsCursor.isAfterLast()) {
  185. do {
  186. String productId = recipeDetailsCursor.getString(1);
  187. if(productId.equals(recipeProductId)) {
  188. existCount++;
  189. }
  190. } while (recipeDetailsCursor.moveToNext());
  191. }
  192. if(existCount != 0) {
  193. status = true;
  194. } else {
  195. status = false;
  196. }
  197. } else {
  198. status = false;
  199. }
  200. }
  201. return status;
  202. }
  203. public void deleteRecipeFromProductId(String recipeProductId, Context context) {
  204. String where = LocalDatabaseSQLiteOpenHelper.PRODUCT_ID + " = " + recipeProductId;
  205. Uri contextUri = Uri.withAppendedPath(DbContentProvider.CONTENT_URI, LocalDatabaseSQLiteOpenHelper.RECIPES_TABLE_NAME);
  206. if(contextUri != null) {
  207. context.getContentResolver().delete(contextUri, where, null);
  208. }
  209. }
  210. /***************************************************/
  211. /********* Latest Recipes table methods ************/
  212. /***************************************************/
  213. /**
  214. * save latest recipes
  215. * @param latestRecipes
  216. * @param activity
  217. */
  218. public void saveLatestRecipes(List<PopularOrLatestRecipe> latestRecipes, Activity activity) {
  219. ContentValues values;
  220. Context context = activity.getApplicationContext();
  221. int latestRecipeCount = 1;
  222. for (PopularOrLatestRecipe latestRecipe : latestRecipes) {
  223. values = new ContentValues();
  224. values.put(LocalDatabaseSQLiteOpenHelper.LATEST_INDEX, latestRecipeCount);
  225. values.put(LocalDatabaseSQLiteOpenHelper.PRODUCT_ID, latestRecipe.getProductId());
  226. values.put(LocalDatabaseSQLiteOpenHelper.RECIPE_NAME, latestRecipe.getRecipeName());
  227. values.put(LocalDatabaseSQLiteOpenHelper.IMAGE_URL_XS, latestRecipe.getImageUrlXS());
  228. values.put(LocalDatabaseSQLiteOpenHelper.IMAGE_URL_S, latestRecipe.getImageUrlS());
  229. values.put(LocalDatabaseSQLiteOpenHelper.IMAGE_URL_M, latestRecipe.getImageUrlM());
  230. values.put(LocalDatabaseSQLiteOpenHelper.IMAGE_URL_L, latestRecipe.getImageUrlL());
  231. values.put(LocalDatabaseSQLiteOpenHelper.IMAGE_URL_T, latestRecipe.getImageUrlT());
  232. //using content provider database access
  233. Uri contextUri = Uri.withAppendedPath(DbContentProvider.CONTENT_URI, LocalDatabaseSQLiteOpenHelper.LATEST_YUMMY_TABLE_NAME);
  234. if(contextUri != null) {
  235. context.getContentResolver().insert(contextUri, values);
  236. }
  237. latestRecipeCount++;
  238. }
  239. }
  240. /**
  241. * delete all existing latest recipes
  242. * @param activity
  243. */
  244. public void deleteAllLatestRecipes(Activity activity) {
  245. Context context = activity.getApplicationContext();
  246. Uri contextUri = Uri.withAppendedPath(DbContentProvider.CONTENT_URI, LocalDatabaseSQLiteOpenHelper.LATEST_YUMMY_TABLE_NAME);
  247. if(contextUri != null) {
  248. context.getContentResolver().delete(contextUri, null, null);
  249. }
  250. }
  251. /****************************************************/
  252. /********* Popular Recipes table methods ************/
  253. /****************************************************/
  254. /**
  255. * save popular recipes
  256. * @param popularRecipes
  257. * @param activity
  258. */
  259. public void savePopularRecipes(List<PopularOrLatestRecipe> popularRecipes, Activity activity) {
  260. Context context = activity.getApplicationContext();
  261. ContentValues values;
  262. int popularRecipeCount = 1;
  263. for (PopularOrLatestRecipe popularRecipe : popularRecipes) {
  264. values = new ContentValues();
  265. values.put(LocalDatabaseSQLiteOpenHelper.POPULAR_INDEX, popularRecipeCount);
  266. values.put(LocalDatabaseSQLiteOpenHelper.PRODUCT_ID, popularRecipe.getProductId());
  267. values.put(LocalDatabaseSQLiteOpenHelper.RECIPE_NAME, popularRecipe.getRecipeName());
  268. values.put(LocalDatabaseSQLiteOpenHelper.IMAGE_URL_XS, popularRecipe.getImageUrlXS());
  269. values.put(LocalDatabaseSQLiteOpenHelper.IMAGE_URL_S, popularRecipe.getImageUrlS());
  270. values.put(LocalDatabaseSQLiteOpenHelper.IMAGE_URL_M, popularRecipe.getImageUrlM());
  271. values.put(LocalDatabaseSQLiteOpenHelper.IMAGE_URL_L, popularRecipe.getImageUrlL());
  272. values.put(LocalDatabaseSQLiteOpenHelper.IMAGE_URL_T, popularRecipe.getImageUrlT());
  273. //using content provider database access
  274. Uri contextUri = Uri.withAppendedPath(DbContentProvider.CONTENT_URI, LocalDatabaseSQLiteOpenHelper.POPULAR_YUMMY_TABLE_NAME);
  275. if(contextUri != null) {
  276. context.getContentResolver().insert(contextUri, values);
  277. }
  278. popularRecipeCount++;
  279. }
  280. }
  281. /**
  282. * delete all existing popular recipes
  283. * @param activity
  284. */
  285. public void deleteAllPopularRecipes(Activity activity) {
  286. Context context = activity.getApplicationContext();
  287. Uri contextUri = Uri.withAppendedPath(DbContentProvider.CONTENT_URI, LocalDatabaseSQLiteOpenHelper.POPULAR_YUMMY_TABLE_NAME);
  288. if(contextUri != null) {
  289. context.getContentResolver().delete(contextUri, null, null);
  290. }
  291. }
  292. }