-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGetStatementsRequest.java
More file actions
266 lines (211 loc) · 5.83 KB
/
GetStatementsRequest.java
File metadata and controls
266 lines (211 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* Copyright 2016-2025 Berry Cloud Ltd. All rights reserved.
*/
package dev.learning.xapi.client;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.learning.xapi.model.Agent;
import dev.learning.xapi.model.StatementFormat;
import dev.learning.xapi.model.Verb;
import java.net.URI;
import java.time.Instant;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Consumer;
import lombok.Builder;
import lombok.Getter;
import lombok.SneakyThrows;
import org.springframework.http.HttpMethod;
import org.springframework.web.util.UriBuilder;
/**
* Request for getting multiple Statements.
*
* @see <a href=
* "https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Communication.md#213-get-statements">GET
* Statements</a>
*
* @author Thomas Turrell-Croft
*/
@Builder
@Getter
public class GetStatementsRequest implements Request {
private static final ObjectMapper objectMapper = new ObjectMapper();
private final Agent agent;
private final URI verb;
private final URI activity;
private final UUID registration;
private final Boolean relatedActivities;
private final Boolean relatedAgents;
private final Instant since;
private final Instant until;
private final Integer limit;
private final StatementFormat format;
private final Boolean attachments;
private final Boolean ascending;
@Override
public HttpMethod getMethod() {
return HttpMethod.GET;
}
@Override
public UriBuilder url(UriBuilder uriBuilder, Map<String, Object> queryParams) {
// All queryParams are optional
uriBuilder.path("/statements");
if (agent != null) {
queryParams.put("agent", agentToJsonString());
uriBuilder.queryParam("agent", "{agent}");
}
if (verb != null) {
queryParams.put("verb", verb);
uriBuilder.queryParam("verb", "{verb}");
}
if (activity != null) {
queryParams.put("activity", activity);
uriBuilder.queryParam("activity", "{activity}");
}
if (since != null) {
queryParams.put("since", since);
uriBuilder.queryParam("since", "{since}");
}
if (until != null) {
queryParams.put("until", until);
uriBuilder.queryParam("until", "{until}");
}
return uriBuilder
.queryParamIfPresent("registration", Optional.ofNullable(registration))
.queryParamIfPresent("related_activities", Optional.ofNullable(relatedActivities))
.queryParamIfPresent("related_agents", Optional.ofNullable(relatedAgents))
.queryParamIfPresent("limit", Optional.ofNullable(limit))
.queryParamIfPresent("format", Optional.ofNullable(format))
.queryParamIfPresent("attachments", Optional.ofNullable(attachments))
.queryParamIfPresent("ascending", Optional.ofNullable(ascending));
}
/**
* Builder for GetStatementsRequest.
*/
public static class Builder {
/**
* Sets the agent.
*
* @param agent The agent of the GetStatementRequest.
*
* @return This builder
*
* @see GetStatementsRequest#agent
*/
public Builder agent(Agent agent) {
this.agent = agent;
return this;
}
/**
* Sets the agent.
*
* @param agent The agent of the GetStatementRequest.
*
* @return This builder
*
* @see GetStatementsRequest#agent
*/
public Builder agent(Consumer<Agent.Builder<?, ?>> agent) {
final dev.learning.xapi.model.Agent.Builder<?, ?> builder = Agent.builder();
agent.accept(builder);
return agent(builder.build());
}
/**
* Sets the verb.
*
* @param verb The verb of the GetStatementRequest.
*
* @return This builder
*
* @see GetStatementsRequest#verb
*/
public Builder verb(URI verb) {
this.verb = verb;
return this;
}
/**
* Sets the verb.
*
* @param verb The verb of the GetStatementRequest.
*
* @return This builder
*
* @see GetStatementsRequest#verb
*/
public Builder verb(String verb) {
this.verb = URI.create(verb);
return this;
}
/**
* Sets the verb.
*
* @param verb The verb of the GetStatementRequest.
*
* @return This builder
*
* @see GetStatementsRequest#verb
*/
public Builder verb(Verb verb) {
this.verb = verb.getId();
return this;
}
/**
* Sets the activity.
*
* @param activity The activity of the GetStatementRequest.
*
* @return This builder
*
* @see GetStatementsRequest#activity
*/
public Builder activity(URI activity) {
this.activity = activity;
return this;
}
/**
* Sets the activity.
*
* @param activity The activity of the GetStatementRequest.
*
* @return This builder
*
* @see GetStatementsRequest#activity
*/
public Builder activity(String activity) {
this.activity = URI.create(activity);
return this;
}
/**
* Sets the registration.
*
* @param registration The registration of the GetStatementRequest.
*
* @return This builder
*
* @see GetStatementsRequest#registration
*/
public Builder registration(UUID registration) {
this.registration = registration;
return this;
}
/**
* Sets the registration.
*
* @param registration The registration of the GetStatementRequest.
*
* @return This builder
*
* @see GetStatementsRequest#registration
*/
public Builder registration(String registration) {
this.registration = UUID.fromString(registration);
return this;
}
// This static class extends the lombok builder.
}
// Exception in write value as string should be impossible.
@SneakyThrows
private String agentToJsonString() {
return objectMapper.writeValueAsString(agent);
}
}