summaryrefslogtreecommitdiff
path: root/src/com/p4square/f1oauth
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2014-09-27 11:05:43 -0700
committerJesse Morgan <jesse@jesterpm.net>2014-09-27 11:05:43 -0700
commitbdf47fc7340ec2a3ecaa6b11bbf87f0f5eaaeea1 (patch)
treeb0faa7efb72587f99cd742caffcfb00d3f625cd9 /src/com/p4square/f1oauth
parent8fa4f1fb4f122ee1e27c6015479bcd883b4b0671 (diff)
Adding support to fetch attributes for a user.
Diffstat (limited to 'src/com/p4square/f1oauth')
-rw-r--r--src/com/p4square/f1oauth/Attribute.java36
-rw-r--r--src/com/p4square/f1oauth/F1API.java16
-rw-r--r--src/com/p4square/f1oauth/F1Access.java84
3 files changed, 128 insertions, 8 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.
*/