-
Notifications
You must be signed in to change notification settings - Fork 1.4k
People management network and data layer #3916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hypest
merged 41 commits into
feature/people-management-sync
from
feature/people-management-network-and-data-layer
Apr 6, 2016
Merged
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
64e6cab
Table created for People
oguzkocer 0f8ee49
Drop table & save methods added for People table
oguzkocer 8ffd738
Adds getPerson to PeopleTable
oguzkocer f0c36fd
Fixed a few issues from the previous commit and renamed role toString…
oguzkocer 883c582
Save person's role in the db, refactor Role enum
oguzkocer ff12174
Role enum method names refactored to make it clear what they do
oguzkocer ecfc163
Merge branch 'feature/people-management-sync' into feature/people-man…
oguzkocer 87ae386
Reset table added to PeopleTable
oguzkocer bdcc913
Blog_id added to Person and blog & person id combination will be uniq…
oguzkocer 78d01e5
Merge branch 'feature/people-management-sync' into feature/people-man…
oguzkocer 0eecf5f
Introduced PeopleUtils with first iteration of fetchUsers implemented
oguzkocer 550e1aa
Fetch users list when the people management activity is created
oguzkocer 90db986
Save people list received from the server
oguzkocer 9d8ba0a
getPersonFromCursor method introduced to reuse it
oguzkocer cf93798
getPeople implemented for PeopleTable
oguzkocer bae0d91
Network & data layers connected to PeopleManagementActivity to show u…
oguzkocer 2a61cf8
Fixed the variable name for retrieving people list from db
oguzkocer 28852ad
The remote blog id should be used to retrieve people from db
oguzkocer 154cd38
Fixed the error message for json parsing error for sites/users request
oguzkocer 055f14f
Add @ to username in Adapter instead of Model
oguzkocer 5f45582
Use local blog id instead of remote site id for people
oguzkocer c0dfeca
getLocalTableBlogId should return int instead of long
oguzkocer b463e57
Merge branch 'feature/people-management-sync' into feature/people-man…
oguzkocer d610bcb
Added People to AppLog
oguzkocer ee9b0d5
Fixed a variable name in PeopleTable
oguzkocer e2d6e17
Fixed a typo in getPersonID method name
oguzkocer b2f6061
Fixed a possible NPE convertion JSON to Person model
oguzkocer 6e8ac8d
Fixed a comment for the change from the remote site id to local one
oguzkocer 7c40dce
Merge branch 'feature/people-management-sync' into feature/people-man…
oguzkocer 526113e
Wrap saving people to DB in a transaction
oguzkocer 05b18fe
Adds braces to if for single statements
oguzkocer f9d4c4f
Adds a comment which explains why we pick the first role from network…
oguzkocer 03002cc
Handle unsupported case for Role enum instead of using default
oguzkocer 4ba9d28
Person ID shouldn't be read as optional from the json
oguzkocer f837075
peopleListFromJSON should be private
oguzkocer a1ead5f
Instead of overriding notifyDataSetChanged pass the people list to Pe…
oguzkocer 50389f6
Remove unnecessary global variables from PeopleManagementActivity
oguzkocer 9a083b9
Remove unnecessary global variables from PersonActivity
oguzkocer 17d3578
Adds onJSONException to PeopleUtils.Callback
oguzkocer 5e7898f
Move saving the people to DB to the Activity from PeopleUtils
oguzkocer 37f109d
Adds @Nullable to fromJSON method in Person
oguzkocer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
WordPress/src/main/java/org/wordpress/android/datasets/PeopleTable.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package org.wordpress.android.datasets; | ||
|
|
||
| import android.content.ContentValues; | ||
| import android.database.Cursor; | ||
| import android.database.sqlite.SQLiteDatabase; | ||
|
|
||
| import org.wordpress.android.WordPress; | ||
| import org.wordpress.android.models.Person; | ||
| import org.wordpress.android.models.Role; | ||
| import org.wordpress.android.util.AppLog; | ||
| import org.wordpress.android.util.SqlUtils; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class PeopleTable { | ||
| public static final String PEOPLE_TABLE = "people"; | ||
|
|
||
| private static SQLiteDatabase getReadableDb() { | ||
| return WordPress.wpDB.getDatabase(); | ||
| } | ||
| private static SQLiteDatabase getWritableDb() { | ||
| return WordPress.wpDB.getDatabase(); | ||
| } | ||
|
|
||
| public static void createTables(SQLiteDatabase db) { | ||
| db.execSQL("CREATE TABLE " + PEOPLE_TABLE + " (" | ||
| + "person_id INTEGER DEFAULT 0," | ||
| + "local_blog_id INTEGER DEFAULT 0," | ||
| + "user_name TEXT," | ||
| + "first_name TEXT," | ||
| + "last_name TEXT," | ||
| + "display_name TEXT," | ||
| + "avatar_url TEXT," | ||
| + "role TEXT," | ||
| + "PRIMARY KEY (person_id, local_blog_id)" | ||
| + ");"); | ||
| } | ||
|
|
||
| private static void dropTables(SQLiteDatabase db) { | ||
| db.execSQL("DROP TABLE IF EXISTS " + PEOPLE_TABLE); | ||
| } | ||
|
|
||
| public static void reset(SQLiteDatabase db) { | ||
| AppLog.i(AppLog.T.PEOPLE, "resetting people table"); | ||
| dropTables(db); | ||
| createTables(db); | ||
| } | ||
|
|
||
| public static void save(Person person) { | ||
| save(person, getWritableDb()); | ||
| } | ||
|
|
||
| public static void save(Person person, SQLiteDatabase database) { | ||
| ContentValues values = new ContentValues(); | ||
| values.put("person_id", person.getPersonID()); | ||
| values.put("local_blog_id", person.getLocalTableBlogId()); | ||
| values.put("user_name", person.getUsername()); | ||
| values.put("first_name", person.getFirstName()); | ||
| values.put("last_name", person.getLastName()); | ||
| values.put("display_name", person.getDisplayName()); | ||
| values.put("avatar_url", person.getAvatarUrl()); | ||
| values.put("role", Role.toKey(person.getRole())); | ||
| database.insertWithOnConflict(PEOPLE_TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE); | ||
| } | ||
|
|
||
| public static void savePeople(List<Person> peopleList) { | ||
| getWritableDb().beginTransaction(); | ||
| try { | ||
| for (Person person : peopleList) { | ||
| PeopleTable.save(person); | ||
| } | ||
| getWritableDb().setTransactionSuccessful(); | ||
| } finally { | ||
| getWritableDb().endTransaction(); | ||
| } | ||
| } | ||
|
|
||
| public static List<Person> getPeople(int localTableBlogId) { | ||
| List<Person> people = new ArrayList<>(); | ||
| String[] args = { Integer.toString(localTableBlogId) }; | ||
| Cursor c = getReadableDb().rawQuery("SELECT * FROM " + PEOPLE_TABLE + " WHERE local_blog_id=?", args); | ||
|
|
||
| try { | ||
| while (c.moveToNext()) { | ||
| Person person = getPersonFromCursor(c, localTableBlogId); | ||
| people.add(person); | ||
| } | ||
|
|
||
| return people; | ||
| } finally { | ||
| SqlUtils.closeCursor(c); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * retrieve a single person | ||
| * @param personId - id of a person in a particular site | ||
| * @param localTableBlogId - the local blog id the user belongs to | ||
| * @return Person if found, null otherwise | ||
| */ | ||
| public static Person getPerson(long personId, int localTableBlogId) { | ||
| String[] args = { Long.toString(personId), Integer.toString(localTableBlogId) }; | ||
| Cursor c = getReadableDb().rawQuery("SELECT * FROM " + PEOPLE_TABLE + " WHERE person_id=? AND local_blog_id=?", args); | ||
| try { | ||
| if (!c.moveToFirst()) { | ||
| return null; | ||
| } | ||
| return getPersonFromCursor(c, localTableBlogId); | ||
| } finally { | ||
| SqlUtils.closeCursor(c); | ||
| } | ||
| } | ||
|
|
||
| private static Person getPersonFromCursor(Cursor c, int localTableBlogId) { | ||
| long personId = c.getInt(c.getColumnIndex("person_id")); | ||
| String username = c.getString(c.getColumnIndex("user_name")); | ||
| String firstName = c.getString(c.getColumnIndex("first_name")); | ||
| String lastName = c.getString(c.getColumnIndex("last_name")); | ||
| String displayName = c.getString(c.getColumnIndex("display_name")); | ||
| String avatarUrl = c.getString(c.getColumnIndex("avatar_url")); | ||
| Role role = Role.fromKey(c.getString(c.getColumnIndex("role"))); | ||
|
|
||
| return new Person(personId, localTableBlogId, username, firstName, lastName, displayName, avatarUrl, role); | ||
| } | ||
| } | ||
16 changes: 0 additions & 16 deletions
16
WordPress/src/main/java/org/wordpress/android/datasets/PersonTable.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,77 @@ | ||
| package org.wordpress.android.models; | ||
|
|
||
| import android.support.annotation.Nullable; | ||
|
|
||
| import org.json.JSONException; | ||
| import org.json.JSONObject; | ||
| import org.wordpress.android.util.AppLog; | ||
|
|
||
| public class Person { | ||
| public long personID; | ||
| private long personID; | ||
| private int localTableBlogId; | ||
|
|
||
| private String username; | ||
| private String firstName; | ||
| private String lastName; | ||
| private String displayName; | ||
| private String imageUrl; | ||
| private String avatarUrl; | ||
| private Role role; | ||
|
|
||
| public Person(long personID, | ||
| int localTableBlogId, | ||
| String username, | ||
| String firstName, | ||
| String lastName, | ||
| String displayName, | ||
| String imageUrl, | ||
| String avatarUrl, | ||
| Role role) { | ||
| this.personID = personID; | ||
| this.localTableBlogId = localTableBlogId; | ||
| this.username = username; | ||
| this.firstName = firstName; | ||
| this.lastName = lastName; | ||
| this.displayName = displayName; | ||
| this.imageUrl = imageUrl; | ||
| this.avatarUrl = avatarUrl; | ||
| this.role = role; | ||
| } | ||
|
|
||
| @Nullable | ||
| public static Person fromJSON(JSONObject json, int localTableBlogId) { | ||
| if (json == null) { | ||
| return null; | ||
| } | ||
|
|
||
| // Response parameters can be found in https://developer.wordpress.com/docs/api/1.1/get/sites/%24site/users/%24user_id/ | ||
| try { | ||
| long personID = Long.parseLong(json.getString("ID")); | ||
| String username = json.optString("login"); | ||
| String firstName = json.optString("first_name"); | ||
| String lastName = json.optString("last_name"); | ||
| String displayName = json.optString("nice_name"); | ||
| String avatarUrl = json.optString("avatar_URL"); | ||
| // We don't support multiple roles, so the first role is picked just as it's in Calypso | ||
| Role role = Role.fromKey(json.optJSONArray("roles").optString(0)); | ||
|
|
||
| return new Person(personID, localTableBlogId, username, firstName, lastName, displayName, avatarUrl, role); | ||
| } catch (JSONException e) { | ||
| AppLog.e(AppLog.T.PEOPLE, "JSON exception occurred while parsing the user json: " + e); | ||
| } catch (NumberFormatException e) { | ||
| AppLog.e(AppLog.T.PEOPLE, "The ID parsed from the JSON couldn't be converted to long: " + e); | ||
| } | ||
|
|
||
| return null; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a remark per se but, perhaps it could be useful to add the |
||
| } | ||
|
|
||
| public long getPersonID() { | ||
| return personID; | ||
| } | ||
|
|
||
| public int getLocalTableBlogId() { | ||
| return localTableBlogId; | ||
| } | ||
|
|
||
| public String getUsername() { | ||
| return "@" + username; | ||
| return username; | ||
| } | ||
|
|
||
| public void setUsername(String username) { | ||
|
|
@@ -66,11 +110,11 @@ public void setRole(Role role) { | |
| this.role = role; | ||
| } | ||
|
|
||
| public String getImageUrl() { | ||
| return imageUrl; | ||
| public String getAvatarUrl() { | ||
| return avatarUrl; | ||
| } | ||
|
|
||
| public void setImageUrl(String imageUrl) { | ||
| this.imageUrl = imageUrl; | ||
| public void setAvatarUrl(String avatarUrl) { | ||
| this.avatarUrl = avatarUrl; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's wrap this body in braces (See: https://source.android.com/source/code-style.html#use-standard-brace-style)