|
@@ -28,6 +28,7 @@ import android.database.sqlite.SQLiteDatabase;
|
28
|
28
|
import android.database.sqlite.SQLiteOpenHelper;
|
29
|
29
|
import android.database.sqlite.SQLiteStatement;
|
30
|
30
|
import android.util.Log;
|
|
31
|
+import java.util.Arrays;
|
31
|
32
|
|
32
|
33
|
import java.util.HashMap;
|
33
|
34
|
import java.util.List;
|
|
@@ -41,6 +42,8 @@ import java.util.Map;
|
41
|
42
|
*/
|
42
|
43
|
public class DataHelper {
|
43
|
44
|
|
|
45
|
+ public static final String[] SHARED_KEYS = new String[] { "contactid" };
|
|
46
|
+
|
44
|
47
|
private static final String DATABASE_NAME = "contexthome.db";
|
45
|
48
|
private static final int DATABASE_VERSION = 1;
|
46
|
49
|
|
|
@@ -67,42 +70,52 @@ public class DataHelper {
|
67
|
70
|
|
68
|
71
|
public void registerAction(final String module, final Map<String, String> actions) {
|
69
|
72
|
// TODO: It'd be nice if the two statements could be merged
|
70
|
|
- insertActionStatement.bindString(0, module);
|
71
|
|
- updateActionStatement.bindString(0, module);
|
|
73
|
+ insertActionStatement.bindString(1, module);
|
|
74
|
+ updateActionStatement.bindString(1, module);
|
72
|
75
|
|
73
|
76
|
for (Map.Entry<String, String> action : actions.entrySet()) {
|
74
|
|
- insertActionStatement.bindString(1, action.getKey());
|
75
|
|
- insertActionStatement.bindString(2, action.getValue());
|
76
|
|
- updateActionStatement.bindString(1, action.getKey());
|
77
|
|
- updateActionStatement.bindString(2, action.getValue());
|
|
77
|
+ insertActionStatement.bindString(2, action.getKey());
|
|
78
|
+ insertActionStatement.bindString(3, action.getValue());
|
|
79
|
+ updateActionStatement.bindString(2, action.getKey());
|
|
80
|
+ updateActionStatement.bindString(3, action.getValue());
|
78
|
81
|
|
79
|
82
|
for (ContextType context : contexts) {
|
80
|
|
- insertActionStatement.bindString(3, context.getName());
|
81
|
|
- insertActionStatement.bindString(4, context.getValue());
|
82
|
|
- updateActionStatement.bindString(3, context.getName());
|
83
|
|
- updateActionStatement.bindString(4, context.getValue());
|
|
83
|
+ insertActionStatement.bindString(4, context.getName());
|
|
84
|
+ insertActionStatement.bindString(5, context.getValue());
|
|
85
|
+ updateActionStatement.bindString(4, context.getName());
|
|
86
|
+ updateActionStatement.bindString(5, context.getValue());
|
84
|
87
|
insertActionStatement.execute();
|
85
|
88
|
updateActionStatement.execute();
|
86
|
89
|
}
|
87
|
90
|
}
|
88
|
91
|
}
|
89
|
92
|
|
90
|
|
- public Map<String, Map<String, Integer>> getActions(final String module) {
|
91
|
|
- final Map<String, Map<String, Integer>> res = new HashMap<String, Map<String, Integer>>();
|
|
93
|
+ public Map<String, Integer> getActions(final String module) {
|
|
94
|
+ final Map<String, Integer> res = new HashMap<String, Integer>();
|
92
|
95
|
|
93
|
96
|
final StringBuilder query = new StringBuilder("SELECT sum(number) AS total, "
|
94
|
|
- + " actiontype, actionvalue FROM actions WHERE module = ? AND (0");
|
|
97
|
+ + " actiontype, actionvalue FROM actions WHERE (module = ?");
|
|
98
|
+
|
|
99
|
+ for (String key : SHARED_KEYS) {
|
|
100
|
+ query.append(" OR actiontype = \'");
|
|
101
|
+ query.append(key);
|
|
102
|
+ query.append('\'');
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ query.append(") AND (0");
|
95
|
106
|
final String[] params = new String[1 + contexts.size() * 2];
|
96
|
107
|
final String extraQuery = " OR (contexttype = ? AND contextvalue = ?)";
|
97
|
108
|
|
98
|
109
|
int i = 1;
|
|
110
|
+ params[0] = module;
|
99
|
111
|
for (ContextType context : contexts) {
|
100
|
112
|
params[i++] = context.getName();
|
101
|
113
|
params[i++] = context.getValue();
|
102
|
114
|
query.append(extraQuery);
|
103
|
115
|
}
|
104
|
116
|
|
105
|
|
- query.append(") GROUP BY contexttype, contextvalue ORDER BY total DESC");
|
|
117
|
+ query.append(") GROUP BY actiontype, actionvalue ORDER BY total DESC");
|
|
118
|
+ Log.d("DataHelper", "Query: " + query + ", Params: " + Arrays.toString(params));
|
106
|
119
|
final Cursor cursor = db.rawQuery(query.toString(), params);
|
107
|
120
|
|
108
|
121
|
if (cursor.moveToFirst()) {
|
|
@@ -111,18 +124,15 @@ public class DataHelper {
|
111
|
124
|
final int valueColumn = cursor.getColumnIndex("actionvalue");
|
112
|
125
|
|
113
|
126
|
do {
|
114
|
|
- final String type = cursor.getString(typeColumn);
|
115
|
|
-
|
116
|
|
- if (!res.containsKey(type)) {
|
117
|
|
- res.put(type, new HashMap<String, Integer>());
|
118
|
|
- }
|
119
|
|
-
|
120
|
|
- res.get(type).put(cursor.getString(valueColumn), cursor.getInt(totalColumn));
|
|
127
|
+ res.put(cursor.getString(typeColumn) + "/"
|
|
128
|
+ + cursor.getString(valueColumn), cursor.getInt(totalColumn));
|
121
|
129
|
} while (cursor.moveToNext());
|
122
|
130
|
}
|
123
|
131
|
|
124
|
132
|
cursor.close();
|
125
|
133
|
|
|
134
|
+ Log.d("DataHelper", res.toString());
|
|
135
|
+
|
126
|
136
|
return res;
|
127
|
137
|
}
|
128
|
138
|
|