~vono22/narau/trunk

64 by Yvon Tanguy
- Rearrange some classes (remame + change package) to prepare the split of databases (one db file per lang)
1
/*
1 by Yvon Tanguy
Initial import of version 0.5
2
 *  Copyright (C) 2012 Yvon TANGUY
81 by Yvon Tanguy
Dictionary:
3
 *
4
 *  This program is free software: you can redistribute it and/or modify
5
 *  it under the terms of the GNU General Public License as published by
6
 *  the Free Software Foundation, either version 3 of the License, or
7
 *  (at your option) any later version.
8
 *
9
 *  This program is distributed in the hope that it will be useful,
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  GNU General Public License for more details.
13
 *
14
 *  You should have received a copy of the GNU General Public License
15
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
1 by Yvon Tanguy
Initial import of version 0.5
16
 */
64 by Yvon Tanguy
- Rearrange some classes (remame + change package) to prepare the split of databases (one db file per lang)
17
package org.vono.narau.db;
1 by Yvon Tanguy
Initial import of version 0.5
18
19
import java.util.ArrayList;
100 by Yvon Tanguy
Dictionary Online:
20
import java.util.Collections;
1 by Yvon Tanguy
Initial import of version 0.5
21
import java.util.HashMap;
29 by Yvon Tanguy
Better KanjiToWords, but not finish yet.
22
import java.util.Iterator;
1 by Yvon Tanguy
Initial import of version 0.5
23
23.1.3 by Yvon Tanguy
- Fix: make the continue button display after downloading the database to work as expected
24
import org.vono.narau.R;
101 by Yvon Tanguy
All:
25
import org.vono.narau.common.Common;
64 by Yvon Tanguy
- Rearrange some classes (remame + change package) to prepare the split of databases (one db file per lang)
26
import org.vono.narau.models.dictionary.Entry;
27
import org.vono.narau.models.dictionary.KanjiElement;
28
import org.vono.narau.models.dictionary.ReadingElement;
29
import org.vono.narau.models.dictionary.Sense;
1 by Yvon Tanguy
Initial import of version 0.5
30
import org.vono.narau.preferences.Preferences;
100 by Yvon Tanguy
Dictionary Online:
31
import org.vono.narau.utils.Language;
1 by Yvon Tanguy
Initial import of version 0.5
32
33
import android.database.Cursor;
72 by Yvon Tanguy
- Dictionary: Globally it work again. Need tweeks and improvements
34
import android.database.sqlite.SQLiteDatabase;
1 by Yvon Tanguy
Initial import of version 0.5
35
import android.util.Log;
69 by Yvon Tanguy
Load dictionary data: should be OK.
36
import android.util.SparseArray;
1 by Yvon Tanguy
Initial import of version 0.5
37
38
/**
39
 * Function to manage the dictionaries.
40
 * 
41
 */
