From 3f54579c721370e069768c6c2ea6947ea878dafb Mon Sep 17 00:00:00 2001 From: Sylvain Cadilhac Date: Thu, 28 Dec 2023 17:41:05 +0100 Subject: [PATCH] Add rule count in policies (API) --- .../java/onl/netfishers/netshot/compliance/Policy.java | 7 +++++++ src/main/java/onl/netfishers/netshot/rest/RestService.java | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/onl/netfishers/netshot/compliance/Policy.java b/src/main/java/onl/netfishers/netshot/compliance/Policy.java index 1d1a8774..d7bcf17d 100644 --- a/src/main/java/onl/netfishers/netshot/compliance/Policy.java +++ b/src/main/java/onl/netfishers/netshot/compliance/Policy.java @@ -28,6 +28,7 @@ import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.OneToMany; +import javax.persistence.Transient; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; @@ -111,6 +112,12 @@ public void addRule(Rule rule) { rule.setPolicy(this); } + @XmlElement + @Transient + public int getRuleCount() { + return this.rules.size(); + } + /** * Check all devices of the target groups against the policy. * diff --git a/src/main/java/onl/netfishers/netshot/rest/RestService.java b/src/main/java/onl/netfishers/netshot/rest/RestService.java index 0877f1ae..1216c5e9 100644 --- a/src/main/java/onl/netfishers/netshot/rest/RestService.java +++ b/src/main/java/onl/netfishers/netshot/rest/RestService.java @@ -4158,8 +4158,9 @@ public List getPolicies(@BeanParam PaginationParams paginationParams) th log.debug("REST request, get policies."); Session session = Database.getSession(true); try { + // Join p.rules to get rule count... not optimal Query query = session - .createQuery("from Policy p left join fetch p.targetGroups", Policy.class); + .createQuery("select distinct p from Policy p left join fetch p.targetGroups left join fetch p.rules", Policy.class); paginationParams.apply(query); return query.list(); }