Search in sources :

Example 1 with Worksheet

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

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

use of org.teiid.translator.google.api.metadata.Worksheet in project teiid by teiid.

the class TestMetadataProcessor method testRemoveColumns.

@Test
public void testRemoveColumns() throws Exception {
    GoogleSpreadsheetConnection conn = Mockito.mock(GoogleSpreadsheetConnection.class);
    SpreadsheetInfo people = new SpreadsheetInfo("People");
    Worksheet worksheet = people.createWorksheet("PeopleList");
    worksheet.setHeaderEnabled(true);
    for (int i = 1; i <= 3; i++) {
        Column newCol = new Column();
        newCol.setAlphaName(Util.convertColumnIDtoString(i));
        newCol.setLabel("c" + i);
        if (i == 1) {
            newCol.setDataType(SpreadsheetColumnType.DATETIME);
        }
        worksheet.addColumn(newCol.getAlphaName(), newCol);
    }
    Column newCol = new Column();
    newCol.setAlphaName("empty");
    worksheet.addColumn(null, newCol);
    Mockito.stub(conn.getSpreadsheetInfo()).toReturn(people);
    MetadataFactory factory = new MetadataFactory("", 1, "", SystemMetadata.getInstance().getRuntimeTypeMap(), new Properties(), "");
    GoogleMetadataProcessor processor = new GoogleMetadataProcessor();
    processor.process(factory, conn);
    Table t = factory.getSchema().getTables().get("PeopleList");
    assertTrue(t.supportsUpdate());
    assertEquals(3, t.getColumns().size());
    assertTrue(t.getColumns().get(0).isUpdatable());
    processor.setAllTypesUpdatable(false);
    factory = new MetadataFactory("", 1, "", SystemMetadata.getInstance().getRuntimeTypeMap(), new Properties(), "");
    processor.process(factory, conn);
    t = factory.getSchema().getTables().get("PeopleList");
    assertFalse(t.getColumns().get(0).isUpdatable());
}
Also used : Table(org.teiid.metadata.Table) MetadataFactory(org.teiid.metadata.MetadataFactory) SpreadsheetInfo(org.teiid.translator.google.api.metadata.SpreadsheetInfo) Column(org.teiid.translator.google.api.metadata.Column) Worksheet(org.teiid.translator.google.api.metadata.Worksheet) Properties(java.util.Properties) GoogleSpreadsheetConnection(org.teiid.translator.google.api.GoogleSpreadsheetConnection) Test(org.junit.Test)

Example 4 with Worksheet

use of org.teiid.translator.google.api.metadata.Worksheet in project teiid by teiid.

the class TestSQLtoSpreadsheetQuery method createSpreadSheetInfo.

@BeforeClass
public static void createSpreadSheetInfo() {
    people = new SpreadsheetInfo("People");
    Worksheet worksheet = people.createWorksheet("PeopleList");
    worksheet.setHeaderEnabled(true);
    for (int i = 1; i <= 4; i++) {
        Column newCol = new Column();
        newCol.setAlphaName(Util.convertColumnIDtoString(i));
        newCol.setLabel(newCol.getAlphaName());
        worksheet.addColumn(newCol.getAlphaName(), newCol);
    }
    worksheet.getColumns().get("C").setDataType(SpreadsheetColumnType.NUMBER);
    worksheet.getColumns().get("D").setDataType(SpreadsheetColumnType.BOOLEAN);
}
Also used : SpreadsheetInfo(org.teiid.translator.google.api.metadata.SpreadsheetInfo) Column(org.teiid.translator.google.api.metadata.Column) Worksheet(org.teiid.translator.google.api.metadata.Worksheet) BeforeClass(org.junit.BeforeClass)

Example 5 with Worksheet

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

Aggregations

Worksheet (org.teiid.translator.google.api.metadata.Worksheet)6 SpreadsheetInfo (org.teiid.translator.google.api.metadata.SpreadsheetInfo)5 SpreadsheetOperationException (org.teiid.translator.google.api.SpreadsheetOperationException)4 Column (org.teiid.translator.google.api.metadata.Column)4 IOException (java.io.IOException)2 Sheet (com.google.api.services.sheets.v4.model.Sheet)1 Spreadsheet (com.google.api.services.sheets.v4.model.Spreadsheet)1 SpreadsheetEntry (com.google.gdata.data.spreadsheet.SpreadsheetEntry)1 WorksheetEntry (com.google.gdata.data.spreadsheet.WorksheetEntry)1 ServiceException (com.google.gdata.util.ServiceException)1 Properties (java.util.Properties)1 BeforeClass (org.junit.BeforeClass)1 Test (org.junit.Test)1 MetadataFactory (org.teiid.metadata.MetadataFactory)1 Table (org.teiid.metadata.Table)1 TranslatorException (org.teiid.translator.TranslatorException)1 GoogleSpreadsheetConnection (org.teiid.translator.google.api.GoogleSpreadsheetConnection)1