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();
}
}
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());
}
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;
}
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());
}
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);
}
Aggregations