@Override public List<Triple<String, Boolean, Permission>> handle(final ResultSet rs) throws SQLException { if (!rs.next()) { return Collections.emptyList(); } final List<Triple<String, Boolean, Permission>> permissions = new ArrayList<>(); do { final String username = rs.getString(3); final int permissionFlag = rs.getInt(4); final boolean val = rs.getBoolean(5); final Permission perm = new Permission(permissionFlag); permissions.add(new Triple<>(username, val, perm)); } while (rs.next()); return permissions; } }
@Test public void testUpdatePermission() throws Exception { createThreeProjects(); final Project project = this.loader.fetchProjectByName("mytestProject"); this.loader.updatePermission(project, project.getLastModifiedUser(), new Permission(Permission.Type.ADMIN), false); final List<Triple<String, Boolean, Permission>> permissionsTriple = this.loader .getProjectPermissions(project); Assert.assertEquals(permissionsTriple.size(), 1); Assert.assertEquals(permissionsTriple.get(0).getFirst(), "testUser1"); Assert.assertEquals(permissionsTriple.get(0).getThird().toString(), "ADMIN"); }
@Override public Project fetchProjectById(final int id) throws ProjectManagerException { Project project = null; final ProjectResultHandler handler = new ProjectResultHandler(); try { final List<Project> projects = this.dbOperator .query(ProjectResultHandler.SELECT_PROJECT_BY_ID, handler, id); if (projects.isEmpty()) { throw new ProjectManagerException("No project with id " + id + " exists in db."); } project = projects.get(0); // Fetch the user permissions for (final Triple<String, Boolean, Permission> perm : fetchPermissionsForProject(project)) { // TODO kunkun-tang: understand why we need to check permission not equal to 0 here. if (perm.getThird().toFlags() != 0) { setProjectPermission(project, perm); } } } catch (final SQLException ex) { logger.error(ProjectResultHandler.SELECT_PROJECT_BY_ID + " failed.", ex); throw new ProjectManagerException("Query for existing project failed. Project " + id, ex); } return project; }
@Override public Project fetchProjectByName(final String name) throws ProjectManagerException { Project project = null; final ProjectResultHandler handler = new ProjectResultHandler(); // select active project from db first, if not exist, select inactive one. // At most one active project with the same name exists in db. try { List<Project> projects = this.dbOperator .query(ProjectResultHandler.SELECT_ACTIVE_PROJECT_BY_NAME, handler, name); if (projects.isEmpty()) { projects = this.dbOperator .query(ProjectResultHandler.SELECT_PROJECT_BY_NAME, handler, name); if (projects.isEmpty()) { throw new ProjectManagerException("No project with name " + name + " exists in db."); } } project = projects.get(0); for (final Triple<String, Boolean, Permission> perm : fetchPermissionsForProject(project)) { if (perm.getThird().toFlags() != 0) { setProjectPermission(project, perm); } } } catch (final SQLException ex) { logger.error(ProjectResultHandler.SELECT_ACTIVE_PROJECT_BY_NAME + " failed.", ex); throw new ProjectManagerException( ProjectResultHandler.SELECT_ACTIVE_PROJECT_BY_NAME + " failed.", ex); } return project; }
@Override public Project fetchProjectById(final int id) throws ProjectManagerException { Project project = null; final ProjectResultHandler handler = new ProjectResultHandler(); try { final List<Project> projects = this.dbOperator .query(ProjectResultHandler.SELECT_PROJECT_BY_ID, handler, id); if (projects.isEmpty()) { throw new ProjectManagerException("No project with id " + id + " exists in db."); } project = projects.get(0); // Fetch the user permissions for (final Triple<String, Boolean, Permission> perm : fetchPermissionsForProject(project)) { // TODO kunkun-tang: understand why we need to check permission not equal to 0 here. if (perm.getThird().toFlags() != 0) { setProjectPermission(project, perm); } } } catch (final SQLException ex) { logger.error(ProjectResultHandler.SELECT_PROJECT_BY_ID + " failed.", ex); throw new ProjectManagerException("Query for existing project failed. Project " + id, ex); } return project; }
@Override public List<Triple<String, Boolean, Permission>> handle(final ResultSet rs) throws SQLException { if (!rs.next()) { return Collections.emptyList(); } final List<Triple<String, Boolean, Permission>> permissions = new ArrayList<>(); do { final String username = rs.getString(3); final int permissionFlag = rs.getInt(4); final boolean val = rs.getBoolean(5); final Permission perm = new Permission(permissionFlag); permissions.add(new Triple<>(username, val, perm)); } while (rs.next()); return permissions; } }
@Override public Project fetchProjectByName(final String name) throws ProjectManagerException { Project project = null; final ProjectResultHandler handler = new ProjectResultHandler(); // select active project from db first, if not exist, select inactive one. // At most one active project with the same name exists in db. try { List<Project> projects = this.dbOperator .query(ProjectResultHandler.SELECT_ACTIVE_PROJECT_BY_NAME, handler, name); if (projects.isEmpty()) { projects = this.dbOperator .query(ProjectResultHandler.SELECT_PROJECT_BY_NAME, handler, name); if (projects.isEmpty()) { throw new ProjectManagerException("No project with name " + name + " exists in db."); } } project = projects.get(0); for (final Triple<String, Boolean, Permission> perm : fetchPermissionsForProject(project)) { if (perm.getThird().toFlags() != 0) { setProjectPermission(project, perm); } } } catch (final SQLException ex) { logger.error(ProjectResultHandler.SELECT_ACTIVE_PROJECT_BY_NAME + " failed.", ex); throw new ProjectManagerException( ProjectResultHandler.SELECT_ACTIVE_PROJECT_BY_NAME + " failed.", ex); } return project; }
@Override public List<Triple<String, Boolean, Permission>> handle(ResultSet rs) throws SQLException { if (!rs.next()) { return Collections.<Triple<String, Boolean, Permission>>emptyList(); } ArrayList<Triple<String, Boolean, Permission>> permissions = new ArrayList<Triple<String, Boolean, Permission>>(); do { //int project_id = rs.getInt(1); //long modifiedTime = rs.getLong(2); String username = rs.getString(3); int permissionFlag = rs.getInt(4); boolean val = rs.getBoolean(5); Permission perm = new Permission(permissionFlag); permissions.add(new Triple<String, Boolean, Permission>(username, val, perm)); } while (rs.next()); return permissions; } }