Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
RAVEENSR committed Nov 13, 2019
1 parent 3080acd commit 24452e8
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import org.wso2.carbon.dashboards.core.bean.DashboardMetadataContent;
import org.wso2.carbon.dashboards.core.exception.DashboardException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.sql.Blob;
import java.sql.Connection;
Expand All @@ -51,6 +54,7 @@ public class DashboardMetadataDao {
private static final String COLUMN_DASHBOARD_NAME = "NAME";
private static final String COLUMN_DASHBOARD_URL = "URL";
private static final String COLUMN_DASHBOARD_OWNER = "OWNER";
private static final String POSTGRESQL_DB_TYPE = "PostgreSQL";

private final DataSource dataSource;
private final QueryManager queryManager;
Expand Down Expand Up @@ -124,13 +128,19 @@ public void update(DashboardMetadata dashboardMetadata) throws DashboardExceptio
connection = getConnection();
query = queryManager.getQuery(connection, QueryManager.UPDATE_DASHBOARD_CONTENT_QUERY);
connection.setAutoCommit(false);
String dbType = connection.getMetaData().getDatabaseProductName();
ps = connection.prepareStatement(query);
ps.setString(1, dashboardMetadata.getName());
ps.setString(2, dashboardMetadata.getDescription());
Blob blob = connection.createBlob();
blob.setBytes(1, toJsonBytes(dashboardMetadata.getContent()));
ps.setBlob(3, blob);
ps.setString(4, dashboardMetadata.getParentId());
if (dbType.equalsIgnoreCase(POSTGRESQL_DB_TYPE)) {
ps.setBinaryStream(3, new ByteArrayInputStream(toJsonBytes(dashboardMetadata.getContent())));
ps.setInt(4, Integer.parseInt(dashboardMetadata.getParentId()));
} else {
Blob blob = connection.createBlob();
blob.setBytes(1, toJsonBytes(dashboardMetadata.getContent()));
ps.setBlob(3, blob);
ps.setString(4, dashboardMetadata.getParentId());
}
ps.setString(5, dashboardMetadata.getLandingPage());
ps.setString(6, dashboardMetadata.getUrl());
ps.executeUpdate();
Expand All @@ -153,16 +163,22 @@ public void add(DashboardMetadata dashboardMetadata) throws DashboardException {
connection = getConnection();
query = queryManager.getQuery(connection, QueryManager.ADD_DASHBOARD_CONTENT_QUERY);
connection.setAutoCommit(false);
String dbType = connection.getMetaData().getDatabaseProductName();
ps = connection.prepareStatement(query);
ps.setString(1, dashboardMetadata.getUrl());
ps.setString(2, dashboardMetadata.getOwner());
ps.setString(3, dashboardMetadata.getName());
ps.setString(4, dashboardMetadata.getDescription());
ps.setString(5, dashboardMetadata.getParentId());
ps.setString(6, dashboardMetadata.getLandingPage());
Blob blob = connection.createBlob();
blob.setBytes(1, toJsonBytes(dashboardMetadata.getContent()));
ps.setBlob(7, blob);
if (dbType.equalsIgnoreCase(POSTGRESQL_DB_TYPE)) {
ps.setInt(5, Integer.parseInt(dashboardMetadata.getParentId()));
ps.setBinaryStream(7, new ByteArrayInputStream(toJsonBytes(dashboardMetadata.getContent())));
} else {
ps.setString(5, dashboardMetadata.getParentId());
Blob blob = connection.createBlob();
blob.setBytes(1, toJsonBytes(dashboardMetadata.getContent()));
ps.setBlob(7, blob);
}
ps.executeUpdate();
connection.commit();
} catch (SQLException e) {
Expand Down Expand Up @@ -202,22 +218,32 @@ public Optional<DashboardMetadata> get(String url) throws DashboardException {
String query = null;
try {
connection = getConnection();
String dbType = connection.getMetaData().getDatabaseProductName();
query = queryManager.getQuery(connection, QueryManager.GET_DASHBOARD_BY_URL_QUERY);
ps = connection.prepareStatement(query);
ps.setString(1, url);
result = ps.executeQuery();

if (result.next()) {
DashboardMetadata dashboardMetadata = toDashboardMetadata(result);
dashboardMetadata.setContent(
parseDashboardMetadataContent(result.getBlob(COLUMN_DASHBOARD_CONTENT)));
if (dbType.equalsIgnoreCase(POSTGRESQL_DB_TYPE)) {
dashboardMetadata.setParentId(String.valueOf(result.getInt(COLUMN_DASHBOARD_PARENT_ID)));
dashboardMetadata.setContent(
parseDashboardMetadataContent(result.getBinaryStream(COLUMN_DASHBOARD_CONTENT)));
} else {
dashboardMetadata.setContent(
parseDashboardMetadataContent(result.getBlob(COLUMN_DASHBOARD_CONTENT)));
}
return Optional.of(dashboardMetadata);
} else {
return Optional.empty();
}
} catch (SQLException e) {
LOGGER.debug("Failed to execute SQL query {}", query);
throw new DashboardException("Cannot retrieve dashboard for URl '" + url + "'.", e);
} catch (IOException e) {
LOGGER.debug("Failed to read dashboard content");
throw new DashboardException("Cannot retrieve dashboard for URl '" + url + "'.", e);
} finally {
closeQuietly(connection, ps, result);
}
Expand Down Expand Up @@ -266,6 +292,20 @@ private static DashboardMetadataContent parseDashboardMetadataContent(Blob blob)
}
}

private static DashboardMetadataContent parseDashboardMetadataContent(InputStream inputStream)
throws IOException {
ByteArrayInputStream binaryStream = (ByteArrayInputStream) inputStream;
byte[] buffer = new byte[binaryStream.available()];
binaryStream.read(buffer);
String content = new String(buffer, StandardCharsets.UTF_8);
try {
return new Gson().fromJson(content, DashboardMetadataContent.class);
} catch (JsonParseException e) {
JsonArray pages = new Gson().fromJson(content, JsonArray.class);
return new DashboardMetadataContent(pages);
}
}

private static DashboardMetadata toDashboardMetadata(ResultSet result) throws SQLException {
DashboardMetadata dashboardMetadata = new DashboardMetadata();
dashboardMetadata.setName(result.getString(COLUMN_DASHBOARD_NAME));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import org.wso2.carbon.dashboards.core.bean.widget.GeneratedWidgetConfigs;
import org.wso2.carbon.dashboards.core.exception.DashboardException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.sql.Blob;
import java.sql.Connection;
Expand All @@ -42,6 +45,7 @@ public class WidgetMetadataDao {
private static final String COLUMN_WIDGET_ID = "WIDGET_ID";
private static final String COLUMN_WIDGET_NAME = "WIDGET_NAME";
private static final String COLUMN_WIDGET_CONFIGS = "WIDGET_CONFIGS";
private static final String POSTGRESQL_DB_TYPE = "PostgreSQL";

private final DataSource dataSource;
private final QueryManager queryManager;
Expand Down Expand Up @@ -117,14 +121,19 @@ public void addGeneratedWidgetConfigs(GeneratedWidgetConfigs generatedWidgetConf
generatedWidgetConfigs.setId(generatedWidgetConfigs.getName().replace(" ", "-"));
try {
connection = getConnection();
String dbType = connection.getMetaData().getDatabaseProductName();
query = queryManager.getQuery(connection, QueryManager.ADD_WIDGET_CONFIG_QUERY);
connection.setAutoCommit(false);
ps = connection.prepareStatement(query);
ps.setString(1, generatedWidgetConfigs.getId());
ps.setString(2, generatedWidgetConfigs.getName());
Blob blob = connection.createBlob();
blob.setBytes(1, toJsonBytes(generatedWidgetConfigs));
ps.setObject(3, blob);
if (dbType.equalsIgnoreCase(POSTGRESQL_DB_TYPE)) {
ps.setBinaryStream(3, new ByteArrayInputStream(toJsonBytes(generatedWidgetConfigs)));
} else {
Blob blob = connection.createBlob();
blob.setBytes(1, toJsonBytes(generatedWidgetConfigs));
ps.setObject(3, blob);
}
ps.executeUpdate();
connection.commit();
} catch (SQLException e) {
Expand All @@ -143,12 +152,17 @@ public void updateGeneratedWidgetConfigs(GeneratedWidgetConfigs generatedWidgetC
generatedWidgetConfigs.setId(generatedWidgetConfigs.getName().replace(" ", "-"));
try {
connection = getConnection();
String dbType = connection.getMetaData().getDatabaseProductName();
query = queryManager.getQuery(connection, QueryManager.UPDATE_WIDGET_CONFIG_QUERY);
connection.setAutoCommit(false);
ps = connection.prepareStatement(query);
Blob blob = connection.createBlob();
blob.setBytes(1, toJsonBytes(generatedWidgetConfigs));
ps.setObject(1, blob);
if (dbType.equalsIgnoreCase(POSTGRESQL_DB_TYPE)) {
ps.setBinaryStream(1, new ByteArrayInputStream(toJsonBytes(generatedWidgetConfigs)));
} else {
Blob blob = connection.createBlob();
blob.setBytes(1, toJsonBytes(generatedWidgetConfigs));
ps.setObject(1, blob);
}
ps.setString(2, generatedWidgetConfigs.getId());
ps.executeUpdate();
connection.commit();
Expand All @@ -172,6 +186,14 @@ private static GeneratedWidgetConfigs fromJsonBytes(Blob byteBlob) throws SQLExc
GeneratedWidgetConfigs.class);
}

private static GeneratedWidgetConfigs fromJsonBytes(InputStream inputStream) throws IOException {
ByteArrayInputStream binaryStream = (ByteArrayInputStream) inputStream;
byte[] buffer = new byte[binaryStream.available()];
binaryStream.read(buffer);
String content = new String(buffer, StandardCharsets.UTF_8);
return GSON.fromJson(content, GeneratedWidgetConfigs.class);
}

public GeneratedWidgetConfigs getGeneratedWidgetConfigsForId(String widgetId) throws
DashboardException {
Connection connection = null;
Expand All @@ -180,17 +202,25 @@ public GeneratedWidgetConfigs getGeneratedWidgetConfigsForId(String widgetId) th
String query = null;
try {
connection = getConnection();
String dbType = connection.getMetaData().getDatabaseProductName();
query = queryManager.getQuery(connection, QueryManager.GET_WIDGET_CONFIG_QUERY);
ps = connection.prepareStatement(query);
ps.setString(1, widgetId);
resultSet = ps.executeQuery();
if (resultSet.next()) {
return fromJsonBytes(resultSet.getBlob(COLUMN_WIDGET_CONFIGS));
if (dbType.equalsIgnoreCase(POSTGRESQL_DB_TYPE)) {
return fromJsonBytes(resultSet.getBinaryStream(COLUMN_WIDGET_CONFIGS));
} else {
return fromJsonBytes(resultSet.getBlob(COLUMN_WIDGET_CONFIGS));
}
}
} catch (SQLException e) {
rollbackQuietly(connection);
LOGGER.debug("Failed to execute SQL query {}", query);
throw new DashboardException("Cannot get widget configuration for widget id " + widgetId + ".", e);
} catch (IOException e) {
LOGGER.debug("Failed to read generated widget configuration");
throw new DashboardException("Cannot get widget configuration for widget id " + widgetId + ".", e);
} finally {
closeQuietly(connection, ps, resultSet);
}
Expand All @@ -204,20 +234,28 @@ public Set<GeneratedWidgetConfigs> getGeneratedWidgetIdSet() throws DashboardExc
String query = null;
try {
connection = getConnection();
String dbType = connection.getMetaData().getDatabaseProductName();
query = queryManager.getQuery(connection, QueryManager.GET_WIDGET_NAME_ID_MAP_QUERY);
ps = connection.prepareStatement(query);
resultSet = ps.executeQuery();
Set<GeneratedWidgetConfigs> widgetNameSet = new HashSet<>();
GeneratedWidgetConfigs generatedWidgetConfigs;
while (resultSet.next()) {
GeneratedWidgetConfigs generatedWidgetConfigs =
fromJsonBytes(resultSet.getBlob(COLUMN_WIDGET_CONFIGS));
if (dbType.equalsIgnoreCase(POSTGRESQL_DB_TYPE)) {
generatedWidgetConfigs = fromJsonBytes(resultSet.getBinaryStream(COLUMN_WIDGET_CONFIGS));
} else {
generatedWidgetConfigs = fromJsonBytes(resultSet.getBlob(COLUMN_WIDGET_CONFIGS));
}
widgetNameSet.add(generatedWidgetConfigs);
}
return widgetNameSet;
} catch (SQLException e) {
rollbackQuietly(connection);
LOGGER.debug("Failed to execute SQL query {}", query);
throw new DashboardException("Failed to get widget widget name set.", e);
} catch (IOException e) {
LOGGER.debug("Failed to read generated widget configurations");
throw new DashboardException("Failed to get widget widget name set.", e);
} finally {
closeQuietly(connection, ps, resultSet);
}
Expand Down

0 comments on commit 24452e8

Please sign in to comment.