diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2014-09-27 11:05:43 -0700 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net> | 2014-09-27 11:05:43 -0700 |
commit | bdf47fc7340ec2a3ecaa6b11bbf87f0f5eaaeea1 (patch) | |
tree | b0faa7efb72587f99cd742caffcfb00d3f625cd9 /src/com/p4square | |
parent | 8fa4f1fb4f122ee1e27c6015479bcd883b4b0671 (diff) |
Adding support to fetch attributes for a user.
Diffstat (limited to 'src/com/p4square')
-rw-r--r-- | src/com/p4square/f1oauth/Attribute.java | 36 | ||||
-rw-r--r-- | src/com/p4square/f1oauth/F1API.java | 16 | ||||
-rw-r--r-- | src/com/p4square/f1oauth/F1Access.java | 84 | ||||
-rw-r--r-- | src/com/p4square/grow/frontend/AssessmentResultsPage.java | 4 | ||||
-rw-r--r-- | src/com/p4square/grow/frontend/ChapterCompletePage.java | 4 | ||||
-rw-r--r-- | src/com/p4square/grow/tools/AttributeTool.java | 55 |
6 files changed, 180 insertions, 19 deletions
diff --git a/src/com/p4square/f1oauth/Attribute.java b/src/com/p4square/f1oauth/Attribute.java index cc7cfc4..64f2507 100644 --- a/src/com/p4square/f1oauth/Attribute.java +++ b/src/com/p4square/f1oauth/Attribute.java @@ -12,11 +12,41 @@ import java.util.Date; * @author Jesse Morgan <jesse@jesterpm.net> */ public class Attribute { + private final String mAttributeName; + private String mId; private Date mStartDate; private Date mEndDate; private String mComment; /** + * @param name The attribute name. + */ + public Attribute(final String name) { + mAttributeName = name; + } + + /** + * @return the Attribute name. + */ + public String getAttributeName() { + return mAttributeName; + } + + /** + * @return the id of this specific attribute instance. + */ + public String getId() { + return mId; + } + + /** + * Set the attribute id to id. + */ + public void setId(final String id) { + mId = id; + } + + /** * @return the start date for the attribute. */ public Date getStartDate() { @@ -26,7 +56,7 @@ public class Attribute { /** * Set the start date for the attribute. */ - public void setStartDate(Date date) { + public void setStartDate(final Date date) { mStartDate = date; } @@ -40,7 +70,7 @@ public class Attribute { /** * Set the end date for the attribute. */ - public void setEndDate(Date date) { + public void setEndDate(final Date date) { mEndDate = date; } @@ -54,7 +84,7 @@ public class Attribute { /** * Set the comment on the attribute. */ - public void setComment(String comment) { + public void setComment(final String comment) { mComment = comment; } } diff --git a/src/com/p4square/f1oauth/F1API.java b/src/com/p4square/f1oauth/F1API.java index 88801db..a525c3f 100644 --- a/src/com/p4square/f1oauth/F1API.java +++ b/src/com/p4square/f1oauth/F1API.java @@ -5,6 +5,7 @@ package com.p4square.f1oauth; import java.io.IOException; +import java.util.List; import java.util.Map; import com.p4square.restlet.oauth.OAuthException; @@ -38,7 +39,18 @@ public interface F1API { * @param attributeName The attribute to add. * @param attribute The attribute to add. */ - boolean addAttribute(String userId, String attributeName, Attribute attribute) - throws F1Exception; + boolean addAttribute(String userId, Attribute attribute) throws F1Exception; + + /** + * Return attributes assigned to user. + * + * A user may be assigned multiple attributes with the same name, thus even if + * attributeName is specified, multiple attributes may be returned. + * + * @param userId The user to query. + * @param attributeName A specific attribute to return, null for all. + * @return A list of Attributes + */ + List<Attribute> getAttribute(String userId, String attributeName) throws F1Exception; } diff --git a/src/com/p4square/f1oauth/F1Access.java b/src/com/p4square/f1oauth/F1Access.java index 32550c4..439e508 100644 --- a/src/com/p4square/f1oauth/F1Access.java +++ b/src/com/p4square/f1oauth/F1Access.java @@ -7,6 +7,7 @@ package com.p4square.f1oauth; import java.io.IOException; import java.net.URLEncoder; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -257,13 +258,13 @@ public class F1Access { * @param attributeName The attribute to add. * @param attribute The attribute to add. */ - public boolean addAttribute(String userId, String attributeName, Attribute attribute) + public boolean addAttribute(String userId, Attribute attribute) throws F1Exception { // Get the attribute id. - String attributeId = getAttributeId(attributeName); + String attributeId = getAttributeId(attribute.getAttributeName()); if (attributeId == null) { - throw new F1Exception("Could not find id for " + attributeName); + throw new F1Exception("Could not find id for " + attribute.getAttributeName()); } // Get Attribute Template @@ -348,6 +349,83 @@ public class F1Access { return false; } + @Override + public List<Attribute> getAttribute(String userId, String attributeNameFilter) + throws F1Exception { + + Map attributesResponse; + + // Get Attributes + { + Request request = new Request(Method.GET, + mBaseUrl + "People/" + userId + "/Attributes.json"); + request.setChallengeResponse(mUser.getChallengeResponse()); + Response response = mOAuthHelper.getResponse(request); + + Representation representation = response.getEntity(); + try { + Status status = response.getStatus(); + if (status.isSuccess()) { + JacksonRepresentation<Map> entity = + new JacksonRepresentation<Map>(response.getEntity(), Map.class); + attributesResponse = entity.getObject(); + + } else { + throw new F1Exception("Failed to retrieve attributes: " + + status); + } + + } catch (IOException e) { + throw new F1Exception("Could not parse attributes.", e); + + } finally { + if (representation != null) { + representation.release(); + } + } + } + + // Parse Response + List<Attribute> result = new ArrayList<>(); + + try { + // I feel like I'm writing lisp here... + Map attributesMap = (Map) attributesResponse.get("attributes"); + if (attributesMap == null) { + return result; + } + + List<Map> attributes = (List<Map>) (attributesMap).get("attribute"); + for (Map attributeMap : attributes) { + String id = (String) attributeMap.get("@id"); + String startDate = (String) attributeMap.get("startDate"); + String endDate = (String) attributeMap.get("endDate"); + String comment = (String) attributeMap.get("comment"); + + Map attributeIdMap = (Map) ((Map) attributeMap.get("attributeGroup")) + .get("attribute"); + String attributeName = (String) attributeIdMap.get("name"); + + if (attributeNameFilter == null || attributeNameFilter.equals(attributeName)) { + Attribute attribute = new Attribute(attributeName); + attribute.setId(id); + if (startDate != null) { + attribute.setStartDate(DATE_FORMAT.parse(startDate)); + } + if (endDate != null) { + attribute.setEndDate(DATE_FORMAT.parse(endDate)); + } + attribute.setComment(comment); + result.add(attribute); + } + } + } catch (Exception e) { + throw new F1Exception("Failed to parse attributes response.", e); + } + + return result; + } + /** * @return an attribute id for the given attribute name. */ diff --git a/src/com/p4square/grow/frontend/AssessmentResultsPage.java b/src/com/p4square/grow/frontend/AssessmentResultsPage.java index ff1832c..2035ce8 100644 --- a/src/com/p4square/grow/frontend/AssessmentResultsPage.java +++ b/src/com/p4square/grow/frontend/AssessmentResultsPage.java @@ -113,12 +113,12 @@ public class AssessmentResultsPage extends FreeMarkerPageResource { String attributeName = "Assessment Complete - " + results.get("result"); try { - Attribute attribute = new Attribute(); + Attribute attribute = new Attribute(attributeName); attribute.setStartDate(new Date()); attribute.setComment(JsonEncodedProvider.MAPPER.writeValueAsString(results)); F1API f1 = mGrowFrontend.getF1Access().getAuthenticatedApi(user); - if (!f1.addAttribute(user.getIdentifier(), attributeName, attribute)) { + if (!f1.addAttribute(user.getIdentifier(), attribute)) { LOG.error("addAttribute failed for " + user.getIdentifier() + " with attribute " + attributeName); } diff --git a/src/com/p4square/grow/frontend/ChapterCompletePage.java b/src/com/p4square/grow/frontend/ChapterCompletePage.java index a2c4ebe..f07a870 100644 --- a/src/com/p4square/grow/frontend/ChapterCompletePage.java +++ b/src/com/p4square/grow/frontend/ChapterCompletePage.java @@ -169,11 +169,11 @@ public class ChapterCompletePage extends FreeMarkerPageResource { String attributeName = "Training Complete - " + mChapter; try { - Attribute attribute = new Attribute(); + Attribute attribute = new Attribute(attributeName); attribute.setStartDate(new Date()); F1API f1 = mGrowFrontend.getF1Access().getAuthenticatedApi(user); - if (!f1.addAttribute(user.getIdentifier(), attributeName, attribute)) { + if (!f1.addAttribute(user.getIdentifier(), attribute)) { LOG.error("addAttribute failed for " + user.getIdentifier() + " with attribute " + attributeName); } diff --git a/src/com/p4square/grow/tools/AttributeTool.java b/src/com/p4square/grow/tools/AttributeTool.java index 0775087..8e0540a 100644 --- a/src/com/p4square/grow/tools/AttributeTool.java +++ b/src/com/p4square/grow/tools/AttributeTool.java @@ -6,6 +6,7 @@ package com.p4square.grow.tools; import java.util.Arrays; import java.util.Date; +import java.util.List; import java.util.Map; import org.restlet.Client; @@ -32,11 +33,13 @@ public class AttributeTool { public static void usage() { System.out.println("java com.p4square.grow.tools.AttributeTool <command>...\n"); System.out.println("Commands:"); - System.out.println("\t--domain <domain> Set config domain"); - System.out.println("\t--dev Set config domain to dev"); - System.out.println("\t--config <file> Merge in config file"); - System.out.println("\t--list List all attributes"); - System.out.println("\t--assign <user> <attribute> <comment> Assign an attribute"); + System.out.println("\t--domain <domain> Set config domain"); + System.out.println("\t--dev Set config domain to dev"); + System.out.println("\t--config <file> Merge in config file"); + System.out.println("\t--list List all attributes"); + System.out.println("\t--assign <userId> <attribute> <comment> Assign an attribute"); + System.out.println("\t--getall <userId> Get an attribute"); + System.out.println("\t--get <userId> <attribute> Get an attribute"); } public static void main(String... args) { @@ -73,6 +76,12 @@ public class AttributeTool { } else if ("--assign".equals(args[offset])) { offset = assign(args, ++offset); + } else if ("--getall".equals(args[offset])) { + offset = getall(args, ++offset); + + } else if ("--get".equals(args[offset])) { + offset = get(args, ++offset); + } else { throw new IllegalArgumentException("Unknown command " + args[offset]); } @@ -128,11 +137,11 @@ public class AttributeTool { final F1API f1 = getF1API(); - Attribute attribute = new Attribute(); + Attribute attribute = new Attribute(attributeName); attribute.setStartDate(new Date()); attribute.setComment(comment); - if (f1.addAttribute(userId, attributeName, attribute)) { + if (f1.addAttribute(userId, attribute)) { System.out.println("Added attribute " + attributeName + " for " + userId); } else { System.out.println("Failed to add attribute " + attributeName + " for " + userId); @@ -140,4 +149,36 @@ public class AttributeTool { return offset; } + + private static int getall(String[] args, int offset) throws Exception { + final String userId = args[offset++]; + + doGet(userId, null); + + return offset; + } + + private static int get(String[] args, int offset) throws Exception { + final String userId = args[offset++]; + final String attributeName = args[offset++]; + + doGet(userId, attributeName); + + return offset; + } + + private static void doGet(final String userId, final String attributeName) throws Exception { + final F1API f1 = getF1API(); + + List<Attribute> attributes = f1.getAttribute(userId, attributeName); + for (Attribute attribute : attributes) { + System.out.printf("%s %s %s %s %s\n%s\n\n", + userId, + attribute.getAttributeName(), + attribute.getId(), + attribute.getStartDate(), + attribute.getEndDate(), + attribute.getComment()); + } + } } |