1 | package tudelft.mentalhealth.perfectfit.updatefuncs;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.time.LocalDate;
|
---|
5 | import java.time.ZoneId;
|
---|
6 | import java.time.ZonedDateTime;
|
---|
7 | import java.time.format.DateTimeFormatter;
|
---|
8 | import java.time.format.DateTimeParseException;
|
---|
9 | import java.time.temporal.ChronoUnit;
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Objects;
|
---|
13 |
|
---|
14 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
15 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
16 |
|
---|
17 | import tudelft.dialogmanager.parameters.DoubleValue;
|
---|
18 | import tudelft.dialogmanager.parameters.ParameterValue;
|
---|
19 | import tudelft.dialogmanager.parameters.Parameters;
|
---|
20 | import tudelft.dialogmanager.parameters.StringValue;
|
---|
21 | import tudelft.dialogmanager.updatefunctions.UpdateFunction;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Function to get a response to a suggested deadline. Will be either a
|
---|
25 | * confirmation message or error message..
|
---|
26 | */
|
---|
27 | public class CheckDeadline implements UpdateFunction {
|
---|
28 | private final String timefieldname;
|
---|
29 | private final String datefieldname;
|
---|
30 | private final String replyfieldname;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * @param datefieldname the name of the parameter contraining the date
|
---|
34 | * string (eg "20-12-2022") in. NOTE the original code
|
---|
35 | * *syggests* they support 3 formats, but a bug in
|
---|
36 | * their code results in effective use of only the
|
---|
37 | * first format.
|
---|
38 | * @param timefieldname the name of OUT parameter to put the the time
|
---|
39 | * (seconds since 1970 as double) in. This is set to
|
---|
40 | * -1 if the datefieldname can not be parsed.
|
---|
41 | * @param replyfieldname the name of the OUT parameter to put messages in.
|
---|
42 | * These must be end-user readable. Set to error
|
---|
43 | * message if timefieldname contains -1, or a
|
---|
44 | * confirmation message if that deadline is OK.
|
---|
45 | */
|
---|
46 | @JsonCreator
|
---|
47 | public CheckDeadline(@JsonProperty("datefieldname") String datefieldname,
|
---|
48 | @JsonProperty("timefieldname") String timefieldname,
|
---|
49 | @JsonProperty("replyfieldname") String replyfieldname)
|
---|
50 | throws IOException {
|
---|
51 |
|
---|
52 | this.timefieldname = timefieldname;
|
---|
53 | this.datefieldname = datefieldname;
|
---|
54 | this.replyfieldname = replyfieldname;
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public int hashCode() {
|
---|
59 | return Objects.hash(datefieldname, replyfieldname, timefieldname);
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public boolean equals(Object obj) {
|
---|
64 | if (this == obj)
|
---|
65 | return true;
|
---|
66 | if (obj == null)
|
---|
67 | return false;
|
---|
68 | if (getClass() != obj.getClass())
|
---|
69 | return false;
|
---|
70 | CheckDeadline other = (CheckDeadline) obj;
|
---|
71 | return Objects.equals(datefieldname, other.datefieldname)
|
---|
72 | && Objects.equals(replyfieldname, other.replyfieldname)
|
---|
73 | && Objects.equals(timefieldname, other.timefieldname);
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public Parameters call(Parameters parameters) {
|
---|
78 |
|
---|
79 | ParameterValue date = parameters.get(datefieldname);
|
---|
80 | // check if string, if not that's a bug somewhere.
|
---|
81 | if (!(date instanceof StringValue))
|
---|
82 | throw new IllegalArgumentException("Field " + datefieldname
|
---|
83 | + " does not contain string but " + date);
|
---|
84 |
|
---|
85 | double deadline = -1;
|
---|
86 | String message = "";
|
---|
87 | try {
|
---|
88 | ZonedDateTime value = parse(((StringValue) date).getValue());
|
---|
89 | deadline = value.toInstant().getEpochSecond();
|
---|
90 |
|
---|
91 | ZonedDateTime now = ZonedDateTime.now();
|
---|
92 | long diff = ChronoUnit.DAYS.between(now, value);
|
---|
93 |
|
---|
94 | if (diff < 7) {
|
---|
95 | deadline = -1;
|
---|
96 | if (diff < 0)
|
---|
97 | message = "Hmm, it seems like you have set a deadline in the past!<br/>Could you set a deadline in the future?";
|
---|
98 | else
|
---|
99 | message = "Hmm, this seems a bit soon. How about setting a new deadline?";
|
---|
100 | } else {
|
---|
101 | if (diff < 14)
|
---|
102 | message = "Okay!<br/>So you want to achieve your goal in "
|
---|
103 | + diff + " days";
|
---|
104 | else if (diff < 70)
|
---|
105 | message = "Okay!<br/>So you want to achieve your goal in around "
|
---|
106 | + (int) (diff / 7) + " weeks";
|
---|
107 | else
|
---|
108 | message = "Okay!<br/>So you want to achieve your goal in around "
|
---|
109 | + (int) (diff / 30) + " months";
|
---|
110 |
|
---|
111 | message = message + " by "
|
---|
112 | + value.format(DateString.dateformat);
|
---|
113 | }
|
---|
114 |
|
---|
115 | } catch (DateTimeParseException e) {
|
---|
116 | message = "Hmm, I didn't quite get that.<br/>Could you please reformulate the goal deadline you want to set?";
|
---|
117 | }
|
---|
118 | return parameters.with(timefieldname, new DoubleValue(deadline))
|
---|
119 | .with(replyfieldname, new StringValue(message));
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Override
|
---|
123 | public List<String> getAssignedParameters() {
|
---|
124 | return Arrays.asList(timefieldname);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * @param date a string using the format "day-month-year".
|
---|
129 | * @return parsed date, or null if we can't parse it. We use the system
|
---|
130 | * timezone to convert the given date to absolute time.
|
---|
131 | */
|
---|
132 | private ZonedDateTime parse(String date) throws DateTimeParseException {
|
---|
133 | return parseZonedDate(date, "d-M-y");
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * @param date string with the date
|
---|
138 | * @param format the expected format
|
---|
139 | * @return Instant which is some date.
|
---|
140 | * @throws DateTimeParseException if date can't be parsed
|
---|
141 | */
|
---|
142 | private ZonedDateTime parseZonedDate(String date, String format)
|
---|
143 | throws DateTimeParseException {
|
---|
144 | DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
---|
145 |
|
---|
146 | return LocalDate.parse(date, formatter)
|
---|
147 | .atStartOfDay(ZoneId.systemDefault());
|
---|
148 |
|
---|
149 | }
|
---|
150 | }
|
---|