/** * Check transition and convert to entity. * * @param vo * new {@link ParameterValueCreateVo} to persist. * @param parameter * The resolved parameter related to the {@link ParameterValue} * @return corresponding entity. */ private ParameterValue createInternal(final ParameterValueCreateVo vo, final Parameter parameter) { return checkSaveOrUpdate(vo, parameter, new ParameterValue()); }
/** * Check transition and convert to entity. * * @param vo * new {@link ParameterValueCreateVo} to be merged into the entity. * @param parameter * The resolved parameter related to the {@link ParameterValue} * @param entity * The entity to update. * @return corresponding entity when accepted for update. <code>null</code> when all constraints are checked, but * the target operation should be a deletion because of the empty value. */ private ParameterValue checkSaveOrUpdate(final ParameterValueCreateVo vo, final Parameter parameter, final ParameterValue entity) { checkConstraints(vo, parameter); checkCompletude(vo, parameter); entity.setData(toData(vo)); entity.setParameter(parameter); // Handle the target empty data if (StringUtils.isBlank(entity.getData())) { // A blank parameter value must be deleted return null; } // Encrypt the data as needed if (parameter.isSecured()) { // Data need to be encrypted entity.setData(cryptoHelper.encryptAsNeeded(entity.getData())); } return entity; }
/** * Check the parameter value is not used in a subscription when the target value becomes <code>null</code> or empty. * * @param entity * The {@link ParameterValue} entity to check. */ private void checkUnusedValue(final ParameterValue entity) { if (entity.getParameter().isMandatory()) { final int nb = subscriptionRepository.countByParameterValue(entity.getId()); if (nb > 0) { throw new ValidationJsonException(entity.getParameter().getId(), "used-parameter-value", "subscriptions", nb); } } }
/** * Parse the raw data to the target type and return this value. * * @param entity * {@link ParameterValue} to be parsed. * @param vo * Target object receiving the typed value. * @return the parsed and typed value. * @param <T> * The object type resolved during the parsing. */ public static <T> T parseValue(final ParameterValue entity, final BasicParameterValueVo vo) { @SuppressWarnings("unchecked") final ParameterValueMapper<T> valueMapper = (ParameterValueMapper<T>) TO_VALUE .get(entity.getParameter().getType()); final T parsedValue = valueMapper.toValue.apply(entity.getData()); valueMapper.setter.accept(vo, parsedValue); return parsedValue; }
/** * {@link ParameterValue} JPA to business object transformer. * * @param entity * The parameter value. * @return The parameter value with parameter definition. */ public ParameterValueVo toVo(final ParameterValue entity) { final ParameterValueVo vo = new ParameterValueVo(); vo.copyAuditData(entity, (Function<String, SimpleUserOrg>) iamProvider[0].getConfiguration().getUserRepository()::toUser); vo.setId(entity.getId()); vo.setParameter(ParameterResource.toVo(entity.getParameter())); // Map node if (entity.getNode() != null) { vo.setNode(NodeResource.toVo(entity.getNode())); } // Map criteria value parseValue(entity, vo); return vo; }
/** * Update the given node parameter values. The old not updated values are deleted. * * @param values * the parameter values to persist. * @param node * The related node. */ public void update(final List<ParameterValueCreateVo> values, final Node node) { // Build the old parameter values final List<ParameterValue> oldList = repository.getParameterValues(node.getId()); final Map<String, ParameterValue> oldMap = oldList.stream() .collect(Collectors.toMap(v -> v.getParameter().getId(), Function.identity())); // Build the target parameter values final Set<String> newParam = values.stream().map(v -> saveOrUpdate(oldMap, v)).filter(Objects::nonNull) .peek(v -> v.setNode(node)).map(repository::saveAndFlush).map(v -> v.getParameter().getId()) .collect(Collectors.toSet()); // Delete the existing but not provided values CollectionUtils.removeAll(oldMap.keySet(), newParam).stream().map(oldMap::get).forEach(repository::delete); cacheManager.getCache("node-parameters").evict(node.getId()); }
private ParameterValue saveOrUpdate(final Map<String, ParameterValue> existing, final ParameterValueCreateVo value) { if (value.isUntouched()) { // Untouched value, keep the previous value but must exists return Optional.ofNullable(existing.get(value.getParameter())).orElseThrow( () -> new BusinessException(BusinessException.KEY_UNKNOW_ID, "parameter", value.getParameter())); } // Updated or created value final ParameterValue entity = existing.get(value.getParameter()); if (entity == null) { // Need to parse and recreate the value return createInternal(value); } // Update mode return checkSaveOrUpdate(value, entity.getParameter(), entity); }
/** * Create the given node parameter values. * * @param values * the parameter values to persist. * @param node * The related node. */ public void create(final List<ParameterValueCreateVo> values, final Node node) { create(values, v -> v.setNode(node)); cacheManager.getCache("node-parameters").evict(node.getId()); }
/** * Create the given subscription parameter values. Value validity is checked. * * @param values * the parameter values to persist. * @param subscription * The related subscription. */ public void create(final List<ParameterValueCreateVo> values, final Subscription subscription) { create(values, v -> v.setSubscription(subscription)); cacheManager.getCache("subscription-parameters").evict(subscription.getId()); }
/** * Delete a {@link ParameterValue}. A value can be deleted only where there is no subscription on the related node, * or the related parameter is not mandatory. * * @param id * The entity's identifier. */ public void delete(final int id) { // Check the value exist and related to a visible writable node final ParameterValue value = findOneExpected(id); // Check the visible node can also be edited nodeResource.checkWritableNode(value.getNode().getId()); // A mandatory parameter can be deleted only when there is no // subscription to the same node checkUnusedValue(value); // Deletion can be performed repository.deleteById(id); }
/** * Find subscriptions where some parameters are defined. * * @param id * node identifier * @return subscriptions with redefined parameters */ protected Map<Subscription, Map<String, String>> findSubscriptionsWithParams(final String id) { final Map<Subscription, Map<String, String>> result = new HashMap<>(); for (final Object[] entityTab : subscriptionRepository.findAllWithValuesByNode(id)) { final ParameterValue value = (ParameterValue) entityTab[1]; result.computeIfAbsent((Subscription) entityTab[0], s -> new HashMap<>()).put(value.getParameter().getId(), value.getData()); } return result; }
final List<ParameterVo> parameters = parameterResource.getNotProvidedAndAssociatedParameters(node, mode); final Map<String, ParameterValue> vmap = repository.getParameterValues(node).stream() .collect(Collectors.toMap(v -> v.getParameter().getId(), Function.identity())); return parameters.stream().map(p -> { final ParameterNodeVo vo = new ParameterNodeVo();
/** * Transform {@link List} to {@link Map} where key is the parameter name. Secured parameters are decrypted. * * @param values * The parameters list. * @return the corresponding key/values. Never <code>null</code>. */ public Map<String, String> toMapValues(final List<ParameterValue> values) { final Map<String, String> result = new HashMap<>(); for (final ParameterValue value : values) { String data; if (value.getParameter().isSecured()) { // Value may be encrypted data = cryptoHelper.decryptAsNeeded(value.getData()); } else { data = value.getData(); } // Trim the data to get only the relevant values data = StringUtils.trimToNull(data); if (data != null) { // Non empty value, can be stored result.put(value.getParameter().getId(), data); } } return result; }
.put(v.getParameter().getId(), ParameterValueResource.parseValue(v, new ParameterValueVo())));
@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; }