Skip to content

Commit

Permalink
Merge pull request #51 from brianjmiller/pr37
Browse files Browse the repository at this point in the history
Add methods populateWithAvailableLanguages, retrieveActivity, retrievePerson
  • Loading branch information
brianjmiller committed Dec 30, 2015
2 parents 80316ee + fc43f15 commit 674a196
Show file tree
Hide file tree
Showing 10 changed files with 350 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/rusticisoftware/tincan/Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.rusticisoftware.tincan;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

Expand All @@ -26,6 +27,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.rusticisoftware.tincan.json.JSONBase;
import com.rusticisoftware.tincan.json.Mapper;
import com.rusticisoftware.tincan.json.StringOfJSON;

/**
* Activity model class
Expand Down Expand Up @@ -67,6 +69,10 @@ public Activity(String id, String name, String description) throws URISyntaxExce
this.setDefinition(new ActivityDefinition(name, description));
}

public Activity(StringOfJSON jsonStr) throws URISyntaxException, IOException {
this(jsonStr.toJSONNode());
}

@Override
public ObjectNode toJSONNode(TCAPIVersion version) {
ObjectNode node = Mapper.getInstance().createObjectNode();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/rusticisoftware/tincan/LRS.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ public interface LRS {
LRSResponse deleteState(StateDocument state);
LRSResponse clearState(Activity activity, Agent agent, UUID registration);

ActivityLRSResponse retrieveActivity(Activity activity);
ProfileKeysLRSResponse retrieveActivityProfileIds(Activity activity);
ActivityProfileLRSResponse retrieveActivityProfile(String id, Activity activity);
LRSResponse saveActivityProfile(ActivityProfileDocument profile);
LRSResponse updateActivityProfile(ActivityProfileDocument profile);
LRSResponse deleteActivityProfile(ActivityProfileDocument profile);

PersonLRSResponse retrievePerson(Agent agent);
ProfileKeysLRSResponse retrieveAgentProfileIds(Agent agent);
AgentProfileLRSResponse retrieveAgentProfile(String id, Agent agent);
LRSResponse saveAgentProfile(AgentProfileDocument profile);
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/com/rusticisoftware/tincan/LanguageMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.List;
import java.util.ArrayList;

import com.rusticisoftware.tincan.json.JSONBase;
import com.rusticisoftware.tincan.json.Mapper;
Expand All @@ -39,6 +41,7 @@ private class LanguageMapIterator implements Iterator<Map.Entry<String, String>>
public LanguageMapIterator() {
iterator = _map.entrySet().iterator();
}

@Override
public boolean hasNext() {
return iterator.hasNext();
Expand All @@ -52,7 +55,8 @@ public Entry<String, String> next() {
@Override
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException(
"LanguageMap iterator does not implement the remove method");
"LanguageMap iterator does not implement the remove method"
);
}
}
public LanguageMap(JsonNode jsonNode) {
Expand All @@ -79,7 +83,7 @@ public ObjectNode toJSONNode(TCAPIVersion version) {
public String put(String key, String val) {
return this._map.put(key, val);
}

public String put(Map.Entry<String, String> entry) {
return this.put(entry.getKey(), entry.getValue());
}
Expand Down Expand Up @@ -109,6 +113,14 @@ public Map.Entry<String, String> findFirstValue(String value) {
return retVal;
}

public void populateWithAvailableLanguages(List<String> list) {
Iterator<Map.Entry<String, String>> it = this.iterator();
while (it.hasNext()) {
Map.Entry<String, String> n = it.next();
list.add(n.getKey());
}
}

@Override
public Iterator<Entry<String, String>> iterator() {
return new LanguageMapIterator();
Expand Down
153 changes: 153 additions & 0 deletions src/main/java/com/rusticisoftware/tincan/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
Copyright 2015 Rustici Software
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.rusticisoftware.tincan;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.rusticisoftware.tincan.json.JSONBase;
import com.rusticisoftware.tincan.json.Mapper;
import com.rusticisoftware.tincan.json.StringOfJSON;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* Person Model class
*/
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
public class Person extends JSONBase {
protected final String objectType = "Person";
private List<String> name;
private List<String> mbox;
private List<String> mbox_sha1sum;
private List<String> openid;
private List<AgentAccount> account;

public Person(StringOfJSON jsonStr) throws IOException {
this(jsonStr.toJSONNode());
}

public Person(JsonNode jsonNode) {
this();

JsonNode nameNode = jsonNode.path("name");
if (! nameNode.isMissingNode()) {
this.name = new ArrayList<String>();

for (JsonNode element : nameNode) {
this.name.add(element.textValue());
}
}

JsonNode mboxNode = jsonNode.path("mbox");
if (! mboxNode.isMissingNode()) {
this.mbox = new ArrayList<String>();

for (JsonNode element : mboxNode) {
this.mbox.add(element.textValue());
}
}

JsonNode mbox_sha1sumNode = jsonNode.path("mbox_sha1sum");
if (! mbox_sha1sumNode.isMissingNode()) {
this.mbox_sha1sum = new ArrayList<String>();

for (JsonNode element : mbox_sha1sumNode) {
this.mbox_sha1sum.add(element.textValue());
}
}

JsonNode openidNode = jsonNode.path("openid");
if (! openidNode.isMissingNode()) {
this.openid = new ArrayList<String>();

for (JsonNode element : openidNode) {
this.openid.add(element.textValue());
}
}

JsonNode accountNode = jsonNode.path("account");
if (! accountNode.isMissingNode()) {
this.account = new ArrayList<AgentAccount>();

for (JsonNode element : accountNode) {
this.account.add(new AgentAccount(element));
}
}
}

@Override
public ObjectNode toJSONNode(TCAPIVersion version) {
ObjectMapper mapper = Mapper.getInstance();
ObjectNode node = mapper.createObjectNode();
node.put("objectType", this.getObjectType());

if (this.name != null && this.name.size() > 0) {
ArrayNode name = mapper.createArrayNode();
node.put("name", name);

for (String element : this.getName()) {
name.add(element);
}
}

if (this.mbox != null && this.mbox.size() > 0) {
ArrayNode mbox = mapper.createArrayNode();
node.put("mbox", mbox);

for (String element : this.getMbox()) {
mbox.add(element);
}
}

if (this.mbox_sha1sum != null && this.mbox_sha1sum.size() > 0) {
ArrayNode mbox_sha1sum = mapper.createArrayNode();
node.put("mbox_sha1sum", mbox_sha1sum);

for (String element : this.getMbox_sha1sum()) {
mbox_sha1sum.add(element);
}
}

if (this.openid != null && this.openid.size() > 0) {
ArrayNode openid = mapper.createArrayNode();
node.put("openid", openid);

for (String element : this.getOpenid()) {
openid.add(element);
}
}

if (this.account != null && this.account.size() > 0) {
ArrayNode account = mapper.createArrayNode();
node.put("account", account);

for (AgentAccount element : this.getAccount()) {
account.add(element.toJSONNode(version));
}
}

return node;
}
}
60 changes: 60 additions & 0 deletions src/main/java/com/rusticisoftware/tincan/RemoteLRS.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.rusticisoftware.tincan.http.HTTPRequest;
import com.rusticisoftware.tincan.http.HTTPResponse;
import com.rusticisoftware.tincan.lrsresponses.*;

