Skip to content
This repository has been archived by the owner on Oct 23, 2020. It is now read-only.

Commit

Permalink
Merge pull request #3 from creighton/tostrings
Browse files Browse the repository at this point in the history
constructors and tostrings
  • Loading branch information
wegrata committed Jul 9, 2014
2 parents b1e6be0 + ca99440 commit e0bf3f7
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 3 deletions.
92 changes: 92 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,98 @@ StatementClient client = new StatementClient(url, username, password);

This has the possiblitiy of throwing a ```MalformedURLException``` if an invalid URL is passed it.

## Creating a Statement

This library has some overloaded model constructors to simplify building common parts of a statement.

```java
// create an activity with just an id
Activity activity = new Activity("http://activity.com");
System.out.println("Activity");
System.out.println( "\t" + activity );

// prints...
// Activity
// http://activity.com

// create an agent with name and mbox
Agent agent = new Agent("tommy", "mailto:[email protected]");
System.out.println("Agent - name & mbox");
System.out.println("\t" + agent);

// prints...
// Agent - name & mbox
// tommy

// create an agent with just an mbox
Agent agent = new Agent(null, "mailto:[email protected]")
System.out.println("Agent - mbox");
System.out.println("\t" + agent);

// prints...
// Agent - mbox
// mailto:[email protected]

// create an agent with openid
Agent agent = new Agent(null, new URI("http://tom-is-me")));
System.out.println("Agent - openid");
System.out.println("\t" + agent);

// prints...
// Agent - openid
// http://tom-is-me

// create an agent with an account
Agent agent = new Agent("", new Account("joe", "http://joe.com"));
System.out.println("Agent - account");
System.out.println("\t" + agent);

// prints...
// Agent - account
// joe (http://joe.com)


// use an ADL verb
System.out.println("ADL verb");
System.out.println("\t" + Verbs.answered());

// prints...
// ADL verb
// answered

// create a verb
Verb verb = new Verb("http://my.verb/didsomething");
System.out.println("my verb");
System.out.println("\t" + verb);

// prints...
// my verb
// http://my.verb/didsomething


// create a verb with display
HashMap<String, String> disp = new HashMap<String, String>();
disp.put("fr", "le ran");
disp.put("en-US", "ran");
Verb verb = new Verb("http://my.verb/ran", disp);
System.out.println("my ran verb");
System.out.println("\t" + verb);

// prints...
// my ran verb
// ran

// create a basic statement
System.out.println("Statement ");
System.out.println("\t" + new Statement(new Agent("tom", "mailto:[email protected]"),
new Verb("http://verb.com/did", getVerbDisp()),
new Activity("act:id")));

// prints...
// Statement
// 7fdbc0cc-aef8-47d6-97ad-b1929afc34b5: tom did act:id
```

## Publishing a Statement

