-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexClient.java
More file actions
190 lines (155 loc) · 6.19 KB
/
IndexClient.java
File metadata and controls
190 lines (155 loc) · 6.19 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* Copyright (c) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.spreadsheet;
import com.google.gdata.data.Person;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.client.spreadsheet.FeedURLFactory;
import com.google.gdata.client.spreadsheet.CellQuery;
import com.google.gdata.data.Feed;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
import com.google.gdata.data.spreadsheet.CellFeed;
import com.google.gdata.data.Entry;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.WorksheetEntry;
import com.google.gdata.data.spreadsheet.CellEntry;
import com.google.gdata.data.spreadsheet.Cell;
import sample.util.SimpleCommandLineParser;
import java.net.URL;
import java.util.List;
import java.util.ArrayList;
/**
* An application that serves as a sample to show how the SpreadhseetService
* can be used to obtain an index of spreadsheets with author and worksheets.
*
*
*/
public class IndexClient {
private SpreadsheetService service;
private FeedURLFactory factory;
/**
* Creates a client object for which the provided username and password
* produces a valid authentication.
*
* @param username the Google service user name
* @param password the corresponding password for the user name
* @throws Exception if error is encountered, such as invalid username and
* password pair
*/
public IndexClient(String username, String password) throws Exception {
factory = FeedURLFactory.getDefault();
service = new SpreadsheetService("gdata-sample-spreadhsheetindex");
service.setUserCredentials(username, password);
}
/**
* Retrieves the spreadsheets that the authenticated user has access to.
*
* @return a list of spreadsheet entries
* @throws Exception if error in retrieving the spreadsheet information
*/
public List<SpreadsheetEntry> getSpreadsheetEntries() throws Exception {
SpreadsheetFeed feed = service.getFeed(
factory.getSpreadsheetsFeedUrl(), SpreadsheetFeed.class);
return feed.getEntries();
}
/**
* Retrieves the worksheet entries from a spreadsheet entry.
*
* @param spreadsheet the spreadsheet entry containing the worksheet entries
* @return a list of worksheet entries
* @throws Exception if error in retrieving the spreadsheet information
*/
public List<WorksheetEntry> getWorksheetEntries(SpreadsheetEntry spreadsheet)
throws Exception {
return spreadsheet.getWorksheets();
}
/**
* Retrieves the columns headers from the cell feed of the worksheet
* entry.
*
* @param worksheet worksheet entry containing the cell feed in question
* @return a list of column headers
* @throws Exception if error in retrieving the spreadsheet information
*/
public List<String> getColumnHeaders(WorksheetEntry worksheet)
throws Exception {
List<String> headers = new ArrayList<String>();
// Get the appropriate URL for a cell feed
URL cellFeedUrl = worksheet.getCellFeedUrl();
// Create a query for the top row of cells only (1-based)
CellQuery cellQuery = new CellQuery(cellFeedUrl);
cellQuery.setMaximumRow(1);
// Get the cell feed matching the query
CellFeed topRowCellFeed = service.query(cellQuery, CellFeed.class);
// Get the cell entries fromt he feed
List<CellEntry> cellEntries = topRowCellFeed.getEntries();
for (CellEntry entry : cellEntries) {
// Get the cell element from the entry
Cell cell = entry.getCell();
headers.add(cell.getValue());
}
return headers;
}
/**
* Prints the usage of this application.
*/
private static void usage() {
System.out.println("Usage: java IndexClient --username [user] " +
"--password [pass] [--authors] [--worksheets] [--headers]");
System.out.println("\nA simple application that uses the provided Google\n"
+ "Account username and password to create\n"
+ "an index of the user's spreadsheets against\n"
+ "the user's Google Spreadsheet account.\n");
}
/**
* Main entry point. Parses arguments and creates and invokes the
* IndexClient.
*/
public static void main(String[] args) throws Exception {
SimpleCommandLineParser parser = new SimpleCommandLineParser(args);
String username = parser.getValue("username", "user", "u");
String password = parser.getValue("password", "pass", "passwd", "pw", "p");
boolean help = parser.containsKey("help", "h");
if (help || (username == null) || (password == null)) {
usage();
System.exit(1);
}
boolean author = parser.containsKey("author", "a");
boolean columns = parser.containsKey("headers", "header", "h");
boolean worksheets = parser.containsKey("worksheets", "worksheet", "w");
IndexClient client = new IndexClient(username, password);
for (SpreadsheetEntry spreadsheet : client.getSpreadsheetEntries()) {
System.out.print(spreadsheet.getTitle().getPlainText());
if (author) {
for (Person person : spreadsheet.getAuthors()) {
System.out.println(" - " + person.getName());
}
} else {
System.out.println();
} //authors (or not)
if (worksheets || columns) {
List<WorksheetEntry> entries = client.getWorksheetEntries(spreadsheet);
for (WorksheetEntry worksheet : entries) {
System.out.println("\t" + worksheet.getTitle().getPlainText());
if (columns) {
List<String> headers = client.getColumnHeaders(worksheet);
for (String header : headers) {
System.out.println("\t\t" + header);
}
} // columns
}
} // worksheets
} // spreadsheets
}
}