@SuppressWarnings("unchecked") @Test public void testApplyOptionalField() { setupMockForApplyOptionalField(); List<EntityBody> entities = new ArrayList<EntityBody>(); entities.add(makeEntityBody(student1Entity)); entities.add(makeEntityBody(student2Entity)); entities = studentAllAttendanceOptionalFieldAppender.applyOptionalField(entities, null); assertEquals("Should be 2", 2, entities.size()); assertNotNull("Should not be null", entities.get(0).get("attendances")); List<EntityBody> attendances1 = (List<EntityBody>) ((EntityBody) entities.get(0).get("attendances")).get("attendances"); assertEquals("Should match", 6, attendances1.size()); List<EntityBody> attendances2 = (List<EntityBody>) ((EntityBody) entities.get(1).get("attendances")).get("attendances"); assertEquals("Should match", 3, attendances2.size()); }
@SuppressWarnings("unchecked") @Override public List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters) { List<String> studentIds = optionalFieldAppenderHelper.getIdList(entities, "id"); NeutralQuery neutralQuery = new NeutralQuery(); neutralQuery.setLimit(0); neutralQuery.addCriteria(new NeutralCriteria(ParameterConstants.STUDENT_ID, NeutralCriteria.CRITERIA_IN, studentIds)); List<EntityBody> attendances = optionalFieldAppenderHelper.queryEntities(ResourceNames.ATTENDANCES, neutralQuery); Map<String, List<EntityBody>> attendancesPerStudent = new HashMap<String, List<EntityBody>>(); for (EntityBody attendance : attendances) { String studentId = (String) attendance.get("studentId"); List<EntityBody> events = new ArrayList<EntityBody>(); if (attendance.containsKey("attendanceEvent")) { List<Map<String, Object>> yearEvents = (List<Map<String, Object>>) attendance.get("attendanceEvent"); if (yearEvents != null) { for (int j = 0; j < yearEvents.size(); j++) { events.add(new EntityBody(yearEvents.get(j))); } } } if (attendancesPerStudent.containsKey(studentId)) { attendancesPerStudent.get(studentId).addAll(events); } else { attendancesPerStudent.put(studentId, events); } } for (EntityBody student : entities) { String id = (String) student.get("id"); List<EntityBody> attendancesForStudent = attendancesPerStudent.get(id); if (attendancesForStudent != null && !attendancesForStudent.isEmpty()) { EntityBody attendancesBody = new EntityBody(); attendancesBody.put(ResourceNames.ATTENDANCES, attendancesForStudent); student.put(ParameterConstants.OPTIONAL_FIELD_ATTENDANCES, attendancesBody); } } return entities; }
StudentAllAttendanceOptionalFieldAppender implements OptionalFieldAppender { @SuppressWarnings("unchecked") @Override public List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters) { List<String> studentIds = optionalFieldAppenderHelper.getIdList(entities, "id"); NeutralQuery neutralQuery = new NeutralQuery(); neutralQuery.setLimit(0); neutralQuery.addCriteria(new NeutralCriteria(ParameterConstants.STUDENT_ID, NeutralCriteria.CRITERIA_IN, studentIds)); List<EntityBody> attendances = optionalFieldAppenderHelper.queryEntities(ResourceNames.ATTENDANCES, neutralQuery); Map<String, List<EntityBody>> attendancesPerStudent = new HashMap<String, List<EntityBody>>(); for (EntityBody attendance : attendances) { String studentId = (String) attendance.get("studentId"); List<EntityBody> events = new ArrayList<EntityBody>(); if (attendance.containsKey("attendanceEvent")) { List<Map<String, Object>> yearEvents = (List<Map<String, Object>>) attendance.get("attendanceEvent"); if (yearEvents != null) { for (int j = 0; j < yearEvents.size(); j++) { events.add(new EntityBody(yearEvents.get(j))); } } } if (attendancesPerStudent.containsKey(studentId)) { attendancesPerStudent.get(studentId).addAll(events); } else { attendancesPerStudent.put(studentId, events); } } for (EntityBody student : entities) { String id = (String) student.get("id"); List<EntityBody> attendancesForStudent = attendancesPerStudent.get(id); if (attendancesForStudent != null && !attendancesForStudent.isEmpty()) { EntityBody attendancesBody = new EntityBody(); attendancesBody.put(ResourceNames.ATTENDANCES, attendancesForStudent); student.put(ParameterConstants.OPTIONAL_FIELD_ATTENDANCES, attendancesBody); } } return entities; } }
StudentAllAttendanceOptionalFieldAppender implements OptionalFieldAppender { @SuppressWarnings("unchecked") @Override public List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters) { List<String> studentIds = optionalFieldAppenderHelper.getIdList(entities, "id"); NeutralQuery neutralQuery = new NeutralQuery(); neutralQuery.setLimit(0); neutralQuery.addCriteria(new NeutralCriteria(ParameterConstants.STUDENT_ID, NeutralCriteria.CRITERIA_IN, studentIds)); List<EntityBody> attendances = optionalFieldAppenderHelper.queryEntities(ResourceNames.ATTENDANCES, neutralQuery); Map<String, List<EntityBody>> attendancesPerStudent = new HashMap<String, List<EntityBody>>(); for (EntityBody attendance : attendances) { String studentId = (String) attendance.get("studentId"); List<EntityBody> events = new ArrayList<EntityBody>(); if (attendance.containsKey("attendanceEvent")) { List<Map<String, Object>> yearEvents = (List<Map<String, Object>>) attendance.get("attendanceEvent"); if (yearEvents != null) { for (int j = 0; j < yearEvents.size(); j++) { events.add(new EntityBody(yearEvents.get(j))); } } } if (attendancesPerStudent.containsKey(studentId)) { attendancesPerStudent.get(studentId).addAll(events); } else { attendancesPerStudent.put(studentId, events); } } for (EntityBody student : entities) { String id = (String) student.get("id"); List<EntityBody> attendancesForStudent = attendancesPerStudent.get(id); if (attendancesForStudent != null && !attendancesForStudent.isEmpty()) { EntityBody attendancesBody = new EntityBody(); attendancesBody.put(ResourceNames.ATTENDANCES, attendancesForStudent); student.put(ParameterConstants.OPTIONAL_FIELD_ATTENDANCES, attendancesBody); } } return entities; }  }
StudentAllAttendanceOptionalFieldAppender implements OptionalFieldAppender { @SuppressWarnings("unchecked") @Override public List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters) { List<String> studentIds = optionalFieldAppenderHelper.getIdList(entities, "id"); NeutralQuery neutralQuery = new NeutralQuery(); neutralQuery.setLimit(0); neutralQuery.addCriteria(new NeutralCriteria(ParameterConstants.STUDENT_ID, NeutralCriteria.CRITERIA_IN, studentIds)); List<EntityBody> attendances = optionalFieldAppenderHelper.queryEntities(ResourceNames.ATTENDANCES, neutralQuery); Map<String, List<EntityBody>> attendancesPerStudent = new HashMap<String, List<EntityBody>>(); for (EntityBody attendance : attendances) { String studentId = (String) attendance.get("studentId"); List<EntityBody> events = new ArrayList<EntityBody>(); if (attendance.containsKey("attendanceEvent")) { List<Map<String, Object>> yearEvents = (List<Map<String, Object>>) attendance.get("attendanceEvent"); if (yearEvents != null) { for (int j = 0; j < yearEvents.size(); j++) { events.add(new EntityBody(yearEvents.get(j))); } } } if (attendancesPerStudent.containsKey(studentId)) { attendancesPerStudent.get(studentId).addAll(events); } else { attendancesPerStudent.put(studentId, events); } } for (EntityBody student : entities) { String id = (String) student.get("id"); List<EntityBody> attendancesForStudent = attendancesPerStudent.get(id); if (attendancesForStudent != null && !attendancesForStudent.isEmpty()) { EntityBody attendancesBody = new EntityBody(); attendancesBody.put(ResourceNames.ATTENDANCES, attendancesForStudent); student.put(ParameterConstants.OPTIONAL_FIELD_ATTENDANCES, attendancesBody); } } return entities; }  @SuppressWarnings("unchecked") @Override List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters); }
StudentAllAttendanceOptionalFieldAppender implements OptionalFieldAppender { @SuppressWarnings("unchecked") @Override public List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters) { List<String> studentIds = optionalFieldAppenderHelper.getIdList(entities, "id"); NeutralQuery neutralQuery = new NeutralQuery(); neutralQuery.setLimit(0); neutralQuery.addCriteria(new NeutralCriteria(ParameterConstants.STUDENT_ID, NeutralCriteria.CRITERIA_IN, studentIds)); List<EntityBody> attendances = optionalFieldAppenderHelper.queryEntities(ResourceNames.ATTENDANCES, neutralQuery); Map<String, List<EntityBody>> attendancesPerStudent = new HashMap<String, List<EntityBody>>(); for (EntityBody attendance : attendances) { String studentId = (String) attendance.get("studentId"); List<EntityBody> events = new ArrayList<EntityBody>(); if (attendance.containsKey("attendanceEvent")) { List<Map<String, Object>> yearEvents = (List<Map<String, Object>>) attendance.get("attendanceEvent"); if (yearEvents != null) { for (int j = 0; j < yearEvents.size(); j++) { events.add(new EntityBody(yearEvents.get(j))); } } } if (attendancesPerStudent.containsKey(studentId)) { attendancesPerStudent.get(studentId).addAll(events); } else { attendancesPerStudent.put(studentId, events); } } for (EntityBody student : entities) { String id = (String) student.get("id"); List<EntityBody> attendancesForStudent = attendancesPerStudent.get(id); if (attendancesForStudent != null && !attendancesForStudent.isEmpty()) { EntityBody attendancesBody = new EntityBody(); attendancesBody.put(ResourceNames.ATTENDANCES, attendancesForStudent); student.put(ParameterConstants.OPTIONAL_FIELD_ATTENDANCES, attendancesBody); } } return entities; }  @SuppressWarnings("unchecked") @Override List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters);  }