Skip to content
This repository was archived by the owner on Jul 25, 2024. It is now read-only.

Commit 68ec133

Browse files
Sam1301timabbott
authored andcommitted
Use server generated Person ids.
1 parent 1993602 commit 68ec133

File tree

4 files changed

+12
-60
lines changed

4 files changed

+12
-60
lines changed

app/src/main/java/com/zulip/android/ZulipApp.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.j256.ormlite.dao.ReferenceObjectCache;
2525
import com.j256.ormlite.dao.RuntimeExceptionDao;
2626
import com.j256.ormlite.misc.TransactionManager;
27-
import com.j256.ormlite.stmt.SelectArg;
2827
import com.zulip.android.activities.ZulipActivity;
2928
import com.zulip.android.database.DatabaseHelper;
3029
import com.zulip.android.models.Emoji;
@@ -266,26 +265,7 @@ public void write(JsonWriter out, UserConfigurationResponse value) throws IOExce
266265

267266
@Override
268267
public UserConfigurationResponse read(JsonReader in) throws IOException {
269-
UserConfigurationResponse res = nestedGson.fromJson(in, UserConfigurationResponse.class);
270-
271-
RuntimeExceptionDao<Person, Object> personDao = ZulipApp.this.getDao(Person.class);
272-
for (int i = 0; i < res.getRealmUsers().size(); i++) {
273-
274-
Person currentPerson = res.getRealmUsers().get(i);
275-
Person foundPerson = null;
276-
try {
277-
foundPerson = personDao.queryBuilder().where().eq(Person.EMAIL_FIELD,
278-
new SelectArg(Person.EMAIL_FIELD, currentPerson.getEmail()))
279-
.queryForFirst();
280-
if (foundPerson != null) {
281-
currentPerson.setId(foundPerson.getId());
282-
}
283-
} catch (SQLException e) {
284-
ZLog.logException(e);
285-
}
286-
}
287-
288-
return res;
268+
return nestedGson.fromJson(in, UserConfigurationResponse.class);
289269
}
290270
})
291271
.registerTypeAdapter(EventsBranch.class, new JsonDeserializer<EventsBranch>() {

app/src/main/java/com/zulip/android/database/DatabaseHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
3030
private static final String DATABASE_NAME = "zulip-%s.db";
3131
// any time you make changes to your database objects, you may have to
3232
// increase the database version
33-
private static final int DATABASE_VERSION = 7;
33+
private static final int DATABASE_VERSION = 8;
3434

3535
public DatabaseHelper(ZulipApp app, String email) {
3636
super(app, String.format(DATABASE_NAME, MD5Util.md5Hex(email)), null,

app/src/main/java/com/zulip/android/models/Person.java

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22

33
import com.google.gson.annotations.SerializedName;
44
import com.j256.ormlite.dao.Dao;
5-
import com.j256.ormlite.dao.GenericRawResults;
6-
import com.j256.ormlite.dao.RawRowMapper;
75
import com.j256.ormlite.dao.RuntimeExceptionDao;
86
import com.j256.ormlite.field.DatabaseField;
7+
import com.j256.ormlite.stmt.SelectArg;
98
import com.j256.ormlite.table.DatabaseTable;
109
import com.zulip.android.ZulipApp;
1110
import com.zulip.android.util.ZLog;
1211

1312
import org.apache.commons.lang.builder.HashCodeBuilder;
1413

15-
import java.io.IOException;
1614
import java.sql.SQLException;
1715
import java.util.Collections;
1816
import java.util.Comparator;
@@ -30,8 +28,8 @@ public class Person {
3028
public static final String ISACTIVE_FIELD = "isActive";
3129
private static final String ID_FIELD = "id";
3230
private static final String AVATARURL_FIELD = "avatarUrl";
33-
@SerializedName("IGNORE_MASK")
34-
@DatabaseField(columnName = ID_FIELD, generatedId = true)
31+
@SerializedName("user_id")
32+
@DatabaseField(columnName = ID_FIELD, id = true)
3533
protected int id;
3634

3735
@SerializedName("full_name")
@@ -104,36 +102,13 @@ public static Person getByEmail(ZulipApp app, String email) {
104102
@SuppressWarnings("WeakerAccess")
105103
public static Person getByEmail(Dao<Person, ?> dao, String email) {
106104
try {
107-
// Using raw query to avoid errors with queryBuilder in ormlite.
108-
// Listing of column names is necessary to avoid haphazard ordering of attributes
109-
// in rawResults.
110-
GenericRawResults<Person> rawResults =
111-
dao.queryRaw(
112-
"select " + Person.ID_FIELD + "," + Person.NAME_FIELD + ","
113-
+ Person.EMAIL_FIELD + "," + Person.AVATARURL_FIELD + ","
114-
+ Person.ISBOT_FIELD + "," + Person.ISACTIVE_FIELD + " from " + "people"
115-
+ " where " + Person.EMAIL_FIELD + " = ? ;",
116-
new RawRowMapper<Person>() {
117-
public Person mapRow(String[] columnNames,
118-
String[] resultColumns) {
119-
return new Person(Integer.parseInt(resultColumns[0]),
120-
resultColumns[1], resultColumns[2], resultColumns[3],
121-
Boolean.parseBoolean(resultColumns[4]),
122-
Boolean.parseBoolean(resultColumns[5]));
123-
}
124-
}, email.toLowerCase(Locale.US));
125-
126-
// we only care about the first result
127-
Person returnValue = rawResults.getFirstResult();
128-
rawResults.close();
129-
return returnValue;
105+
return dao.queryBuilder().where()
106+
.eq(Person.EMAIL_FIELD, new SelectArg(email.toLowerCase()))
107+
.queryForFirst();
130108
} catch (SQLException e) {
131109
throw new RuntimeException(e);
132-
} catch (IOException e) {
133-
ZLog.logException(e);
134110
}
135111

136-
return null;
137112
}
138113

139114
@SuppressWarnings("WeakerAccess")

app/src/main/java/com/zulip/android/networking/AsyncGetEvents.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,11 @@ public Void call() throws Exception {
189189
Person person = people.get(i);
190190
person.setActive(true);
191191
try {
192-
// Ormlite uses a query-by-id to see if it needs to update
193-
// or create a new row using createOrUpdate()
194-
// Hence, the object passed should always have an id.
195-
if (Person.getByEmail(app, person.getEmail()) == null) {
196-
personDao.create(person);
197-
} else {
198-
personDao.update(person);
192+
if (person.getEmail().equalsIgnoreCase(app.getYou().getEmail())) {
193+
// change the id
194+
personDao.updateId(app.getYou(), person.getId());
199195
}
196+
personDao.createOrUpdate(person);
200197
} catch (Exception e) {
201198
ZLog.logException(e);
202199
}

0 commit comments

Comments
 (0)