import lombok.Data;
import lombok.NoArgsConstructor;

Expand All @@ -42,6 +43,7 @@
import org.eclipse.jetty.http.HttpMethods;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.ByteArrayBuffer;

import com.rusticisoftware.tincan.exceptions.*;
import com.rusticisoftware.tincan.json.Mapper;
import com.rusticisoftware.tincan.json.StringOfJSON;
Expand Down Expand Up @@ -694,6 +696,35 @@ public LRSResponse clearState(Activity activity, Agent agent, UUID registration)
return deleteDocument("activities/state", queryParams);
}

@Override
public ActivityLRSResponse retrieveActivity(Activity activity) {
HTTPRequest request = new HTTPRequest();
request.setMethod(HttpMethods.GET);
request.setResource("activities");
request.setQueryParams(new HashMap<String, String>());
request.getQueryParams().put("activityId", activity.getId().toString());

HTTPResponse response = makeSyncRequest(request);
int status = response.getStatus();

ActivityLRSResponse lrsResponse = new ActivityLRSResponse(request, response);

if (status == 200) {
lrsResponse.setSuccess(true);
try {
lrsResponse.setContent(new Activity(new StringOfJSON(response.getContent())));
} catch (Exception ex) {
lrsResponse.setErrMsg("Exception: " + ex.toString());
lrsResponse.setSuccess(false);
}
}
else {
lrsResponse.setSuccess(false);
}

return lrsResponse;
}

@Override
public ProfileKeysLRSResponse retrieveActivityProfileIds(Activity activity) {
HashMap<String, String> queryParams = new HashMap<String, String>();
Expand Down Expand Up @@ -753,6 +784,35 @@ public LRSResponse deleteActivityProfile(ActivityProfileDocument profile) {
return deleteDocument("activities/profile", queryParams);
}

@Override
public PersonLRSResponse retrievePerson(Agent agent) {
HTTPRequest request = new HTTPRequest();
request.setMethod(HttpMethods.GET);
request.setResource("agents");
request.setQueryParams(new HashMap<String, String>());
request.getQueryParams().put("agent", agent.toJSON(this.getVersion(), this.usePrettyJSON()));

HTTPResponse response = makeSyncRequest(request);
int status = response.getStatus();

PersonLRSResponse lrsResponse = new PersonLRSResponse(request, response);

if (status == 200) {
lrsResponse.setSuccess(true);
try {
lrsResponse.setContent(new Person(new StringOfJSON(response.getContent())));
} catch (Exception ex) {
lrsResponse.setErrMsg("Exception: " + ex.toString());
lrsResponse.setSuccess(false);
}
}
else {
lrsResponse.setSuccess(false);
}

return lrsResponse;
}

@Override
public ProfileKeysLRSResponse retrieveAgentProfileIds(Agent agent) {
HashMap<String, String> queryParams = new HashMap<String, String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ public String getContent() {
}

public String getContentType() { return this.getHeader("Content-Type"); }
public String getEtag() { return this.getHeader("ETag").toLowerCase(); }
public String getEtag() {
String etag = this.getHeader("ETag");
if (etag == null) {
return etag;
}

return etag.toLowerCase();
}
public DateTime getLastModified() {
DateTimeFormatter RFC1123_DATE_TIME_FORMATTER =
DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss 'GMT'").withZoneUTC();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2015 Rustici Software
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.rusticisoftware.tincan.lrsresponses;

import com.rusticisoftware.tincan.Activity;
import com.rusticisoftware.tincan.http.HTTPRequest;
import com.rusticisoftware.tincan.http.HTTPResponse;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
public class ActivityLRSResponse extends LRSResponse{
private Activity content;

public ActivityLRSResponse (HTTPRequest initRequest, HTTPResponse initResponse) {
super(initRequest, initResponse);
}
}
Loading

0 comments on commit 674a196

Please sign in to comment.