To publish a statement you'll need a Verb (There is a predefined list of ADL verbs available in ```gov.adlnet.xapi.model.Verbs``` class), an Actor who compled the activity the statment describes, and the Statement object, which can be an Activity, a SubStatement, or a StatementRef
Expand Down
37 changes: 37 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,43 @@
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-dependency-plugin
</artifactId>
<versionRange>
[2.5.1,)
</versionRange>
<goals>
<goal>
copy-dependencies
</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/gov/adlnet/xapi/model/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
public class Account {
private String homePage;
private String name;

public Account() {}

public Account(String name, String homepage) {
this.name = name;
this.homePage = homepage;
}

public String getHomePage() {
return homePage;
Expand Down Expand Up @@ -32,4 +39,8 @@ public JsonElement serialize() {
}
return obj;
}

public String toString() {
return String.format("%s (%s)", name, homePage);
}
}
18 changes: 18 additions & 0 deletions src/main/java/gov/adlnet/xapi/model/Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ public class Activity implements IStatementObject {
private String id;

private ActivityDefinition definition;

public Activity() {}

public Activity(String id) {
this.id = id;
}

public Activity(String id, ActivityDefinition definition) {
this.id = id;
this.definition = definition;
}

public String getObjectType() {
return ACTIVITY;
Expand Down Expand Up @@ -40,4 +51,11 @@ public JsonElement serialize() {
}
return obj;
}

public String toString() {
return String.format(
"%s", (definition == null ||
definition.toString() == null ||
definition.toString().isEmpty()) ? id : definition.toString());
}
}
11 changes: 11 additions & 0 deletions src/main/java/gov/adlnet/xapi/model/ActivityDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public class ActivityDefinition {
private ArrayList<InteractionComponent> source;
private ArrayList<InteractionComponent> target;
private ArrayList<InteractionComponent> steps;

public ActivityDefinition() {}

public ActivityDefinition(HashMap<String, String>name, HashMap<String, String>description) {
this.name = name;
this.description = description;
}

private JsonElement serializeMap(HashMap<String, String> map) {
JsonObject obj = new JsonObject();
Expand Down Expand Up @@ -189,4 +196,8 @@ public ArrayList<InteractionComponent> getSteps() {
public void setSteps(ArrayList<InteractionComponent> steps) {
this.steps = steps;
}

public String toString() {
return name.get("en-US");
}
}
11 changes: 11 additions & 0 deletions src/main/java/gov/adlnet/xapi/model/Actor.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,15 @@ public JsonElement serialize() {
obj.addProperty("objectType", this.getObjectType());
return obj;
}

public String toString() {
if (name != null && ! name.isEmpty()) return name;

String ret = "Anonymous";
if (mbox != null && ! mbox.isEmpty()) ret = mbox;
else if (mbox_sha1sum != null && ! mbox_sha1sum.isEmpty()) ret = mbox_sha1sum;
else if (openid != null && ! openid.toString().isEmpty()) ret = openid.toString();
else if (account != null) ret = account.toString();
return ret;
}
}
22 changes: 22 additions & 0 deletions src/main/java/gov/adlnet/xapi/model/Agent.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
package gov.adlnet.xapi.model;

import java.net.URI;

public class Agent extends Actor implements IStatementObject {

public static final String AGENT = "Agent";

public Agent() {}

public Agent(String name, String mbox) {
super();
setName(name);
setMbox(mbox);
}

public Agent(String name, URI openid) {
super();
setName(name);
setOpenid(openid);
}

public Agent(String name, Account account) {
super();
setName(name);
setAccount(account);
}

@Override
public String getObjectType() {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/gov/adlnet/xapi/model/Group.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package gov.adlnet.xapi.model;

import java.util.ArrayList;

import com.google.gson.*;
public class Group extends Actor {

public static final String GROUP = "Group";

public Group(ArrayList<Agent> members) {
super();
setMember(members);
}

@Override
public String getObjectType() {
return GROUP;
Expand All @@ -25,4 +32,10 @@ public JsonElement serialize(){
obj.add("member", members);
return obj;
}

public String toString() {
String ret = super.toString();
if (ret == null || ret.isEmpty() || "Anonymous".equals(ret)) ret = member.toString();
return ret;
}
}
3 changes: 0 additions & 3 deletions src/main/java/gov/adlnet/xapi/model/Result.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package gov.adlnet.xapi.model;

import java.util.HashMap;
import java.util.Map.Entry;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/gov/adlnet/xapi/model/Statement.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gov.adlnet.xapi.model;

import java.util.ArrayList;
import java.util.UUID;

public class Statement {
private String id;
Expand All @@ -16,6 +17,15 @@ public class Statement {
private Actor authority;
private ArrayList<Attachment> attachments;

public Statement() {}

public Statement(Actor actor, Verb verb, IStatementObject object) {
this.id = UUID.randomUUID().toString();
this.actor = actor;
this.verb = verb;
this.object = object;
}

public String getTimestamp() {
return timestamp;
}
Expand Down Expand Up @@ -82,4 +92,8 @@ public Result getResult() {
public void setResult(Result result) {
this.result = result;
}

public String toString() {
return String.format("%s: %s %s %s", id, actor, verb, object);
}
}
20 changes: 20 additions & 0 deletions src/main/java/gov/adlnet/xapi/model/Verb.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
import com.google.gson.*;

public class Verb {

public Verb() {}

public Verb(String id) {
this.id = id;
}

public Verb(String id, HashMap<String, String> display) {
this.id = id;
this.display = display;
}

public JsonElement serialize() {
JsonObject obj = new JsonObject();
if (this.id != null) {
Expand Down Expand Up @@ -35,6 +47,14 @@ public HashMap<String, String> getDisplay() {
public void setDisplay(HashMap<String, String> display) {
this.display = display;
}

public String toString() {
String ret = id;
if (display != null &&
display.get("en-US") != null &&
! display.get("en-US").isEmpty()) ret = display.get("en-US");
return ret;
}

private String id;
private HashMap<String, String> display;
Expand Down

0 comments on commit e0bf3f7

Please sign in to comment.