From cde7018f9aa39df7eeee4bc4d7b10b0c7bd18d23 Mon Sep 17 00:00:00 2001 From: tom creighton Date: Tue, 8 Jul 2014 08:03:04 -0400 Subject: [PATCH 1/3] need pluginManagement to resolve errors --- pom.xml | 49 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/pom.xml b/pom.xml index 524f0f0..7132535 100644 --- a/pom.xml +++ b/pom.xml @@ -8,20 +8,10 @@ jar xapi http://maven.apache.org - - - Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - repo - - - - https://github.com/adlnet/jxapi - - org.apache.maven.plugins + org.apache.maven.plugins maven-compiler-plugin 3.1 @@ -64,6 +54,43 @@ + + + + + org.eclipse.m2e + lifecycle-mapping + 1.0.0 + + + + + + + org.apache.maven.plugins + + + maven-dependency-plugin + + + [2.5.1,) + + + + copy-dependencies + + + + + + + + + + + + + UTF-8 From ec972c58dc91babd769f94e5650be9370f2eaee7 Mon Sep 17 00:00:00 2001 From: tom creighton Date: Tue, 8 Jul 2014 15:18:42 -0400 Subject: [PATCH 2/3] added toString --- .../java/gov/adlnet/xapi/model/Account.java | 11 ++++++++++ .../java/gov/adlnet/xapi/model/Activity.java | 18 +++++++++++++++ .../adlnet/xapi/model/ActivityDefinition.java | 11 ++++++++++ .../java/gov/adlnet/xapi/model/Actor.java | 11 ++++++++++ .../java/gov/adlnet/xapi/model/Agent.java | 22 +++++++++++++++++++ .../gov/adlnet/xapi/model/Definition.java | 17 ++++++-------- .../java/gov/adlnet/xapi/model/Group.java | 13 +++++++++++ .../java/gov/adlnet/xapi/model/Result.java | 15 ++++--------- .../java/gov/adlnet/xapi/model/Statement.java | 14 ++++++++++++ src/main/java/gov/adlnet/xapi/model/Verb.java | 20 +++++++++++++++++ 10 files changed, 131 insertions(+), 21 deletions(-) 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/Definition.java b/src/main/java/gov/adlnet/xapi/model/Definition.java index 29f4ff4..5a3f459 100644 --- a/src/main/java/gov/adlnet/xapi/model/Definition.java +++ b/src/main/java/gov/adlnet/xapi/model/Definition.java @@ -1,22 +1,19 @@ package gov.adlnet.xapi.model; -import java.util.HashMap; -import java.util.Map.Entry; import com.google.gson.*; public class Definition { - private HashMap extensions; + private JsonObject extensions; public JsonElement serialize(){ - JsonObject extensionsObj = new JsonObject(); - for (Entry item : extensions.entrySet()){ - extensionsObj.addProperty(item.getKey(), item.getValue()); - } - return extensionsObj; + + JsonObject self = new JsonObject(); + self.add("extensions", extensions); + return self; } - public HashMap getExtensions() { + public JsonObject getExtensions() { return extensions; } - public void setExtensions(HashMap extensions) { + public void setExtensions(JsonObject extensions) { this.extensions = extensions; } } 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 1b9454d..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; @@ -47,11 +44,11 @@ public void setDuration(String duration) { this.duration = duration; } - public HashMap getExtensions() { + public JsonObject getExtensions() { return extensions; } - public void setExtensions(HashMap extensions) { + public void setExtensions(JsonObject extensions) { this.extensions = extensions; } @@ -70,11 +67,7 @@ public JsonElement serialize() { obj.addProperty("duration", this.duration); } if (this.extensions != null) { - JsonObject extensionsObj = new JsonObject(); - obj.add("extensions", extensionsObj); - for (Entry item : extensions.entrySet()) { - extensionsObj.addProperty(item.getKey(), item.getValue()); - } + obj.add("extensions", extensions); } if (this.score != null) { obj.add("score", this.score.serialize()); @@ -87,5 +80,5 @@ public JsonElement serialize() { private Boolean completion; private String response; private String duration; - private HashMap extensions; + private JsonObject extensions; } 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; From ca99440450027ad73588eed7b591d772ce3c7768 Mon Sep 17 00:00:00 2001 From: tom creighton Date: Wed, 9 Jul 2014 09:28:50 -0400 Subject: [PATCH 3/3] Update README.md --- README.md | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) 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