@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; }
@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; }