Search in sources :

Example 1 with SpreadsheetOperationException

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

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

the class GDataClientLoginAPI method getSpreadsheetEntry.

public SpreadsheetEntry getSpreadsheetEntry(String sheetName, boolean key) {
    if (key) {
        try {
            return getSpreadsheetEntry(new URL(factory.getSpreadsheetsFeedUrl(), "full/" + sheetName), SpreadsheetEntry.class);
        } catch (MalformedURLException e) {
            throw new SpreadsheetOperationException(e);
        }
    }
    SpreadsheetQuery squery = new SpreadsheetQuery(factory.getSpreadsheetsFeedUrl());
    squery.setTitleExact(true);
    squery.setTitleQuery(sheetName);
    SpreadsheetFeed feed = (SpreadsheetFeed) getSpreadsheetFeedQuery(squery, SpreadsheetFeed.class);
    List<SpreadsheetEntry> entry = feed.getEntries();
    if (entry.size() == 0)
        throw new SpreadsheetOperationException("Couldn't find spreadsheet:" + sheetName);
    if (entry.size() > 1) {
        throw new SpreadsheetOperationException("Multiple worksheets with the given title:" + sheetName + ".  Consider using a sheet key instead.");
    }
    return entry.get(0);
}
Also used : MalformedURLException(java.net.MalformedURLException) SpreadsheetQuery(com.google.gdata.client.spreadsheet.SpreadsheetQuery) SpreadsheetOperationException(org.teiid.translator.google.api.SpreadsheetOperationException) SpreadsheetFeed(com.google.gdata.data.spreadsheet.SpreadsheetFeed) SpreadsheetEntry(com.google.gdata.data.spreadsheet.SpreadsheetEntry) URL(java.net.URL)

Example 3 with SpreadsheetOperationException

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

the class SpreadsheetMetadataExtractor method extractMetadata.

public SpreadsheetInfo extractMetadata(String spreadsheetName, boolean isKey) {
    SpreadsheetEntry sentry = gdataAPI.getSpreadsheetEntry(spreadsheetName, isKey);
    SpreadsheetInfo metadata = new SpreadsheetInfo(spreadsheetName);
    metadata.setSpreadsheetKey(sentry.getKey());
    try {
        for (WorksheetEntry wentry : sentry.getWorksheets()) {
            String title = wentry.getTitle().getPlainText();
            Worksheet worksheet = metadata.createWorksheet(title);
            worksheet.setId(wentry.getId().substring(wentry.getId().lastIndexOf('/') + 1));
            List<Column> cols = visualizationAPI.getMetadata(sentry.getKey(), 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);
            }
        }
    } catch (IOException ex) {
        throw new SpreadsheetOperationException(SpreadsheetManagedConnectionFactory.UTIL.gs("metadata_error"), // $NON-NLS-1$
        ex);
    } catch (ServiceException ex) {
        throw new SpreadsheetOperationException(SpreadsheetManagedConnectionFactory.UTIL.gs("metadata_error"), // $NON-NLS-1$
        ex);
    }
    return metadata;
}
Also used : ServiceException(com.google.gdata.util.ServiceException) 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) SpreadsheetEntry(com.google.gdata.data.spreadsheet.SpreadsheetEntry) IOException(java.io.IOException) WorksheetEntry(com.google.gdata.data.spreadsheet.WorksheetEntry)

Example 4 with SpreadsheetOperationException

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

the class SpreadsheetSQLVisitor method replaceElementName.

/**
 * Return only col name e.g. "A"
 */
@Override
protected String replaceElementName(String group, String element) {
    Worksheet worksheetSourceName = info.getWorksheetByName(worksheetTitle);
    if (worksheetSourceName == null) {
        // $NON-NLS-1$
        throw new SpreadsheetOperationException(SpreadsheetExecutionFactory.UTIL.gs("missing_worksheet", worksheetTitle));
    }
    String columnId = worksheetSourceName.getColumnID(element);
    if (columnId == null) {
        throw new SpreadsheetOperationException("Column " + element + " doesn't exist in the worksheet " + worksheetTitle);
    }
    return columnId;
}
Also used : SpreadsheetOperationException(org.teiid.translator.google.api.SpreadsheetOperationException) Worksheet(org.teiid.translator.google.api.metadata.Worksheet)

Example 5 with SpreadsheetOperationException

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

the class SpreadsheetConnectionImpl4 method updateRows.

@Override
public UpdateResult updateRows(String worksheetTitle, String criteria, List<UpdateSet> set) {
    SpreadsheetInfo info = getV2SpreadsheetInfo();
    org.teiid.translator.google.api.metadata.Worksheet sheet = info.getWorksheetByName(worksheetTitle);
    if (sheet == null) {
        // $NON-NLS-1$
        throw new SpreadsheetOperationException(SpreadsheetManagedConnectionFactory.UTIL.getString("not_visible"));
    }
    return gdata.listFeedUpdate(info.getSpreadsheetKey(), sheet.getId(), criteria, set, sheet.getColumnsAsList());
}
Also used : SpreadsheetInfo(org.teiid.translator.google.api.metadata.SpreadsheetInfo) SpreadsheetOperationException(org.teiid.translator.google.api.SpreadsheetOperationException)

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