Skip to content

Commit

Permalink
refactor StateFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
mbussolotto committed Jan 9, 2025
1 parent 85f9f3f commit f8b814a
Showing 1 changed file with 83 additions and 41 deletions.
124 changes: 83 additions & 41 deletions java/code/src/com/redhat/rhn/domain/state/StateFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.query.Query;
import org.hibernate.type.LongType;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Factory class for working with states.
Expand Down Expand Up @@ -95,9 +96,17 @@ public static void save(StateRevision stateRevision) {
* @return the latest package states for this server
*/
public static Optional<Set<PackageState>> latestPackageStates(MinionServer server) {
Optional<ServerStateRevision> revision = latestRevision(ServerStateRevision.class,
"server", server);
return revision.map(ServerStateRevision::getPackageStates);

String sql = "SELECT *, null as created, null as creator_id FROM suseServerStateRevision WHERE server_id = :server";
Query<ServerStateRevision> query = getSession().createNativeQuery(sql, ServerStateRevision.class);
query.setParameter("server", server.getId(), LongType.INSTANCE);
List<ServerStateRevision> servers = query.getResultList();
return servers.stream()
.map(o -> o.getPackageStates().stream()
.max(Comparator.comparingLong(PackageState::getId)))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.collectingAndThen(Collectors.toSet(), Optional::of));
}

/**
Expand All @@ -107,9 +116,16 @@ public static Optional<Set<PackageState>> latestPackageStates(MinionServer serve
* @return the latest package states for this server group
*/
public static Optional<Set<PackageState>> latestPackageStates(ServerGroup group) {
Optional<ServerGroupStateRevision> revision = latestRevision(
ServerGroupStateRevision.class, "group", group);
return revision.map(ServerGroupStateRevision::getPackageStates);
String sql = "SELECT *, null as created, null as creator_id FROM suseServerGroupStateRevision WHERE group_id = :group";
Query<ServerGroupStateRevision> query = getSession().createNativeQuery(sql, ServerGroupStateRevision.class);
query.setParameter("group", group.getId(), LongType.INSTANCE);
List<ServerGroupStateRevision> groups = query.getResultList();
return groups.stream()
.map(o -> o.getPackageStates().stream()
.max(Comparator.comparingLong(PackageState::getId)))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.collectingAndThen(Collectors.toSet(), Optional::of));
}

/**
Expand All @@ -119,9 +135,16 @@ public static Optional<Set<PackageState>> latestPackageStates(ServerGroup group)
* @return the latest package states for this organization
*/
public static Optional<Set<PackageState>> latestPackageStates(Org org) {
Optional<OrgStateRevision> revision = latestRevision(OrgStateRevision.class,
"org", org);
return revision.map(OrgStateRevision::getPackageStates);
String sql = "SELECT *, null as created, null as creator_id FROM suseOrgStateRevision WHERE org_id = :org";
Query<OrgStateRevision> query = getSession().createNativeQuery(sql, OrgStateRevision.class);
query.setParameter("org", org.getId(), LongType.INSTANCE);
List<OrgStateRevision> orgs = query.getResultList();
return orgs.stream()
.map(o -> o.getPackageStates().stream()
.max(Comparator.comparingLong(PackageState::getId)))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.collectingAndThen(Collectors.toSet(), Optional::of));
}

/**
Expand All @@ -130,7 +153,11 @@ public static Optional<Set<PackageState>> latestPackageStates(Org org) {
* @return the optional {@link OrgStateRevision}
*/
public static Optional<OrgStateRevision> latestStateRevision(Org org) {
return latestRevision(OrgStateRevision.class, "org", org);
String sql = "SELECT *, null as created, null as creator_id FROM suseOrgStateRevision WHERE org_id = :org";
Query<OrgStateRevision> query = getSession().createNativeQuery(sql, OrgStateRevision.class);
query.setParameter("org", org.getId(), LongType.INSTANCE);
List<OrgStateRevision> orgs = query.getResultList();
return orgs.stream().max(Comparator.comparingLong(OrgStateRevision::getId));
}

/**
Expand All @@ -140,7 +167,11 @@ public static Optional<OrgStateRevision> latestStateRevision(Org org) {
*/
public static Optional<ServerGroupStateRevision> latestStateRevision(
ServerGroup group) {
return latestRevision(ServerGroupStateRevision.class, "group", group);
String sql = "SELECT *, null as created, null as creator_id FROM suseServerGroupStateRevision WHERE group_id = :group";
Query<ServerGroupStateRevision> query = getSession().createNativeQuery(sql, ServerGroupStateRevision.class);
query.setParameter("group", group.getId(), LongType.INSTANCE);
List<ServerGroupStateRevision> serversgroup = query.getResultList();
return serversgroup.stream().max(Comparator.comparingLong(ServerGroupStateRevision::getId));
}

