Search in sources :

Example 61 with IDatabaseConnection

use of org.pentaho.database.model.IDatabaseConnection in project data-access by pentaho.

the class JDBCDatasourceResource method addOrUpdate.

/**
 * Add or update a JDBC datasource connection
 *
 * <p><b>Example Request:</b><br />
 *    PUT pentaho/plugin/data-access/api/datasource/jdbc/connection/TestDatasource
 * </p>
 * <br /><b>POST data:</b>
 *  <pre function="syntax.xml">
 *    {
 *      "changed": true,
 *      "usingConnectionPool": true,
 *      "connectSql": "",
 *      "databaseName": "SampleData",
 *      "databasePort": "9001",
 *      "hostname": "localhost",
 *      "name": "TestDataSourceResource",
 *      "password": "password",
 *      "username": "pentaho_user",
 *      "attributes": {},
 *      "connectionPoolingProperties": {},
 *      "extraOptions": {},
 *      "accessType": "NATIVE",
 *      "databaseType": {
 *        "defaultDatabasePort": 9001,
 *        "extraOptionsHelpUrl": "http://hsqldb.sourceforge.net/doc/guide/ch04.html#N109DA",
 *        "name": "Hypersonic",
 *        "shortName": "HYPERSONIC",
 *        "supportedAccessTypes": [
 *          "NATIVE",
 *          "ODBC",
 *          "JNDI"
 *        ]
 *      }
 *    }
 *  </pre>
 * </p>
 *
 * @param connection A DatabaseConnection in JSON representation
 *
 * @return A jax-rs Response object with the appropriate status code, header, and body.
 *
 * <p><b>Example Response:</b></p>
 *    <pre function="syntax.xml">
 *      This response does not contain data.
 *    </pre>
 */
@PUT
@Path("/{connectionId : .+}")
@Consumes({ APPLICATION_JSON })
@StatusCodes({ @ResponseCode(code = 200, condition = "JDBC datasource added successfully."), @ResponseCode(code = 403, condition = "User is not authorized to add JDBC datasources."), @ResponseCode(code = 304, condition = "Datasource was not modified"), @ResponseCode(code = 500, condition = "An unexected error occurred while adding the JDBC datasource.") })
public Response addOrUpdate(@PathParam("connectionId") String connectionName, DatabaseConnection connection) {
    try {
        validateAccess();
        // Prefer the path name over the one in the DTO object
        connection.setId(connectionName);
        IDatabaseConnection savedConn = null;
        try {
            savedConn = service.getConnectionByName(connectionName);
        } catch (ConnectionServiceException e) {
        // unfortunatley getConnectionById throws an exception not returning null when the conneciton is not present.
        } catch (NullPointerException e) {
        // unfortunatley getConnectionById throws an exception not returning null when the conneciton is not present.
        }
        boolean success = false;
        if (savedConn != null) {
            if (StringUtils.isBlank(connection.getPassword())) {
                connection.setPassword(savedConn.getPassword());
            }
            connection.setId(savedConn.getId());
            success = service.updateConnection(connection);
        } else {
            success = service.addConnection(connection);
        }
        if (success) {
            return buildOkResponse();
        } else {
            return buildNotModifiedResponse();
        }
    } catch (PentahoAccessControlException t) {
        throw new WebApplicationException(Response.Status.UNAUTHORIZED);
    } catch (Throwable t) {
        logger.error("Error " + t.getMessage());
        return buildServerErrorResponse();
    }
}
Also used : ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) WebApplicationException(javax.ws.rs.WebApplicationException) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) StatusCodes(org.codehaus.enunciate.jaxrs.StatusCodes) PUT(javax.ws.rs.PUT)

Example 62 with IDatabaseConnection

use of org.pentaho.database.model.IDatabaseConnection in project data-access by pentaho.

the class DatasourceModelTest method testRelationalDatasourceDTO.

