use of com.google.gdata.data.spreadsheet.WorksheetEntry in project OpenRefine by OpenRefine.
the class GDataImportingController method doInitializeParserUI.
private void doInitializeParserUI(HttpServletRequest request, HttpServletResponse response, Properties parameters) throws ServletException, IOException {
String token = TokenCookie.getToken(request);
String type = parameters.getProperty("docType");
String urlString = parameters.getProperty("docUrl");
URL url = new URL(urlString);
try {
JSONObject result = new JSONObject();
JSONObject options = new JSONObject();
JSONUtilities.safePut(result, "status", "ok");
JSONUtilities.safePut(result, "options", options);
// number of initial data lines to skip
JSONUtilities.safePut(options, "skipDataLines", 0);
JSONUtilities.safePut(options, "storeBlankRows", true);
JSONUtilities.safePut(options, "storeBlankCellsAsNulls", true);
if ("spreadsheet".equals(type)) {
// number of blank lines at the beginning to ignore
JSONUtilities.safePut(options, "ignoreLines", -1);
// number of header lines
JSONUtilities.safePut(options, "headerLines", 1);
JSONArray worksheets = new JSONArray();
JSONUtilities.safePut(options, "worksheets", worksheets);
List<WorksheetEntry> worksheetEntries = reallyTryToGetWorksheetEntriesForDoc(url, token);
for (WorksheetEntry worksheetEntry : worksheetEntries) {
JSONObject worksheetO = new JSONObject();
JSONUtilities.safePut(worksheetO, "name", worksheetEntry.getTitle().getPlainText());
JSONUtilities.safePut(worksheetO, "rows", worksheetEntry.getRowCount());
JSONUtilities.safePut(worksheetO, "link", worksheetEntry.getSelfLink().getHref());
JSONUtilities.append(worksheets, worksheetO);
}
} else if ("table".equals(type)) {
// No metadata for a fusion table.
}
/* TODO: else */
HttpUtilities.respond(response, result.toString());
} catch (ServiceException e) {
e.printStackTrace();
HttpUtilities.respond(response, "error", "Internal error: " + e.getLocalizedMessage());
}
}
use of com.google.gdata.data.spreadsheet.WorksheetEntry in project OpenRefine by OpenRefine.
the class UploadCommand method uploadSpreadsheet.
private static String uploadSpreadsheet(final Project project, final Engine engine, final Properties params, String token, String name, List<Exception> exceptions) {
Drive driveService = GDataExtension.getDriveService(token);
final SpreadsheetService spreadsheetService = GDataExtension.getSpreadsheetService(token);
try {
File body = new File();
body.setTitle(name);
body.setDescription("Spreadsheet uploaded from OpenRefine project: " + name);
body.setMimeType("application/vnd.google-apps.spreadsheet");
File file = driveService.files().insert(body).execute();
String fileID = file.getId();
// Iterate through all spreadsheets to find one with our ID
SpreadsheetEntry spreadsheetEntry2 = null;
SpreadsheetFeed feed = spreadsheetService.getFeed(new URL(SPREADSHEET_FEED), SpreadsheetFeed.class);
List<com.google.gdata.data.spreadsheet.SpreadsheetEntry> spreadsheets = feed.getEntries();
for (com.google.gdata.data.spreadsheet.SpreadsheetEntry spreadsheet : spreadsheets) {
if (spreadsheet.getId().endsWith(fileID)) {
spreadsheetEntry2 = spreadsheet;
}
}
// Bail if we didn't find our spreadsheet (shouldn't happen)
if (spreadsheetEntry2 == null) {
logger.error("Failed to find match for ID: " + fileID);
return null;
}
int[] size = CustomizableTabularExporterUtilities.countColumnsRows(project, engine, params);
URL worksheetFeedUrl = spreadsheetEntry2.getWorksheetFeedUrl();
WorksheetEntry worksheetEntry = new WorksheetEntry(size[1], size[0]);
worksheetEntry.setTitle(new PlainTextConstruct("Uploaded Data"));
final WorksheetEntry worksheetEntry2 = spreadsheetService.insert(worksheetFeedUrl, worksheetEntry);
spreadsheetEntry2.getDefaultWorksheet().delete();
final SpreadsheetEntry spreadsheetEntry3 = spreadsheetEntry2;
new Thread() {
@Override
public void run() {
spreadsheetService.setProtocolVersion(SpreadsheetService.Versions.V3);
try {
uploadToCellFeed(project, engine, params, spreadsheetService, spreadsheetEntry3, worksheetEntry2);
} catch (Exception e) {
logger.error("Error uploading data to Google Spreadsheets", e);
}
}
}.start();
return spreadsheetEntry2.getSpreadsheetLink().getHref();
} catch (IOException | ServiceException e) {
exceptions.add(e);
}
return null;
}
use of com.google.gdata.data.spreadsheet.WorksheetEntry in project OpenRefine by OpenRefine.
the class GDataImporter method parseOneWorkSheet.
public static void parseOneWorkSheet(SpreadsheetService service, Project project, ProjectMetadata metadata, final ImportingJob job, URL docURL, URL worksheetURL, int limit, JSONObject options, List<Exception> exceptions) {
try {
WorksheetEntry worksheetEntry = service.getEntry(worksheetURL, WorksheetEntry.class);
String spreadsheetName = docURL.toExternalForm();
try {
SpreadsheetEntry spreadsheetEntry = service.getEntry(docURL, SpreadsheetEntry.class);
spreadsheetName = spreadsheetEntry.getTitle().getPlainText();
} catch (ServiceException e) {
// RedirectRequiredException among others
// fall back to just using the URL (better for traceability anyway?)
}
String fileSource = spreadsheetName + " # " + worksheetEntry.getTitle().getPlainText();
setProgress(job, fileSource, 0);
TabularImportingParserBase.readTable(project, metadata, job, new WorksheetBatchRowReader(job, fileSource, service, worksheetEntry, 20), fileSource, limit, options, exceptions);
setProgress(job, fileSource, 100);
} catch (IOException e) {
e.printStackTrace();
exceptions.add(e);
} catch (ServiceException e) {
e.printStackTrace();
exceptions.add(e);
}
}
Aggregations