use of org.ligoj.app.model.ParameterValue in project ligoj-api by ligoj.
the class NodeResource method findSubscriptionsWithParams.
/**
* 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;
}
use of org.ligoj.app.model.ParameterValue in project ligoj-api by ligoj.
the class NodeResource method toVoParameters.
/**
* JPA {@link Node} associated to {@link ParameterValue} to detailed {@link NodeVo} converter. This not a one to one
* {@link Function}.
*
* @param nodesAndValues
* Nodes with values.
* @return The corresponding VO objects with recursive redefined reference.
*/
public static Map<String, NodeVo> toVoParameters(final List<Object[]> nodesAndValues) {
// Build the nodes
final Map<String, NodeVo> nodes = new HashMap<>();
final Map<String, Node> entities = new HashMap<>();
for (final Object[] resultSet : nodesAndValues) {
final Node node = (Node) resultSet[0];
final NodeVo vo = nodes.computeIfAbsent(node.getId(), id -> {
// Build the first encountered parameter for this node
entities.put(id, node);
return toVoParameter(node);
});
// Copy the parameter value if present
Optional.ofNullable((ParameterValue) resultSet[1]).ifPresent(v -> vo.getParameters().put(v.getParameter().getId(), ParameterValueResource.parseValue(v, new ParameterValueVo())));
}
// Complete the hierarchy
entities.entrySet().stream().filter(entry -> entry.getValue().isRefining()).forEach(entry -> {
// Complete the hierarchy for this node
final NodeVo node = nodes.get(entry.getKey());
final NodeVo parent = nodes.get(entry.getValue().getRefined().getId());
node.setRefined(parent);
node.getParameters().putAll(parent.getParameters());
});
return nodes;
}
use of org.ligoj.app.model.ParameterValue in project ligoj-api by ligoj.
the class ParameterValueResource method toMapValues.
/**
* 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;
}
use of org.ligoj.app.model.ParameterValue in project ligoj-api by ligoj.
the class ParameterValueResource method delete.
/**
* 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);
}
use of org.ligoj.app.model.ParameterValue in project ligoj-api by ligoj.
the class NodeResourceTest method createOverrideParameter.
@Test
public void createOverrideParameter() {
final ParameterValue nodeParameter = new ParameterValue();
nodeParameter.setParameter(parameterRepository.findOneExpected("service:bt:jira:url"));
nodeParameter.setNode(repository.findOneExpected("service:bt:jira"));
nodeParameter.setData("http://localhost");
parameterValueRepository.saveAndFlush(nodeParameter);
Assertions.assertNull(resource.findAll().get("service:bt:jira:some-7"));
final NodeEditionVo node = new NodeEditionVo();
node.setId("service:bt:jira:some-7");
node.setMode(SubscriptionMode.LINK);
node.setName("Jira 7");
node.setNode("service:bt:jira");
final ParameterValueCreateVo value = new ParameterValueCreateVo();
value.setParameter("service:bt:jira:url");
value.setText("any");
node.setParameters(Collections.singletonList(value));
Assertions.assertThrows(ValidationJsonException.class, () -> resource.create(node));
}
Aggregations