@Test
public void testRelationalDatasourceDTO() throws Exception {
    DatasourceModel datasourceModel = spy(new DatasourceModel());
    doReturn("testdatasource").when(datasourceModel).generateTableName();
    datasourceModel.setDatasourceName("testDatasource");
    datasourceModel.setDatasourceType(DatasourceType.SQL);
    datasourceModel.setGuiStateModel(contructRelationalModel(datasourceModel.getGuiStateModel()));
    datasourceModel.setSelectedRelationalConnection(datasourceModel.getGuiStateModel().getConnections().get(0));
    DatasourceDTO dto = DatasourceDTO.generateDTO(datasourceModel);
    assertNotNull(dto);
    assertEquals(datasourceModel.getDatasourceName(), dto.getDatasourceName());
    assertEquals(datasourceModel.getDatasourceType(), dto.getDatasourceType());
    assertEquals(datasourceModel.getQuery(), dto.getQuery());
    assertEquals(datasourceModel.getSelectedRelationalConnection().getName(), dto.getConnectionName());
    DatasourceModel datasourceModel2 = spy(new DatasourceModel());
    doReturn("testdatasource").when(datasourceModel2).generateTableName();
    IDatabaseConnection connection = new DatabaseConnection();
    connection.setAccessType(DatabaseAccessType.NATIVE);
    // connection.setDriverClass("org.hsqldb.jdbcDriver");
    connection.setName("SampleData");
    connection.setPassword("password");
    // connection.setUrl("jdbc:hsqldb:file:target/test-classes/solution1/system/data/sampledata");
    connection.setUsername("pentaho_user");
    datasourceModel2.getGuiStateModel().setConnections(Collections.singletonList(connection));
    DatasourceDTO.populateModel(dto, datasourceModel2);
    assertEquals(datasourceModel.getDatasourceName(), datasourceModel2.getDatasourceName());
    assertEquals(datasourceModel.getDatasourceType(), datasourceModel2.getDatasourceType());
    assertEquals(datasourceModel.getQuery(), datasourceModel2.getQuery());
    assertEquals(datasourceModel.getSelectedRelationalConnection().getName(), datasourceModel2.getSelectedRelationalConnection().getName());
}
Also used : DatabaseConnection(org.pentaho.database.model.DatabaseConnection) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) Test(org.junit.Test)

Example 63 with IDatabaseConnection

use of org.pentaho.database.model.IDatabaseConnection in project data-access by pentaho.

the class DatasourceModelTest method contructRelationalModel.

private GuiStateModel contructRelationalModel(GuiStateModel guiStateModel) {
    IDatabaseConnection connection = new DatabaseConnection();
    connection.setAccessType(DatabaseAccessType.NATIVE);
    // connection.setDriverClass("org.hsqldb.jdbcDriver");
    connection.setName("SampleData");
    connection.setPassword("password");
    // connection.setUrl("jdbc:hsqldb:file:target/test-classes/solution1/system/data/sampledata");
    connection.setUsername("pentaho_user");
    List<IDatabaseConnection> connectionList = new ArrayList<IDatabaseConnection>();
    connectionList.add(connection);
    guiStateModel.setConnections(connectionList);
    guiStateModel.setPreviewLimit("10");
    guiStateModel.validateRelational();
    LogicalColumn logColumn = new LogicalColumn();
    logColumn.setDataType(DataType.NUMERIC);
    List<AggregationType> aggTypeList = new ArrayList<AggregationType>();
    aggTypeList.add(AggregationType.AVERAGE);
    logColumn.setAggregationList(aggTypeList);
    logColumn.setName(new LocalizedString("En", "Column1"));
    BusinessData businessData = new BusinessData();
    List<List<String>> dataSample = new ArrayList<List<String>>();
    List<String> rowData = new ArrayList<String>();
    rowData.add("Data1");
    rowData.add("Data2");
    rowData.add("Data3");
    rowData.add("Data4");
    dataSample.add(rowData);
    String locale = LocaleHelper.getLocale().toString();
    SqlPhysicalModel model = new SqlPhysicalModel();
    SqlDataSource dataSource = new SqlDataSource();
    dataSource.setDatabaseName("SampleData");
    model.setDatasource(dataSource);
    SqlPhysicalTable table = new SqlPhysicalTable(model);
    model.getPhysicalTables().add(table);
    table.setTargetTableType(TargetTableType.INLINE_SQL);
    table.setTargetTable("select * from customers");
    SqlPhysicalColumn column = new SqlPhysicalColumn(table);
    column.setTargetColumn("customername");
    column.setName(new LocalizedString(locale, "Customer Name"));
    column.setDescription(new LocalizedString(locale, "Customer Name Desc"));
    column.setDataType(DataType.STRING);
    table.getPhysicalColumns().add(column);
    LogicalModel logicalModel = new LogicalModel();
    model.setId("MODEL");
    model.setName(new LocalizedString(locale, "My Model"));
    model.setDescription(new LocalizedString(locale, "A Description of the Model"));
    LogicalTable logicalTable = new LogicalTable();
    logicalTable.setPhysicalTable(table);
    logicalModel.getLogicalTables().add(logicalTable);
    LogicalColumn logicalColumn = new LogicalColumn();
    logicalColumn.setId("LC_CUSTOMERNAME");
    logicalColumn.setPhysicalColumn(column);
    logicalTable.addLogicalColumn(logicalColumn);
    Category mainCategory = new Category();
    mainCategory.setId("CATEGORY");
    mainCategory.setName(new LocalizedString(locale, "Category"));
    mainCategory.addLogicalColumn(logicalColumn);
    logicalModel.getCategories().add(mainCategory);
    Domain domain = new Domain();
    domain.addPhysicalModel(model);
    domain.addLogicalModel(logicalModel);
    List<LocaleType> localeTypeList = new ArrayList<LocaleType>();
    localeTypeList.add(new LocaleType("Code", "Locale Description"));
    domain.setLocales(localeTypeList);
    businessData.setData(dataSample);
    businessData.setDomain(domain);
    guiStateModel.setLocaleCode("en");
    guiStateModel.setLogicalModels(domain.getLogicalModels());
    guiStateModel.validateRelational();
    return guiStateModel;
}
Also used : BusinessData(org.pentaho.platform.dataaccess.datasource.beans.BusinessData) LogicalColumn(org.pentaho.metadata.model.LogicalColumn) Category(org.pentaho.metadata.model.Category) ArrayList(java.util.ArrayList) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) SqlPhysicalColumn(org.pentaho.metadata.model.SqlPhysicalColumn) LogicalTable(org.pentaho.metadata.model.LogicalTable) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) SqlPhysicalModel(org.pentaho.metadata.model.SqlPhysicalModel) AggregationType(org.pentaho.metadata.model.concept.types.AggregationType) SqlPhysicalTable(org.pentaho.metadata.model.SqlPhysicalTable) LogicalModel(org.pentaho.metadata.model.LogicalModel) SqlDataSource(org.pentaho.metadata.model.SqlDataSource) LocaleType(org.pentaho.metadata.model.concept.types.LocaleType) DatabaseConnection(org.pentaho.database.model.DatabaseConnection) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) ArrayList(java.util.ArrayList) List(java.util.List) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) Domain(org.pentaho.metadata.model.Domain)

