Search in sources :

Example 6 with SpreadsheetOperationException

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

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

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

the class SpreadsheetInsertVisitor method visit.

public void visit(Insert obj) {
    worksheetTitle = obj.getTable().getName();
    if (obj.getTable().getMetadataObject().getNameInSource() != null) {
        worksheetTitle = obj.getTable().getMetadataObject().getNameInSource();
    }
    worksheetKey = info.getWorksheetByName(worksheetTitle).getId();
    ExpressionValueSource evs = (ExpressionValueSource) obj.getValueSource();
    for (int i = 0; i < evs.getValues().size(); i++) {
        Expression e = evs.getValues().get(i);
        if (!(e instanceof Literal)) {
            throw new SpreadsheetOperationException("Only literals are allowed in the values section");
        }
        Literal l = (Literal) e;
        if (l.getValue() == null) {
            continue;
        }
        ColumnReference columnReference = obj.getColumns().get(i);
        columnNameValuePair.put(columnReference.getMetadataObject().getSourceName(), l.getValue());
    }
}
Also used : Expression(org.teiid.language.Expression) Literal(org.teiid.language.Literal) SpreadsheetOperationException(org.teiid.translator.google.api.SpreadsheetOperationException) ExpressionValueSource(org.teiid.language.ExpressionValueSource) ColumnReference(org.teiid.language.ColumnReference)

Example 9 with SpreadsheetOperationException

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

the class SpreadsheetConnectionImpl4 method deleteRows.

@Override
public UpdateResult deleteRows(String worksheetTitle, String criteria) {
    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.listFeedDelete(info.getSpreadsheetKey(), sheet.getId(), criteria);
}
Also used : SpreadsheetInfo(org.teiid.translator.google.api.metadata.SpreadsheetInfo) SpreadsheetOperationException(org.teiid.translator.google.api.SpreadsheetOperationException)

Example 10 with SpreadsheetOperationException

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

the class GoogleJSONParser method parseLiteral.

private Object parseLiteral(PushbackReader r, char c) throws IOException {
    sb.setLength(0);
    do {
        sb.append(c);
        int i = r.read();
        if (i == -1) {
            break;
        }
        c = (char) i;
    } while (!Character.isWhitespace(c) && c != ',' && c != ']' && c != '}');
    // date handling
    if (areEquals(sb, "new")) {
        // $NON-NLS-1$
        sb.setLength(0);
        int length = 0;
        Arrays.fill(parts, 0);
        for (int i = 0; ; i++) {
            if (i > 5) {
                // remove " Date("
                if (c == ',' || c == ')') {
                    if (length > 6) {
                        throw new SpreadsheetOperationException("Too many date fields");
                    }
                    parts[length++] = Integer.valueOf(sb.toString());
                    if (c == ')') {
                        break;
                    }
                    sb.setLength(0);
                } else {
                    sb.append(c);
                }
            }
            int chr = r.read();
            if (chr == -1) {
                throw new SpreadsheetOperationException("Encountered end of stream in date value");
            }
            c = (char) chr;
        }
        if (length > 3) {
            Calendar calendar = getCalendar();
            calendar.set(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]);
            Timestamp ts = new Timestamp(calendar.getTimeInMillis());
            // convert from millis to nanos
            ts.setNanos(parts[6] * 1000000);
            return ts;
        }
        Calendar calendar = getCalendar();
        calendar.set(parts[0], parts[1], parts[2]);
        return new java.sql.Date(cal.getTimeInMillis());
    }
    if (!Character.isWhitespace(c)) {
        // the terminating character is still needed by the caller
        r.unread(c);
    // TODO could hold this state so that a pushback reader is not needed
    }
    if (areEquals(sb, "false")) {
        // $NON-NLS-1$
        return Boolean.FALSE;
    } else if (areEquals(sb, "true")) {
        // $NON-NLS-1$
        return Boolean.TRUE;
    } else if (areEquals(sb, "null")) {
        // $NON-NLS-1$
        return null;
    }
    return Double.valueOf(sb.toString());
}
Also used : SpreadsheetOperationException(org.teiid.translator.google.api.SpreadsheetOperationException) Calendar(java.util.Calendar) Timestamp(java.sql.Timestamp)

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