Search in sources :

Example 1 with WorksheetEntry

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());
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(com.google.gdata.util.ServiceException) JSONArray(org.json.JSONArray) URL(java.net.URL) WorksheetEntry(com.google.gdata.data.spreadsheet.WorksheetEntry)

Example 2 with WorksheetEntry

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;
}
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 3 with WorksheetEntry

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);
    }
}
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)

Aggregations

WorksheetEntry (com.google.gdata.data.spreadsheet.WorksheetEntry)3 ServiceException (com.google.gdata.util.ServiceException)3 SpreadsheetEntry (com.google.gdata.data.spreadsheet.SpreadsheetEntry)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 SpreadsheetService (com.google.gdata.client.spreadsheet.SpreadsheetService)1 PlainTextConstruct (com.google.gdata.data.PlainTextConstruct)1 SpreadsheetFeed (com.google.gdata.data.spreadsheet.SpreadsheetFeed)1 MalformedURLException (java.net.MalformedURLException)1 ServletException (javax.servlet.ServletException)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1