Search in sources :

Example 1 with ParameterValue

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;
}
Also used : Parameter(org.ligoj.app.model.Parameter) Produces(javax.ws.rs.Produces) BiFunction(java.util.function.BiFunction) Path(javax.ws.rs.Path) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) SubscriptionMode(org.ligoj.app.api.SubscriptionMode) DataTableAttributes(org.ligoj.bootstrap.core.json.datatable.DataTableAttributes) Map(java.util.Map) ListUtils(org.apache.commons.collections4.ListUtils) DefaultValue(javax.ws.rs.DefaultValue) Subscription(org.ligoj.app.model.Subscription) DELETE(javax.ws.rs.DELETE) Context(javax.ws.rs.core.Context) NodeRepository(org.ligoj.app.dao.NodeRepository) NodeStatus(org.ligoj.app.api.NodeStatus) Transactional(javax.transaction.Transactional) Collection(java.util.Collection) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) OnNullReturn404(org.ligoj.bootstrap.core.resource.OnNullReturn404) Entry(java.util.Map.Entry) Optional(java.util.Optional) UriInfo(javax.ws.rs.core.UriInfo) TableItem(org.ligoj.bootstrap.core.json.TableItem) ParameterRepository(org.ligoj.app.dao.ParameterRepository) LongTaskRunner(org.ligoj.app.resource.plugin.LongTaskRunner) PathParam(javax.ws.rs.PathParam) CacheRemoveAll(javax.cache.annotation.CacheRemoveAll) GET(javax.ws.rs.GET) ToolPlugin(org.ligoj.app.api.ToolPlugin) HashMap(java.util.HashMap) NamedBean(org.ligoj.bootstrap.core.NamedBean) Scheduled(org.springframework.scheduling.annotation.Scheduled) EventRepository(org.ligoj.app.dao.EventRepository) Function(java.util.function.Function) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) SubscriptionRepository(org.ligoj.app.dao.SubscriptionRepository) SecurityHelper(org.ligoj.bootstrap.core.security.SecurityHelper) EnumUtils(org.apache.commons.lang3.EnumUtils) Service(org.springframework.stereotype.Service) ParameterValue(org.ligoj.app.model.ParameterValue) NodeVo(org.ligoj.app.api.NodeVo) PaginationJson(org.ligoj.bootstrap.core.json.PaginationJson) POST(javax.ws.rs.POST) Node(org.ligoj.app.model.Node) SubscriptionStatusWithData(org.ligoj.app.api.SubscriptionStatusWithData) EventType(org.ligoj.app.model.EventType) BusinessException(org.ligoj.bootstrap.core.resource.BusinessException) CacheResult(javax.cache.annotation.CacheResult) ServicePlugin(org.ligoj.app.api.ServicePlugin) PUT(javax.ws.rs.PUT) ParameterValue(org.ligoj.app.model.ParameterValue) HashMap(java.util.HashMap) Subscription(org.ligoj.app.model.Subscription) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with ParameterValue

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;
}
Also used : Parameter(org.ligoj.app.model.Parameter) Produces(javax.ws.rs.Produces) BiFunction(java.util.function.BiFunction) Path(javax.ws.rs.Path) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) SubscriptionMode(org.ligoj.app.api.SubscriptionMode) DataTableAttributes(org.ligoj.bootstrap.core.json.datatable.DataTableAttributes) Map(java.util.Map) ListUtils(org.apache.commons.collections4.ListUtils) DefaultValue(javax.ws.rs.DefaultValue) Subscription(org.ligoj.app.model.Subscription) DELETE(javax.ws.rs.DELETE) Context(javax.ws.rs.core.Context) NodeRepository(org.ligoj.app.dao.NodeRepository) NodeStatus(org.ligoj.app.api.NodeStatus) Transactional(javax.transaction.Transactional) Collection(java.util.Collection) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) OnNullReturn404(org.ligoj.bootstrap.core.resource.OnNullReturn404) Entry(java.util.Map.Entry) Optional(java.util.Optional) UriInfo(javax.ws.rs.core.UriInfo) TableItem(org.ligoj.bootstrap.core.json.TableItem) ParameterRepository(org.ligoj.app.dao.ParameterRepository) LongTaskRunner(org.ligoj.app.resource.plugin.LongTaskRunner) PathParam(javax.ws.rs.PathParam) CacheRemoveAll(javax.cache.annotation.CacheRemoveAll) GET(javax.ws.rs.GET) ToolPlugin(org.ligoj.app.api.ToolPlugin) HashMap(java.util.HashMap) NamedBean(org.ligoj.bootstrap.core.NamedBean) Scheduled(org.springframework.scheduling.annotation.Scheduled) EventRepository(org.ligoj.app.dao.EventRepository) Function(java.util.function.Function) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) SubscriptionRepository(org.ligoj.app.dao.SubscriptionRepository) SecurityHelper(org.ligoj.bootstrap.core.security.SecurityHelper) EnumUtils(org.apache.commons.lang3.EnumUtils) Service(org.springframework.stereotype.Service) ParameterValue(org.ligoj.app.model.ParameterValue) NodeVo(org.ligoj.app.api.NodeVo) PaginationJson(org.ligoj.bootstrap.core.json.PaginationJson) POST(javax.ws.rs.POST) Node(org.ligoj.app.model.Node) SubscriptionStatusWithData(org.ligoj.app.api.SubscriptionStatusWithData) EventType(org.ligoj.app.model.EventType) BusinessException(org.ligoj.bootstrap.core.resource.BusinessException) CacheResult(javax.cache.annotation.CacheResult) ServicePlugin(org.ligoj.app.api.ServicePlugin) PUT(javax.ws.rs.PUT) ParameterValue(org.ligoj.app.model.ParameterValue) HashMap(java.util.HashMap) Node(org.ligoj.app.model.Node) NodeVo(org.ligoj.app.api.NodeVo)

Example 3 with ParameterValue

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;
}
Also used : ParameterValue(org.ligoj.app.model.ParameterValue) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with ParameterValue

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);
}
Also used : ParameterValue(org.ligoj.app.model.ParameterValue)

Example 5 with ParameterValue

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));
}
Also used : ParameterValue(org.ligoj.app.model.ParameterValue) Test(org.junit.jupiter.api.Test) AbstractAppTest(org.ligoj.app.AbstractAppTest)

Aggregations

ParameterValue (org.ligoj.app.model.ParameterValue)35 Test (org.junit.jupiter.api.Test)28 AbstractAppTest (org.ligoj.app.AbstractAppTest)27 Parameter (org.ligoj.app.model.Parameter)16 ArrayList (java.util.ArrayList)7 Node (org.ligoj.app.model.Node)6 HashMap (java.util.HashMap)5 Subscription (org.ligoj.app.model.Subscription)4 Date (java.util.Date)3 List (java.util.List)3 Map (java.util.Map)3 Function (java.util.function.Function)3 Collectors (java.util.stream.Collectors)3 NodeStatus (org.ligoj.app.api.NodeStatus)3 NodeVo (org.ligoj.app.api.NodeVo)3 Collection (java.util.Collection)2 Entry (java.util.Map.Entry)2 Optional (java.util.Optional)2 BiFunction (java.util.function.BiFunction)2 CacheRemoveAll (javax.cache.annotation.CacheRemoveAll)2