|
2 | 2 |
|
3 | 3 | import com.google.gson.annotations.SerializedName;
|
4 | 4 | import com.j256.ormlite.dao.Dao;
|
| 5 | +import com.j256.ormlite.dao.GenericRawResults; |
| 6 | +import com.j256.ormlite.dao.RawRowMapper; |
5 | 7 | import com.j256.ormlite.dao.RuntimeExceptionDao;
|
6 | 8 | import com.j256.ormlite.field.DatabaseField;
|
7 |
| -import com.j256.ormlite.stmt.SelectArg; |
8 | 9 | import com.j256.ormlite.table.DatabaseTable;
|
9 | 10 | import com.zulip.android.ZulipApp;
|
10 | 11 | import com.zulip.android.util.ZLog;
|
11 | 12 |
|
12 | 13 | import org.apache.commons.lang.builder.HashCodeBuilder;
|
13 | 14 |
|
| 15 | +import java.io.IOException; |
14 | 16 | import java.sql.SQLException;
|
15 | 17 | import java.util.Collections;
|
16 | 18 | import java.util.Comparator;
|
@@ -73,6 +75,15 @@ public Person(String name, String email, String avatarURL) {
|
73 | 75 | this.isActive = false;
|
74 | 76 | }
|
75 | 77 |
|
| 78 | + public Person(int id, String name, String email, String avatarURL, boolean isBot, boolean isActive) { |
| 79 | + this.setId(id); |
| 80 | + this.setName(name); |
| 81 | + this.setEmail(email); |
| 82 | + this.setAvatarURL(avatarURL); |
| 83 | + this.setActive(isActive); |
| 84 | + this.setBot(isBot); |
| 85 | + } |
| 86 | + |
76 | 87 | /**
|
77 | 88 | * Construct an empty Person object.
|
78 | 89 | */
|
@@ -163,16 +174,42 @@ public static Person getByEmail(ZulipApp app, String email) {
|
163 | 174 | return null;
|
164 | 175 | }
|
165 | 176 |
|
| 177 | + @SuppressWarnings("WeakerAccess") |
166 | 178 | public static Person getByEmail(Dao<Person, ?> dao, String email) {
|
167 | 179 | try {
|
168 |
| - return dao.queryBuilder().where() |
169 |
| - .eq(Person.EMAIL_FIELD, new SelectArg(email.toLowerCase())) |
170 |
| - .queryForFirst(); |
| 180 | + // Using raw query to avoid errors with queryBuilder in ormlite. |
| 181 | + // Listing of column names is necessary to avoid haphazard ordering of attributes |
| 182 | + // in rawResults. |
| 183 | + GenericRawResults<Person> rawResults = |
| 184 | + dao.queryRaw( |
| 185 | + "select " + Person.ID_FIELD + "," + Person.NAME_FIELD + "," |
| 186 | + + Person.EMAIL_FIELD + "," + Person.AVATARURL_FIELD + "," |
| 187 | + + Person.ISBOT_FIELD + "," + Person.ISACTIVE_FIELD + " from " + "people" |
| 188 | + + " where " + Person.EMAIL_FIELD + " = ? ;", |
| 189 | + new RawRowMapper<Person>() { |
| 190 | + public Person mapRow(String[] columnNames, |
| 191 | + String[] resultColumns) { |
| 192 | + return new Person(Integer.parseInt(resultColumns[0]), |
| 193 | + resultColumns[1], resultColumns[2], resultColumns[3], |
| 194 | + Boolean.parseBoolean(resultColumns[4]), |
| 195 | + Boolean.parseBoolean(resultColumns[5])); |
| 196 | + } |
| 197 | + }, email.toLowerCase()); |
| 198 | + |
| 199 | + // we only care about the first result |
| 200 | + Person returnValue = rawResults.getFirstResult(); |
| 201 | + rawResults.close(); |
| 202 | + return returnValue; |
171 | 203 | } catch (SQLException e) {
|
172 | 204 | throw new RuntimeException(e);
|
| 205 | + } catch (IOException e) { |
| 206 | + ZLog.logException(e); |
173 | 207 | }
|
| 208 | + |
| 209 | + return null; |
174 | 210 | }
|
175 | 211 |
|
| 212 | + @SuppressWarnings("WeakerAccess") |
176 | 213 | public static Person getOrUpdate(ZulipApp app, String email, String name,
|
177 | 214 | String avatarURL, Map<String, Person> personCache) {
|
178 | 215 |
|
@@ -214,6 +251,11 @@ public static Person getOrUpdate(ZulipApp app, String email, String name,
|
214 | 251 | return getOrUpdate(app, email, name, avatarURL, null);
|
215 | 252 | }
|
216 | 253 |
|
| 254 | + @SuppressWarnings("WeakerAccess") |
| 255 | + public void setBot(boolean isBot) { |
| 256 | + this.isBot = isBot; |
| 257 | + } |
| 258 | + |
217 | 259 | public void setActive(boolean active) {
|
218 | 260 | isActive = active;
|
219 | 261 | }
|
|
0 commit comments