blob: 942f5340bb8fe2d2f58fb01ffa4477dcf60cc893 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
/*
* Copyright 2014 Jesse Morgan
*/
package com.p4square.f1oauth;
import java.util.Map;
import com.p4square.restlet.oauth.OAuthException;
import com.p4square.restlet.oauth.OAuthUser;
/**
*
* @author Jesse Morgan <jesse@jesterpm.net>
*/
public class F1User extends OAuthUser {
public static final String ID = "@id";
public static final String FIRST_NAME = "firstName";
public static final String LAST_NAME = "lastName";
public static final String ICODE = "@iCode";
private final String mBaseUrl;
private final Map mData;
/**
* Copy the user information from user into a new F1User.
*
* @param user Original user.
* @param data F1 Person Record.
* @throws IllegalStateException if data.get("person") is null.
*/
public F1User(String baseUrl, OAuthUser user, Map data) {
super(user.getLocation(), user.getToken());
mBaseUrl = baseUrl;
mData = (Map) data.get("person");
if (mData == null) {
throw new IllegalStateException("Bad data");
}
setIdentifier(getString(ID));
setFirstName(getString(FIRST_NAME));
setLastName(getString(LAST_NAME));
}
/**
* Get a String from the map.
*
* @param key The map key.
* @return The value associated with the key, or null.
*/
public String getString(String key) {
Object blob = get(key);
if (blob instanceof String) {
return (String) blob;
} else {
return null;
}
}
/**
* Fetch an object from the F1 record.
*
* @param key The map key
* @return The object in the map or null.
*/
public Object get(String key) {
return mData.get(key);
}
/**
* @return the F1 API base url.
*/
public String getBaseUrl() {
return mBaseUrl;
}
/*
public addAttribute(Attribute attribute, String comment) {
String baseUrl = getBaseUrl();
Map newAttributeTemplate = null;
// Get Attribute Template
Request request = new Request(Method.GET,
baseUrl + "People/" + getIdentifier() + "/Attributes/new.json");
request.setChallengeResponse(getChallengeResponse());
Response response = getContext().getClientDispatcher().handle(request);
Representation representation = response.getEntity();
try {
Status status = response.getStatus();
if (status.isSuccess()) {
JacksonRepresentation<Map> entity = new JacksonRepresentation<Map>(response.getEntity(), Map.class);
newAttributeTemplate = entity.getObject();
}
} finally {
if (representation != null) {
representation.release();
}
}
if (newAttributeTemplate == null) {
LOG.error("Could not retrieve attribute template!");
return;
}
// Populate Attribute Template
// POST new attribute
Request request = new Request(Method.POST,
baseUrl + "People/" + getIdentifier() + "/Attributes.json");
request.setChallengeResponse(getChallengeResponse());
Response response = getContext().getClientDispatcher().handle(request);
Representation representation = response.getEntity();
try {
Status status = response.getStatus();
if (status.isSuccess()) {
JacksonRepresentation<Map> entity = new JacksonRepresentation<Map>(response.getEntity(), Map.class);
newAttributeTemplate = entity.getObject();
}
} finally {
if (representation != null) {
representation.release();
}
}
if (newAttributeTemplate == null) {
LOG.error("Could retrieve attribute template!");
return;
}
}
*/
}
|