/**
Expand All @@ -149,7 +180,11 @@ public static Optional<ServerGroupStateRevision> latestStateRevision(
* @return the optional {@link OrgStateRevision}
*/
public static Optional<ServerStateRevision> latestStateRevision(MinionServer server) {
return latestRevision(ServerStateRevision.class, "server", server);
String sql = "SELECT *, null as created, null as creator_id FROM suseServerStateRevision WHERE server_id = :server";
Query<ServerStateRevision> query = getSession().createNativeQuery(sql, ServerStateRevision.class);
query.setParameter("server", server.getId(), LongType.INSTANCE);
List<ServerStateRevision> servers = query.getResultList();
return servers.stream().max(Comparator.comparingLong(ServerStateRevision::getId));
}

/**
Expand All @@ -159,10 +194,16 @@ public static Optional<ServerStateRevision> latestStateRevision(MinionServer ser
* @return the latest config channels for this server
*/
public static Optional<List<ConfigChannel>> latestConfigChannels(MinionServer server) {
Optional<ServerStateRevision> revision = latestRevision(
ServerStateRevision.class, "server", server);
return Optional
.ofNullable(revision.map(StateRevision::getConfigChannels).orElse(null));
String sql = "SELECT *, null as created, null as creator_id FROM suseServerStateRevision WHERE server_id = :server";
Query<ServerStateRevision> query = getSession().createNativeQuery(sql, ServerStateRevision.class);
query.setParameter("server", server.getId(), LongType.INSTANCE);
List<ServerStateRevision> servers = query.getResultList();
return servers.stream()
.map(o -> o.getConfigChannels().stream()
.max(Comparator.comparingLong(ConfigChannel::getId)))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.collectingAndThen(Collectors.toList(), Optional::of));
}

/**
Expand All @@ -172,10 +213,16 @@ public static Optional<List<ConfigChannel>> latestConfigChannels(MinionServer se
* @return the latest config channels for this server
*/
public static Optional<List<ConfigChannel>> latestConfigChannels(ServerGroup group) {
Optional<ServerGroupStateRevision> revision = latestRevision(
ServerGroupStateRevision.class, "group", group);
return Optional
.ofNullable(revision.map(StateRevision::getConfigChannels).orElse(null));
String sql = "SELECT *, null as created, null as creator_id FROM suseServerGroupStateRevision WHERE group_id = :group";
Query<ServerGroupStateRevision> query = getSession().createNativeQuery(sql, ServerGroupStateRevision.class);
query.setParameter("group", group.getId(), LongType.INSTANCE);
List<ServerGroupStateRevision> serverGroups = query.getResultList();
return serverGroups.stream()
.map(o -> o.getConfigChannels().stream()
.max(Comparator.comparingLong(ConfigChannel::getId)))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.collectingAndThen(Collectors.toList(), Optional::of));
}

/**
Expand All @@ -185,23 +232,18 @@ public static Optional<List<ConfigChannel>> latestConfigChannels(ServerGroup gro
* @return the latest config channels for this server
*/
public static Optional<List<ConfigChannel>> latestConfigChannels(Org org) {
Optional<OrgStateRevision> revision = latestRevision(
OrgStateRevision.class, "org", org);
return Optional
.ofNullable(revision.map(StateRevision::getConfigChannels).orElse(null));
}

private static <T extends StateRevision> Optional<T> latestRevision(
Class<T> revisionType, String field, Object bean) {
DetachedCriteria maxQuery = DetachedCriteria.forClass(revisionType)
.add(Restrictions.eq(field, bean))
.setProjection(Projections.max("id"));
T revision = (T) getSession()
.createCriteria(revisionType)
.add(Restrictions.eq(field, bean))
.add(Property.forName("id").eq(maxQuery))
.uniqueResult();
return Optional.ofNullable(revision);
String sql =
"SELECT *, null as created, null as creator_id FROM " +
"suseOrgStateRevision WHERE org_id = :org ";
Query<OrgStateRevision> query = getSession().createNativeQuery(sql, OrgStateRevision.class);
query.setParameter("org", org.getId(), LongType.INSTANCE);
List<OrgStateRevision> orgs = query.getResultList();
return orgs.stream()
.map(o -> o.getConfigChannels().stream()
.max(Comparator.comparingLong(ConfigChannel::getId)))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.collectingAndThen(Collectors.toList(), Optional::of));
}

/**
Expand Down

0 comments on commit f8b814a

Please sign in to comment.