/** * Check optional but secure assertions. */ private void checkCompletude(final BasicParameterValueVo vo, final Parameter parameter) { Arrays.stream(new Supplier<?>[] { vo::getText, vo::getBool, vo::getDate, vo::getIndex, vo::getInteger, vo::getTags, vo::getSelections }).map(Supplier::get).filter(Objects::nonNull).skip(1).findFirst() .ifPresent(e -> { final ValidationJsonException exception = new ValidationJsonException(); exception.addError(parameter.getId(), "Too many values"); throw exception; }); }
/** * Default constructor initializing the type mappings. */ public ParameterValueResource() { typeToChecker.put(ParameterType.BOOL, (b, p) -> assertNotnull(b.getBool(), p.getId())); typeToChecker.put(ParameterType.DATE, (b, p) -> { assertNotnull(b.getDate(), p.getId()); assertTrue(b.getDate().getTime() > 0, p.getId(), "Min", 0); }); typeToChecker.put(ParameterType.INTEGER, this::checkInteger); typeToChecker.put(ParameterType.SELECT, this::checkSelect); typeToChecker.put(ParameterType.MULTIPLE, this::checkMultiple); typeToChecker.put(ParameterType.TAGS, this::checkTags); typeToChecker.put(ParameterType.TEXT, this::checkText); }
/** * 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; }
/** * Check text */ private void checkText(final BasicParameterValueVo vo, final Parameter parameter) { // Check the value if not empty if (StringUtils.isNotBlank(vo.getText()) && StringUtils.isNotBlank(parameter.getData())) { // Check the pattern if present final Map<String, String> stringProperties = ParameterResource.toMapString(parameter.getData()); final String patternString = stringProperties.get("pattern"); if (StringUtils.isNotBlank(patternString)) { // Pattern is provided, check the string final Pattern pattern = Pattern.compile(patternString); assertTrue(pattern.matcher(vo.getText()).matches(), javax.validation.constraints.Pattern.class.getSimpleName(), parameter.getId(), "regexp", pattern.pattern()); } } }
/** * Check integer */ private void checkInteger(final BasicParameterValueVo vo, final Parameter parameter) { assertNotnull(vo.getInteger(), parameter.getId()); final Map<String, Integer> minMax = ParameterResource.toMapInteger(parameter.getData()); // Check minimal value Optional.ofNullable(minMax.get("max")).ifPresent(m -> checkMax(vo.getInteger(), m, parameter)); // Check maximal value Optional.ofNullable(minMax.get("min")).ifPresent(m -> checkMin(vo.getInteger(), m, parameter)); }
/** * 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()); }
/** * Check multiple selection */ private void checkMultiple(final BasicParameterValueVo vo, final Parameter parameter) { assertNotnull(vo.getSelections(), parameter.getId()); final List<String> multiple = ParameterResource.toListString(parameter.getData()); // Check each index vo.getSelections().forEach(i -> checkArrayBound(i, multiple.size(), parameter)); }
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();
.put(v.getParameter().getId(), ParameterValueResource.parseValue(v, new ParameterValueVo())));
/** * Check simple selection */ private void checkSelect(final BasicParameterValueVo vo, final Parameter parameter) { assertNotnull(vo.getIndex(), parameter.getId()); final List<String> single = ParameterResource.toListString(parameter.getData()); // Check the index checkArrayBound(vo.getIndex(), single.size(), parameter); }
/** * Check the parameter is related to the given node. * * @param parameter * The parameter to check. * @param node * The node scope the parameter must be related to. */ public void checkOwnership(final Parameter parameter, final Node node) { if (!equalsOrParentOf(parameter.getOwner(), node)) { // This parameter is detached from the node's hierarchy throw new BusinessException("invalid-parameter-node-ownership", "parameter", parameter.getId(), "node", node.getId()); } }
/** * 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; }
@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; }
/** * 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); } } }
/** * {@link Parameter} JPA to {@link ParameterVo} transformer. * * @param entity * The source JPA entity to convert. * @return The VO with all attributes : full node reference, and definition. */ public static ParameterVo toVo(final Parameter entity) { final ParameterVo vo = new ParameterVo(); // Copy basic data vo.setId(entity.getId()); vo.setType(entity.getType()); vo.setMandatory(entity.isMandatory()); vo.setSecured(entity.isSecured()); vo.setOwner(NodeResource.toVo(entity.getOwner())); vo.setDefaultValue(entity.getDefaultValue()); vo.setDepends(entity.getDepends().stream().map(Persistable::getId).collect(Collectors.toSet())); // Map constraint data if (entity.getType().isArray()) { vo.setValues(toConfiguration(entity.getData(), LIST_STRING_TYPE)); } else if (entity.getType() == ParameterType.INTEGER) { final Map<String, Integer> minMax = toConfiguration(entity.getData(), MAP_STRING_TYPE); vo.setMax(minMax.get("max")); vo.setMin(minMax.get("min")); } return vo; }