Search in sources :

Example 1 with UpdateResult

use of org.teiid.translator.google.api.result.UpdateResult in project teiid by teiid.

the class GDataClientLoginAPI method listFeedUpdate.

/**
 * Updates spreadsheet using the listfeed.
 *
 * @param spreadsheetKey  key that identifies spreadsheet
 * @param worksheetID  id that identifies worksheet
 * @param criteria  update criteria
 * @param updateSet  fields that should be updated
 * @param allColumns
 * @return number of updated rows
 */
public UpdateResult listFeedUpdate(String spreadsheetKey, String worksheetID, String criteria, List<UpdateSet> updateSet, List<Column> allColumns) {
    SpreadsheetQuery query = null;
    try {
        // $NON-NLS-1$ //$NON-NLS-2$
        query = new SpreadsheetQuery(factory.getListFeedUrl(spreadsheetKey, worksheetID, "private", "full"));
        if (criteria != null) {
            // $NON-NLS-1$
            query.setStringCustomParameter("sq", criteria);
        }
    } catch (MalformedURLException e) {
        throw new SpreadsheetOperationException("Error getting spreadsheet URL: " + e);
    }
    ListFeed listfeed = (ListFeed) getSpreadsheetFeedQuery(query, ListFeed.class);
    int counter = 0;
    // TEIID-4870 existing string values can get corrupted unless we re-set the entry
    List<Column> stringColumns = new ArrayList<Column>();
    for (Column c : allColumns) {
        if (c.getLabel() != null && c.getDataType() == SpreadsheetColumnType.STRING) {
            stringColumns.add(c);
        // could skip if in the update set
        }
    }
    for (ListEntry row : listfeed.getEntries()) {
        for (int i = 0; i < stringColumns.size(); i++) {
            Column c = stringColumns.get(i);
            String value = row.getCustomElements().getValue(c.getLabel());
            if (value != null && !value.isEmpty()) {
                // $NON-NLS-1$
                row.getCustomElements().setValueLocal(c.getLabel(), "'" + value);
            }
        }
        for (UpdateSet set : updateSet) {
            row.getCustomElements().setValueLocal(set.getColumnID(), set.getValue());
        }
        try {
            row.update();
        } catch (IOException e) {
            LogManager.logWarning(this.getClass().getName(), e, "Error occured when updating spreadsheet row");
            continue;
        } catch (ServiceException e2) {
            LogManager.logWarning(this.getClass().getName(), e2, "Error occured when updating spreadsheet row");
            continue;
        }
        counter++;
    }
    return new UpdateResult(listfeed.getEntries().size(), counter);
}
Also used : MalformedURLException(java.net.MalformedURLException) SpreadsheetQuery(com.google.gdata.client.spreadsheet.SpreadsheetQuery) SpreadsheetOperationException(org.teiid.translator.google.api.SpreadsheetOperationException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ListFeed(com.google.gdata.data.spreadsheet.ListFeed) ServiceException(com.google.gdata.util.ServiceException) Column(org.teiid.translator.google.api.metadata.Column) UpdateSet(org.teiid.translator.google.api.UpdateSet) UpdateResult(org.teiid.translator.google.api.result.UpdateResult) ListEntry(com.google.gdata.data.spreadsheet.ListEntry)

Example 2 with UpdateResult

use of org.teiid.translator.google.api.result.UpdateResult in project teiid by teiid.

the class GDataClientLoginAPI method listFeedDelete.

/**
 * Deletes spreadsheet rows using the listfeed.
 *
 * @param spreadsheetKey  key that identifies spreadsheet
 * @param worksheetID  id that identifies worksheet
 * @param criteria  delete criteria
 * @return number of deleted rows
 */
public UpdateResult listFeedDelete(String spreadsheetKey, String worksheetID, String criteria) {
    SpreadsheetQuery query = null;
    try {
        // $NON-NLS-1$ //$NON-NLS-2$
        query = new SpreadsheetQuery(factory.getListFeedUrl(spreadsheetKey, worksheetID, "private", "full"));
        if (criteria != null) {
            // $NON-NLS-1$
            query.setStringCustomParameter("sq", criteria);
        }
    } catch (MalformedURLException e) {
        throw new SpreadsheetOperationException("Error getting spreadsheet URL: " + e);
    }
    ListFeed listfeed = (ListFeed) getSpreadsheetFeedQuery(query, ListFeed.class);
    int counter = 0;
    for (int i = listfeed.getEntries().size() - 1; i > -1; i--) {
        ListEntry row = listfeed.getEntries().get(i);
        try {
            row.delete();
        } catch (IOException e) {
            LogManager.logWarning(this.getClass().getName(), e, "Error occured when deleting spreadsheet row");
            continue;
        } catch (ServiceException e2) {
            LogManager.logWarning(this.getClass().getName(), e2, "Error occured when deleting spreadsheet row");
            continue;
        }
        counter++;
    }
    return new UpdateResult(listfeed.getEntries().size(), counter);
}
Also used : MalformedURLException(java.net.MalformedURLException) ListFeed(com.google.gdata.data.spreadsheet.ListFeed) ServiceException(com.google.gdata.util.ServiceException) SpreadsheetQuery(com.google.gdata.client.spreadsheet.SpreadsheetQuery) SpreadsheetOperationException(org.teiid.translator.google.api.SpreadsheetOperationException) IOException(java.io.IOException) UpdateResult(org.teiid.translator.google.api.result.UpdateResult) ListEntry(com.google.gdata.data.spreadsheet.ListEntry)

Example 3 with UpdateResult

use of org.teiid.translator.google.api.result.UpdateResult in project teiid by teiid.

the class GDataClientLoginAPI method listFeedInsert.

/**
 * Insert row into spreadsheet using listfeed
 * @param spreadsheetKey  key that identifies spreadsheet
 * @param worksheetID  key that identifies worksheet
 * @param pair name - value pair that should be inserted into spreadsheet
 * @return 1 if the row is successfully inserted
 */
public UpdateResult listFeedInsert(String spreadsheetKey, String worksheetID, Map<String, Object> pair) {
    SpreadsheetQuery query = null;
    try {
        // $NON-NLS-1$ //$NON-NLS-2$
        query = new SpreadsheetQuery(factory.getListFeedUrl(spreadsheetKey, worksheetID, "private", "full"));
    } catch (MalformedURLException e) {
        throw new SpreadsheetOperationException("Error getting spreadsheet URL: " + e);
    }
    ListEntry row = new ListEntry();
    for (Entry<String, Object> entry : pair.entrySet()) {
        Object value = entry.getValue();
        Class<?> type = value.getClass();
        String valString = null;
        if (type.equals(DataTypeManager.DefaultDataClasses.STRING)) {
            // $NON-NLS-1$
            valString = "'" + value;
        } else {
            valString = value.toString();
        }
        row.getCustomElements().setValueLocal(entry.getKey(), valString);
    }
    try {
        service.insert(query.getFeedUrl(), row);
    } catch (Exception e) {
        throw new SpreadsheetOperationException("Error inserting spreadsheet row: " + e);
    }
    return new UpdateResult(1, 1);
}
Also used : MalformedURLException(java.net.MalformedURLException) SpreadsheetQuery(com.google.gdata.client.spreadsheet.SpreadsheetQuery) SpreadsheetOperationException(org.teiid.translator.google.api.SpreadsheetOperationException) ServiceException(com.google.gdata.util.ServiceException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SpreadsheetOperationException(org.teiid.translator.google.api.SpreadsheetOperationException) UpdateResult(org.teiid.translator.google.api.result.UpdateResult) ListEntry(com.google.gdata.data.spreadsheet.ListEntry)

Example 4 with UpdateResult

use of org.teiid.translator.google.api.result.UpdateResult 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)

Aggregations

IOException (java.io.IOException)4 SpreadsheetOperationException (org.teiid.translator.google.api.SpreadsheetOperationException)4 UpdateResult (org.teiid.translator.google.api.result.UpdateResult)4 SpreadsheetQuery (com.google.gdata.client.spreadsheet.SpreadsheetQuery)3 ListEntry (com.google.gdata.data.spreadsheet.ListEntry)3 ServiceException (com.google.gdata.util.ServiceException)3 MalformedURLException (java.net.MalformedURLException)3 ListFeed (com.google.gdata.data.spreadsheet.ListFeed)2 ArrayList (java.util.ArrayList)2 ValueRange (com.google.api.services.sheets.v4.model.ValueRange)1 UpdateSet (org.teiid.translator.google.api.UpdateSet)1 Column (org.teiid.translator.google.api.metadata.Column)1