Example 64 with IDatabaseConnection

use of org.pentaho.database.model.IDatabaseConnection in project data-access by pentaho.

the class GuiStateModelTest method test.

@Test
public void test() {
    GuiStateModel guiStateModel = new GuiStateModel();
    Assert.assertEquals(0, guiStateModel.getConnections().size());
    Assert.assertEquals(false, guiStateModel.isRelationalValidated());
    IDatabaseConnection connection = new DatabaseConnection();
    connection.setAccessType(DatabaseAccessType.NATIVE);
    // connection.setDriverClass("org.hsqldb.jdbcDriver");
    connection.setName("SampleData");
    connection.setPassword("password");
    // connection.setUrl("jdbc:hsqldb:file:target/test-classes/solution1/system/data/sampledata");
    connection.setUsername("pentaho_user");
    List<IDatabaseConnection> connectionList = new ArrayList<IDatabaseConnection>();
    connectionList.add(connection);
    guiStateModel.setConnections(connectionList);
    guiStateModel.setPreviewLimit("10");
    guiStateModel.validateRelational();
    Assert.assertEquals(true, guiStateModel.isRelationalPreviewValidated());
    // Assert.assertEquals(false, relationalModel.isValidated());
    LogicalColumn logColumn = new LogicalColumn();
    logColumn.setDataType(DataType.NUMERIC);
    List<AggregationType> aggTypeList = new ArrayList<AggregationType>();
    aggTypeList.add(AggregationType.AVERAGE);
    logColumn.setAggregationList(aggTypeList);
    logColumn.setName(new LocalizedString("En", "Column1"));
    BusinessData businessData = new BusinessData();
    List<List<String>> dataSample = new ArrayList<List<String>>();
    List<String> rowData = new ArrayList<String>();
    rowData.add("Data1");
    rowData.add("Data2");
    rowData.add("Data3");
    rowData.add("Data4");
    dataSample.add(rowData);
    String locale = LocaleHelper.getLocale().toString();
    SqlPhysicalModel model = new SqlPhysicalModel();
    SqlDataSource dataSource = new SqlDataSource();
    dataSource.setDatabaseName("SampleData");
    model.setDatasource(dataSource);
    SqlPhysicalTable table = new SqlPhysicalTable(model);
    model.getPhysicalTables().add(table);
    table.setTargetTableType(TargetTableType.INLINE_SQL);
    table.setTargetTable("select * from customers");
    SqlPhysicalColumn column = new SqlPhysicalColumn(table);
    column.setTargetColumn("customername");
    column.setName(new LocalizedString(locale, "Customer Name"));
    column.setDescription(new LocalizedString(locale, "Customer Name Desc"));
    column.setDataType(DataType.STRING);
    table.getPhysicalColumns().add(column);
    LogicalModel logicalModel = new LogicalModel();
    model.setId("MODEL");
    model.setName(new LocalizedString(locale, "My Model"));
    model.setDescription(new LocalizedString(locale, "A Description of the Model"));
    LogicalTable logicalTable = new LogicalTable();
    logicalTable.setPhysicalTable(table);
    logicalModel.getLogicalTables().add(logicalTable);
    LogicalColumn logicalColumn = new LogicalColumn();
    logicalColumn.setId("LC_CUSTOMERNAME");
    logicalColumn.setPhysicalColumn(column);
    logicalTable.addLogicalColumn(logicalColumn);
    Category mainCategory = new Category();
    mainCategory.setId("CATEGORY");
    mainCategory.setName(new LocalizedString(locale, "Category"));
    mainCategory.addLogicalColumn(logicalColumn);
    logicalModel.getCategories().add(mainCategory);
    Domain domain = new Domain();
    domain.addPhysicalModel(model);
    domain.addLogicalModel(logicalModel);
    List<LocaleType> localeTypeList = new ArrayList<LocaleType>();
    localeTypeList.add(new LocaleType("Code", "Locale Description"));
    domain.setLocales(localeTypeList);
    businessData.setData(dataSample);
    businessData.setDomain(domain);
    guiStateModel.setLogicalModels(domain.getLogicalModels());
    guiStateModel.setLocaleCode("en");
    Assert.assertEquals(true, guiStateModel.isRelationalValidated());
}
Also used : BusinessData(org.pentaho.platform.dataaccess.datasource.beans.BusinessData) LogicalColumn(org.pentaho.metadata.model.LogicalColumn) Category(org.pentaho.metadata.model.Category) ArrayList(java.util.ArrayList) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) SqlPhysicalColumn(org.pentaho.metadata.model.SqlPhysicalColumn) LogicalTable(org.pentaho.metadata.model.LogicalTable) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) SqlPhysicalModel(org.pentaho.metadata.model.SqlPhysicalModel) AggregationType(org.pentaho.metadata.model.concept.types.AggregationType) SqlPhysicalTable(org.pentaho.metadata.model.SqlPhysicalTable) LogicalModel(org.pentaho.metadata.model.LogicalModel) SqlDataSource(org.pentaho.metadata.model.SqlDataSource) LocaleType(org.pentaho.metadata.model.concept.types.LocaleType) DatabaseConnection(org.pentaho.database.model.DatabaseConnection) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) ArrayList(java.util.ArrayList) List(java.util.List) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) Domain(org.pentaho.metadata.model.Domain) Test(org.junit.Test)

