Search in sources :

Example 1 with CellQuery

use of com.google.gdata.client.spreadsheet.CellQuery in project OpenRefine by OpenRefine.

the class UploadCommand method uploadToCellFeed.

private static void uploadToCellFeed(Project project, Engine engine, Properties params, final SpreadsheetService service, final SpreadsheetEntry spreadsheetEntry, final WorksheetEntry worksheetEntry) throws IOException, ServiceException {
    final URL cellFeedUrl = worksheetEntry.getCellFeedUrl();
    final CellEntry[][] cellEntries = new CellEntry[worksheetEntry.getRowCount()][worksheetEntry.getColCount()];
    {
        CellQuery cellQuery = new CellQuery(cellFeedUrl);
        cellQuery.setReturnEmpty(true);
        CellFeed fetchingCellFeed = service.getFeed(cellQuery, CellFeed.class);
        for (CellEntry cellEntry : fetchingCellFeed.getEntries()) {
            Cell cell = cellEntry.getCell();
            cellEntries[cell.getRow() - 1][cell.getCol() - 1] = cellEntry;
        }
    }
    TabularSerializer serializer = new TabularSerializer() {

        CellFeed cellFeed = service.getFeed(cellFeedUrl, CellFeed.class);

        CellFeed batchRequest = null;

        int row = 0;

        @Override
        public void startFile(JSONObject options) {
        }

        @Override
        public void endFile() {
            if (batchRequest != null) {
                sendBatch();
            }
        }

        private void sendBatch() {
            try {
                Link batchLink = cellFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
                CellFeed batchResponse = service.batch(new URL(batchLink.getHref()), batchRequest);
                for (CellEntry entry : batchResponse.getEntries()) {
                    String batchId = BatchUtils.getBatchId(entry);
                    if (!BatchUtils.isSuccess(entry)) {
                        BatchStatus status = BatchUtils.getBatchStatus(entry);
                        logger.warn(String.format("Error: %s failed (%s) %s\n", batchId, status.getReason(), status.getContent()));
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            batchRequest = null;
        }

        @Override
        public void addRow(List<CellData> cells, boolean isHeader) {
            if (batchRequest == null) {
                batchRequest = new CellFeed();
            }
            for (int c = 0; c < cells.size(); c++) {
                CellData cellData = cells.get(c);
                if (cellData != null && cellData.text != null) {
                    String cellId = String.format("R%sC%s", row + 1, c + 1);
                    CellEntry cellEntry = cellEntries[row][c];
                    cellEntry.changeInputValueLocal(cellData.text);
                    if (cellData.link != null) {
                        cellEntry.addHtmlLink(cellData.link, null, cellData.text);
                    }
                    cellEntry.setId(cellId);
                    BatchUtils.setBatchId(cellEntry, cellId);
                    BatchUtils.setBatchOperationType(cellEntry, BatchOperationType.UPDATE);
                    batchRequest.getEntries().add(cellEntry);
                }
            }
            row++;
            if (row % 20 == 0) {
                sendBatch();
            }
        }
    };
    CustomizableTabularExporterUtilities.exportRows(project, engine, params, serializer);
}
Also used : CellFeed(com.google.gdata.data.spreadsheet.CellFeed) URL(java.net.URL) ServletException(javax.servlet.ServletException) ServiceException(com.google.gdata.util.ServiceException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) JSONObject(org.json.JSONObject) TabularSerializer(com.google.refine.exporters.TabularSerializer) BatchStatus(com.google.gdata.data.batch.BatchStatus) LinkedList(java.util.LinkedList) List(java.util.List) CellEntry(com.google.gdata.data.spreadsheet.CellEntry) Cell(com.google.gdata.data.spreadsheet.Cell) CellQuery(com.google.gdata.client.spreadsheet.CellQuery) Link(com.google.gdata.data.Link)

Aggregations

CellQuery (com.google.gdata.client.spreadsheet.CellQuery)1 Link (com.google.gdata.data.Link)1 BatchStatus (com.google.gdata.data.batch.BatchStatus)1 Cell (com.google.gdata.data.spreadsheet.Cell)1 CellEntry (com.google.gdata.data.spreadsheet.CellEntry)1 CellFeed (com.google.gdata.data.spreadsheet.CellFeed)1 ServiceException (com.google.gdata.util.ServiceException)1 TabularSerializer (com.google.refine.exporters.TabularSerializer)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ServletException (javax.servlet.ServletException)1 JSONObject (org.json.JSONObject)1