1
1
package gov .adlnet .xapi .client ;
2
2
3
3
import gov .adlnet .xapi .model .Activity ;
4
+ import gov .adlnet .xapi .model .Agent ;
4
5
5
6
import java .io .IOException ;
6
7
import java .net .MalformedURLException ;
7
8
import java .net .URL ;
8
9
9
10
import com .google .gson .JsonArray ;
10
11
import com .google .gson .JsonElement ;
12
+ import com .google .gson .JsonObject ;
11
13
12
14
public class ActivityClient extends BaseClient {
13
15
@@ -27,27 +29,111 @@ public Activity getActivity(String activityId)
27
29
String result = issueGet (path );
28
30
return getDecoder ().fromJson (result , Activity .class );
29
31
}
30
- public JsonArray getActivities (String activityId , String since ) throws MalformedURLException , IOException {
32
+
33
+ private String createProfilePath (String activityId , String profileId ){
34
+ StringBuilder sb = new StringBuilder ();
35
+ sb .append ("/xAPI/activities/profile?activityId=" );
36
+ sb .append (activityId );
37
+ sb .append ("&profileId=" );
38
+ sb .append (profileId );
39
+ return sb .toString ();
40
+ }
41
+
42
+ public JsonObject getActivityProfile (String activityId , String profileId )
43
+ throws MalformedURLException , IOException {
44
+ String result = issueGet (createProfilePath (activityId , profileId ));
45
+ return getDecoder ().fromJson (result , JsonObject .class );
46
+ }
47
+
48
+ public JsonArray getActivityProfiles (String activityId , String since ) throws MalformedURLException , IOException {
31
49
String path = "/xapi/activities/profile?activityId=" + activityId ;
32
50
if (since != null && since .length () > 0 ){
33
51
path += ("&since=" + since );
34
52
}
35
53
String result = issueGet (path );
36
54
return getDecoder ().fromJson (result , JsonArray .class );
37
55
}
38
- private String createProfilePath (String activityId , String profileId ){
39
- StringBuilder sb = new StringBuilder ();
40
- sb .append ("/xAPI/activities/profile?activityId=" );
41
- sb .append (activityId );
42
- sb .append ("&profileId=" );
43
- sb .append (profileId );
44
- return sb .toString ();
45
- }
46
- public boolean publishActivityProfile (String activityId , String profileId ,
47
- JsonElement obj ) throws IOException {
48
- String json = getDecoder ().toJson (obj );
56
+
57
+ public boolean postActivityProfile (String activityId , String profileId ,
58
+ JsonElement profile ) throws IOException {
59
+ String json = getDecoder ().toJson (profile );
49
60
String response = issuePost (createProfilePath (activityId , profileId ),
50
61
json );
51
- return response != null ;
62
+ return response . isEmpty () ;
52
63
}
64
+
65
+ public boolean putActivityProfile (String activityId , String profileId ,
66
+ JsonElement profile ) throws IOException {
67
+ String json = getDecoder ().toJson (profile );
68
+ String response = issuePut (createProfilePath (activityId , profileId ),
69
+ json );
70
+ return response .isEmpty ();
71
+ }
72
+
73
+ public boolean deleteActivityProfile (String activityId , String profileId ) throws IOException {
74
+ String response = issueDelete (createProfilePath (activityId , profileId ));
75
+ return response .isEmpty ();
76
+ }
77
+
78
+ private String createStatePath (String activityId , Agent agent , String registration , String stateId ){
79
+ String path = String .format ("/xapi/activities/state?activityId=%s&agent=%s&stateId=%s" , activityId ,
80
+ getDecoder ().toJson (agent .serialize ()), stateId );
81
+ if (registration != null && registration .length () > 0 ){
82
+ path += ("®istration=" + registration );
83
+ }
84
+ return path ;
85
+ }
86
+
87
+ public JsonObject getActivityState (String activityId , Agent agent , String registration , String stateId )
88
+ throws MalformedURLException , IOException {
89
+ String result = issueGet (createStatePath (activityId , agent , registration , stateId ));
90
+ return getDecoder ().fromJson (result , JsonObject .class );
91
+ }
92
+
93
+ public boolean postActivityState (String activityId , Agent agent , String registration , String stateId ,
94
+ JsonElement state )
95
+ throws MalformedURLException , IOException {
96
+ String json = getDecoder ().toJson ((state ));
97
+ String result = issuePost (createStatePath (activityId , agent , registration , stateId ), json );
98
+ return result .isEmpty ();
99
+ }
100
+
101
+ public boolean putActivityState (String activityId , Agent agent , String registration , String stateId ,
102
+ JsonElement state )
103
+ throws MalformedURLException , IOException {
104
+ String json = getDecoder ().toJson ((state ));
105
+ String result = issuePut (createStatePath (activityId , agent , registration , stateId ), json );
106
+ return result .isEmpty ();
107
+ }
108
+
109
+ public boolean deleteActivityState (String activityId , Agent agent , String registration , String stateId )
110
+ throws MalformedURLException , IOException {
111
+ String result = issueDelete (createStatePath (activityId , agent , registration , stateId ));
112
+ return result .isEmpty ();
113
+ }
114
+
115
+ public JsonArray getActivityStates (String activityId , Agent agent , String registration , String since )
116
+ throws MalformedURLException , IOException {
117
+ String path = String .format ("/xapi/activities/state?activityId=%s&agent=%s" , activityId ,
118
+ getDecoder ().toJson (agent .serialize ()));
119
+ if (registration != null && registration .length () > 0 ){
120
+ path += ("®istration=" + registration );
121
+ }
122
+ if (since != null && since .length () > 0 ){
123
+ path += ("&since=" + since );
124
+ }
125
+ String result = issueGet (path );
126
+ return getDecoder ().fromJson (result , JsonArray .class );
127
+ }
128
+
129
+ public boolean deleteActivityStates (String activityId , Agent agent , String registration )
130
+ throws MalformedURLException , IOException {
131
+ String path = String .format ("/xapi/activities/state?activityId=%s&agent=%s" , activityId ,
132
+ getDecoder ().toJson (agent .serialize ()));
133
+ if (registration != null && registration .length () > 0 ){
134
+ path += ("®istration=" + registration );
135
+ }
136
+ String result = issueDelete (path );
137
+ return result .isEmpty ();
138
+ }
53
139
}
0 commit comments