Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.Collection;
import java.util.List;
import java.util.Map;

public interface SnmpCollectionMibGroupDao extends OnmsDao<SnmpCollectionMibGroup, Integer> {
SnmpCollectionMibGroup get(Integer id);
Expand All @@ -43,4 +44,13 @@ public interface SnmpCollectionMibGroupDao extends OnmsDao<SnmpCollectionMibGrou

void deleteAll(final Collection<SnmpCollectionMibGroup> list);

void saveAll(Collection<SnmpCollectionMibGroup> list);

void deleteBySourceId(Integer sourceId);

List<SnmpCollectionMibGroup> filterEventConf(String name,String ifType, String vendor, String collectionSourceName, int offset, int limit);

Map<String, Object> findByDataCollectionGroupId(Integer dataCollectionGroupId, String mibGroupFilter, String sortBy, String order, Integer totalRecords, Integer offset, Integer limit);


}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.Collection;
import java.util.List;
import java.util.Map;

public interface SnmpCollectionResourceTypeDao extends OnmsDao<SnmpCollectionResourceType, Integer> {
SnmpCollectionResourceType get(Integer id);
Expand All @@ -43,4 +44,13 @@ public interface SnmpCollectionResourceTypeDao extends OnmsDao<SnmpCollectionRes

void deleteAll(final Collection<SnmpCollectionResourceType> list);

void saveAll(Collection<SnmpCollectionResourceType> list);

void deleteBySourceId(Integer sourceId);

List<SnmpCollectionResourceType> filterEventConf(String name,String label, String vendor, String collectionSourceName, int offset, int limit);

Map<String, Object> findByDataCollectionGroupId(Integer dataCollectionGroupId, String resourceTypeFilter, String sortBy, String order, Integer totalRecords, Integer offset, Integer limit);


}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.Collection;
import java.util.List;
import java.util.Map;

