Skip to content

Commit

Permalink
Fix retrieveActivity and retrievePerson methods
Browse files Browse the repository at this point in the history
* Adds a Person model class
* Adds a PersonLRSResponse class and an ActivityLRSResponse class
* Updates the RemoteLRS implementations of .retrieveActivty and
  .retrievePerson to return new LRS response types with their content
  set to the correct model class
* Adds constructor from StringOfJSON objects for Activity
  • Loading branch information
brianjmiller committed Dec 30, 2015
1 parent 016ecd4 commit fc43f15
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 58 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
4 changes: 2 additions & 2 deletions src/main/java/com/rusticisoftware/tincan/LRS.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public interface LRS {
LRSResponse deleteState(StateDocument state);
LRSResponse clearState(Activity activity, Agent agent, UUID registration);

ActivityProfileLRSResponse retrieveActivity(Activity activity);
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);

AgentProfileLRSResponse retrievePerson(Agent agent);
PersonLRSResponse retrievePerson(Agent agent);
ProfileKeysLRSResponse retrieveAgentProfileIds(Agent agent);
AgentProfileLRSResponse retrieveAgentProfile(String id, Agent agent);
LRSResponse saveAgentProfile(AgentProfileDocument profile);
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;
}
}
62 changes: 40 additions & 22 deletions src/main/java/com/rusticisoftware/tincan/RemoteLRS.java
Original file line number Diff line number Diff line change
Expand Up @@ -697,20 +697,29 @@ public LRSResponse clearState(Activity activity, Agent agent, UUID registration)
}

@Override
public ActivityProfileLRSResponse retrieveActivity(Activity activity) {
HashMap<String, String> queryParams = new HashMap<String, String>();
queryParams.put("activityId", activity.getId().toString());
ActivityProfileDocument profileDocument = new ActivityProfileDocument();
profileDocument.setActivity(activity);
profileDocument.setId(null);
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());

LRSResponse lrsResp = getDocument("activities", queryParams, profileDocument);
HTTPResponse response = makeSyncRequest(request);
int status = response.getStatus();

ActivityProfileLRSResponse lrsResponse = new ActivityProfileLRSResponse(lrsResp.getRequest(), lrsResp.getResponse());
lrsResponse.setSuccess(lrsResp.getSuccess());
ActivityLRSResponse lrsResponse = new ActivityLRSResponse(request, response);

if (lrsResponse.getResponse().getStatus() == 200) {
lrsResponse.setContent(profileDocument);
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;
Expand Down Expand Up @@ -776,20 +785,29 @@ public LRSResponse deleteActivityProfile(ActivityProfileDocument profile) {
}

@Override
public AgentProfileLRSResponse retrievePerson(Agent agent) {
HashMap<String, String> queryParams = new HashMap<String, String>();
queryParams.put("agent", agent.toJSON(TCAPIVersion.V100));
AgentProfileDocument profileDocument = new AgentProfileDocument();
profileDocument.setAgent(agent);
profileDocument.setId(null);
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()));

LRSResponse lrsResp = getDocument("agents", queryParams, profileDocument);
HTTPResponse response = makeSyncRequest(request);
int status = response.getStatus();

AgentProfileLRSResponse lrsResponse = new AgentProfileLRSResponse(lrsResp.getRequest(), lrsResp.getResponse());
lrsResponse.setSuccess(lrsResp.getSuccess());
PersonLRSResponse lrsResponse = new PersonLRSResponse(request, response);

if (lrsResponse.getResponse().getStatus() == 200) {
lrsResponse.setContent(profileDocument);
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;
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);
}
}
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.Person;
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 PersonLRSResponse extends LRSResponse{
private Person content;

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

0 comments on commit fc43f15

Please sign in to comment.