/** * Add a user to a group in the database * * @param group The group the user should be added to * @param user The user to be added */ public void addUserToAGroupInTheDatabase(Group group, User user) { ContentValues values = new ContentValues(); values.put(GroupColumns._ID, group.getId()); values.put(UserColumns.USER_NAME, user.getUserName()); System.out.println(userExistsInGroup(group, user)); if (!userExistsInGroup(group, user)) { context.getContentResolver().insert(UserGroupProvider.CONTENT_URI, values); } Log.i( "USER GROUP MANAGER", "User: "******" added to group: " + group.getName()); }
/** * Add a user to the database if the user doesn't exists from before * * @param user The user to be added */ public void addUserToUserDatabase(User user) { ContentValues values = new ContentValues(); values.put(UserColumns._ID, user.getId()); values.put(UserColumns.USER_NAME, user.getUserName()); if (!userExists(user)) { context.getContentResolver().insert(UserColumns.CONTENT_URI, values); Log.i("USER GROUP MANAGER", "User: "******" added to the database"); } else { Log.i("USER GROUP MANAGER", "User: "******" already exists"); } }
/** * Deletes a user from the database * * @param user The user to be deleted */ public void deleteUserFromDatabase(User user) { String where = UserColumns.USER_NAME + " = " + user.getUserName(); context.getContentResolver().delete(UserColumns.CONTENT_URI, where, null); context.getContentResolver().delete(UserGroupProvider.CONTENT_URI, where, null); }
/** * Removes a user from a group in the database * * @param group The group the user should be removed from * @param user The user to be removed */ public void removeUserFromAGroupInTheDatabase(Group group, User user) { String where = GroupColumns._ID + " = '" + group.getId() + "'" + " AND " + UserColumns.USER_NAME + " = '" + user.getUserName() + "'"; context.getContentResolver().delete(UserGroupProvider.CONTENT_URI, where, null); Log.i( "USER GROUP MANAGER", "User: "******" removed from group: " + group.getName()); }
/** * Checks if a user already exists in the database * * @param user The user to be checked * @return True if the user already exists */ public Boolean userExists(User user) { String[] userTableColumns = new String[] {UserColumns.USER_NAME}; String where = UserColumns.USER_NAME + " = " + "'" + user.getUserName() + "'"; Cursor c = context .getContentResolver() .query(UserProvider.CONTENT_URI, userTableColumns, where, null, null); int numberOfRowsReturned = c.getCount(); c.close(); if (numberOfRowsReturned != 0) { return true; } else { return false; } }
/** * Gets all the groups in the database connected to a given user * * @param user The user connected to groups * @return A list of the groups connected to the user */ public ArrayList<Group> getAllGroupsConnectedToAUser(User user) { ArrayList<Group> allConnectedGroups = new ArrayList<Group>(); String[] userGroupsTableColumns = new String[] {GroupColumns._ID, UserColumns.USER_NAME}; String whereStatement = UserColumns.USER_NAME + " = '" + user.getUserName() + "'"; Cursor c = context .getContentResolver() .query( UserGroupProvider.CONTENT_URI, userGroupsTableColumns, whereStatement, null, null); if (c.moveToFirst()) { do { String groupID = c.getString(c.getColumnIndex(GroupColumns._ID)); allConnectedGroups.add(getGroupFromDatabase(groupID)); } while (c.moveToNext()); } c.close(); return allConnectedGroups; }