/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }