Guest User

Untitled

a guest
Jun 25th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.85 KB | None | 0 0
  1. package applab.search.client;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.text.SimpleDateFormat;
  7. import java.util.ArrayList;
  8. import java.util.Calendar;
  9. import java.util.List;
  10.  
  11. import net.sf.json.JSONArray;
  12. import net.sf.json.JSONException;
  13. import net.sf.json.JSONObject;
  14. import net.sf.json.JSONSerializer;
  15.  
  16. import android.content.ContentValues;
  17. import android.os.Bundle;
  18. import android.os.Handler;
  19. import android.os.Message;
  20. import android.util.Log;
  21. import applab.client.ApplabActivity;
  22. import applab.client.PropertyStorage;
  23. import applab.client.search.R;
  24.  
  25. /**
  26. * A custom XML parser that also sorts and adds content to search sequence
  27. * database.
  28. */
  29.  
  30. public class KeywordParser {
  31. /** for debugging purposes in adb logcat */
  32. private static final String LOG_TAG = "KeywordParser";
  33. private static final String ALLOWED_MENU = "allowed";
  34. private static final String UPDATED_MENU_ITEMS = "updatedItems";
  35. private static final String DELETED_MENU_ITEMS = "deletedItems";
  36. private static final String UPDATED_IMAGES = "updatedImages";
  37. private static final String DELETED_IMAGES = "deletedImages";
  38. private static final String MENU_NAME = "menuName";
  39. private static final String MENU_ID = "menuId";
  40. private static final String ITEM_NAME = "itemName";
  41. private static final String ITEM_ID = "itemId";
  42. private static final String ITEM_PARENT_ID = "pId";
  43. private static final String ITEM_POSITION = "position";
  44. private static final String ITEM_CONTENT = "content";
  45. private static final String MENU_DATABASE_TABLE = null;
  46. private int progressLevel = 0;
  47. private Storage storage;
  48.  
  49. /** handler to which progress updates are sent */
  50. private Handler progressHandler;
  51.  
  52. private InputStream keywordStream;
  53.  
  54. /** handler to which responses are sent */
  55. private Handler responseHandler;
  56.  
  57. private Bundle bundle;
  58. private Integer nodeCount;
  59. private static String keywordVersion;
  60. private Integer addedNodes;
  61. private Integer deletedNodes;
  62. private String jsonKeywordString;
  63. private JSONObject jsonKeywordObject;
  64. private ArrayList<String> Ids = new ArrayList<String>();
  65. private ContentRow contentRow = new ContentRow();
  66.  
  67. public KeywordParser(Handler progressHandler, Handler responseHandler,
  68. InputStream newKeywordStream) {
  69. this.keywordStream = newKeywordStream;
  70. this.responseHandler = responseHandler;
  71. this.progressHandler = progressHandler;
  72. }
  73.  
  74. /**
  75. * Obsoleted: loading the xml file into DOM takes a lot of memory. Now using
  76. * walk() instead, which uses XMLPullParser
  77. */
  78. public void run() {
  79. try {
  80. addedNodes = 0;
  81. deletedNodes = 0;
  82.  
  83. // This line was causing problems on android 2.2 (IDEOS)
  84. // this.xmlParser.reset();
  85.  
  86. jsonKeywordParser();
  87. if (nodeCount == null || keywordVersion == null) {
  88. this.responseHandler
  89. .sendEmptyMessage(GlobalConstants.KEYWORD_PARSE_ERROR);
  90. } else if (keywordVersion != "") {
  91. KeywordParser.storeKeywordsVersion(keywordVersion);
  92. Log.d(LOG_TAG, "Stored version: " + keywordVersion);
  93.  
  94. // let UI handler know
  95. Log.d(LOG_TAG, "Finished Parsing Keywords ... Added: "
  96. + addedNodes + ", Deleted: " + deletedNodes);
  97. this.responseHandler
  98. .sendEmptyMessage(GlobalConstants.KEYWORD_PARSE_SUCCESS);
  99. }
  100. } catch (IllegalStateException e) {
  101. this.responseHandler
  102. .sendEmptyMessage(GlobalConstants.KEYWORD_PARSE_ERROR);
  103. Log.d(LOG_TAG, "IllegalStateException: " + e);
  104. } finally {
  105. if (this.storage != null) {
  106. this.storage.close();
  107. }
  108. }
  109. }
  110.  
  111. /**
  112. * @param rowId
  113. * @param order
  114. * @param category
  115. * @param attribution
  116. * @param updated
  117. * @param keyword
  118. * @param content
  119. * @param menu
  120. * @param menuId
  121. * @param position
  122. * @param pid
  123. */
  124. public void addMenuItemRecord(String rowId, Integer position,
  125. String content, String pid, String menuName, String menuId) {
  126. ContentValues addValues = new ContentValues();
  127.  
  128. addValues.put(Storage.KEY_ROWID, rowId);
  129. addValues.put(Storage.KEY_PARENT_ID, pid);
  130. addValues.put(Storage.KEY_POSITION, position);
  131. addValues.put(Storage.KEY_MENU, menuName.replace("_", " "));
  132. addValues.put(Storage.KEY_CONTENT, content.replace("_", " "));
  133. addValues.put(Storage.KEY_MENU_ID, menuId);
  134.  
  135. storage.insertContent(GlobalConstants.MENU_ITEMS_DATABASE_TABLE,
  136. addValues);
  137. addedNodes++;
  138. // Use this to increment progress
  139. incrementProgressLevel();
  140. }
  141.  
  142. public void addMenuRecord(String rowId, String menuName) {
  143. ContentValues addValues = new ContentValues();
  144.  
  145. addValues.put(Storage.KEY_ROWID, rowId);
  146. addValues.put(Storage.KEY_MENU, menuName.replace("_", " "));
  147.  
  148. storage.insertContent(GlobalConstants.MENU_DATABASE_TABLE, addValues);
  149. addedNodes++;
  150. // Use this to increment progress
  151. incrementProgressLevel();
  152. }
  153.  
  154. /**
  155. * Call this each time to increment the progress bar by one level
  156. */
  157. private void incrementProgressLevel() {
  158. Message message = progressHandler.obtainMessage();
  159. bundle.putInt("node", ++progressLevel);
  160. Log.d(LOG_TAG, "Processed : " + progressLevel + " of " + nodeCount);
  161. message.setData(bundle);
  162. progressHandler.sendMessage(message);
  163. }
  164.  
  165. /**
  166. * Record last update version in preferences
  167. *
  168. * @param document
  169. */
  170. public void storeKeywordsVersion() {
  171. String version = getKeywordsVersion();
  172. storeKeywordsVersion(version);
  173. }
  174.  
  175. static void storeKeywordsVersion(String version) {
  176. PropertyStorage.getLocal().setValue(
  177. GlobalConstants.KEYWORDS_VERSION_KEY, version);
  178. }
  179.  
  180. public String getKeywordsVersion() {
  181. // TODO: Get keywords version from json string
  182. // keywordVersion =
  183. // this.jsonKeywordObject.getString(VERSION_ATTRIBUTE_NAME);
  184. // for now keywordversion is set to current date
  185. Calendar currentDate = Calendar.getInstance();
  186. SimpleDateFormat formatter = new SimpleDateFormat(
  187. "yyyy/MMM/dd HH:mm:ss");
  188. String version = formatter.format(currentDate.getTime());
  189. if (version.length() > 0) {
  190. return version;
  191. }
  192. return "";
  193. }
  194.  
  195. private void jsonKeywordParser() {
  196. this.jsonKeywordConvertor();
  197. try {
  198.  
  199. // Getting Arrays
  200. JSONArray menusArray = this.jsonKeywordObject
  201. .getJSONArray(ALLOWED_MENU);
  202. nodeCount += menusArray.size();
  203. JSONArray updatedMenuItemsArray = this.jsonKeywordObject
  204. .getJSONArray(UPDATED_MENU_ITEMS);
  205. nodeCount += updatedMenuItemsArray.size();
  206. JSONArray deletedMenuItemsArray = this.jsonKeywordObject
  207. .getJSONArray(DELETED_MENU_ITEMS);
  208. nodeCount += deletedMenuItemsArray.size();
  209. JSONArray updatedImageArray = this.jsonKeywordObject
  210. .getJSONArray(UPDATED_IMAGES);
  211. nodeCount += updatedImageArray.size();
  212. JSONArray deletedImageArray = this.jsonKeywordObject
  213. .getJSONArray(DELETED_IMAGES);
  214. nodeCount += deletedImageArray.size();
  215.  
  216. if (keywordVersion != "" && nodeCount > 0) {
  217. nodeCount += 1; // Add one for the start document node
  218. Log.d(LOG_TAG, "Total nodes: " + nodeCount);
  219. Log.d(LOG_TAG, "Keyword version: " + keywordVersion);
  220.  
  221. bundle = new Bundle();
  222.  
  223. // Show parse dialog (send signal with total node count)
  224. Message message = responseHandler.obtainMessage();
  225. bundle.putInt("nodeCount", nodeCount);
  226. message.what = GlobalConstants.KEYWORD_PARSE_GOT_NODE_TOTAL;
  227. message.setData(bundle);
  228. responseHandler.sendMessage(message);
  229.  
  230. // If this is the first node, we open the storage
  231. if (storage == null) {
  232. storage = new Storage(ApplabActivity.getGlobalContext());
  233. storage.open();
  234. }
  235. }
  236.  
  237. // pass arrays to due methods for processing
  238. updateMenuTable(menusArray);
  239. updateMenuItemsTable(updatedMenuItemsArray);
  240. deleteMenuItems(deletedMenuItemsArray);
  241. updateImages(updatedImageArray, deletedImageArray);
  242. } catch (JSONException e) {
  243. e.printStackTrace();
  244. }
  245. storeKeywordsVersion();
  246. }
  247.  
  248. /**
  249. * Loops through all Deleted MenuItems
  250. *
  251. * @param deletedMenuItemsArray
  252. */
  253. private void deleteMenuItems(JSONArray deletedMenuItemsArray) {
  254.  
  255. for (int i = 0; i < deletedMenuItemsArray.size(); i++) {
  256. JSONObject menuItems = deletedMenuItemsArray.getJSONObject(i);
  257.  
  258. // Pick the item id whose row to delete
  259. String rowId = menuItems.getString(ITEM_ID);
  260. if (storage != null) {
  261. boolean isDeleted = storage.deleteMenuItemEntry(
  262. GlobalConstants.MENU_ITEMS_DATABASE_TABLE, rowId);
  263. if (isDeleted == true) {
  264. deletedNodes++;
  265. // Use this to increment progress
  266. incrementProgressLevel();
  267. }
  268. }
  269. }
  270. }
  271.  
  272. /**
  273. * Loops through all Updated MenuItems and upserts
  274. *
  275. * @param updatedMenuItemsArray
  276. */
  277. private void updateMenuItemsTable(JSONArray updatedMenuItemsArray) {
  278. for (int i = 0; i < updatedMenuItemsArray.size(); i++) {
  279. JSONObject menuItems = updatedMenuItemsArray.getJSONObject(i);
  280.  
  281. // Updating database with new info
  282. contentRow.setRowId(menuItems.getString(MENU_ID));
  283. contentRow.setMenuId(menuItems.getString(ITEM_ID));
  284. contentRow.setName(menuItems.getString(ITEM_NAME));
  285. contentRow.setParentId(menuItems.getString(ITEM_PARENT_ID));
  286. contentRow.setPosition(menuItems.getInt(ITEM_POSITION));
  287. contentRow.setContent(menuItems.getString(ITEM_CONTENT));
  288. contentRow.itemSave();
  289. contentRow = null;
  290. }
  291. }
  292.  
  293. /**
  294. * Loops through all Menus: If a menu exists thats not in the updated list
  295. * then delete it together with all its children; otherwise upsert
  296. *
  297. * @param menusArray
  298. *
  299. */
  300. private void updateMenuTable(JSONArray menusArray) {
  301. Ids = storage.getLocalMenuIds(MENU_DATABASE_TABLE);
  302. ArrayList<String> newIds = new ArrayList<String>();
  303. JSONObject menu = new JSONObject();
  304. for (int i = 0; i < menusArray.size(); i++) {
  305. menu = menusArray.getJSONObject(i);
  306. newIds.add(menu.getString(MENU_ID));
  307.  
  308. }
  309. // Check if local menu ids match the new ones; if local id doesn't exist
  310. // among new ids delete it and all its children
  311. if (!Ids.isEmpty()) {
  312. for (int i = 0; i < menusArray.size(); i++) {
  313. if (!newIds.contains(Ids.get(i))) {
  314. storage.deleteMenuEntry(
  315. GlobalConstants.MENU_DATABASE_TABLE, Ids.get(i));
  316. storage.deleteMenuItemEntry(
  317. GlobalConstants.MENU_ITEMS_DATABASE_TABLE,
  318. Ids.get(i));
  319. }
  320. }
  321. }
  322. for (int i = 0; i < menusArray.size(); i++) {
  323. contentRow.setMenuId(newIds.get(i));
  324. contentRow.setName(menu.getString(MENU_NAME));
  325. contentRow.save();
  326. contentRow = null;
  327. }
  328. }
  329.  
  330. /**
  331. * @param updatedImageArray
  332. * @param deletedImageArray
  333. */
  334. private void updateImages(JSONArray updatedImageArray,
  335. JSONArray deletedImageArray) {
  336. List<String> updatedImages = new ArrayList<String>();
  337. List<String> deletedImages = new ArrayList<String>();
  338.  
  339. // Looping through all updated Images to load them into a list
  340. for (int i = 0; i < updatedImageArray.size(); i++) {
  341. JSONObject menuItems = deletedImageArray.getJSONObject(i);
  342.  
  343. // Pick the item id whose row to delete
  344. updatedImages.add(menuItems.getString(ITEM_ID));
  345. }
  346.  
  347. for (int i = 0; i < deletedImageArray.size(); i++) {
  348. JSONObject menuItems = deletedImageArray.getJSONObject(i);
  349.  
  350. // Pick the item id whose row to delete
  351. deletedImages.add(menuItems.getString(ITEM_ID));
  352. }
  353. ImageManager.updatePhoneImages(updatedImages, deletedImages);
  354. }
  355.  
  356. public JSONObject jsonKeywordConvertor() {
  357. try {
  358. BufferedReader reader = new BufferedReader(new InputStreamReader(
  359. this.keywordStream, "iso-8859-1"), 8);
  360. StringBuilder sb = new StringBuilder();
  361. String line = null;
  362. while ((line = reader.readLine()) != null) {
  363. sb.append(line + "\n");
  364. }
  365. this.keywordStream.close();
  366. this.jsonKeywordString = sb.toString();
  367. } catch (Exception e) {
  368. Log.e("Buffer Error", "Error converting result " + e.toString());
  369. }
  370.  
  371. // Parse the JSON string to a JSON object
  372. try {
  373. this.jsonKeywordObject = (JSONObject) JSONSerializer
  374. .toJSON(this.jsonKeywordString);
  375. } catch (JSONException e) {
  376. Log.e("JSON Parser", "Error parsing data " + e.toString());
  377. }
  378.  
  379. // return JSON Object
  380. return jsonKeywordObject;
  381. }
  382.  
  383. private class ContentRow {
  384. // Row Fields
  385. String rowId = null;
  386. Integer position = null;
  387. String menuId = null;
  388. String content = "";
  389. String itemId = null;
  390. String menuName = null;
  391. String parentId = null;
  392.  
  393. public void itemSave() {
  394. addMenuItemRecord(rowId, position, content, parentId, itemId,
  395. menuId);
  396. }
  397.  
  398. public void save() {
  399. addMenuRecord(menuId, menuName);
  400. }
  401.  
  402. public void setParentId(String pId) {
  403. this.parentId = pId;
  404.  
  405. }
  406.  
  407. public void setPosition(int posn) {
  408. this.position = posn;
  409.  
  410. }
  411.  
  412. public void setMenuId(String menuId) {
  413. this.menuId = menuId;
  414. }
  415.  
  416. public void setName(String menuName) {
  417. this.menuName = menuName;
  418.  
  419. }
  420.  
  421. public void setContent(String content) {
  422. this.content += content;
  423. }
  424.  
  425. public void setRowId(String id) {
  426. this.rowId = id;
  427. }
  428. }
  429. }
Add Comment
Please, Sign In to add comment