Newer
Older
package smash.appointment.parse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
public class AppointmentDao {
Logger logger = Logger.getLogger(AppointmentDao.class);
private List<AppointmentEntry> appointments = new ArrayList<>();
public void addAppointments(List<AppointmentEntry> appointmentsToAdd) {
for (AppointmentEntry appointmentEntry : appointmentsToAdd) {
addAppointment(appointmentEntry);
}
}
/**
* @return the appointments
* @see #appointments
*/
public List<AppointmentEntry> getAppointments() {
return appointments;
}
/**
* @param appointments
* the appointments to set
* @see #appointments
*/
public void setAppointments(List<AppointmentEntry> appointments) {
this.appointments = appointments;
}
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
public List<Visit> getVisits() {
List<Visit> result = new ArrayList<>();
Map<Subject, List<AppointmentEntry>> subjectAppointments = new HashMap<>();
for (AppointmentEntry entry : appointments) {
if (subjectAppointments.get(entry.getSubject()) == null) {
subjectAppointments.put(entry.getSubject(), new ArrayList<AppointmentEntry>());
}
subjectAppointments.get(entry.getSubject()).add(entry);
}
for (Subject subject : subjectAppointments.keySet()) {
result.addAll(getVisitsForSubject(subject, subjectAppointments.get(subject)));
}
return result;
}
private List<Visit> getVisitsForSubject(Subject subject, List<AppointmentEntry> list) {
Comparator<AppointmentEntry> comparator = new Comparator<AppointmentEntry>() {
@Override
public int compare(AppointmentEntry o1, AppointmentEntry o2) {
String date1 = o1.getDay().substring(0, 10);
String date2 = o2.getDay().substring(0, 10);
if (date1.compareTo(date2) == 0) {
if (o1.getTypes().contains(AppointmentType.LEVEL_A) || o1.getTypes().contains(AppointmentType.LEVEL_A_TQ)) {
return -1;
} else if (o2.getTypes().contains(AppointmentType.LEVEL_A) || o2.getTypes().contains(AppointmentType.LEVEL_A_TQ)) {
return 1;
} else {
return 0;
}
} else {
return date1.compareTo(date2);
}
}
};
Collections.sort(list, comparator);
List<Visit> result = new ArrayList<>();
Visit currentVisit = new Visit(subject);
for (AppointmentEntry appointmentEntry : list) {
if (appointmentEntry.getTypes().contains(AppointmentType.LEVEL_A) || appointmentEntry.getTypes().contains(AppointmentType.LEVEL_A_TQ)) {
if (currentVisit.getAppointments().size() > 0) {
result.add(currentVisit);
}
currentVisit = new Visit(subject);
currentVisit.addAppointment(appointmentEntry);
} else {
String date = currentVisit.getLastAppointmentDate();
if (date.equals(appointmentEntry.getDay().substring(0, 10))) {
currentVisit.getLastAppointment().addTypes(appointmentEntry.getTypes());
String source = appointmentEntry.getSource() + "\n"+currentVisit.getLastAppointment().getSource();
currentVisit.getLastAppointment().setSource(source);
} else {
currentVisit.addAppointment(appointmentEntry);
}
}
}
if (currentVisit.getAppointments().size() > 0) {
result.add(currentVisit);
}
return result;
}
public void addAppointment(AppointmentEntry appointment) {
appointments.add(appointment);
}