Search in sources :

Example 1 with SpreadsheetEntry

use of com.google.gdata.data.spreadsheet.SpreadsheetEntry 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;
}
Also used : SpreadsheetService(com.google.gdata.client.spreadsheet.SpreadsheetService) SpreadsheetEntry(com.google.gdata.data.spreadsheet.SpreadsheetEntry) IOException(java.io.IOException) URL(java.net.URL) WorksheetEntry(com.google.gdata.data.spreadsheet.WorksheetEntry) PlainTextConstruct(com.google.gdata.data.PlainTextConstruct) ServletException(javax.servlet.ServletException) ServiceException(com.google.gdata.util.ServiceException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SpreadsheetEntry(com.google.gdata.data.spreadsheet.SpreadsheetEntry) ServiceException(com.google.gdata.util.ServiceException) SpreadsheetFeed(com.google.gdata.data.spreadsheet.SpreadsheetFeed) Drive(com.google.api.services.drive.Drive) File(com.google.api.services.drive.model.File)

Example 2 with SpreadsheetEntry

use of com.google.gdata.data.spreadsheet.SpreadsheetEntry 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);
    }
}
Also used : ServiceException(com.google.gdata.util.ServiceException) SpreadsheetEntry(com.google.gdata.data.spreadsheet.SpreadsheetEntry) IOException(java.io.IOException) WorksheetEntry(com.google.gdata.data.spreadsheet.WorksheetEntry)

Example 3 with SpreadsheetEntry

use of com.google.gdata.data.spreadsheet.SpreadsheetEntry in project OpenRefine by OpenRefine.

the class GDataImportingController method getWorksheetEntriesForDoc.

private List<WorksheetEntry> getWorksheetEntriesForDoc(URL docUrl, String token) throws IOException, ServiceException {
    if (token != null) {
        try {
            SpreadsheetService spreadsheetService = GDataExtension.getSpreadsheetService(token);
            SpreadsheetEntry spreadsheetEntry = spreadsheetService.getEntry(docUrl, SpreadsheetEntry.class);
            return spreadsheetEntry.getWorksheets();
        } catch (ServiceException e) {
        // Ignore and fall through, pretending that we're not logged in.
        }
    }
    return getWorksheetEntriesForDoc(docUrl);
}
Also used : ServiceException(com.google.gdata.util.ServiceException) SpreadsheetService(com.google.gdata.client.spreadsheet.SpreadsheetService) SpreadsheetEntry(com.google.gdata.data.spreadsheet.SpreadsheetEntry)

Example 4 with SpreadsheetEntry

use of com.google.gdata.data.spreadsheet.SpreadsheetEntry in project OpenRefine by OpenRefine.

the class GDataImportingController method listSpreadsheets.

private void listSpreadsheets(DocsService service, JSONWriter writer) throws IOException, ServiceException, JSONException {
    URL metafeedUrl = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
    SpreadsheetFeed feed = service.getFeed(metafeedUrl, SpreadsheetFeed.class);
    for (SpreadsheetEntry entry : feed.getEntries()) {
        writer.object();
        writer.key("docId");
        writer.value(entry.getId());
        writer.key("docLink");
        writer.value(entry.getHtmlLink().getHref());
        writer.key("docSelfLink");
        writer.value(entry.getSelfLink().getHref());
        writer.key("title");
        writer.value(entry.getTitle().getPlainText());
        writer.key("type");
        writer.value("spreadsheet");
        DateTime updated = entry.getUpdated();
        if (updated != null) {
            writer.key("updated");
            writer.value(updated.toStringRfc822());
        }
        writer.key("authors");
        writer.array();
        for (Person person : entry.getAuthors()) {
            writer.value(person.getName());
        }
        writer.endArray();
        writer.endObject();
    }
}
Also used : SpreadsheetFeed(com.google.gdata.data.spreadsheet.SpreadsheetFeed) SpreadsheetEntry(com.google.gdata.data.spreadsheet.SpreadsheetEntry) Person(com.google.gdata.data.Person) URL(java.net.URL) DateTime(com.google.gdata.data.DateTime)

Aggregations

SpreadsheetEntry (com.google.gdata.data.spreadsheet.SpreadsheetEntry)4 ServiceException (com.google.gdata.util.ServiceException)3 SpreadsheetService (com.google.gdata.client.spreadsheet.SpreadsheetService)2 SpreadsheetFeed (com.google.gdata.data.spreadsheet.SpreadsheetFeed)2 WorksheetEntry (com.google.gdata.data.spreadsheet.WorksheetEntry)2 IOException (java.io.IOException)2 URL (java.net.URL)2 Drive (com.google.api.services.drive.Drive)1 File (com.google.api.services.drive.model.File)1 DateTime (com.google.gdata.data.DateTime)1 Person (com.google.gdata.data.Person)1 PlainTextConstruct (com.google.gdata.data.PlainTextConstruct)1 MalformedURLException (java.net.MalformedURLException)1 ServletException (javax.servlet.ServletException)1