Example 65 with IDatabaseConnection

use of org.pentaho.database.model.IDatabaseConnection in project data-access by pentaho.

the class ConnectionServiceImplTest method testGetConnectionById.

@Test
public void testGetConnectionById() throws Exception {
    doNothing().when(connectionServiceImpl).ensureDataAccessPermission();
    doReturn(mockDBConnection).when(connectionServiceImpl.datasourceMgmtSvc).getDatasourceById(CONN_ID);
    IDatabaseConnection connection = connectionServiceImpl.getConnectionById(CONN_ID);
    verify(connectionServiceImpl).getConnectionById(CONN_ID);
    assertEquals(mockDBConnection, connection);
}
Also used : IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) Test(org.junit.Test)

Aggregations

IDatabaseConnection (org.pentaho.database.model.IDatabaseConnection)99 Test (org.junit.Test)29 DatabaseConnection (org.pentaho.database.model.DatabaseConnection)29 ArrayList (java.util.ArrayList)18 Bindable (org.pentaho.ui.xul.stereotype.Bindable)15 RequestException (com.google.gwt.http.client.RequestException)14 Request (com.google.gwt.http.client.Request)13 RequestBuilder (com.google.gwt.http.client.RequestBuilder)13 RequestCallback (com.google.gwt.http.client.RequestCallback)13 Response (com.google.gwt.http.client.Response)13 ConnectionServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException)13 IDatasourceMgmtService (org.pentaho.platform.api.repository.datasource.IDatasourceMgmtService)11 DatasourceMgmtServiceException (org.pentaho.platform.api.repository.datasource.DatasourceMgmtServiceException)9 List (java.util.List)8 Response (javax.ws.rs.core.Response)8 Path (javax.ws.rs.Path)7 GET (javax.ws.rs.GET)6 Produces (javax.ws.rs.Produces)6 Facet (org.codehaus.enunciate.Facet)5 DatabaseDialectService (org.pentaho.database.service.DatabaseDialectService)5