/** * Gets the projects list for the dashboard. * * @return the projects list for the dashboard. */ public List<StoreProject> getDashboardProjects() { List<StoreProject> result = new ArrayList<>(); CriteriaBuilder cb = getBaseEAO().getCriteriaBuilder(); CriteriaQuery<StoreProject> cq = getBaseEAO().createCriteriaQuery(); Root<StoreProject> root = cq.from(StoreProject.class); cq.distinct(true); List<Predicate> predicates = new ArrayList<>(); predicates.add(cb.equal(root.get(StoreProject_.enabled), true)); Join<StoreProject, StoreApplication> applications = (Join<StoreProject, StoreApplication>) root.fetch(StoreProject_.applications, JoinType.LEFT); predicates.add(cb.equal(applications.get(StoreApplication_.enabled), true)); Join<StoreApplication, StoreSystem> systems = (Join<StoreApplication, StoreSystem>) applications.fetch(StoreApplication_.systems, JoinType.LEFT); predicates.add(cb.equal(systems.get(StoreSystem_.enabled), true)); cq.where(cb.and(predicates.toArray(new Predicate[predicates.size()]))); try { TypedQuery<StoreProject> typeQuery = getBaseEAO().createTypedQuery(cq); result = typeQuery.getResultList(); } catch (NoResultException ex) { // do nothing } return result; }
@SuppressWarnings("unchecked") private static List<Predicate> deviceSpecificPrincipalPredicates(CriteriaBuilder cb, Root<Device> from, Optional<HivePrincipal> principal) { final List<Predicate> predicates = new LinkedList<>(); final Join<Device, Network> networkJoin = (Join) from.fetch("network", JoinType.LEFT); final Join<Device, DeviceType> deviceTypeJoin = (Join) from.fetch("deviceType", JoinType.LEFT); principal.ifPresent(p -> { UserVO user = p.getUser(); if (user != null && !user.isAdmin()) { // Joining after check to prevent duplicate objects final Join<Device, Network> usersJoin = (Join) networkJoin.fetch("users", JoinType.LEFT); predicates.add(cb.equal(usersJoin.<Long>get("id"), user.getId())); } if (p.getNetworkIds() != null) { predicates.add(networkJoin.<Long>get("id").in(p.getNetworkIds())); } if (p.getDeviceTypeIds() != null) { predicates.add(deviceTypeJoin.<Long>get("id").in(p.getDeviceTypeIds())); } }); return predicates; } }