diff --git a/README.md b/README.md index 6b0f9fa..f12144c 100644 --- a/README.md +++ b/README.md @@ -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:tommy@example.com"); +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:tommy@example.com") +System.out.println("Agent - mbox"); +System.out.println("\t" + agent); + +// prints... +// Agent - mbox +// mailto:tommy@example.com + +// 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 disp = new HashMap(); +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:tom@example.com"), + 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 diff --git a/pom.xml b/pom.xml index cbe7e64..fd9dfb3 100644 --- a/pom.xml +++ b/pom.xml @@ -109,6 +109,43 @@ + + + + + org.eclipse.m2e + lifecycle-mapping + 1.0.0 + + + + + + + org.apache.maven.plugins + + + maven-dependency-plugin + + + [2.5.1,) + + + + copy-dependencies + + + + + + + + + + + + + UTF-8 diff --git a/src/main/java/gov/adlnet/xapi/model/Account.java b/src/main/java/gov/adlnet/xapi/model/Account.java index 6f52e8c..a80f2c8 100644 --- a/src/main/java/gov/adlnet/xapi/model/Account.java +++ b/src/main/java/gov/adlnet/xapi/model/Account.java @@ -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; @@ -32,4 +39,8 @@ public JsonElement serialize() { } return obj; } + + public String toString() { + return String.format("%s (%s)", name, homePage); + } } diff --git a/src/main/java/gov/adlnet/xapi/model/Activity.java b/src/main/java/gov/adlnet/xapi/model/Activity.java index f22378d..33ce602 100644 --- a/src/main/java/gov/adlnet/xapi/model/Activity.java +++ b/src/main/java/gov/adlnet/xapi/model/Activity.java @@ -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; @@ -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()); + } } diff --git a/src/main/java/gov/adlnet/xapi/model/ActivityDefinition.java b/src/main/java/gov/adlnet/xapi/model/ActivityDefinition.java index a56ea44..f03310a 100644 --- a/src/main/java/gov/adlnet/xapi/model/ActivityDefinition.java +++ b/src/main/java/gov/adlnet/xapi/model/ActivityDefinition.java @@ -25,6 +25,13 @@ public class ActivityDefinition { private ArrayList source; private ArrayList target; private ArrayList steps; + + public ActivityDefinition() {} + + public ActivityDefinition(HashMapname, HashMapdescription) { + this.name = name; + this.description = description; + } private JsonElement serializeMap(HashMap map) { JsonObject obj = new JsonObject(); @@ -189,4 +196,8 @@ public ArrayList getSteps() { public void setSteps(ArrayList steps) { this.steps = steps; } + + public String toString() { + return name.get("en-US"); + } } diff --git a/src/main/java/gov/adlnet/xapi/model/Actor.java b/src/main/java/gov/adlnet/xapi/model/Actor.java index 8b2f07d..7891806 100644 --- a/src/main/java/gov/adlnet/xapi/model/Actor.java +++ b/src/main/java/gov/adlnet/xapi/model/Actor.java @@ -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; + } } diff --git a/src/main/java/gov/adlnet/xapi/model/Agent.java b/src/main/java/gov/adlnet/xapi/model/Agent.java index 00deb0b..a125730 100644 --- a/src/main/java/gov/adlnet/xapi/model/Agent.java +++ b/src/main/java/gov/adlnet/xapi/model/Agent.java @@ -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() { diff --git a/src/main/java/gov/adlnet/xapi/model/Group.java b/src/main/java/gov/adlnet/xapi/model/Group.java index 6ec6cf9..42a2d3c 100644 --- a/src/main/java/gov/adlnet/xapi/model/Group.java +++ b/src/main/java/gov/adlnet/xapi/model/Group.java @@ -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 members) { + super(); + setMember(members); + } + @Override public String getObjectType() { return GROUP; @@ -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; + } } diff --git a/src/main/java/gov/adlnet/xapi/model/Result.java b/src/main/java/gov/adlnet/xapi/model/Result.java index d62bf13..37d324c 100644 --- a/src/main/java/gov/adlnet/xapi/model/Result.java +++ b/src/main/java/gov/adlnet/xapi/model/Result.java @@ -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; diff --git a/src/main/java/gov/adlnet/xapi/model/Statement.java b/src/main/java/gov/adlnet/xapi/model/Statement.java index a132854..f5060d3 100644 --- a/src/main/java/gov/adlnet/xapi/model/Statement.java +++ b/src/main/java/gov/adlnet/xapi/model/Statement.java @@ -1,6 +1,7 @@ package gov.adlnet.xapi.model; import java.util.ArrayList; +import java.util.UUID; public class Statement { private String id; @@ -16,6 +17,15 @@ public class Statement { private Actor authority; private ArrayList 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; } @@ -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); + } } diff --git a/src/main/java/gov/adlnet/xapi/model/Verb.java b/src/main/java/gov/adlnet/xapi/model/Verb.java index 2bcb84d..f1eee3e 100644 --- a/src/main/java/gov/adlnet/xapi/model/Verb.java +++ b/src/main/java/gov/adlnet/xapi/model/Verb.java @@ -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 display) { + this.id = id; + this.display = display; + } + public JsonElement serialize() { JsonObject obj = new JsonObject(); if (this.id != null) { @@ -35,6 +47,14 @@ public HashMap getDisplay() { public void setDisplay(HashMap 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 display;