Added Android code
[wl-app.git] / Android / folioreader / src / main / java / com / folioreader / model / sqlite / DictionaryTable.java
1 package com.folioreader.model.sqlite;
2
3 import android.content.ContentValues;
4 import android.content.Context;
5 import android.database.Cursor;
6 import android.database.sqlite.SQLiteDatabase;
7
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.Map;
11
12 /**
13  * @author gautam chibde on 28/6/17.
14  */
15
16 public class DictionaryTable {
17
18     public static final String TABLE_NAME = "dictionary_table";
19
20     public static final String ID = "_id";
21     public static final String WORD = "word";
22     public static final String MEANING = "meaning";
23     private SQLiteDatabase database;
24
25     public DictionaryTable(Context context) {
26         FolioDatabaseHelper dbHelper = new FolioDatabaseHelper(context);
27         database = dbHelper.getWritableDatabase();
28     }
29
30     public static final String SQL_CREATE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ( " + ID
31             + " INTEGER PRIMARY KEY AUTOINCREMENT" + ","
32             + WORD + " TEXT" + ","
33             + MEANING + " TEXT" + ")";
34
35     public static final String SQL_DROP = "DROP TABLE IF EXISTS " + TABLE_NAME;
36
37     public boolean insertWord(String word, String meaning) {
38         ContentValues values = new ContentValues();
39         values.put(WORD, word);
40         values.put(MEANING, meaning);
41         return database.insert(TABLE_NAME, null, values) > 0;
42     }
43
44     public void insert(Map<String, String> map) {
45         database.beginTransaction();
46         for (String key : map.keySet()) {
47             insertWord(key.toLowerCase(), map.get(key));
48         }
49         database.setTransactionSuccessful();
50         database.endTransaction();
51     }
52
53     public String getMeaningForWord(String word) {
54         Cursor c = database.rawQuery("SELECT * FROM "
55                 + TABLE_NAME +
56                 " WHERE " + WORD + " = '" + word.trim() + "'", null);
57         if (c.moveToFirst()) {
58             String toRetuen = c.getString(2);
59             c.close();
60             return toRetuen;
61         }
62         c.close();
63         return null;
64     }
65
66     public List<String> getMeaning(String word) {
67         List<String> words = new ArrayList<>();
68         String meaning = getMeaningForWord(word);
69         if (meaning != null) {
70             words.add(meaning);
71             return words;
72         } else {
73             return getProbableCombinations(word);
74         }
75     }
76
77     private List<String> getProbableCombinations(String word) {
78         List<String> combinations = new ArrayList<>();
79         for (int i = 1; i <= 3; i++) {
80             String m = getMeaningForWord(word.substring(0, word.length() - i));
81             if (m != null) {
82                 combinations.add(m);
83             }
84         }
85         return combinations;
86     }
87 }