Advertisement
Guest User

Untitled

a guest
Mar 7th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.22 KB | None | 0 0
  1. public MysqliteHelper(Context context) {
  2. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  3. //this.myDatabase = this.getWritableDatabase();
  4. }
  5.  
  6. /*
  7. * public void SQLiteDatabase(){
  8. *
  9. * }
  10. */
  11. @Override
  12. public void onCreate(SQLiteDatabase database) {
  13. database.execSQL(STORE_CACHE_BOOK_TABLE_CREATE);
  14. database.execSQL(STORE_CACHE_COMMENTS_TABLE_CREATE);
  15. database.execSQL(USER_LOGIN_CREDENTIALS_TABLE_CREATE);
  16. database.execSQL(USER_LIBRARY_DETAILS_TABLE_CREATE);
  17. database.execSQL(USER_STORE_DETAILS);
  18. }
  19.  
  20. @Override
  21. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  22. Log.w(MysqliteHelper.class.getName(),
  23. "Upgrading database from version " + oldVersion + " to "
  24. + newVersion + ", which will destroy all old data");
  25. // database.execSQL("DROP TABLE IF EXISTS " + StoreBookCache);
  26. onCreate(db);
  27. }
  28.  
  29. /* Insert Values for Book_DESCRIPTION_OF_STORE */
  30.  
  31. public void insertStoreBookDetailsIntoCache(String prod_str_Id,
  32. String prod_str_desc, String prod_str_Name, String prod_str_author,
  33. String prod_str_price, String prod_str_currency)
  34. throws SQLException {
  35. myDatabase = this.getWritableDatabase();
  36.  
  37.  
  38. Log.d("INeer'", "INSERT METHOD" + myDatabase.getPageSize());
  39.  
  40. ContentValues contentValues = new ContentValues();
  41. // Put the values for Store Table.
  42. contentValues.put(CONST_STR_BOOK_ID, prod_str_Id);
  43. contentValues.put(CONST_STR_PROD_DESCR, prod_str_desc);
  44. contentValues.put(CONST_STR_PROD_NAME, prod_str_Name);
  45. contentValues.put(CONST_STR_PROD_AUTHOR, prod_str_author);
  46. contentValues.put(CONST_STR_PROD_PRICE, prod_str_price);
  47. contentValues.put(CONST_STR_PROD_CUR, prod_str_currency);
  48.  
  49. myDatabase
  50. .insertOrThrow(CONST_TABLE_STORE_DETAILS, null, contentValues);
  51.  
  52. //myDatabase.close();
  53.  
  54. }
  55. public static final String Book_STR_Id = "book_str_Id";
  56. public static final String Book_STR_des = "book_str_des";
  57. public static final String Book_STR_name = "book_str_name";
  58. public static final String Book_STR_author = "book_str_author";
  59. public static final String Book_STR_price = "book_str_price";
  60. public static final String Book_STR_curr = "book_str_curr";
  61.  
  62. public Map<String, String> getStoreItemsDetails_FromCache(String Bookstrid) {
  63. Map<String, String> storeItemsDetails = new HashMap<String, String>();
  64. myDatabase = this.getReadableDatabase();
  65. /*final String IS_BOOK_IN_STR_CACHE_DETAILS = "SELECT * FROM "
  66. + CONST_TABLE_STORE_DETAILS + " WHERE "+ CONST_STR_BOOK_ID + "="+ Bookstrid;*/
  67.  
  68. /* Cursor cur = myDatabase.query(IS_BOOK_IN_STR_CACHE_DETAILS,
  69. new String[] { CONST_STR_BOOK_ID, CONST_STR_PROD_DESCR,
  70. CONST_STR_PROD_NAME, CONST_STR_PROD_AUTHOR,
  71. CONST_STR_PROD_PRICE, CONST_STR_PROD_CUR }, null, null,
  72. null, null, null, null);*/
  73.  
  74. Cursor cur = myDatabase.query(CONST_TABLE_STORE_DETAILS,
  75. new String[] { CONST_STR_BOOK_ID, CONST_STR_PROD_DESCR,
  76. CONST_STR_PROD_NAME, CONST_STR_PROD_AUTHOR,
  77. CONST_STR_PROD_PRICE, CONST_STR_PROD_CUR }, CONST_STR_BOOK_ID + "="+ Bookstrid, null,
  78. null, null, null, null);
  79.  
  80.  
  81. if (cur.getCount() <= 0)
  82. return storeItemsDetails;
  83.  
  84. cur.moveToFirst();
  85. while (!cur.isAfterLast()) {
  86. String book_storeId = cur.getString(0);
  87. String book_storedesc = cur.getString(1);
  88. String book_storename = cur.getString(2);
  89. String book_storeauthor = cur.getString(3);
  90. String book_storeprice = cur.getString(4);
  91. String book_storecurr = cur.getString(5);
  92.  
  93. Log.d(MysqliteHelper.class.getName(), String.format(
  94. "Fetched Book : %s and its cover location on SD Card :%s",
  95. book_storeId, book_storedesc, book_storename,
  96. book_storeauthor, book_storeprice, book_storecurr));
  97. storeItemsDetails.put(Book_STR_Id, book_storeId);// ,
  98. // book_storedesc,book_storename,book_storeauthor,book_storeprice,book_storecurr
  99. storeItemsDetails.put(Book_STR_des, book_storedesc);
  100. storeItemsDetails.put(Book_STR_name, book_storename);
  101. storeItemsDetails.put(Book_STR_author, book_storeauthor);
  102. storeItemsDetails.put(Book_STR_price, book_storeprice);
  103. storeItemsDetails.put(Book_STR_curr, book_storecurr);
  104.  
  105. cur.moveToNext();
  106. }
  107.  
  108. cur.close();
  109. // myDatabase.close();
  110. return storeItemsDetails;
  111. }
  112.  
  113. /* Insert Values for Book_DESCRIPTION_OF_LIBRARY */
  114.  
  115. public void insertLibraryBookDetailsIntoCache(String prod_lib_Id,
  116. String prod_lib_desc, String prod_lib_Name, String prod_lib_author,
  117. String prod_lib_price, String prod_lib_currency)
  118. throws SQLException {
  119. myDatabase = this.getWritableDatabase();
  120.  
  121. ContentValues contentValues = new ContentValues();
  122. // Put the values for Store Table.
  123. contentValues.put(CONST_LIB_BOOK_ID, prod_lib_Id);
  124. contentValues.put(CONST_LIB_PROD_DESCR, prod_lib_desc);
  125. contentValues.put(CONST_LIB_PROD_NAME, prod_lib_Name);
  126. contentValues.put(CONST_LIB_PROD_AUTHOR, prod_lib_author);
  127. contentValues.put(CONST_LIB_PROD_PRICE, prod_lib_price);
  128. contentValues.put(CONST_LIB_PROD_CUR, prod_lib_currency);
  129.  
  130. myDatabase.insertOrThrow(CONST_TABLE_LIBRARY_DETAILS, null,
  131. contentValues);
  132. //myDatabase.close();
  133. }
  134. public static final String Book_LIB_Id = "book_lib_Id";
  135. public static final String Book_LIB_des = "book_lib_des";
  136. public static final String Book_LIB_name = "book_lib_name";
  137. public static final String Book_LIB_author = "book_lib_author";
  138. public static final String Book_LIB_price = "book_lib_price";
  139. public static final String Book_LIB_curr = "book_lib_curr";
  140. public Map<String, String> getLibraryItemsDetails_FromCache(String BooklibId) {
  141. Map<String, String> libraryItemsDetails = new HashMap<String, String>();
  142. myDatabase = this.getReadableDatabase();
  143. /* String IS_BOOK_IN_LIB_CACHE_DETAILS = "SELECT * FROM "
  144. + CONST_TABLE_LIBRARY_DETAILS + " WHERE "+ CONST_LIB_BOOK_ID +"="+ BooklibId;*/
  145.  
  146.  
  147. Cursor cur = myDatabase.query(CONST_TABLE_LIBRARY_DETAILS,
  148. new String[] { CONST_LIB_BOOK_ID, CONST_LIB_PROD_DESCR,
  149. CONST_LIB_PROD_NAME, CONST_LIB_PROD_AUTHOR,
  150. CONST_LIB_PROD_PRICE, CONST_LIB_PROD_CUR }, CONST_LIB_BOOK_ID +"="+ BooklibId , null,
  151. null, null, null, null);
  152. if (cur.getCount() <= 0)
  153. return libraryItemsDetails;
  154.  
  155. cur.moveToFirst();
  156. while (!cur.isAfterLast()) {
  157. String book_libraryId = cur.getString(0);
  158. String book_librarydesc = cur.getString(1);
  159. String book_libraryname = cur.getString(2);
  160. String book_libraryauthor = cur.getString(3);
  161. String book_libraryprice = cur.getString(4);
  162. String book_librarycurr = cur.getString(5);
  163.  
  164. Log.d(MysqliteHelper.class.getName(), String.format(
  165. "Fetched Book : %s and its cover location on SD Card :%s",
  166. book_libraryId, book_librarydesc, book_libraryname,
  167. book_libraryauthor, book_libraryprice, book_librarycurr));
  168. libraryItemsDetails.put("book_lib_Id", book_libraryId);// ,
  169. // book_storedesc,book_storename,book_storeauthor,book_storeprice,book_storecurr
  170. libraryItemsDetails.put(Book_LIB_des, book_librarydesc);
  171. libraryItemsDetails.put(Book_LIB_name, book_libraryname);
  172. libraryItemsDetails.put(Book_LIB_author, book_libraryauthor);
  173. libraryItemsDetails.put(Book_LIB_price, book_libraryprice);
  174. libraryItemsDetails.put(Book_LIB_curr, book_librarycurr);
  175.  
  176. cur.moveToNext();
  177. }
  178.  
  179. cur.close();
  180. //myDatabase.close();
  181. return libraryItemsDetails;
  182. }
  183.  
  184. /*
  185. * public void updateStoreCacheWithDetails(String prod_str_Id, String
  186. * prod_str_desc, String prod_str_Name, String prod_str_author, String
  187. * prod_str_price, String prod_str_currency){ Log.d(
  188. * MysqliteHelper.class.getName(), String.format(
  189. * "Local Image Location: %s, URL :%s", prod_str_Id,
  190. * prod_str_desc,prod_str_Name,
  191. * prod_str_author,prod_str_price,prod_str_currency) ); myDatabase =
  192. * this.getWritableDatabase(); ContentValues contentValues = new
  193. * ContentValues(); contentValues.put(CONST_STR_BOOK_ID, prod_str_Id);
  194. * contentValues.put(CONST_STR_PROD_DESCR, prod_str_desc);
  195. * contentValues.put(CONST_STR_PROD_NAME, prod_str_Name);
  196. * contentValues.put(CONST_STR_PROD_AUTHOR, prod_str_author);
  197. * contentValues.put(CONST_STR_PROD_PRICE, prod_str_price);
  198. * contentValues.put(CONST_STR_PROD_CUR, prod_str_currency);
  199. *
  200. *
  201. * myDatabase.update( CONST_TABLE_STORE_CACHE , contentValues,
  202. * CONST_STR_BOOK_ID+"=?", new String[] { prod_str_Id });
  203. * //myDatabase.close(); }
  204. */
  205.  
  206. public void updateStoreCacheWithLocalImageLocation(
  207. String localImageLocation, String imageURLOnServer) {
  208. Log.d(MysqliteHelper.class.getName(), String.format(
  209. "Local Image Location: %s, URL :%s", localImageLocation,
  210. imageURLOnServer));
  211. myDatabase = this.getWritableDatabase();
  212. //SQLiteDatabase.openDatabase(DATABASE_NAME, null, 0);
  213.  
  214.  
  215.  
  216. ContentValues contentValues = new ContentValues();
  217. contentValues.put(CONST_LOCAL_IMAGE_PATH, localImageLocation);
  218.  
  219. myDatabase.update(CONST_TABLE_STORE_CACHE, contentValues,
  220. CONST_IMAGE_URL + "=?", new String[] { imageURLOnServer });
  221. // myDatabase.close();
  222. }
  223.  
  224. public void insertStoreBookIntoCache(String bookId, String bookName,
  225. String author, String publisher, String description,
  226. String priceValue, String priceCurrency, String averageRating,
  227. String imageUrl, String localImagePath) throws SQLException {
  228. myDatabase = this.getWritableDatabase();
  229. ContentValues contentValues = new ContentValues();
  230.  
  231. // Put the values for Store Table.
  232. contentValues.put(CONST_BOOK_ID, bookId);
  233. contentValues.put(CONST_BOOK_NAME, bookName);
  234. contentValues.put(CONST_BOOK_DESCRIPTION, description);
  235. contentValues.put(CONST_AVERAGE_RATING, averageRating);
  236. contentValues.put(CONST_IMAGE_URL, imageUrl);
  237. Log.d(MysqliteHelper.class.getName(), "Image URL is :" + imageUrl);
  238. myDatabase.insertOrThrow(CONST_TABLE_STORE_CACHE, null, contentValues);
  239. //myDatabase.close();
  240. }
  241.  
  242. public boolean isStoreCacheEmpty() {
  243. myDatabase =this. getReadableDatabase();
  244. Cursor cur = myDatabase.query(CONST_TABLE_STORE_CACHE,
  245. new String[] { CONST_BOOK_ID }, null, null, null, null, null);
  246. boolean isEmpty = cur.getCount() <= 0;
  247. cur.close();
  248. // myDatabase.close();
  249. return isEmpty;
  250. }
  251.  
  252. public void insertStoreBookCommentsIntoCache(String comment, String userId,
  253. String bookId) throws SQLException {
  254. myDatabase = this.getWritableDatabase();
  255.  
  256. ContentValues contentValues = new ContentValues();
  257.  
  258. // Put the values for Store Table.
  259. contentValues.put(CONST_COMMENT_BOOK_ID, bookId);
  260. contentValues.put(CONST_COMMENT_TEXT, comment);
  261. contentValues.put(CONST_COMMENT_USER_ID, userId);
  262.  
  263. myDatabase
  264. .insertOrThrow(CONST_TABLE_BOOK_COMMENTS, null, contentValues);
  265. //myDatabase.close();
  266. }
  267.  
  268. public int rollBackStoreBookCacheRecord(String bookId) {
  269. myDatabase = this.getWritableDatabase();
  270.  
  271. int status = myDatabase.delete(CONST_TABLE_STORE_CACHE, CONST_BOOK_ID
  272. + "=?", new String[] { bookId });
  273.  
  274. //myDatabase.close();
  275.  
  276. return status;
  277. }
  278.  
  279. public Map<String, String> getStoreItemsFromCache() {
  280. Map<String, String> storeItems = new HashMap<String, String>();
  281. myDatabase = this.getReadableDatabase();
  282. Cursor cur = myDatabase.query(CONST_TABLE_STORE_CACHE, new String[] {
  283. CONST_BOOK_ID, CONST_LOCAL_IMAGE_PATH }, null, null, null,
  284. null, null);
  285. if (cur.getCount() <= 0)
  286. return storeItems;
  287.  
  288. cur.moveToFirst();
  289. while (!cur.isAfterLast()) {
  290. String bookId = cur.getString(0);
  291. String bookCoverLocationOnSDCard = cur.getString(1);
  292.  
  293. Log.d(MysqliteHelper.class.getName(), String.format(
  294. "Fetched Book : %s and its cover location on SD Card :%s",
  295. bookId, bookCoverLocationOnSDCard));
  296. storeItems.put(bookId, bookCoverLocationOnSDCard);
  297.  
  298. cur.moveToNext();
  299.  
  300. }
  301. cur.close();
  302.  
  303. //myDatabase.close();
  304. return storeItems;
  305. }
  306.  
  307. /*
  308. * public ArrayList getStoreBooksInCache() { myDatabase =
  309. * this.getReadableDatabase(); ArrayList prodIDs = new ArrayList();
  310. *
  311. * Cursor cur = myDatabase.query( CONST_TABLE_STORE_CACHE , new String[]
  312. * {CONST_BOOK_ID},null, null, null, null, null); if( cur.getCount() <= 0 )
  313. * return prodIDs;
  314. *
  315. * cur.moveToFirst(); while( ! cur.isAfterLast() ) {
  316. * prodIDs.add(cur.getString(0)); cur.moveToNext(); } cur.close(); return
  317. * prodIDs; }
  318. */
  319.  
  320. public void insertintoUserCredentialTable(String username, String password)
  321. throws SQLException {
  322. myDatabase = this.getWritableDatabase();
  323.  
  324. ContentValues contentValues = new ContentValues();
  325.  
  326. contentValues.put(CUR_USERNAME, username);
  327. contentValues.put(CUR_PASSWORD, password);
  328. String countQuery = "SELECT * FROM " + USER_LOGIN_CREDENTIALS;
  329. int userCount = 0;
  330. Cursor cur = myDatabase.rawQuery(countQuery, null);
  331. System.out.println("cursor count" + cur.getCount());
  332. if (cur.getCount() < 1)
  333. userCount = 1;
  334. cur.close();
  335. if (userCount == 1)
  336. myDatabase.insertOrThrow(USER_LOGIN_CREDENTIALS, null,
  337. contentValues);
  338. else
  339. myDatabase
  340. .update(USER_LOGIN_CREDENTIALS, contentValues, null, null);
  341. cur.close();
  342. //myDatabase.close();
  343. }
  344.  
  345. public static final String CONST_USER_NAME_KEY = "cached_username";
  346. public static final String CONST_USER_PASSWORD_KEY = "cached_password";
  347.  
  348. public Map<String, String> getUserCreds() {
  349. Map<String, String> userLogs = new HashMap<String, String>();
  350. myDatabase = this.getReadableDatabase();
  351. Cursor cur = myDatabase.query(USER_LOGIN_CREDENTIALS, new String[] {
  352. CUR_USERNAME, CUR_PASSWORD }, null, null, null, null, null,
  353. null);
  354. // System.out.println("cursor" +cur.toString());
  355. Log.d("cursorrrr", cur.toString());
  356. // startManagingCursor(cur);
  357. cur.moveToFirst();
  358. while (!cur.isAfterLast()) {
  359. String cachedUsername = cur.getString(0);
  360. String cachedPassword = cur.getString(1);
  361. Log.d(MysqliteHelper.class.getName(), String.format(
  362. "Fetched credentials : %s and password :%s",
  363. cachedUsername, cachedPassword));
  364. userLogs.put(CONST_USER_NAME_KEY, cachedUsername);
  365. userLogs.put(CONST_USER_PASSWORD_KEY, cachedPassword);
  366.  
  367. cur.moveToNext();
  368.  
  369.  
  370. }
  371. cur.close();
  372.  
  373. //myDatabase.close();
  374. // myDatabase.close();
  375. return userLogs;
  376.  
  377. }
  378.  
  379. public static final int BOOK_DETAILS_IN_STORE_CACHE = 2;
  380. public static final int BOOK_DETAILS_IN_LIBRARY_CACHE = 1;
  381.  
  382. private static final String IS_BOOK_IN_LIBRARY_CACHE = "SELECT " +CONST_LIB_BOOK_ID + " FROM "
  383. + CONST_TABLE_LIBRARY_DETAILS + " WHERE "+ CONST_LIB_BOOK_ID +"=?";
  384. private static final String IS_BOOK_IN_STORE_CACHE = "SELECT " +CONST_STR_BOOK_ID + " FROM "
  385. + CONST_TABLE_STORE_DETAILS + " WHERE " + CONST_STR_BOOK_ID + "=?";
  386.  
  387. public int isBookInCache(String bookId) {
  388. myDatabase = this.getReadableDatabase();
  389. //myDatabase = this.getWritableDatabase();
  390. Cursor cur = myDatabase.rawQuery(IS_BOOK_IN_LIBRARY_CACHE,
  391. new String[] { bookId });
  392. if (cur.getCount() > 0) {
  393. return BOOK_DETAILS_IN_LIBRARY_CACHE;
  394. } else {
  395. cur = myDatabase.rawQuery(IS_BOOK_IN_STORE_CACHE,
  396. new String[] { bookId });
  397. if (cur.getCount() > 0) {
  398. return BOOK_DETAILS_IN_STORE_CACHE;
  399. }
  400. }
  401. cur.close();
  402. //myDatabase.close();
  403. return -1;
  404.  
  405. }
  406.  
  407. MysqliteHelper sqlhelpe = new MysqliteHelper(this);
  408. result = sqlhelpe.isBookInCache(Book_Id);
  409. Log.d("Book details are in :" + result,"");
  410. if (result == sqlhelpe.BOOK_DETAILS_IN_LIBRARY_CACHE) {
  411. BookDetailsScreen bkds=new BookDetailsScreen();
  412. System.out.println(Book_Id);
  413. // bkds.GetLibDetails(Book_Id);
  414. GetLibDetails(Book_Id);
  415. Log.d("Getting book details from Library cache","");
  416. // Get the details from Library cache
  417. } else if (result == sqlhelpe.BOOK_DETAILS_IN_STORE_CACHE) {
  418. GetStrDetails(Book_Id);
  419. Log.d("Getting book details from store cache","");
  420. // Get the details from Store cache
  421. } else {
  422. Log.d("Getting book details from the server","");
  423. new AsyncBookDetail_Store().execute("");
  424.  
  425. }
  426.  
  427. public String GetStrDetails(String book_Id){
  428. // HashMap<String, String> cachedProdLib = new HashMap<String, String>();
  429.  
  430. Map<String, String> cachestr = sqlHelper.getStoreItemsDetails_FromCache(book_Id);
  431. //if (!cachestr.entrySet().isEmpty()) { //Iterator<String> LibbookIds = cachedProdList.keySet().iterator();
  432. //String StrBook_Id=cachedProdList.get("book_lib_Id");
  433. StrBook_Id=cachestr.get(MysqliteHelper.Book_STR_Id);
  434. //Log.d(StrBook_Id, "Book Id Fom Data base");
  435. StrBook_Name=cachestr.get(MysqliteHelper.Book_STR_name);
  436. StrBook_Desc=cachestr.get(MysqliteHelper.Book_STR_des);
  437. StrBook_author=cachestr.get(MysqliteHelper.Book_STR_author);
  438. StrBook_Price=cachestr.get(MysqliteHelper.Book_STR_price);
  439. StrBook_Curr=cachestr.get(MysqliteHelper.Book_STR_curr);
  440. Message myMessage = new Message();
  441. Bundle resBundle = new Bundle();
  442. resBundle.putString("descripti",
  443. StrBook_Desc);
  444. resBundle.putString("book_Name",
  445. StrBook_Name);
  446. resBundle.putString("author", StrBook_author);
  447. resBundle.putString("price",StrBook_Price);
  448. resBundle.putString("currency",
  449. StrBook_Curr);
  450.  
  451. myMessage.setData(resBundle);
  452. handler.sendMessage(myMessage);
  453. //}
  454.  
  455. return null;
  456.  
  457. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement