Add methods to filter commercialfeatures from metadata

pull/28/head
prathabanKavin 2 years ago
parent 9e0f29fcc0
commit ace7442f2a

@ -67,6 +67,16 @@ public interface MetadataManagementService {
*/ */
PaginationResult retrieveAllMetadata(PaginationRequest request) throws MetadataManagementException; PaginationResult retrieveAllMetadata(PaginationRequest request) throws MetadataManagementException;
/**
* Get a paginated list of Metadata entries.
*
* @param request {@link PaginationRequest} obtained from the user
* @param filter Filter word
* @return {@link PaginationResult} enriched with metadata entries
* @throws MetadataManagementException If a data source related exception occurred
*/
PaginationResult retrieveAllMetadataWithFilter(PaginationRequest request, String filter) throws MetadataManagementException;
/** /**
* Update the provided Metadata entry. * Update the provided Metadata entry.
* a new entry will be created if the provided Metadata.metaKey is not exist * a new entry will be created if the provided Metadata.metaKey is not exist

@ -160,6 +160,37 @@ public class MetadataManagementServiceImpl implements MetadataManagementService
} }
} }
@Override
public PaginationResult retrieveAllMetadataWithFilter(PaginationRequest request, String filter) throws MetadataManagementException {
if (log.isDebugEnabled()) {
log.debug("Retrieving Metadata entries for given PaginationRequest [rowCount:" +
request.getRowCount() + ", startIndex:" + request.getStartIndex() + "]");
}
PaginationResult paginationResult = new PaginationResult();
request = DeviceManagerUtil.validateMetadataListPageSize(request);
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
try {
MetadataManagementDAOFactory.openConnection();
List<Metadata> metadata = metadataDAO.getAllMetadataWithFilter(request, tenantId, filter);
int count = metadataDAO.getMetadataCountWithFilter(tenantId, filter);
paginationResult.setData(metadata);
paginationResult.setRecordsFiltered(count);
paginationResult.setRecordsTotal(count);
return paginationResult;
} catch (MetadataManagementDAOException e) {
String msg = "Error occurred while retrieving metadata entries for given PaginationRequest [rowCount:" +
request.getRowCount() + ", startIndex:" + request.getStartIndex() + "]";
log.error(msg, e);
throw new MetadataManagementException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the data source";
log.error(msg, e);
throw new MetadataManagementException(msg, e);
} finally {
MetadataManagementDAOFactory.closeConnection();
}
}
@Override @Override
public Metadata updateMetadata(Metadata metadata) throws MetadataManagementException { public Metadata updateMetadata(Metadata metadata) throws MetadataManagementException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {

@ -91,6 +91,17 @@ public interface MetadataDAO {
*/ */
List<Metadata> getAllMetadata(PaginationRequest request, int tenantId) throws MetadataManagementDAOException; List<Metadata> getAllMetadata(PaginationRequest request, int tenantId) throws MetadataManagementDAOException;
/**
* Select Metadata entries based on PaginationRequest and Filter.
*
* @param request {@link PaginationRequest}
* @param tenantId Tenant Id
* @param filter Filter word
* @return a list of Metadata entries
* @throws MetadataManagementDAOException might occur while executing database queries
*/
List<Metadata> getAllMetadataWithFilter(PaginationRequest request, int tenantId, String filter) throws MetadataManagementDAOException;
/** /**
* Count number of Metadata entries. * Count number of Metadata entries.
* *
@ -100,4 +111,14 @@ public interface MetadataDAO {
*/ */
int getMetadataCount(int tenantId) throws MetadataManagementDAOException; int getMetadataCount(int tenantId) throws MetadataManagementDAOException;
/**
* Count number of Metadata entries with filter.
*
* @param tenantId Tenant Id
* @param filter Filter word
* @return Metadata entry count of given tenant
* @throws MetadataManagementDAOException might occur while executing database queries
*/
int getMetadataCountWithFilter(int tenantId, String filter) throws MetadataManagementDAOException;
} }

@ -152,6 +152,31 @@ public abstract class AbstractMetadataDAOImpl implements MetadataDAO {
return metadataCount; return metadataCount;
} }
@Override
public int getMetadataCountWithFilter(int tenantId, String filter) throws MetadataManagementDAOException {
int metadataCount = 0;
String keyFilter = "%" + filter + "%";
try {
Connection conn = MetadataManagementDAOFactory.getConnection();
String sql =
"SELECT COUNT(*) AS METADATA_COUNT FROM DM_METADATA WHERE TENANT_ID = ? AND METADATA_KEY LIKE ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tenantId);
stmt.setString(2, keyFilter);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
metadataCount = rs.getInt("METADATA_COUNT");
}
}
}
} catch (SQLException e) {
String msg = "Error occurred while counting metadata with filter";
log.error(msg, e);
throw new MetadataManagementDAOException(msg, e);
}
return metadataCount;
}
@Override @Override
public boolean isExist(int tenantId, String metaKey) throws MetadataManagementDAOException { public boolean isExist(int tenantId, String metaKey) throws MetadataManagementDAOException {
try { try {

@ -74,6 +74,42 @@ public class GenericMetadataDAOImpl extends AbstractMetadataDAOImpl {
return metadata; return metadata;
} }
public List<Metadata> getAllMetadataWithFilter(PaginationRequest request, int tenantId, String filter)
throws MetadataManagementDAOException {
List<Metadata> metadata;
String keyFilter = "%" + filter + "%";
String sql = "SELECT DATA_TYPE, METADATA_KEY, METADATA_VALUE " +
"FROM DM_METADATA " +
"WHERE TENANT_ID = ? " +
"AND METADATA_KEY LIKE ? " +
"ORDER BY METADATA_KEY";
if (request.getRowCount() != -1) {
sql = sql + " LIMIT ? OFFSET ?";
}
try {
Connection conn = MetadataManagementDAOFactory.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tenantId);
stmt.setString(2, keyFilter);
if (request.getRowCount() != -1) {
stmt.setInt(3, request.getRowCount());
stmt.setInt(4, request.getStartIndex());
}
try (ResultSet rs = stmt.executeQuery()) {
metadata = new ArrayList<>();
while (rs.next()) {
metadata.add(MetadataDAOUtil.getMetadata(rs));
}
}
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving all metadata with filter";
log.error(msg, e);
throw new MetadataManagementDAOException(msg, e);
}
return metadata;
}
@Override @Override
public boolean isExist(int tenantId, String metaKey) throws MetadataManagementDAOException { public boolean isExist(int tenantId, String metaKey) throws MetadataManagementDAOException {
try { try {

@ -74,4 +74,40 @@ public class OracleMetadataDAOImpl extends AbstractMetadataDAOImpl {
return metadata; return metadata;
} }
@Override
public List<Metadata> getAllMetadataWithFilter(PaginationRequest request, int tenantId, String filter) throws MetadataManagementDAOException {
List<Metadata> metadata;
String keyFilter = "%" + filter + "%";
String sql = "SELECT DATA_TYPE, METADATA_KEY, METADATA_VALUE " +
"FROM DM_METADATA " +
"WHERE TENANT_ID = ? " +
"AND METADATA_KEY LIKE ? " +
"ORDER BY METADATA_KEY";
if (request.getRowCount() != -1) {
sql = sql + " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
}
try {
Connection conn = MetadataManagementDAOFactory.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tenantId);
stmt.setString(2, keyFilter);
if (request.getRowCount() != -1) {
stmt.setInt(3, request.getStartIndex());
stmt.setInt(4, request.getRowCount());
}
try (ResultSet rs = stmt.executeQuery()) {
metadata = new ArrayList<>();
while (rs.next()) {
metadata.add(MetadataDAOUtil.getMetadata(rs));
}
}
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving all metadata";
log.error(msg, e);
throw new MetadataManagementDAOException(msg, e);
}
return metadata;
}
} }

@ -74,4 +74,40 @@ public class SQLServerMetadataDAOImpl extends AbstractMetadataDAOImpl {
return metadata; return metadata;
} }
@Override
public List<Metadata> getAllMetadataWithFilter(PaginationRequest request, int tenantId, String filter) throws MetadataManagementDAOException {
List<Metadata> metadata;
String keyFilter = "%" + filter + "%";
String sql = "SELECT DATA_TYPE, METADATA_KEY, METADATA_VALUE " +
"FROM DM_METADATA " +
"WHERE TENANT_ID = ? " +
"AND METADATA_KEY LIKE ? " +
"ORDER BY METADATA_KEY";
if (request.getRowCount() != -1) {
sql = sql + " LIMIT ? OFFSET ?";
}
try {
Connection conn = MetadataManagementDAOFactory.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tenantId);
stmt.setString(2, keyFilter);
if (request.getRowCount() != -1) {
stmt.setInt(3, request.getRowCount());
stmt.setInt(4, request.getStartIndex());
}
try (ResultSet rs = stmt.executeQuery()) {
metadata = new ArrayList<>();
while (rs.next()) {
metadata.add(MetadataDAOUtil.getMetadata(rs));
}
}
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving all metadata with filter";
log.error(msg, e);
throw new MetadataManagementDAOException(msg, e);
}
return metadata;
}
} }

Loading…
Cancel
Save