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/f1oauth/F1Access.java | |
| parent | 8fa4f1fb4f122ee1e27c6015479bcd883b4b0671 (diff) | |
Adding support to fetch attributes for a user.
Diffstat (limited to 'src/com/p4square/f1oauth/F1Access.java')
| -rw-r--r-- | src/com/p4square/f1oauth/F1Access.java | 84 | 
1 files changed, 81 insertions, 3 deletions
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.           */  | 
