Skip to content

Commit

Permalink
Merge pull request #4100 from wordpress-mobile/feature/people-managem…
Browse files Browse the repository at this point in the history
…ent-sync

People Management v1
  • Loading branch information
hypest committed May 27, 2016
2 parents ffa4114 + 7e05ae7 commit 9db1602
Show file tree
Hide file tree
Showing 37 changed files with 1,886 additions and 11 deletions.
6 changes: 6 additions & 0 deletions WordPress/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,12 @@
android:theme="@style/CalypsoTheme"
android:windowSoftInputMode="stateHidden" />

<!--People Management-->
<activity
android:name=".ui.people.PeopleManagementActivity"
android:label="@string/people"
android:theme="@style/Calypso.NoActionBar"/>

<!-- Me activities -->
<activity
android:name=".ui.prefs.MyProfileActivity"
Expand Down
21 changes: 18 additions & 3 deletions WordPress/src/main/java/org/wordpress/android/WordPressDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.json.JSONArray;
import org.wordpress.android.datasets.AccountTable;
import org.wordpress.android.datasets.CommentTable;
import org.wordpress.android.datasets.PeopleTable;
import org.wordpress.android.datasets.SiteSettingsTable;
import org.wordpress.android.datasets.SuggestionTable;
import org.wordpress.android.models.Account;
Expand Down Expand Up @@ -84,7 +85,7 @@ public class WordPressDB {
public static final String COLUMN_NAME_VIDEO_PRESS_SHORTCODE = "videoPressShortcode";
public static final String COLUMN_NAME_UPLOAD_STATE = "uploadState";

private static final int DATABASE_VERSION = 44;
private static final int DATABASE_VERSION = 46;

private static final String CREATE_TABLE_BLOGS = "create table if not exists accounts (id integer primary key autoincrement, "
+ "url text, blogName text, username text, password text, imagePlacement text, centerThumbnail boolean, fullSizeImage boolean, maxImageWidth text, maxImageWidthId integer);";
Expand Down Expand Up @@ -225,6 +226,9 @@ public class WordPressDB {
// add plan_product_name_short to blog
private static final String ADD_BLOGS_PLAN_PRODUCT_NAME_SHORT = "alter table accounts add plan_product_name_short text default '';";

// add capabilities to blog
private static final String ADD_BLOGS_CAPABILITIES = "alter table accounts add capabilities text default '';";

// used for migration
private static final String DEPRECATED_WPCOM_USERNAME_PREFERENCE = "wp_pref_wpcom_username";
private static final String DEPRECATED_ACCESS_TOKEN_PREFERENCE = "wp_pref_wpcom_access_token";
Expand Down Expand Up @@ -256,7 +260,7 @@ public WordPressDB(Context ctx) {
boolean isNewInstall = (currentVersion == 0);

if (!isNewInstall && currentVersion != DATABASE_VERSION) {
AppLog.d(T.DB, "updgrading database from version " + currentVersion + " to " + DATABASE_VERSION);
AppLog.d(T.DB, "upgrading database from version " + currentVersion + " to " + DATABASE_VERSION);
}

switch (currentVersion) {
Expand Down Expand Up @@ -409,6 +413,12 @@ public WordPressDB(Context ctx) {
case 43:
db.execSQL(ADD_BLOGS_PLAN_PRODUCT_NAME_SHORT);
currentVersion++;
case 44:
PeopleTable.createTables(db);
currentVersion++;
case 45:
db.execSQL(ADD_BLOGS_CAPABILITIES);
currentVersion++;
}
db.setVersion(DATABASE_VERSION);
}
Expand Down Expand Up @@ -519,6 +529,7 @@ public boolean addBlog(Blog blog) {
}
values.put("isAdmin", blog.isAdmin());
values.put("isHidden", blog.isHidden());
values.put("capabilities", blog.getCapabilities());
return db.insert(BLOGS_TABLE, null, values) > -1;
}

Expand Down Expand Up @@ -719,6 +730,7 @@ public boolean saveBlog(Blog blog) {
values.put("isHidden", blog.isHidden());
values.put("plan_product_id", blog.getPlanID());
values.put("plan_product_name_short", blog.getPlanShortName());
values.put("capabilities", blog.getCapabilities());
if (blog.getWpVersion() != null) {
values.put("wpVersion", blog.getWpVersion());
} else {
Expand Down Expand Up @@ -748,6 +760,7 @@ public boolean deleteBlog(Context ctx, int id) {
int rowsAffected = db.delete(BLOGS_TABLE, "id=?", new String[]{Integer.toString(id)});
deleteQuickPressShortcutsForLocalTableBlogId(ctx, id);
deleteAllPostsForLocalTableBlogId(id);
PeopleTable.deletePeopleForLocalBlogId(id);
return (rowsAffected > 0);
}

Expand All @@ -757,6 +770,7 @@ public boolean deleteWordPressComBlogs(Context ctx) {
int localBlogId = MapUtils.getMapInt(blog, "id");
deleteQuickPressShortcutsForLocalTableBlogId(ctx, localBlogId);
deleteAllPostsForLocalTableBlogId(localBlogId);
PeopleTable.deletePeopleForLocalBlogId(localBlogId);
}

// H4ck alert: We need to delete the Jetpack sites that were added in the initial
Expand Down Expand Up @@ -813,7 +827,7 @@ public Blog instantiateBlogByLocalId(int localId) {
"blogId", "dotcomFlag", "dotcom_username", "dotcom_password", "api_key",
"api_blogid", "wpVersion", "postFormats", "isScaledImage",
"scaledImgWidth", "homeURL", "blog_options", "isAdmin", "isHidden",
"plan_product_id", "plan_product_name_short"};
"plan_product_id", "plan_product_name_short", "capabilities"};
Cursor c = db.query(BLOGS_TABLE, fields, "id=?", new String[]{Integer.toString(localId)}, null, null, null);

Blog blog = null;
Expand Down Expand Up @@ -871,6 +885,7 @@ public Blog instantiateBlogByLocalId(int localId) {
blog.setHidden(c.getInt(c.getColumnIndex("isHidden")) > 0);
blog.setPlanID(c.getLong(c.getColumnIndex("plan_product_id")));
blog.setPlanShortName(c.getString(c.getColumnIndex("plan_product_name_short")));
blog.setCapabilities(c.getString(c.getColumnIndex("capabilities")));
}
}
c.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ public static Comment getComment(int localBlogId, long commentId) {
String[] args = {Integer.toString(localBlogId), Long.toString(commentId)};
Cursor c = getReadableDb().rawQuery("SELECT * FROM " + COMMENTS_TABLE + " WHERE blog_id=? AND comment_id=?", args);
try {
if (!c.moveToFirst())
if (!c.moveToFirst()) {
return null;
}
return getCommentFromCursor(c);
} finally {
SqlUtils.closeCursor(c);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
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.ui.people.utils.PeopleUtils;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.SqlUtils;

import java.util.ArrayList;
import java.util.List;

public class PeopleTable {
private 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,"
+ "blog_id TEXT,"
+ "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("blog_id", person.getBlogId());
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", person.getRole());
database.insertWithOnConflict(PEOPLE_TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE);
}

public static void savePeople(List<Person> peopleList, int localTableBlogId, boolean isFreshList) {
getWritableDb().beginTransaction();
try {
//We have a fresh list, remove the previous list of people in case it was deleted on remote
if (isFreshList) {
PeopleTable.deletePeopleForLocalBlogId(localTableBlogId);
}

for (Person person : peopleList) {
PeopleTable.save(person);
}
getWritableDb().setTransactionSuccessful();
} finally {
getWritableDb().endTransaction();
}
}

public static int getPeopleCountForLocalBlogId(int localTableBlogId) {
String[] args = new String[]{Integer.toString(localTableBlogId)};
String sql = "SELECT COUNT(*) FROM " + PEOPLE_TABLE + " WHERE local_blog_id=?";
return SqlUtils.intForQuery(getReadableDb(), sql, args);
}

public static void deletePeopleForLocalBlogId(int localTableBlogId) {
String[] args = new String[]{Integer.toString(localTableBlogId)};
getWritableDb().delete(PEOPLE_TABLE, "local_blog_id=?", args);
}

public static void deletePeopleForLocalBlogIdExceptForFirstPage(int localTableBlogId) {
int size = getPeopleCountForLocalBlogId(localTableBlogId);
int fetchLimit = PeopleUtils.FETCH_USERS_LIMIT;
if (size > fetchLimit) {
int deleteCount = size - fetchLimit;
String[] args = new String[] {
Integer.toString(localTableBlogId),
Integer.toString(deleteCount)
};
getWritableDb().delete(PEOPLE_TABLE, "local_blog_id=?1 AND person_id " +
"IN (SELECT person_id FROM " + PEOPLE_TABLE + " WHERE local_blog_id=?1 " +
"ORDER BY display_name DESC LIMIT ?2)", args);
}
}

public static void deletePerson(long personID, int localTableBlogId) {
String[] args = new String[]{Long.toString(personID), Integer.toString(localTableBlogId)};
getWritableDb().delete(PEOPLE_TABLE, "person_id=? AND local_blog_id=?", args);
}

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=? ORDER BY lower(display_name), lower(user_name)", 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 blog
* @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 blogId = c.getString(c.getColumnIndex("blog_id"));

Person person = new Person(personId, blogId, localTableBlogId);
person.setUsername(c.getString(c.getColumnIndex("user_name")));
person.setFirstName(c.getString(c.getColumnIndex("first_name")));
person.setLastName(c.getString(c.getColumnIndex("last_name")));
person.setDisplayName(c.getString(c.getColumnIndex("display_name")));
person.setAvatarUrl(c.getString(c.getColumnIndex("avatar_url")));
person.setRole(c.getString(c.getColumnIndex("role")));

return person;
}
}
27 changes: 26 additions & 1 deletion WordPress/src/main/java/org/wordpress/android/models/Blog.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class Blog {
private String httppassword = "";
private String postFormats;
private String blogOptions = "{}";
private String capabilities;
private boolean isAdmin;
private boolean isHidden;
private long planID;
Expand All @@ -47,7 +48,7 @@ public class Blog {
public Blog() {
}

public Blog(int localTableBlogId, String url, String homeURL, String blogName, String username, String password, String imagePlacement, boolean featuredImageCapable, boolean fullSizeImage, boolean scaledImage, int scaledImageWidth, String maxImageWidth, int maxImageWidthId, int remoteBlogId, String dotcom_username, String dotcom_password, String api_key, String api_blogid, boolean dotcomFlag, String wpVersion, String httpuser, String httppassword, String postFormats, String blogOptions, boolean isAdmin, boolean isHidden) {
public Blog(int localTableBlogId, String url, String homeURL, String blogName, String username, String password, String imagePlacement, boolean featuredImageCapable, boolean fullSizeImage, boolean scaledImage, int scaledImageWidth, String maxImageWidth, int maxImageWidthId, int remoteBlogId, String dotcom_username, String dotcom_password, String api_key, String api_blogid, boolean dotcomFlag, String wpVersion, String httpuser, String httppassword, String postFormats, String blogOptions, String capabilities, boolean isAdmin, boolean isHidden) {
this.localTableBlogId = localTableBlogId;
this.url = url;
this.homeURL = homeURL;
Expand All @@ -72,6 +73,7 @@ public Blog(int localTableBlogId, String url, String homeURL, String blogName, S
this.httppassword = httppassword;
this.postFormats = postFormats;
this.blogOptions = blogOptions;
this.capabilities = capabilities;
this.isAdmin = isAdmin;
this.isHidden = isHidden;
}
Expand Down Expand Up @@ -515,7 +517,30 @@ public void setPlanID(long planID) {
public String getPlanShortName() {
return StringUtils.notNullStr(planShortName);
}

public void setPlanShortName(String name) {
this.planShortName = StringUtils.notNullStr(name);
}

public String getCapabilities() {
return StringUtils.notNullStr(capabilities);
}

public void setCapabilities(String capabilities) {
this.capabilities = capabilities;
}

public boolean hasCapability(Capability capability) {
// If a capability is missing it means the user don't have it.
if (capabilities.isEmpty() || capability == null) {
return false;
}
try {
JSONObject jsonObject = new JSONObject(capabilities);
return jsonObject.optBoolean(capability.getLabel());
} catch (JSONException e) {
AppLog.e(T.PEOPLE, "Capabilities is not a valid json: " + capabilities);
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.wordpress.android.models;

/**
* Used to decide what can the current user do in a particular blog
* A list of capabilities can be found in: https://codex.wordpress.org/Roles_and_Capabilities#Capabilities
*/
public enum Capability {
LIST_USERS("list_users"), // Check if user can visit People page
PROMOTE_USERS("promote_users"), // Check if user can change another user's role
REMOVE_USERS("remove_users"); // Check if user can remove another user

private final String label;

Capability(String label) {
this.label = label;
}

public String getLabel() {
return label;
}
}
Loading

0 comments on commit 9db1602

Please sign in to comment.