/** * Convert a {@link Subscription} to a {@link SubscriptionVo} with status, and put it in the target map if not * existing. * * @param subscriptions * The map of already converted entities. * @param entity * The {@link Subscription} * @return The related converted {@link SubscriptionVo} newly created or existing one. */ private SubscriptionVo addVo(final Map<Integer, SubscriptionVo> subscriptions, final Subscription entity) { return subscriptions.computeIfAbsent(entity.getId(), id -> { // Build the subscription root instance final SubscriptionVo vo = new SubscriptionVo(); vo.copyAuditData(entity, userConverter); vo.setId(entity.getId()); vo.setNode(NodeResource.toVo(entity.getNode())); vo.setParameters(new HashMap<>()); subscriptions.put(entity.getId(), vo); // Add subscription status final EventVo lastEvent = subscriptionStatus.get(entity.getId()); if (lastEvent != null) { vo.setStatus(NodeStatus.valueOf(lastEvent.getValue())); } return vo; }); }
@Override public ProjectVo apply(final Project entity) { final ProjectVo vo = new ProjectVo(); vo.copyAuditData(entity, userConverter); DescribedBean.copy(entity, vo); vo.setPkey(entity.getPkey()); vo.setTeamLeader(userConverter.apply(entity.getTeamLeader())); // Build the subscriptions final Map<Integer, SubscriptionVo> subscriptions = new TreeMap<>(); for (final Object[] resultSet : this.subscriptionsAndParam) { // Add subscription value final ParameterValue parameterValue = (ParameterValue) resultSet[1]; addVo(subscriptions, (Subscription) resultSet[0]).getParameters().put(parameterValue.getParameter().getId(), ParameterValueResource.parseValue(parameterValue, new ParameterValueVo())); } // Merge with subscription without parameters entity.getSubscriptions().forEach(s -> addVo(subscriptions, s)); // Return the subscription to order by the related node vo.setSubscriptions(subscriptions.values().stream().sorted(Comparator.comparing(s -> s.getNode().getId(), String::compareTo)) .collect(Collectors.toList())); return vo; }