public interface SnmpCollectionSourceDao extends OnmsDao<SnmpCollectionSource, Integer> {

Expand All @@ -40,4 +41,9 @@ public interface SnmpCollectionSourceDao extends OnmsDao<SnmpCollectionSource, I

void deleteAll(final Collection<SnmpCollectionSource> list);

Map<Integer, String> getIdToNameMap();

Map<String, Object> filterDataCollectionSource(final String filter, final String sortBy, final String order, Integer totalRecords,
Integer offset, Integer limit);

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.Collection;
import java.util.List;
import java.util.Map;

public interface SnmpCollectionSystemDefDao extends OnmsDao<SnmpCollectionSystemDef, Integer> {
SnmpCollectionSystemDef get(Integer id);
Expand All @@ -43,4 +44,13 @@ public interface SnmpCollectionSystemDefDao extends OnmsDao<SnmpCollectionSystem

void deleteAll(final Collection<SnmpCollectionSystemDef> list);

void saveAll(Collection<SnmpCollectionSystemDef> list);

void deleteBySourceId(Integer sourceId);

List<SnmpCollectionSystemDef> filterSystemDefsConf(String name,String vendor, String collectionSourceName, int offset, int limit);

Map<String, Object> findByDataCollectionGroupId(Integer dataCollectionGroupId, String systemDefsFilter, String sortBy, String order, Integer totalRecords, Integer offset, Integer limit);


}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.List;
import java.util.*;

public class SnmpCollectionMibGroupDaoHibernate extends AbstractDaoHibernate<SnmpCollectionMibGroup, Integer> implements SnmpCollectionMibGroupDao {

Expand Down Expand Up @@ -63,4 +62,133 @@ public List<SnmpCollectionMibGroup> findAllBySource(Integer sourceId) {
public void deleteAll(final Collection<SnmpCollectionMibGroup> list) {
super.deleteAll(list);
}

@Override
public void saveAll(Collection<SnmpCollectionMibGroup> list) {
if (list == null || list.isEmpty()) {
return;
}

int batchSize = 50;
int i = 0;
for (SnmpCollectionMibGroup mibGroup : list) {
getHibernateTemplate().saveOrUpdate(mibGroup);
i++;
if (i % batchSize == 0) {
getHibernateTemplate().flush();
getHibernateTemplate().clear();
}

}
getHibernateTemplate().flush();
getHibernateTemplate().clear();
}

@Override
public void deleteBySourceId(Integer sourceId) {
getHibernateTemplate().bulkUpdate("delete from SnmpCollectionMibGroup g where g.collectionSource.id = ?", sourceId);
}

@Override
public List<SnmpCollectionMibGroup> filterEventConf(String name, String ifType, String vendor, String collectionSourceName, int offset, int limit) {
List<Object> queryParamList = new ArrayList<>();
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("from SnmpCollectionMibGroup g where 1=1 ");
if (name != null && !name.trim().isEmpty()) {
queryBuilder.append(" and lower(g.name) like ? escape '\\' ");
queryParamList.add("%" + escapeLike(name.trim().toLowerCase()) + "%"); // contains match
}

if (ifType != null && !ifType.trim().isEmpty()) {
queryBuilder.append(" and lower(g.ifType) like ? escape '\\' ");
queryParamList.add("%" + escapeLike(ifType.trim().toLowerCase()) + "%"); // contains match
}

if (vendor != null && !vendor.trim().isEmpty()) {
queryBuilder.append(" and lower(t.collectionSource.vendor) like ? escape '\\' ");
queryParamList.add("%" + escapeLike(vendor.trim().toLowerCase()) + "%");
}

if (collectionSourceName != null && !collectionSourceName.trim().isEmpty()) {
queryBuilder.append(" and lower(g.collectionSource.name) like ? escape '\\' ");
queryParamList.add("%" + escapeLike(collectionSourceName.trim().toLowerCase()) + "%");
}

queryBuilder.append(" order by g.createdTime desc ");

return findWithPagination(queryBuilder.toString(), queryParamList.toArray(), offset, limit);
}

@Override
public Map<String, Object> findByDataCollectionGroupId(Integer dataCollectionGroupId, String mibGroupFilter, String sortBy, String order, Integer totalRecords, Integer offset, Integer limit) {
int resultCount = (totalRecords != null) ? totalRecords : 0;
List<Object> queryParams = new ArrayList<>();
List<String> conditions = new ArrayList<>();

String whereClause = "where g.collectionSource.id = ? ";
queryParams.add(dataCollectionGroupId);

// Add filter conditions dynamically
if (mibGroupFilter != null && !mibGroupFilter.trim().isEmpty()) {
String escapedFilter = "%" + escapeLike(mibGroupFilter.trim().toLowerCase()) + "%";
conditions.add("lower(g.name) like ? escape '\\'");
queryParams.add(escapedFilter);

conditions.add("lower(g.ifType) like ? escape '\\'");
queryParams.add(escapedFilter);

}

whereClause = whereClause + (conditions.isEmpty() ? "" : " AND ( " + String.join(" OR ", conditions)+ ")");

// COUNT QUERY: get total matching records if not already provided
if (resultCount == 0) {
String countQuery = "select count(g.id) from SnmpCollectionMibGroup g " + whereClause;
resultCount = super.queryInt(countQuery, queryParams.toArray());
}

// DATA QUERY: fetch paginated results if resultCount > 0
List<SnmpCollectionMibGroup> mibGroupList = Collections.emptyList();
if (resultCount > 0) {

String orderBy;
String sortField = sortBy;

String sortOrder = "ASC".equalsIgnoreCase(order) ? "ASC" : "DESC";

Set<String> allowedSortFields = Set.of("name", "ifType");

if (sortBy == null || !allowedSortFields.contains(sortBy)) {
sortField = "name";
}

orderBy = " order by g." + sortField + " " + sortOrder;



String dataQuery = "from SnmpCollectionMibGroup g " + whereClause + orderBy;
mibGroupList = findWithPagination(dataQuery, queryParams.toArray(), offset, limit);
}

// Return map with results
return Map.of("totalRecords", resultCount, "mibGroupList", mibGroupList);
}
/**
* Escapes special characters (% , _ , \, ., /, [, ]) in a string
* to make it safe for SQL LIKE queries.
*
* @param input the input string
* @return the escaped string
*/
private String escapeLike(String input) {
return input
.replace("\\", "\\\\")
.replace("%", "\\%")
.replace("_", "\\_")
.replace("@", "\\@")
.replace("/", "\\/")
.replace("[", "\\[")
.replace("]", "\\]")
.replace(".", "\\.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.List;
import java.util.*;

public class SnmpCollectionResourceTypeDaoHibernate extends AbstractDaoHibernate<SnmpCollectionResourceType, Integer> implements SnmpCollectionResourceTypeDao {

Expand Down Expand Up @@ -62,4 +61,129 @@ public List<SnmpCollectionResourceType> findAllEnabled() {
public void deleteAll(final Collection<SnmpCollectionResourceType> list) {
super.deleteAll(list);
}

@Override
public void saveAll(Collection<SnmpCollectionResourceType> list) {
if (list == null || list.isEmpty()) {
return;
}

int batchSize = 50;
int i = 0;
for (SnmpCollectionResourceType resourceType : list) {
getHibernateTemplate().saveOrUpdate(resourceType);
i++;
if (i % batchSize == 0) {
getHibernateTemplate().flush();
getHibernateTemplate().clear();
}

}
getHibernateTemplate().flush();
getHibernateTemplate().clear();
}

@Override
public void deleteBySourceId(Integer sourceId) {
getHibernateTemplate().bulkUpdate("delete from SnmpCollectionResourceType t where t.collectionSource.id = ?", sourceId);
}

@Override
public List<SnmpCollectionResourceType> filterEventConf(String name, String label, String vendor, String collectionSourceName, int offset, int limit) {
List<Object> queryParamList = new ArrayList<>();
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("from SnmpCollectionResourceType t where 1=1 ");
if (label != null && !label.trim().isEmpty()) {
queryBuilder.append(" and lower(t.label) like ? escape '\\' ");
queryParamList.add("%" + escapeLike(label.trim().toLowerCase()) + "%"); // contains match
}

if (vendor != null && !vendor.trim().isEmpty()) {
queryBuilder.append(" and lower(t.collectionSource.vendor) like ? escape '\\' ");
queryParamList.add("%" + escapeLike(vendor.trim().toLowerCase()) + "%");
}

if (name != null && !name.trim().isEmpty()) {
queryBuilder.append(" and lower(t.collectionSource.name) like ? escape '\\' ");
queryParamList.add("%" + escapeLike(name.trim().toLowerCase()) + "%");
}

queryBuilder.append(" order by t.createdTime desc ");

return findWithPagination(queryBuilder.toString(), queryParamList.toArray(), offset, limit);
}

@Override
public Map<String, Object> findByDataCollectionGroupId(Integer dataCollectionGroupId, String resourceTypeFilter, String sortBy, String order, Integer totalRecords, Integer offset, Integer limit) {
int resultCount = (totalRecords != null) ? totalRecords : 0;
List<Object> queryParams = new ArrayList<>();
List<String> conditions = new ArrayList<>();

String whereClause = "where t.collectionSource.id = ? ";
queryParams.add(dataCollectionGroupId);

// Add filter conditions dynamically
if (resourceTypeFilter != null && !resourceTypeFilter.trim().isEmpty()) {
String escapedFilter = "%" + escapeLike(resourceTypeFilter.trim().toLowerCase()) + "%";
conditions.add("lower(t.name) like ? escape '\\'");
queryParams.add(escapedFilter);

conditions.add("lower(t.label) like ? escape '\\'");
queryParams.add(escapedFilter);

}

whereClause = whereClause + (conditions.isEmpty() ? "" : " AND ( " + String.join(" OR ", conditions)+ ")");

// COUNT QUERY: get total matching records if not already provided
if (resultCount == 0) {
String countQuery = "select count(t.id) from SnmpCollectionResourceType t " + whereClause;
resultCount = super.queryInt(countQuery, queryParams.toArray());
}

// DATA QUERY: fetch paginated results if resultCount > 0
List<SnmpCollectionResourceType> resourceTypeList = Collections.emptyList();
if (resultCount > 0) {

String orderBy;
String sortField = sortBy;

String sortOrder = "ASC".equalsIgnoreCase(order) ? "ASC" : "DESC";

Set<String> allowedSortFields = Set.of("name", "label");

if (sortBy == null || !allowedSortFields.contains(sortBy)) {
sortField = "name";
}

orderBy = " order by t." + sortField + " " + sortOrder;



String dataQuery = "from SnmpCollectionResourceType t " + whereClause + orderBy;
resourceTypeList = findWithPagination(dataQuery, queryParams.toArray(), offset, limit);
}

// Return map with results
return Map.of("totalRecords", resultCount, "resourceTypeList", resourceTypeList);
}

/**
* Escapes special characters (% , _ , \, ., /, [, ]) in a string
* to make it safe for SQL LIKE queries.
*
* @param input the input string
* @return the escaped string
*/
private String escapeLike(String input) {
return input
.replace("\\", "\\\\")
.replace("%", "\\%")
.replace("_", "\\_")
.replace("@", "\\@")
.replace("/", "\\/")
.replace("[", "\\[")
.replace("]", "\\]")
.replace(".", "\\.");
}
}
Loading