bbende commented on a change in pull request #3536: NIFI-6380: Introduced the 
notion of Parameters and Parameter Contexts…
URL: https://github.com/apache/nifi/pull/3536#discussion_r294359875
 
 

 ##########
 File path: 
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/dao/impl/StandardParameterContextDAO.java
 ##########
 @@ -0,0 +1,231 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.web.dao.impl;
+
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.controller.ComponentNode;
+import org.apache.nifi.controller.FlowController;
+import org.apache.nifi.controller.ProcessorNode;
+import org.apache.nifi.controller.PropertyConfiguration;
+import org.apache.nifi.controller.flow.FlowManager;
+import org.apache.nifi.controller.service.ControllerServiceNode;
+import org.apache.nifi.controller.service.ControllerServiceState;
+import org.apache.nifi.groups.ProcessGroup;
+import org.apache.nifi.parameter.Parameter;
+import org.apache.nifi.parameter.ParameterContext;
+import org.apache.nifi.parameter.ParameterDescriptor;
+import org.apache.nifi.parameter.ParameterReference;
+import org.apache.nifi.parameter.ParameterReferenceManager;
+import org.apache.nifi.web.ResourceNotFoundException;
+import org.apache.nifi.web.api.dto.ParameterContextDTO;
+import org.apache.nifi.web.api.dto.ParameterDTO;
+import org.apache.nifi.web.dao.ParameterContextDAO;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class StandardParameterContextDAO implements ParameterContextDAO {
+    private FlowManager flowManager;
+
+    @Override
+    public boolean hasParameterContext(final String parameterContextId) {
+        return 
flowManager.getParameterContextManager().getParameterContext(parameterContextId)
 != null;
+    }
+
+    @Override
+    public void verifyCreate(final ParameterContextDTO parameterContextDto) {
+        verifyNoNamingConflict(parameterContextDto.getName());
+    }
+
+    @Override
+    public ParameterContext createParameterContext(final ParameterContextDTO 
parameterContextDto) {
+        final Set<Parameter> parameters = getParameters(parameterContextDto);
+        return flowManager.createParameterContext(parameterContextDto.getId(), 
parameterContextDto.getName(), parameters);
+    }
+
+    private Set<Parameter> getParameters(final ParameterContextDTO 
parameterContextDto) {
+        final Set<ParameterDTO> parameterDtos = 
parameterContextDto.getParameters();
+        if (parameterDtos == null) {
+            return Collections.emptySet();
+        }
+
+        return parameterContextDto.getParameters().stream()
+            .map(this::createParameter)
+            .collect(Collectors.toSet());
+    }
+
+    private Parameter createParameter(final ParameterDTO dto) {
+        final ParameterDescriptor descriptor = new 
ParameterDescriptor.Builder()
+            .name(dto.getName())
+            .description(dto.getDescription())
+            .sensitive(Boolean.TRUE.equals(dto.getSensitive()))
+            .build();
+
+        return new Parameter(descriptor, dto.getValue());
+    }
+
+    @Override
+    public ParameterContext getParameterContext(final String 
parameterContextId) {
+        final ParameterContext context = 
flowManager.getParameterContextManager().getParameterContext(parameterContextId);
+        if (context == null) {
+            throw new ResourceNotFoundException(String.format("Unable to find 
Parameter Context with id '%s'.", parameterContextId));
+        }
+
+        return context;
+    }
+
+    @Override
+    public Set<ParameterContext> getParameterContexts() {
+        return flowManager.getParameterContextManager().getParameterContexts();
+    }
+
+    @Override
+    public ParameterContext updateParameterContext(final ParameterContextDTO 
parameterContextDto) {
+        verifyUpdate(parameterContextDto, true);
+
+        final ParameterContext context = 
getParameterContext(parameterContextDto.getId());
+
+        if (parameterContextDto.getName() != null) {
+            final boolean conflict = 
flowManager.getParameterContextManager().getParameterContexts().stream()
+                .anyMatch(paramContext -> 
paramContext.getName().equals(parameterContextDto.getName()));
 
 Review comment:
   Should the stream filter out the id of the incoming DTO? 
   
   Right now if you specify the same name in the update request it will say its 
a conflict because another context already has the name, but really it should 
be allowed.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to