source: PerfectFit/Dialog/src/main/java/tudelft/mentalhealth/perfectfit/updatefuncs/CheckDeadline.java

Last change on this file was 7, checked in by Wouter Pasman, 9 months ago

#124 release PerfectFit sources

File size: 5.1 KB
Line 
1package tudelft.mentalhealth.perfectfit.updatefuncs;
2
3import java.io.IOException;
4import java.time.LocalDate;
5import java.time.ZoneId;
6import java.time.ZonedDateTime;
7import java.time.format.DateTimeFormatter;
8import java.time.format.DateTimeParseException;
9import java.time.temporal.ChronoUnit;
10import java.util.Arrays;
11import java.util.List;
12import java.util.Objects;
13
14import com.fasterxml.jackson.annotation.JsonCreator;
15import com.fasterxml.jackson.annotation.JsonProperty;
16
17import tudelft.dialogmanager.parameters.DoubleValue;
18import tudelft.dialogmanager.parameters.ParameterValue;
19import tudelft.dialogmanager.parameters.Parameters;
20import tudelft.dialogmanager.parameters.StringValue;
21import 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 */
27public 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}
Note: See TracBrowser for help on using the repository browser.