64 by Yvon Tanguy
- Rearrange some classes (remame + change package) to prepare the split of databases (one db file per lang)
42
public class DictionaryDB extends CommonDB {
1 by Yvon Tanguy
Initial import of version 0.5
43
	private static final String TAG = DictionaryDB.class.getSimpleName();
44
81 by Yvon Tanguy
Dictionary:
45
	static final int VALUE_DB_VERSION = 2;
1 by Yvon Tanguy
Initial import of version 0.5
46
47
	private static final String TABLE_KANJI_ELEM = "k_ele";
48
	private static final String TABLE_READING_ELEM = "r_ele";
49
	private static final String TABLE_SENSE = "sense";
68 by Yvon Tanguy
- Update most of the code to the Split database (the dictionary part still not update)
50
	private static final String TABLE_GLOSS = "gloss";
1 by Yvon Tanguy
Initial import of version 0.5
51
52
	private static final String COL_ID = "id";
53
	private static final String COL_KEB = "keb";
54
	private static final String COL_REB = "reb";
55
	private static final String COL_PRI = "pri";
56
	private static final String COL_INF = "inf";
57
	private static final String COL_NUM = "num";
58
	private static final String COL_STAGK = "stagk";
59
	private static final String COL_STAGR = "stagr";
60
	private static final String COL_POS = "pos";
61
	private static final String COL_XREF = "xref";
62
	private static final String COL_GLOSS_TEXT = "gloss";
63
69 by Yvon Tanguy
Load dictionary data: should be OK.
64
	private static final String[] ARRAY_COL_ID = { COL_ID };
1 by Yvon Tanguy
Initial import of version 0.5
65
66
	/**
67
	 * COL_KEB, COL_PRI, COL_INF
68
	 */
69
	private static final String[] ARRAY_COLS_KELE = { COL_KEB, COL_PRI, COL_INF };
70
71
	/**
72
	 * COL_REB, COL_PRI, COL_INF
73
	 */
74
	private static final String[] ARRAY_COLS_RELE = { COL_REB, COL_PRI, COL_INF };
75
76
	/**
77
	 * COL_STAGK, COL_STAGR, COL_POS, COL_XREF
78
	 */
34 by Yvon Tanguy
KanjiToWordActivity: Almost finish (good for now)
79
	private static final String[] ARRAY_COLS_SENSE = { COL_STAGK, COL_STAGR, COL_POS, COL_XREF, COL_NUM };
1 by Yvon Tanguy
Initial import of version 0.5
80
81
	/**
82
	 * COL_GLOSS_TEXT
83
	 */
84
	private static final String[] ARRAY_COLS_GLOSS = { COL_GLOSS_TEXT };
85
86
	/**
90 by Yvon Tanguy
Dictionary:
87
	 * A big where clause, to match only word (which can have a space or a
88
	 * slash, before and after.
1 by Yvon Tanguy
Initial import of version 0.5
89
	 */
69 by Yvon Tanguy
Load dictionary data: should be OK.
90
	private static final String WHERE_LIKE_WORDMATCH_SELECTION = "%1$s LIKE ? " + "OR %1$s LIKE ? " + "OR %1$s LIKE ? "
91
			+ "OR %1$s LIKE ? " + "OR %1$s LIKE ? " + "OR %1$s LIKE ? " + "OR %1$s LIKE ? " + "OR %1$s LIKE ? "
92
			+ "OR %1$s LIKE ?";
93
	private static final String[] WHERE_LIKE_WORDMATCH_SELECTION_ARGS = { "%1$s", "%1$s %%", "%1$s/%%", "%% %1$s",
94
			"%%/%1$s", "%% %1$s %%", "%%/%1$s/%%", "%%/%1$s %%", "%% %1$s/%%" };
1 by Yvon Tanguy
Initial import of version 0.5
95
	private static final String WHERE_LIKE = "%1$s LIKE ?";
77 by Yvon Tanguy
Dictionary:
96
	private static final String WHERE_ID_EQUAL = COL_ID + " = ?";
97
	private static final String WHERE_ID_NUM_EQUAL = WHERE_ID_EQUAL + " AND " + COL_NUM + " = ?";
1 by Yvon Tanguy
Initial import of version 0.5
98
69 by Yvon Tanguy
Load dictionary data: should be OK.
99
	// Database management:
68 by Yvon Tanguy
- Update most of the code to the Split database (the dictionary part still not update)
100
	private static InfosDB mainDB = null;
101
	private static final ArrayList<InfosDB> langsDB = new ArrayList<InfosDB>();
94 by Yvon Tanguy
Downloader:
102
	private static DatabaseState state;
69 by Yvon Tanguy
Load dictionary data: should be OK.
103
	private static SparseArray<Entry> loadedEntries = null;
1 by Yvon Tanguy
Initial import of version 0.5
104
68 by Yvon Tanguy
- Update most of the code to the Split database (the dictionary part still not update)
105
	/**
106
	 * Init the DictionaryDB.state value
107
	 */
108
	static void initDB() {
94 by Yvon Tanguy
Downloader:
109
		DictionaryDB.state = DatabaseState.bad;
68 by Yvon Tanguy
- Update most of the code to the Split database (the dictionary part still not update)
110
111
		boolean haveMain = false;
112
		boolean haveLang = false;
113
		final ArrayList<InfosDB> listDBs = CommonDB.listDBInfos;
114
		final int size = listDBs.size();
115
		for (int i = 0; i < size; i++) {
116
			final InfosDB db = listDBs.get(i);
98 by Yvon Tanguy
Online dictionary search: partially done.
117
			if (db.type == DatabaseType.dictonary) {
103 by Yvon TANGUY
Dictionary:
118
				if (Language.JAPANESE.equals(db.language)) {
68 by Yvon Tanguy
- Update most of the code to the Split database (the dictionary part still not update)
119
					haveMain = true;
120
					DictionaryDB.mainDB = db;
121
				} else {
122
					haveLang = true;
123
					DictionaryDB.langsDB.add(db);
124
				}
125
			}
126
		}
127
		if (haveMain && haveLang) {
94 by Yvon Tanguy
Downloader:
128
			DictionaryDB.state = DatabaseState.good;
68 by Yvon Tanguy
- Update most of the code to the Split database (the dictionary part still not update)
129
		}
69 by Yvon Tanguy
Load dictionary data: should be OK.
130
131
		DictionaryDB.loadedEntries = new SparseArray<Entry>();
68 by Yvon Tanguy
- Update most of the code to the Split database (the dictionary part still not update)
132
	}
133
94 by Yvon Tanguy
Downloader:
134
	protected static void cleanDB() {
135
		DictionaryDB.state = DatabaseState.bad;
68 by Yvon Tanguy
- Update most of the code to the Split database (the dictionary part still not update)
136
		DictionaryDB.langsDB.clear();
137
		DictionaryDB.mainDB = null;
69 by Yvon Tanguy
Load dictionary data: should be OK.
138
		if (null != loadedEntries) {
139
			DictionaryDB.loadedEntries.clear();
140
			DictionaryDB.loadedEntries = null;
141
		}
68 by Yvon Tanguy
- Update most of the code to the Split database (the dictionary part still not update)
142
	}
143
144
	static boolean isReady() {
94 by Yvon Tanguy
Downloader:
145
		return DictionaryDB.state == DatabaseState.good;
1 by Yvon Tanguy
Initial import of version 0.5
146
	}
147
26 by Yvon Tanguy
* Reports from fdroid branch v0.5.1:
148
	/**
81 by Yvon Tanguy
Dictionary:
149
	 * Can be called only by Preferences.getLangsPriorities()<br/>
72 by Yvon Tanguy
- Dictionary: Globally it work again. Need tweeks and improvements
150
	 * Use Preferences.getLangsPriorities() instead
26 by Yvon Tanguy
* Reports from fdroid branch v0.5.1:
151
	 * 
152
	 * @return All available translation languages in the dictionary database.
153
	 */
100 by Yvon Tanguy
Dictionary Online:
154
	public static ArrayList<Language> getLangs() {
1 by Yvon Tanguy
Initial import of version 0.5
155
100 by Yvon Tanguy
Dictionary Online:
156
		ArrayList<Language> languages = null;
94 by Yvon Tanguy
Downloader:
157
		if (DatabaseState.good == DictionaryDB.state) {
100 by Yvon Tanguy
Dictionary Online:
158
			languages = new ArrayList<Language>(langsDB.size());
68 by Yvon Tanguy
- Update most of the code to the Split database (the dictionary part still not update)
159
			for (InfosDB infosDB : langsDB) {
100 by Yvon Tanguy
Dictionary Online:
160
				languages.add(infosDB.language );
1 by Yvon Tanguy
Initial import of version 0.5
161
			}
162
		}
100 by Yvon Tanguy
Dictionary Online:
163
		Collections.sort(languages);
68 by Yvon Tanguy
- Update most of the code to the Split database (the dictionary part still not update)
164
100 by Yvon Tanguy
Dictionary Online:
165
		return languages;
1 by Yvon Tanguy
Initial import of version 0.5
166
	}
167
68 by Yvon Tanguy
- Update most of the code to the Split database (the dictionary part still not update)
168
	private static ArrayList<InfosDB> getSortedListLangDB() {
169
		final ArrayList<InfosDB> listLangDB = new ArrayList<InfosDB>();
100 by Yvon Tanguy
Dictionary Online:
170
		final ArrayList<Language> languages = Preferences.getLangsPriorities(DatabaseType.dictonary);
68 by Yvon Tanguy
- Update most of the code to the Split database (the dictionary part still not update)
171
172
		// Quick & Dirty
100 by Yvon Tanguy
Dictionary Online:
173
		for (Language language : languages) {
68 by Yvon Tanguy
- Update most of the code to the Split database (the dictionary part still not update)
174
			for (InfosDB dbInfos : DictionaryDB.langsDB) {
100 by Yvon Tanguy
Dictionary Online:
175
				if (language.equals(dbInfos.language)) {
68 by Yvon Tanguy
- Update most of the code to the Split database (the dictionary part still not update)
176
					listLangDB.add(dbInfos);
177
				}
178
			}
179
		}
180
181
		return listLangDB;
182
	}
183
69 by Yvon Tanguy
Load dictionary data: should be OK.
184
	public static Entry loadEntry(int id) {
185
		Entry entry = loadedEntries.get(id);
186
		if (null == entry) {
77 by Yvon Tanguy
Dictionary:
187
			final String[] selectionArgs = new String[1];
188
			selectionArgs[0] = Integer.toString(id);
69 by Yvon Tanguy
Load dictionary data: should be OK.
189
190
			// get kanji element(s)
77 by Yvon Tanguy
Dictionary:
191
			ArrayList<KanjiElement> k_ele = DictionaryDB.loadKanjiElements(selectionArgs);
69 by Yvon Tanguy
Load dictionary data: should be OK.
192
193
			// get reading element(s)
77 by Yvon Tanguy
Dictionary:
194
			ArrayList<ReadingElement> r_ele = DictionaryDB.loadReadingElements(selectionArgs);
69 by Yvon Tanguy
Load dictionary data: should be OK.
195
196
			// get sense element(s)
77 by Yvon Tanguy
Dictionary:
197
			ArrayList<Sense> senses = DictionaryDB.loadSenseElements(selectionArgs);
69 by Yvon Tanguy
Load dictionary data: should be OK.
198
199
			// Build entry:
200
			entry = new Entry(id);
201
			entry.k_ele = k_ele;
202
			entry.r_ele = r_ele;
203
			entry.senses = senses;
72 by Yvon Tanguy
- Dictionary: Globally it work again. Need tweeks and improvements
204
205
			loadedEntries.put(id, entry);
1 by Yvon Tanguy
Initial import of version 0.5
206
		}
207
69 by Yvon Tanguy
Load dictionary data: should be OK.
208
		return entry;
1 by Yvon Tanguy
Initial import of version 0.5
209
	}
210
77 by Yvon Tanguy
Dictionary:
211
	private static ArrayList<KanjiElement> loadKanjiElements(String[] selectionArgs) {
1 by Yvon Tanguy
Initial import of version 0.5
212
		ArrayList<KanjiElement> k_ele = null;
77 by Yvon Tanguy
Dictionary:
213
		Cursor cursor = mainDB.db.query(TABLE_KANJI_ELEM, ARRAY_COLS_KELE, WHERE_ID_EQUAL, selectionArgs, null, null,
214
				COL_NUM);
1 by Yvon Tanguy
Initial import of version 0.5
215
		if (cursor.moveToFirst()) {
216
			k_ele = new ArrayList<KanjiElement>(cursor.getCount());
217
			do {
218
				KanjiElement ke = new KanjiElement();
219
				ke.keb = cursor.getString(0);
220
221
				String tmp = cursor.getString(1);
222
				if (null != tmp) {
223
					ke.ke_pri = Common.splitString(tmp, Common.SLASH);
224
				}
225
226
				tmp = cursor.getString(2);
227
				if (null != tmp) {
228
					ke.ke_inf = Common.splitString(tmp, Common.SLASH);
229
				}
230
231
				k_ele.add(ke);
232
233
			} while (cursor.moveToNext());
234
		}
235
		cursor.close();
236
237
		return k_ele;
238
	}
239
77 by Yvon Tanguy
Dictionary:
240
	private static ArrayList<ReadingElement> loadReadingElements(String[] selectionArgs) {
1 by Yvon Tanguy
Initial import of version 0.5
241
		ArrayList<ReadingElement> r_ele = null;
77 by Yvon Tanguy
Dictionary:
242
		Cursor cursor = mainDB.db.query(TABLE_READING_ELEM, ARRAY_COLS_RELE, WHERE_ID_EQUAL, selectionArgs, null, null,
243
				COL_NUM);
1 by Yvon Tanguy
Initial import of version 0.5
244
		if (cursor.moveToFirst()) {
245
			r_ele = new ArrayList<ReadingElement>(cursor.getCount());
246
247
			do {
248
				ReadingElement re = new ReadingElement();
249
				re.reb = cursor.getString(0);
250
251
				String tmp = cursor.getString(1);
252
				if (null != tmp) {
253
					re.re_pri = Common.splitString(tmp, Common.SLASH);
254
				}
255
256
				tmp = cursor.getString(2);
257
				if (null != tmp) {
258
					re.re_inf = Common.splitString(tmp, Common.SLASH);
259
				}
260
				r_ele.add(re);
261
262
			} while (cursor.moveToNext());
263
264
		} else {
77 by Yvon Tanguy
Dictionary:
265
			Log.e(TAG, "no reading element for: " + selectionArgs[0]);
1 by Yvon Tanguy
Initial import of version 0.5
266
		}
267
		cursor.close();
268
269
		return r_ele;
270
	}
271
77 by Yvon Tanguy
Dictionary:
272
	private static ArrayList<Sense> loadSenseElements(String[] selectionArgs) {
1 by Yvon Tanguy
Initial import of version 0.5
273
		ArrayList<Sense> senses = null;
77 by Yvon Tanguy
Dictionary:
274
		Cursor cursor = mainDB.db.query(TABLE_SENSE, ARRAY_COLS_SENSE, WHERE_ID_EQUAL, selectionArgs, null, null,
275
				COL_NUM);
1 by Yvon Tanguy
Initial import of version 0.5
276
		if (cursor.moveToFirst()) {
277
			senses = new ArrayList<Sense>(cursor.getCount());
278
69 by Yvon Tanguy
Load dictionary data: should be OK.
279
			final ArrayList<InfosDB> lLangsDB = DictionaryDB.langsDB;
280
			final int numLangs = lLangsDB.size();
1 by Yvon Tanguy
Initial import of version 0.5
281
77 by Yvon Tanguy
Dictionary:
282
			final String[] glossSelectionArgs = new String[2];
283
			glossSelectionArgs[0] = selectionArgs[0];
284
1 by Yvon Tanguy
Initial import of version 0.5
285
			do {
286
				Sense sense = new Sense();
287
				sense.stagk = cursor.getString(0);
288
				sense.stagr = cursor.getString(1);
289
				sense.pos = cursor.getString(2);
290
				sense.xref = cursor.getString(3);
291
				int num = cursor.getInt(4);
292
100 by Yvon Tanguy
Dictionary Online:
293
				sense.glosses = new HashMap<Language, String>(numLangs);
77 by Yvon Tanguy
Dictionary:
294
				glossSelectionArgs[1] = Integer.toString(num);
69 by Yvon Tanguy
Load dictionary data: should be OK.
295
				for (int i = 0; i < numLangs; i++) {
296
					final InfosDB infosDB = lLangsDB.get(i);
77 by Yvon Tanguy
Dictionary:
297
					String gloss = loadGloss(infosDB, glossSelectionArgs);
1 by Yvon Tanguy
Initial import of version 0.5
298
					if (null != gloss) {
100 by Yvon Tanguy
Dictionary Online:
299
						sense.glosses.put(infosDB.language, gloss);
1 by Yvon Tanguy
Initial import of version 0.5
300
					}
301
				}
302
303
				senses.add(sense);
304
			} while (cursor.moveToNext());
305
		}
306
		cursor.close();
307
308
		return senses;
309
	}
310
77 by Yvon Tanguy
Dictionary:
311
	private static String loadGloss(InfosDB infosDB, String[] selectionArgs) {
1 by Yvon Tanguy
Initial import of version 0.5
312
		String gloss = null;
313
77 by Yvon Tanguy
Dictionary:
314
		Cursor cursor = infosDB.db.query(TABLE_GLOSS, ARRAY_COLS_GLOSS, WHERE_ID_NUM_EQUAL, selectionArgs, null, null,
315
				null);
1 by Yvon Tanguy
Initial import of version 0.5
316
		if (cursor.moveToFirst()) {
317
			gloss = cursor.getString(0);
318
		}
319
		cursor.close();
320
321
		return gloss;
322
	}
323
29 by Yvon Tanguy
Better KanjiToWords, but not finish yet.
324
	/**
69 by Yvon Tanguy
Load dictionary data: should be OK.
325
	 * Common class for Dictionary iterators.
326
	 * 
327
	 */
328
	private static abstract class InternalIterator implements Iterator<Entry> {
329
		protected Cursor cursor;
330
		protected ArrayList<InfosDB> dbLangs;
331
		protected String limit;
332
333
		public InternalIterator() {
334
			this.cursor = null;
335
			this.dbLangs = DictionaryDB.getSortedListLangDB();
336
			this.limit = null;
337
		}
338
115 by Yvon Tanguy
Bug fixed, in both releases.
339
		@Override
69 by Yvon Tanguy
Load dictionary data: should be OK.
340
		protected void finalize() throws Throwable {
341
			this.close();
342
			super.finalize();
343
		}
344
345
		/**
346
		 * Close the SQLite connection.
347
		 */
348
		public void close() {
349
			if (null != this.cursor) {
350
				this.cursor.close();
351
				this.cursor = null;
352
			}
353
		}
354
355
		public boolean hasNext() {
356
			boolean ret = false;
357
			if (null != this.cursor && !this.cursor.isLast()) {
358
				ret = true;
359
			}
360
			return ret;
361
		}
362
363
		/**
364
		 * 
365
		 * @return the number of elements fetch, or -1 if close.
366
		 */
367
		public int getCount() {
368
			if (null != this.cursor && !this.cursor.isClosed()) {
369
				return this.cursor.getCount();
370
			}
371
			return -1;
372
		}
373
374
		/**
375
		 * throw UnsupportedOperationException()
376
		 */
377
		public void remove() {
378
			throw new UnsupportedOperationException("Can't delete data from the dictionary database");
379
		}
380
381
	}
382
383
	/**
384
	 * The dictionary iterator used by the dictionary activity.
385
	 */
386
	public static class DictionaryIterator extends InternalIterator {
90 by Yvon Tanguy
Dictionary:
387
		private boolean wordMatch;
69 by Yvon Tanguy
Load dictionary data: should be OK.
388
389
		public DictionaryIterator() {
390
			super();
77 by Yvon Tanguy
Dictionary:
391
			super.limit = Integer.toString(Preferences.getInt(R.string.prefNameDictionaryMaxResult));
90 by Yvon Tanguy
Dictionary:
392
			this.wordMatch = Preferences.getBoolean(R.string.prefNameDictionaryWordMatch);
69 by Yvon Tanguy
Load dictionary data: should be OK.
393
		}
394
77 by Yvon Tanguy
Dictionary:
395
		public boolean searchFromJpnKana(String text, boolean withLimit) {
72 by Yvon Tanguy
- Dictionary: Globally it work again. Need tweeks and improvements
396
			super.close();
397
398
			String selection = String.format(WHERE_LIKE, COL_REB);
399
			String[] selectionArgs = new String[1];
90 by Yvon Tanguy
Dictionary:
400
			if (this.wordMatch) {
401
				selectionArgs[0] = text;
402
			} else {
403
				selectionArgs[0] = "%" + text + "%";
404
			}
72 by Yvon Tanguy
- Dictionary: Globally it work again. Need tweeks and improvements
405
77 by Yvon Tanguy
Dictionary:
406
			String limitClause = withLimit ? super.limit : null;
407
72 by Yvon Tanguy
- Dictionary: Globally it work again. Need tweeks and improvements
408
			super.cursor = DictionaryDB.mainDB.db.query(TABLE_READING_ELEM, ARRAY_COL_ID, selection, selectionArgs,
77 by Yvon Tanguy
Dictionary:
409
					null, null, null, limitClause);
72 by Yvon Tanguy
- Dictionary: Globally it work again. Need tweeks and improvements
410
411
			return super.cursor.getCount() > 0;
412
		}
413
77 by Yvon Tanguy
Dictionary:
414
		public boolean searchFromJpnKanji(String text, boolean withLimit) {
72 by Yvon Tanguy
- Dictionary: Globally it work again. Need tweeks and improvements
415
			super.close();
416
417
			String selection = String.format(WHERE_LIKE, COL_KEB);
418
			String[] selectionArgs = new String[1];
90 by Yvon Tanguy
Dictionary:
419
			if (this.wordMatch) {
420
				selectionArgs[0] = text;
421
			} else {
422
				selectionArgs[0] = "%" + text + "%";
423
			}
72 by Yvon Tanguy
- Dictionary: Globally it work again. Need tweeks and improvements
424
77 by Yvon Tanguy
Dictionary:
425
			String limitClause = withLimit ? super.limit : null;
426
72 by Yvon Tanguy
- Dictionary: Globally it work again. Need tweeks and improvements
427
			super.cursor = DictionaryDB.mainDB.db.query(TABLE_KANJI_ELEM, ARRAY_COL_ID, selection, selectionArgs, null,
77 by Yvon Tanguy
Dictionary:
428
					null, null, limitClause);
72 by Yvon Tanguy
- Dictionary: Globally it work again. Need tweeks and improvements
429
430
			return super.cursor.getCount() > 0;
431
		}
432
100 by Yvon Tanguy
Dictionary Online:
433
		public boolean searchToJpn(String text, Language language, boolean withLimit) {
72 by Yvon Tanguy
- Dictionary: Globally it work again. Need tweeks and improvements
434
			super.close();
435
436
			SQLiteDatabase db = null;
437
			for (InfosDB infosDB : DictionaryDB.langsDB) {
100 by Yvon Tanguy
Dictionary Online:
438
				if (infosDB.language.equals(language)) {
72 by Yvon Tanguy
- Dictionary: Globally it work again. Need tweeks and improvements
439
					db = infosDB.db;
440
					break;
441
				}
442
			}
443
444
			if (null == db) {
445
				return false;
69 by Yvon Tanguy
Load dictionary data: should be OK.
446
			}
447
90 by Yvon Tanguy
Dictionary:
448
			final String selection;
449
			final String[] selectionArgs;
450
			if (this.wordMatch) {
451
				selection = String.format(WHERE_LIKE_WORDMATCH_SELECTION, COL_GLOSS_TEXT);
452
				final int lenWhere = WHERE_LIKE_WORDMATCH_SELECTION_ARGS.length;
453
				selectionArgs = new String[lenWhere];
454
				for (int i = 0; i < lenWhere; i++) {
455
					selectionArgs[i] = String.format(WHERE_LIKE_WORDMATCH_SELECTION_ARGS[i], text);
456
				}
457
			} else {
458
				selection = String.format(WHERE_LIKE, COL_GLOSS_TEXT);
459
				selectionArgs = new String[1];
460
				selectionArgs[0] = "%" + text + "%";
69 by Yvon Tanguy
Load dictionary data: should be OK.
461
			}
462
77 by Yvon Tanguy
Dictionary:
463
			String limitClause = withLimit ? super.limit : null;
464
465
			super.cursor = db.query(TABLE_GLOSS, ARRAY_COL_ID, selection, selectionArgs, null, null, null, limitClause);
69 by Yvon Tanguy
Load dictionary data: should be OK.
466
467
			return super.cursor.getCount() > 0;
468
		}
469
470
		public Entry next() {
471
			Entry entry = null;
472
473
			if (null != super.cursor && !super.cursor.isClosed() && cursor.moveToNext()) {
474
				int id = cursor.getInt(0);
475
				entry = DictionaryDB.loadEntry(id);
476
			}
477
			return entry;
478
		}
479
480
	}
481
482
	/**
29 by Yvon Tanguy
Better KanjiToWords, but not finish yet.
483
	 * An iterator for the KanjiToWords activity.
484
	 * 
485
	 */
69 by Yvon Tanguy
Load dictionary data: should be OK.
486
	public static class KanjiToWordsIterator extends InternalIterator {
487
29 by Yvon Tanguy
Better KanjiToWords, but not finish yet.
488
		private static final String[] ARRAY_COLS_KANJI = { "id", "keb", "pri" };
489
		private static final String[] ARRAY_COLS_READING = { "reb" };
34 by Yvon Tanguy
KanjiToWordActivity: Almost finish (good for now)
490
		private static final String SELECTION_KANJI = "keb like ? and num=?";
29 by Yvon Tanguy
Better KanjiToWords, but not finish yet.
491
		private static final String SELECTION_READING = "id=?";
492
		private static final String SELECTION_GLOSS = "id=? and num=?";
493
		private static final String ORDER_READING = "num asc";
494
		private static final String ORDER_GLOSS = "num asc";
495
496
		public KanjiToWordsIterator() {
69 by Yvon Tanguy
Load dictionary data: should be OK.
497
			super();
77 by Yvon Tanguy
Dictionary:
498
			super.limit = Integer.toString(Preferences.getInt(R.string.prefNameKanjiToWordsNWTL));
29 by Yvon Tanguy
Better KanjiToWords, but not finish yet.
499
		}
500
501
		/**
502
		 * 
503
		 * @param codepoint
504
		 *            Set the current kanji to look words
34 by Yvon Tanguy
KanjiToWordActivity: Almost finish (good for now)
505
		 * @param noLimit
506
		 *            If true, remove the limit of results in the query
29 by Yvon Tanguy
Better KanjiToWords, but not finish yet.
507
		 * @return true if there is some words founds
508
		 */
34 by Yvon Tanguy
KanjiToWordActivity: Almost finish (good for now)
509
		public boolean setCodepoint(int codepoint, boolean noLimit) {
29 by Yvon Tanguy
Better KanjiToWords, but not finish yet.
510
511
			if (this.cursor != null) {
512
				this.cursor.close();
513
			}
514
			final String[] selectionArgs = new String[2];
515
			selectionArgs[0] = "%" + new String(Character.toChars(codepoint)) + "%";
516
			selectionArgs[1] = "0";
34 by Yvon Tanguy
KanjiToWordActivity: Almost finish (good for now)
517
			String limitClause = this.limit;
518
			if (noLimit) {
519
				limitClause = null;
520
			}
69 by Yvon Tanguy
Load dictionary data: should be OK.
521
			this.cursor = DictionaryDB.mainDB.db.query(DictionaryDB.TABLE_KANJI_ELEM,
522
					KanjiToWordsIterator.ARRAY_COLS_KANJI, KanjiToWordsIterator.SELECTION_KANJI, selectionArgs, null,
523
					null, null, limitClause);
29 by Yvon Tanguy
Better KanjiToWords, but not finish yet.
524
			return this.cursor.getCount() != 0;
525
		}
526
527
		/**
90 by Yvon Tanguy
Dictionary:
528
		 * @return A minimal Dictionary Entry (id/keb/pri, common reading, one
529
		 *         sense).
29 by Yvon Tanguy
Better KanjiToWords, but not finish yet.
530
		 */
531
		public Entry next() {
532
			if (null != this.cursor && this.cursor.moveToNext()) {
533
534
				final int id = cursor.getInt(0);
535
				final String keb = cursor.getString(1);
536
				final String pri = cursor.getString(2);
537
538
				// The kanji element
539
				Entry entry = new Entry(id);
79 by Yvon Tanguy
DictionaryActivity & KanjiToWordActivity : Use now a ListFragment in common to display dictionary entries.
540
				entry.partial = true;
29 by Yvon Tanguy
Better KanjiToWords, but not finish yet.
541
				KanjiElement ke = new KanjiElement();
542
				ke.keb = keb;
543
544
				ke.ke_pri = Common.splitString(pri, Common.SLASH);
545
				entry.k_ele = new ArrayList<KanjiElement>(1);
546
				entry.k_ele.add(ke);
547
548
				// The reading element
549
				String[] selectionArgs = new String[1];
550
				selectionArgs[0] = Integer.toString(id); // id=
551
69 by Yvon Tanguy
Load dictionary data: should be OK.
552
				Cursor lcursor = DictionaryDB.mainDB.db.query(TABLE_READING_ELEM,
553
						KanjiToWordsIterator.ARRAY_COLS_READING, KanjiToWordsIterator.SELECTION_READING, selectionArgs,
554
						null, null, KanjiToWordsIterator.ORDER_READING);
29 by Yvon Tanguy
Better KanjiToWords, but not finish yet.
555
				if (lcursor.moveToFirst()) {
556
					entry.r_ele = new ArrayList<ReadingElement>(lcursor.getCount());
557
					do {
558
						ReadingElement re = new ReadingElement();
559
						re.reb = lcursor.getString(0);
560
						entry.r_ele.add(re);
561
					} while (lcursor.moveToNext());
562
				}
563
				lcursor.close();
564
				// Sense
565
				selectionArgs = new String[2];
566
				selectionArgs[0] = Integer.toString(id); // id=
567
				selectionArgs[1] = Integer.toString(0); // num=
568
68 by Yvon Tanguy
- Update most of the code to the Split database (the dictionary part still not update)
569
				final ArrayList<InfosDB> lDBLangs = this.dbLangs;
570
				final int langCount = lDBLangs.size();
29 by Yvon Tanguy
Better KanjiToWords, but not finish yet.
571
				entry.senses = new ArrayList<Sense>();
572
				for (int i = 0; i < langCount; i++) {
68 by Yvon Tanguy
- Update most of the code to the Split database (the dictionary part still not update)
573
					final InfosDB infosDB = lDBLangs.get(i);
574
					lcursor = infosDB.db.query(DictionaryDB.TABLE_GLOSS, DictionaryDB.ARRAY_COLS_GLOSS,
575
							KanjiToWordsIterator.SELECTION_GLOSS, selectionArgs, null, null,
576
							KanjiToWordsIterator.ORDER_GLOSS);
29 by Yvon Tanguy
Better KanjiToWords, but not finish yet.
577
					if (lcursor.moveToFirst()) {
578
						Sense sense = new Sense();
100 by Yvon Tanguy
Dictionary Online:
579
						sense.glosses = new HashMap<Language, String>(1);
580
						sense.glosses.put(infosDB.language, lcursor.getString(0));
29 by Yvon Tanguy
Better KanjiToWords, but not finish yet.
581
						entry.senses.add(sense);
582
						i = langCount;
583
					}
584
					lcursor.close();
585
				}
586
				return entry;
587
			}
588
			this.cursor.close();
589
			this.cursor = null;
590
			return null;
591
		}
592
593
	}
594
1 by Yvon Tanguy
Initial import of version 0.5
595
}