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
|
/*
* Copyright 2013 Jesse Morgan
*/
package com.p4square.grow.frontend;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import freemarker.template.Template;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.ext.freemarker.TemplateRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.ServerResource;
import org.apache.log4j.Logger;
import com.p4square.fmfacade.json.JsonRequestClient;
import com.p4square.fmfacade.json.JsonResponse;
import com.p4square.fmfacade.FreeMarkerPageResource;
import com.p4square.grow.config.Config;
/**
* TrainingPageResource handles rendering the training page.
*
* This resource expects the user to be authenticated and the ClientInfo User object
* to be populated.
*
* @author Jesse Morgan <jesse@jesterpm.net>
*/
public class TrainingPageResource extends FreeMarkerPageResource {
private static Logger cLog = Logger.getLogger(TrainingPageResource.class);
private Config mConfig;
private Template mTrainingTemplate;
private JsonRequestClient mJsonClient;
// Fields pertaining to this request.
private String mChapter;
private String mUserId;
@Override
public void doInit() {
super.doInit();
GrowFrontend growFrontend = (GrowFrontend) getApplication();
mConfig = growFrontend.getConfig();
mTrainingTemplate = growFrontend.getTemplate("templates/training.ftl");
if (mTrainingTemplate == null) {
cLog.fatal("Could not find training template.");
setStatus(Status.SERVER_ERROR_INTERNAL);
}
mJsonClient = new JsonRequestClient(getContext().getClientDispatcher());
mChapter = getAttribute("chapter");
mUserId = getRequest().getClientInfo().getUser().getIdentifier();
}
/**
* Return a page of videos.
*/
@Override
protected Representation get() {
try {
// Get the current chapter.
if (mChapter == null) {
// TODO: Get user's current question
mChapter = "seeker";
}
// Get videos for the chapter.
List<Map<String, Object>> videos = null;
{
JsonResponse response = backendGet("/training/" + mChapter);
if (!response.getStatus().isSuccess()) {
setStatus(Status.CLIENT_ERROR_NOT_FOUND);
return null;
}
videos = (List<Map<String, Object>>) response.getMap().get("videos");
}
// Get list of completed videos
Map<String, Object> trainingRecord = null;
Map<String, Object> completedVideos = new HashMap<String, Object>();
{
JsonResponse response = backendGet("/accounts/" + mUserId + "/training");
if (response.getStatus().isSuccess()) {
trainingRecord = response.getMap();
completedVideos = (Map<String, Object>) trainingRecord.get("videos");
}
}
// Mark the completed videos as completed
int chapterProgress = 0;
for (Map<String, Object> video : videos) {
boolean completed = (null != completedVideos.get(video.get("id")));
video.put("completed", completed);
if (completed) {
chapterProgress++;
}
}
chapterProgress = chapterProgress * 100 / videos.size();
Map root = getRootObject();
root.put("chapter", mChapter);
root.put("chapterProgress", chapterProgress);
root.put("videos", videos);
root.put("completedVideos", completedVideos);
return new TemplateRepresentation(mTrainingTemplate, root, MediaType.TEXT_HTML);
} catch (Exception e) {
cLog.fatal("Could not render page: " + e.getMessage(), e);
setStatus(Status.SERVER_ERROR_INTERNAL);
return ErrorPage.RENDER_ERROR;
}
}
/**
* @return The backend endpoint URI
*/
private String getBackendEndpoint() {
return mConfig.getString("backendUri", "riap://component/backend");
}
/**
* Helper method to send a GET to the backend.
*/
private JsonResponse backendGet(final String uri) {
cLog.debug("Sending backend GET " + uri);
final JsonResponse response = mJsonClient.get(getBackendEndpoint() + uri);
final Status status = response.getStatus();
if (!status.isSuccess() && !Status.CLIENT_ERROR_NOT_FOUND.equals(status)) {
cLog.warn("Error making backend request for '" + uri + "'. status = " + response.getStatus().toString());
}
return response;
}
private JsonResponse backendPut(final String uri, final Map data) {
cLog.debug("Sending backend PUT " + uri);
final JsonResponse response = mJsonClient.put(getBackendEndpoint() + uri, data);
final Status status = response.getStatus();
if (!status.isSuccess() && !Status.CLIENT_ERROR_NOT_FOUND.equals(status)) {
cLog.warn("Error making backend request for '" + uri + "'. status = " + response.getStatus().toString());
}
return response;
}
}
|