Search in sources :

Example 11 with SpreadsheetOperationException

use of org.teiid.translator.google.api.SpreadsheetOperationException in project teiid by teiid.

the class SheetsAPI method insert.

/**
 * Insert row into spreadsheet
 * @param spreadsheetKey  key that identifies spreadsheet
 * @param pairs  key that identifies worksheet
 * @param worksheet name - value pair that should be inserted into spreadsheet
 * @return 1 if the row is successfully inserted
 */
public UpdateResult insert(String spreadsheetId, Map<String, Object> pairs, Worksheet worksheet) {
    ValueRange content = new ValueRange();
    List<Object> row = new ArrayList<>();
    for (String label : worksheet.getColumns().keySet()) {
        Object value = pairs.get(label);
        if (value != null) {
            if (value instanceof String) {
                // $NON-NLS-1$
                value = "'" + value;
            } else if (!(value instanceof Boolean || value instanceof Double)) {
                value = value.toString();
            }
        // else directly supported
        }
        row.add(value);
    }
    content.setValues(Arrays.asList(row));
    try {
        service.spreadsheets().values().append(spreadsheetId, worksheet.getName(), content).setValueInputOption(// $NON-NLS-1$ -- TODO: this could be configurable
        "USER_ENTERED").execute();
    } catch (IOException e) {
        throw new SpreadsheetOperationException("Error inserting spreadsheet row", e);
    }
    return new UpdateResult(1, 1);
}
Also used : ValueRange(com.google.api.services.sheets.v4.model.ValueRange) SpreadsheetOperationException(org.teiid.translator.google.api.SpreadsheetOperationException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) UpdateResult(org.teiid.translator.google.api.result.UpdateResult)

Example 12 with SpreadsheetOperationException

use of org.teiid.translator.google.api.SpreadsheetOperationException in project teiid by teiid.

the class SpreadsheetMetadataExtractor method extractMetadata.

public SpreadsheetInfo extractMetadata(String spreadsheetId) {
    try {
        Spreadsheet spreadsheet = sheetsAPI.getSpreadsheet(spreadsheetId);
        SpreadsheetInfo metadata = new SpreadsheetInfo(spreadsheet.getProperties().getTitle());
        metadata.setSpreadsheetKey(spreadsheet.getSpreadsheetId());
        for (Sheet sheet : spreadsheet.getSheets()) {
            String title = sheet.getProperties().getTitle();
            Worksheet worksheet = metadata.createWorksheet(title);
            worksheet.setId(sheet.getProperties().getSheetId().toString());
            List<Column> cols = dataProtocolAPI.getMetadata(spreadsheet.getSpreadsheetId(), title);
            if (!cols.isEmpty()) {
                if (cols.get(0).getLabel() != null) {
                    worksheet.setHeaderEnabled(true);
                }
            }
            for (Column c : cols) {
                worksheet.addColumn(c.getLabel() != null ? c.getLabel() : c.getAlphaName(), c);
            }
        }
        return metadata;
    } catch (IOException ex) {
        throw new SpreadsheetOperationException(SpreadsheetManagedConnectionFactory.UTIL.gs("metadata_error"), // $NON-NLS-1$
        ex);
    }
}
Also used : Spreadsheet(com.google.api.services.sheets.v4.model.Spreadsheet) SpreadsheetInfo(org.teiid.translator.google.api.metadata.SpreadsheetInfo) Column(org.teiid.translator.google.api.metadata.Column) SpreadsheetOperationException(org.teiid.translator.google.api.SpreadsheetOperationException) Worksheet(org.teiid.translator.google.api.metadata.Worksheet) IOException(java.io.IOException) Sheet(com.google.api.services.sheets.v4.model.Sheet)

Example 13 with SpreadsheetOperationException

use of org.teiid.translator.google.api.SpreadsheetOperationException in project teiid by teiid.

the class AbstractSpreadsheetExecution method checkHeaders.

void checkHeaders(String worksheetTitle) throws TranslatorException {
    SpreadsheetInfo info = connection.getSpreadsheetInfo();
    Worksheet worksheet = info.getWorksheetByName(worksheetTitle);
    if (worksheet == null) {
        // $NON-NLS-1$
        throw new SpreadsheetOperationException(SpreadsheetExecutionFactory.UTIL.gs("missing_worksheet", worksheetTitle));
    }
    if (!worksheet.isHeaderEnabled()) {
        // $NON-NLS-1$
        throw new TranslatorException(SpreadsheetExecutionFactory.UTIL.gs("headers_required"));
    }
}
Also used : SpreadsheetInfo(org.teiid.translator.google.api.metadata.SpreadsheetInfo) SpreadsheetOperationException(org.teiid.translator.google.api.SpreadsheetOperationException) Worksheet(org.teiid.translator.google.api.metadata.Worksheet) TranslatorException(org.teiid.translator.TranslatorException)

Aggregations

SpreadsheetOperationException (org.teiid.translator.google.api.SpreadsheetOperationException)13 IOException (java.io.IOException)6 SpreadsheetInfo (org.teiid.translator.google.api.metadata.SpreadsheetInfo)5 SpreadsheetQuery (com.google.gdata.client.spreadsheet.SpreadsheetQuery)4 ServiceException (com.google.gdata.util.ServiceException)4 MalformedURLException (java.net.MalformedURLException)4 Worksheet (org.teiid.translator.google.api.metadata.Worksheet)4 UpdateResult (org.teiid.translator.google.api.result.UpdateResult)4 ListEntry (com.google.gdata.data.spreadsheet.ListEntry)3 Column (org.teiid.translator.google.api.metadata.Column)3 ListFeed (com.google.gdata.data.spreadsheet.ListFeed)2 SpreadsheetEntry (com.google.gdata.data.spreadsheet.SpreadsheetEntry)2 ArrayList (java.util.ArrayList)2 Sheet (com.google.api.services.sheets.v4.model.Sheet)1 Spreadsheet (com.google.api.services.sheets.v4.model.Spreadsheet)1 ValueRange (com.google.api.services.sheets.v4.model.ValueRange)1 SpreadsheetFeed (com.google.gdata.data.spreadsheet.SpreadsheetFeed)1 WorksheetEntry (com.google.gdata.data.spreadsheet.WorksheetEntry)1 URL (java.net.URL)1 Timestamp (java.sql.Timestamp)1