@Override public List<Project> handle(ResultSet rs) throws SQLException { if (!rs.next()) { return Collections.<Project>emptyList(); } ArrayList<Project> projects = new ArrayList<Project>(); do { int id = rs.getInt(1); String name = rs.getString(2); boolean active = rs.getBoolean(3); long modifiedTime = rs.getLong(4); long createTime = rs.getLong(5); int version = rs.getInt(6); String lastModifiedBy = rs.getString(7); String description = rs.getString(8); int encodingType = rs.getInt(9); byte[] data = rs.getBytes(10); Project project; if (data != null) { EncodingType encType = EncodingType.fromInteger(encodingType); Object blobObj; try { // Convoluted way to inflate strings. Should find common package or // helper function. if (encType == EncodingType.GZIP) { // Decompress the sucker. String jsonString = GZIPUtils.unGzipString(data, "UTF-8"); blobObj = JSONUtils.parseJSONFromString(jsonString); } else { String jsonString = new String(data, "UTF-8"); blobObj = JSONUtils.parseJSONFromString(jsonString); } project = Project.projectFromObject(blobObj); } catch (IOException e) { throw new SQLException("Failed to get project.", e); } } else { project = new Project(id, name); } // update the fields as they may have changed project.setActive(active); project.setLastModifiedTimestamp(modifiedTime); project.setCreateTimestamp(createTime); project.setVersion(version); project.setLastModifiedUser(lastModifiedBy); project.setDescription(description); projects.add(project); } while (rs.next()); return projects; }
@Override public void updateDescription(Project project, String description, String user) throws ProjectManagerException { QueryRunner runner = createQueryRunner(); final String UPDATE_PROJECT_DESCRIPTION = "UPDATE projects SET description=?,modified_time=?,last_modified_by=? WHERE id=?"; long updateTime = System.currentTimeMillis(); try { runner.update(UPDATE_PROJECT_DESCRIPTION, description, updateTime, user, project.getId()); project.setDescription(description); project.setLastModifiedTimestamp(updateTime); project.setLastModifiedUser(user); } catch (SQLException e) { logger.error(e); throw new ProjectManagerException( "Error marking project " + project.getName() + " as inactive", e); } }
@Override public void changeProjectVersion(Project project, int version, String user) throws ProjectManagerException { long timestamp = System.currentTimeMillis(); QueryRunner runner = createQueryRunner(); try { final String UPDATE_PROJECT_VERSION = "UPDATE projects SET version=?,modified_time=?,last_modified_by=? WHERE id=?"; runner.update(UPDATE_PROJECT_VERSION, version, timestamp, user, project.getId()); project.setVersion(version); project.setLastModifiedTimestamp(timestamp); project.setLastModifiedUser(user); } catch (SQLException e) { logger.error(e); throw new ProjectManagerException( "Error updating switching project version " + project.getName(), e); } }
@SuppressWarnings("unchecked") public static Project projectFromObject(Object object) { Map<String, Object> projectObject = (Map<String, Object>) object; int id = (Integer) projectObject.get("id"); String name = (String) projectObject.get("name"); String description = (String) projectObject.get("description"); String lastModifiedUser = (String) projectObject.get("lastModifiedUser"); long createTimestamp = coerceToLong(projectObject.get("createTimestamp")); long lastModifiedTimestamp = coerceToLong(projectObject.get("lastModifiedTimestamp")); String source = (String) projectObject.get("source"); Boolean active = (Boolean) projectObject.get("active"); active = active == null ? true : active; int version = (Integer) projectObject.get("version"); Project project = new Project(id, name); project.setVersion(version); project.setDescription(description); project.setCreateTimestamp(createTimestamp); project.setLastModifiedTimestamp(lastModifiedTimestamp); project.setLastModifiedUser(lastModifiedUser); project.setActive(active); if (source != null) { project.setSource(source); } List<Map<String, Object>> users = (List<Map<String, Object>>) projectObject.get("users"); for (Map<String, Object> user : users) { String userid = (String) user.get("userId"); Permission perm = new Permission(); List<String> list = (List<String>) user.get("permissions"); perm.addPermissionsByName(list); project.setUserPermission(userid, perm); } return project; }