Skip to content
This repository was archived by the owner on Jun 17, 2024. It is now read-only.

Commit 343ea6d

Browse files
author
Jorge
committed
Merge pull request #2 from jogonzal-msft/master
Updated picker to use $expand
2 parents b6da668 + a113d40 commit 343ea6d

10 files changed

+180
-240
lines changed

Diff for: src/com/microsoft/onenote/pickerlib/ApiAsyncResponse.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
package com.microsoft.onenote.pickerlib;
77

88
interface ApiAsyncResponse {
9-
void onApiResponse(ApiResponse[] responses, Exception error);
9+
void onApiResponse(ApiNotebookResponse[] responses, Exception error);
1010
}

Diff for: src/com/microsoft/onenote/pickerlib/ApiController.java

+3-76
Original file line numberDiff line numberDiff line change
@@ -5,89 +5,16 @@
55
//----------------------------------------------------------------------------
66
package com.microsoft.onenote.pickerlib;
77

8-
import java.util.LinkedList;
9-
108
abstract class ApiController implements ApiAsyncResponse {
11-
final LinkedList<ApiRequestAsyncTask> mQueuedTasks = new LinkedList<ApiRequestAsyncTask>();
129
private final String mAccessToken;
13-
ApiRequestAsyncTask mTopPriorityTask = null;
14-
1510

1611
public ApiController(String accessToken) {
1712
mAccessToken = accessToken;
1813
}
1914

20-
@Override
21-
public synchronized void onApiResponse(ApiResponse[] responses, Exception error) {
22-
if (error != null) {
23-
onApiError(error);
24-
return;
25-
}
26-
handleApiResponse(responses);
27-
executeNext();
28-
}
29-
30-
protected synchronized void executeNext() {
31-
if (mTopPriorityTask == null) {
32-
ApiRequestAsyncTask task = mQueuedTasks.poll();
33-
if (task != null) {
34-
task.execute();
35-
}
36-
} else {
37-
mTopPriorityTask.execute();
38-
mTopPriorityTask = null;
39-
}
40-
}
41-
42-
private void handleApiResponse(ApiResponse[] responses) {
43-
for (ApiResponse response : responses) {
44-
ApiRequestAsyncTask task = buildApiRequestTask(response);
45-
if (task != null) {
46-
mQueuedTasks.add(task);
47-
}
48-
}
49-
}
50-
51-
private ApiRequestAsyncTask buildApiRequestTask(ApiResponse resultData) {
52-
ApiRequestEndpoint primaryEndpoint = null;
53-
if (resultData instanceof ApiNotebookResponse) {
54-
primaryEndpoint = ApiRequestEndpoint.NOTEBOOKS;
55-
} else if (resultData instanceof ApiSectionGroupResponse) {
56-
primaryEndpoint = ApiRequestEndpoint.SECTION_GROUPS;
57-
}
58-
if (primaryEndpoint != null) {
59-
ApiRequestAsyncTask task = new ApiRequestAsyncTask(mAccessToken,
60-
new ApiRequest(primaryEndpoint, resultData.getId(),
61-
ApiRequestEndpoint.SECTIONS),
62-
new ApiRequest(primaryEndpoint, resultData.getId(),
63-
ApiRequestEndpoint.SECTION_GROUPS)
64-
);
65-
task.addDelegate(this);
66-
task.addDelegate(getApiResponseDelegate(resultData));
67-
return task;
68-
} else {
69-
return null;
70-
}
71-
}
72-
73-
public void begin(ApiAsyncResponse initialDelegate) {
74-
ApiRequestAsyncTask initialTask = new ApiRequestAsyncTask(mAccessToken,
75-
new ApiRequest(ApiRequestEndpoint.NOTEBOOKS));
15+
public void begin() {
16+
ApiRequestAsyncTask initialTask = new ApiRequestAsyncTask(mAccessToken, new ApiRequest(ApiRequestEndpoint.EXPAND));
7617
initialTask.addDelegate(this);
77-
initialTask.addDelegate(initialDelegate);
78-
79-
mQueuedTasks.add(initialTask);
80-
executeNext();
81-
}
82-
83-
public void prioritize(ApiResponse resultData) {
84-
mTopPriorityTask = buildApiRequestTask(resultData);
85-
// Remove prioritized task from the queue
86-
mQueuedTasks.remove(mTopPriorityTask);
18+
initialTask.execute();
8719
}
88-
89-
protected abstract ApiAsyncResponse getApiResponseDelegate(ApiResponse response);
90-
91-
protected abstract void onApiError(Exception error);
92-
9320
}

Diff for: src/com/microsoft/onenote/pickerlib/ApiNotebookResponse.java

+10
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,22 @@
55
//----------------------------------------------------------------------------
66
package com.microsoft.onenote.pickerlib;
77

8+
import org.json.JSONException;
9+
import org.json.JSONObject;
10+
811
class ApiNotebookResponse extends ApiSectionGroupResponse {
912
protected boolean isDefault;
1013
protected String createdBy;
1114
protected String userRole;
1215
protected String ownerName;
1316

17+
public ApiNotebookResponse(JSONObject object) throws JSONException{
18+
super(object);
19+
setIsDefault(object.optBoolean("isDefault"));
20+
setUserRole(object.optString("userRole"));
21+
setOwnerName(object.optString("ownerName"));
22+
}
23+
1424
public boolean getIsDefault() {
1525
return isDefault;
1626
}

Diff for: src/com/microsoft/onenote/pickerlib/ApiRequest.java

+4-34
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,13 @@
66
package com.microsoft.onenote.pickerlib;
77

88
class ApiRequest {
9-
ApiRequestEndpoint primaryEndpoint = null;
10-
String resourceId = null;
11-
ApiRequestEndpoint secondaryEndpoint = null;
9+
ApiRequestEndpoint endpoint = null;
1210

1311
public ApiRequest(ApiRequestEndpoint endpoint) {
14-
this.primaryEndpoint = endpoint;
15-
}
16-
17-
public ApiRequest(ApiRequestEndpoint primaryEndpoint, String resourceId, ApiRequestEndpoint secondEndpoint) {
18-
this.primaryEndpoint = primaryEndpoint;
19-
this.resourceId = resourceId;
20-
this.secondaryEndpoint = secondEndpoint;
21-
}
22-
23-
public ApiRequestEndpoint getPrimaryEndpoint() {
24-
return primaryEndpoint;
25-
}
26-
27-
public String getResourceId() {
28-
return resourceId;
29-
}
30-
31-
public ApiRequestEndpoint getSecondaryEndpoint() {
32-
return secondaryEndpoint;
33-
}
34-
35-
public boolean hasResourceId() {
36-
return resourceId != null;
37-
}
38-
39-
public boolean hasSecondaryEndpoint() {
40-
return secondaryEndpoint != null;
12+
this.endpoint = endpoint;
4113
}
4214

4315
public String getEndpointURL() {
44-
return primaryEndpoint.toString() + "/"
45-
+ (hasResourceId() ? resourceId + "/" : "")
46-
+ (hasSecondaryEndpoint() ? secondaryEndpoint.toString() + "/" : "");
16+
return endpoint.toString();
4717
}
48-
}
18+
}

Diff for: src/com/microsoft/onenote/pickerlib/ApiRequestAsyncTask.java

+44-53
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
import android.os.AsyncTask;
99

1010
import org.json.JSONArray;
11+
import org.json.JSONException;
1112
import org.json.JSONObject;
1213

1314
import java.io.BufferedReader;
1415
import java.io.InputStream;
1516
import java.io.InputStreamReader;
17+
import java.net.MalformedURLException;
1618
import java.net.URL;
1719
import java.util.ArrayList;
1820
import java.util.Arrays;
@@ -21,7 +23,7 @@
2123
import javax.net.ssl.HttpsURLConnection;
2224

2325
class ApiRequestAsyncTask extends
24-
AsyncTask<Void, Void, ApiResponse[]> {
26+
AsyncTask<Void, Void, ApiNotebookResponse[]> {
2527
private final static String API_ROOT = "https://www.onenote.com/api/v1.0/";
2628
public List<ApiAsyncResponse> delegates = new ArrayList<ApiAsyncResponse>(2);
2729
public ApiRequest[] mRequests;
@@ -38,10 +40,10 @@ public void addDelegate(ApiAsyncResponse delegate) {
3840
}
3941

4042
@Override
41-
protected ApiResponse[] doInBackground(Void... voids) {
42-
List<ApiResponse> responseList = new ArrayList<ApiResponse>();
43+
protected ApiNotebookResponse[] doInBackground(Void... voids) {
44+
List<ApiNotebookResponse> responseList = new ArrayList<ApiNotebookResponse>();
4345
for (ApiRequest request : mRequests) {
44-
ApiResponse[] apiResponses;
46+
ApiNotebookResponse[] apiResponses;
4547
try {
4648
apiResponses = requestResource(request);
4749
} catch (Exception ex) {
@@ -51,22 +53,27 @@ protected ApiResponse[] doInBackground(Void... voids) {
5153
}
5254
responseList.addAll(Arrays.asList(apiResponses));
5355
}
54-
return responseList.toArray(new ApiResponse[responseList.size()]);
56+
return responseList.toArray(new ApiNotebookResponse[responseList.size()]);
5557
}
5658

57-
private ApiResponse[] requestResource(ApiRequest request) throws Exception {
59+
private ApiNotebookResponse[] requestResource(ApiRequest request) throws Exception {
5860
JSONObject responseJSON = getJSONResponse(API_ROOT + request.getEndpointURL());
5961

60-
List<ApiResponse> apiResponses = new ArrayList<ApiResponse>();
62+
List<ApiNotebookResponse> notebookResponses = new ArrayList<ApiNotebookResponse>();
6163
if (responseJSON != null) {
6264
// Determine if values were returned or an error occurred
6365
if (responseJSON.has("value")) {
6466
// Get response values
65-
JSONArray values = responseJSON.getJSONArray("value");
67+
JSONArray notebookObjects = responseJSON.getJSONArray("value");
6668
// Iterate over all values, and convert JSON objects into library objects
67-
for (int i = 0; i < values.length(); i++) {
68-
JSONObject object = values.getJSONObject(i);
69-
apiResponses.add(buildApiResponse(request, object));
69+
for (int i = 0; i < notebookObjects.length(); i++) {
70+
JSONObject notebookObject = notebookObjects.getJSONObject(i);
71+
72+
ApiNotebookResponse notebook = new ApiNotebookResponse(notebookObject);
73+
notebook.sections = getSections(notebookObject);
74+
notebook.sectionGroups = getSectionGroups(notebookObject);
75+
76+
notebookResponses.add(notebook);
7077
}
7178
} else if (responseJSON.has("error")) {
7279
// Process and throw an API error
@@ -85,50 +92,34 @@ private ApiResponse[] requestResource(ApiRequest request) throws Exception {
8592
}
8693
}
8794

88-
return apiResponses.toArray(new ApiResponse[apiResponses.size()]);
95+
return notebookResponses.toArray(new ApiNotebookResponse[notebookResponses.size()]);
8996
}
90-
91-
private ApiResponse buildApiResponse(ApiRequest request, JSONObject object) throws Exception {
92-
ApiResponse apiResponse = null;
93-
// Get links
94-
JSONObject links = object.optJSONObject("links");
95-
if (request.getPrimaryEndpoint() == ApiRequestEndpoint.NOTEBOOKS
96-
&& !request.hasResourceId()
97-
&& !request.hasSecondaryEndpoint()) {
98-
// Build notebook response
99-
apiResponse = new ApiNotebookResponse();
100-
((ApiNotebookResponse) apiResponse).setIsDefault(
101-
object.optBoolean("isDefault"));
102-
((ApiNotebookResponse) apiResponse).setUserRole(
103-
object.optString("userRole"));
104-
((ApiNotebookResponse) apiResponse).setOwnerName(
105-
object.optString("ownerName"));
106-
} else if (request.getSecondaryEndpoint() == ApiRequestEndpoint.SECTIONS) {
107-
// Build section response
108-
apiResponse = new ApiSectionResponse();
109-
((ApiSectionResponse) apiResponse).setIsDefault(
110-
object.optBoolean("isDefault"));
111-
if (links != null) {
112-
((ApiSectionResponse) apiResponse).setPagesUrl(
113-
new URL(links.getJSONObject("pagesUrl").getString("href")));
114-
} else {
115-
((ApiSectionResponse) apiResponse).setPagesUrl(
116-
new URL(object.getString("pagesUrl")));
117-
}
118-
119-
} else if (request.getSecondaryEndpoint() == ApiRequestEndpoint.SECTION_GROUPS) {
120-
// Build section groups response
121-
apiResponse = new ApiSectionGroupResponse();
97+
98+
private List<ApiSectionGroupResponse> getSectionGroups(JSONObject parentObject) throws JSONException, MalformedURLException {
99+
List<ApiSectionGroupResponse> sectionGroups = new ArrayList<ApiSectionGroupResponse>();
100+
JSONArray sectionGroupObjects = parentObject.getJSONArray("sectionGroups");
101+
102+
for (int k = 0; k < sectionGroupObjects.length(); k++)
103+
{
104+
JSONObject sectionGroupObject = sectionGroupObjects.getJSONObject(k);
105+
ApiSectionGroupResponse sectionGroup = new ApiSectionGroupResponse(sectionGroupObject);
106+
sectionGroup.sectionGroups = getSectionGroups(sectionGroupObject);
107+
sectionGroup.sections = getSections(sectionGroupObject);
108+
sectionGroups.add(sectionGroup);
122109
}
123-
if (apiResponse != null) {
124-
// Apply properties common to all response types
125-
apiResponse.setId(object.getString("id"));
126-
apiResponse.setName(object.getString("name"));
127-
apiResponse.setCreatedTime(object.optString("createdTime"));
128-
apiResponse.setModifiedTime(object.optString("lastModifiedTime"));
129-
apiResponse.setLastModifiedBy(object.optString("lastModifiedBy"));
110+
111+
return sectionGroups;
112+
}
113+
114+
private List<ApiSectionResponse> getSections(JSONObject parentObject) throws JSONException, MalformedURLException
115+
{
116+
List<ApiSectionResponse> sections = new ArrayList<ApiSectionResponse>();
117+
JSONArray sectionObjects = parentObject.getJSONArray("sections");
118+
for (int k = 0; k < sectionObjects.length(); k++)
119+
{
120+
sections.add(new ApiSectionResponse(sectionObjects.getJSONObject(k)));
130121
}
131-
return apiResponse;
122+
return sections;
132123
}
133124

134125

@@ -184,7 +175,7 @@ private JSONObject getJSONResponse(String endpoint) throws Exception {
184175
}
185176

186177
@Override
187-
protected void onPostExecute(ApiResponse[] responses) {
178+
protected void onPostExecute(ApiNotebookResponse[] responses) {
188179
super.onPostExecute(responses);
189180
for (ApiAsyncResponse delegate : delegates) {
190181
delegate.onApiResponse(responses, mCaughtException);

Diff for: src/com/microsoft/onenote/pickerlib/ApiRequestEndpoint.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
package com.microsoft.onenote.pickerlib;
77

88
enum ApiRequestEndpoint {
9-
NOTEBOOKS("Notebooks"), SECTION_GROUPS("SectionGroups"), SECTIONS("Sections");
9+
EXPAND("Notebooks?$expand=sections,sectionGroups($expand=sections,sectionGroups($expand=sections;$levels=max))");
1010

1111
private final String string;
1212

@@ -17,5 +17,4 @@ private ApiRequestEndpoint(String s) {
1717
public String toString() {
1818
return string;
1919
}
20-
2120
}

Diff for: src/com/microsoft/onenote/pickerlib/ApiResponse.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,29 @@
55
//----------------------------------------------------------------------------
66
package com.microsoft.onenote.pickerlib;
77

8-
import java.net.URL;
98
import java.text.ParseException;
109
import java.text.SimpleDateFormat;
1110
import java.util.Date;
1211

12+
import org.json.JSONException;
13+
import org.json.JSONObject;
14+
1315
class ApiResponse {
1416
protected String id;
1517
protected String name;
1618
protected Date createdTime;
1719
protected Date modifiedTime;
1820
protected String lastModifiedBy;
1921

22+
public ApiResponse(JSONObject object) throws JSONException{
23+
// Apply properties common to all response types
24+
setId(object.getString("id"));
25+
setName(object.getString("name"));
26+
setCreatedTime(object.optString("createdTime"));
27+
setModifiedTime(object.optString("lastModifiedTime"));
28+
setLastModifiedBy(object.optString("lastModifiedBy"));
29+
}
30+
2031
public String getId() {
2132
return id;
2233
}

Diff for: src/com/microsoft/onenote/pickerlib/ApiSectionGroupResponse.java

+11
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,22 @@
66
package com.microsoft.onenote.pickerlib;
77

88
import java.net.URL;
9+
import java.util.List;
10+
11+
import org.json.JSONException;
12+
import org.json.JSONObject;
913

1014
class ApiSectionGroupResponse extends ApiResponse {
1115
protected URL sectionsUrl;
1216
protected URL sectionGroupsUrl;
1317

18+
public ApiSectionGroupResponse(JSONObject object) throws JSONException{
19+
super(object);
20+
}
21+
22+
public List<ApiSectionResponse> sections;
23+
public List<ApiSectionGroupResponse> sectionGroups;
24+
1425
public URL getSectionsUrl() {
1526
return sectionsUrl;
1627
}

0 commit comments

Comments
 (0)