Github user jfrazee commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/1312#discussion_r102500181
  
    --- Diff: 
nifi-nar-bundles/nifi-ccda-bundle/nifi-ccda-processors/src/main/java/org/apache/nifi/processors/ccda/ExtractCCDAAttributes.java
 ---
    @@ -0,0 +1,383 @@
    +/*
    + * 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.processors.ccda;
    +
    +import java.io.IOException;
    +import java.io.InputStream;
    +import java.io.OutputStream;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.HashSet;
    +import java.util.LinkedHashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Map.Entry;
    +import java.util.Properties;
    +import java.util.Set;
    +import java.util.TreeMap;
    +
    +import org.apache.commons.jexl3.JexlBuilder;
    +import org.apache.commons.jexl3.JexlContext;
    +import org.apache.commons.jexl3.JexlEngine;
    +import org.apache.commons.jexl3.JexlExpression;
    +import org.apache.commons.jexl3.MapContext;
    +import org.apache.commons.lang3.StringUtils;
    +import org.apache.nifi.annotation.behavior.InputRequirement;
    +import org.apache.nifi.annotation.behavior.InputRequirement.Requirement;
    +import org.apache.nifi.annotation.behavior.SideEffectFree;
    +import org.apache.nifi.annotation.behavior.SupportsBatching;
    +import org.apache.nifi.annotation.documentation.CapabilityDescription;
    +import org.apache.nifi.annotation.documentation.Tags;
    +import org.apache.nifi.components.PropertyDescriptor;
    +import org.apache.nifi.flowfile.FlowFile;
    +import org.apache.nifi.flowfile.attributes.CoreAttributes;
    +import org.apache.nifi.processor.AbstractProcessor;
    +import org.apache.nifi.processor.ProcessContext;
    +import org.apache.nifi.processor.ProcessSession;
    +import org.apache.nifi.processor.ProcessorInitializationContext;
    +import org.apache.nifi.processor.Relationship;
    +import org.apache.nifi.processor.exception.ProcessException;
    +import org.apache.nifi.processor.io.OutputStreamCallback;
    +import org.apache.nifi.processor.util.StandardValidators;
    +import org.eclipse.emf.common.util.Diagnostic;
    +import org.openhealthtools.mdht.uml.cda.CDAPackage;
    +import org.openhealthtools.mdht.uml.cda.ClinicalDocument;
    +import org.openhealthtools.mdht.uml.cda.ccd.CCDPackage;
    +import org.openhealthtools.mdht.uml.cda.consol.ConsolPackage;
    +import org.openhealthtools.mdht.uml.cda.hitsp.HITSPPackage;
    +import org.openhealthtools.mdht.uml.cda.ihe.IHEPackage;
    +import org.openhealthtools.mdht.uml.cda.util.CDAUtil;
    +import org.openhealthtools.mdht.uml.cda.util.CDAUtil.ValidationHandler;
    +
    +import com.google.gson.Gson;
    +import com.google.gson.GsonBuilder;
    +
    +@SideEffectFree
    +@SupportsBatching
    +@InputRequirement(Requirement.INPUT_REQUIRED)
    +@Tags({"CCDA", "healthcare", "extract", "JSON", "attributes"})
    +@CapabilityDescription("Extracts information from an Consolidated CDA 
formatted FlowFile and provides JSON as FlowFile content "
    +        + "and individual attributes as FlowFile attributes. The 
attributes are named as <Parent> <dot> <Key>. "
    +        + "If the Parent is repeating, the naming will be <Parent> 
<underscore> <Parent Index> <dot> <Key>. "
    +        + "For example, section.act_07.observation.name=Essential 
hypertension")
    +public class ExtractCCDAAttributes extends AbstractProcessor {
    +
    +    private static final String APPLICATION_JSON = "application/json";
    +    private static final char FIELD_SEPARATOR = '@';
    +    private static final char KEY_VALUE_SEPARATOR = '#';
    +
    +    private Map<String, Map<String, String>> processMap = new 
LinkedHashMap<String, Map<String, String>>(); // stores mapping data for Parser
    +    private List<String> timingStats = new ArrayList<String>(); // stores 
timing statistics
    +    private Map<String, String> attributes = new TreeMap<String, 
String>(); // stores CDA attributes
    +    private JexlEngine jexl = null; // JEXL Engine to execute code for 
mapping
    +    private JexlContext jexlCtx = null; // JEXL Context to hold element 
being processed
    +
    +    private List<PropertyDescriptor> properties;
    +    private Set<Relationship> relationships;
    +
    +    /**
    +     * SKIP_VALIDATION - Indicates whether to validate the CDA document 
after loading.
    +     * if true and the document is not valid, then ProcessException will 
be thrown
    +     */
    +    public static final PropertyDescriptor SKIP_VALIDATION = new 
PropertyDescriptor.Builder().name("skip-validation")
    +            .displayName("Skip Validation").description("Whether or not to 
validate CDA message values").required(true)
    +            .allowableValues("true", 
"false").defaultValue("true").addValidator(StandardValidators.BOOLEAN_VALIDATOR)
    +            .build();
    +
    +    /**
    +     * PRETTY_PRINTING - Choice of whether generated JSON in the FileFlow 
content is
    +     * pretty printed with new line and tabs or just a continuous string 
of characters
    +     */
    +    public static final PropertyDescriptor PRETTY_PRINTING = new 
PropertyDescriptor.Builder().name("pretty-printing")
    +            .displayName("Pretty Printing").description("Whether or not to 
Pretty Print JSON output").required(true)
    +            .allowableValues("true", 
"false").defaultValue("false").addValidator(StandardValidators.BOOLEAN_VALIDATOR)
    +            .build();
    +
    +    /**
    +     * REL_SUCCESS - Value to be returned in case the processor succeeds
    +     */
    +    public static final Relationship REL_SUCCESS = new 
Relationship.Builder()
    +            .name("success")
    +            .description("A FlowFile is routed to this relationship if it 
is properly parsed as CDA and its content stored in FlowFile")
    +            .build();
    +
    +    /**
    +     * REL_FAILURE - Value to be returned in case the processor fails
    +     */
    +    public static final Relationship REL_FAILURE = new 
Relationship.Builder()
    +            .name("failure")
    +            .description("A FlowFile is routed to this relationship if it 
cannot be parse CDA and store content to FlowFile.")
    +            .build();
    +
    +    @Override
    +    protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
    +        return properties;
    +    }
    +
    +    @Override
    +    public Set<Relationship> getRelationships() {
    +        return relationships;
    +    }
    +
    +    @Override
    +    protected void init(final ProcessorInitializationContext context) {
    +        getLogger().info("Loading packages");
    +
    +        relationships = new HashSet<>();
    +        relationships.add(REL_SUCCESS);
    +        relationships.add(REL_FAILURE);
    +
    +        properties = new ArrayList<>();
    +        properties.add(SKIP_VALIDATION);
    +        properties.add(PRETTY_PRINTING);
    +
    +        long start = System.currentTimeMillis();
    +
    +        // Load required MDHT packages
    +        System.setProperty( 
"org.eclipse.emf.ecore.EPackage.Registry.INSTANCE",
    +                "org.eclipse.emf.ecore.impl.EPackageRegistryImpl" );
    +        CDAPackage.eINSTANCE.eClass();
    +        HITSPPackage.eINSTANCE.eClass();
    +        CCDPackage.eINSTANCE.eClass();
    +        ConsolPackage.eINSTANCE.eClass();
    +        IHEPackage.eINSTANCE.eClass();
    +        timingStats.add(String.format("Loaded packages in %d ms", 
System.currentTimeMillis() - start));
    +
    +        // Initialize JEXL
    +        jexl = new 
JexlBuilder().cache(1024).debug(false).silent(true).strict(false).create();
    +        jexlCtx = new MapContext();
    +
    +        getLogger().info("Loading mappings");
    +        loadMappings(); // Load CDA mappings for parser
    +
    +    }
    +
    +    @Override
    +    public void onTrigger(final ProcessContext context, final 
ProcessSession session) {
    +        getLogger().info("Processing CCDA");
    +
    +        FlowFile flowFile = session.get();
    +        if ( flowFile == null ) {
    +            return;
    +        }
    +
    +        if(processMap.isEmpty()) {
    +            getLogger().error("Process Mapping is not loaded");
    +            session.transfer(flowFile, REL_FAILURE);
    +            return;
    +        }
    +
    +        final Boolean skipValidation = 
context.getProperty(SKIP_VALIDATION).asBoolean();
    +        final Boolean prettyPrinting = 
context.getProperty(PRETTY_PRINTING).asBoolean();
    +
    +        long start = System.currentTimeMillis(), total = start;
    +
    +        ClinicalDocument cd = loadDocument(session.read(flowFile), 
skipValidation); // Load and optionally validate CDA document
    +
    +        start = System.currentTimeMillis();
    +        getLogger().info("Processing elements");
    +        Map<String, Object> elements = processElement(null, cd); // 
Process CDA element using mapping data
    +
    +        start = System.currentTimeMillis();
    +        getLogger().info("Generating JSON");
    +
    +        // Initialize GSON with optional pretty printing
    +        Gson gson = prettyPrinting ? new 
GsonBuilder().setPrettyPrinting().create() : new GsonBuilder().create();
    +        String json = gson.toJson(elements);
    +        timingStats.add(String.format("JSON created in %d ms", 
System.currentTimeMillis() - start));
    +        timingStats.add(String.format("CCDA Processed in %d ms", 
System.currentTimeMillis() - total));
    +
    +        logDetails(json);
    +
    +        flowFile = session.write(flowFile, new OutputStreamCallback() {
    +
    +            @Override
    +            public void process(OutputStream output) throws IOException {
    +                output.write(json.getBytes("UTF-8"));
    +            }
    +        });
    +        flowFile = session.putAttribute(flowFile, 
CoreAttributes.MIME_TYPE.key(), APPLICATION_JSON);
    +        flowFile = session.putAllAttributes(flowFile, attributes);
    +
    +        session.transfer(flowFile, REL_SUCCESS);
    +
    +    }
    +
    +    /**
    +     * Process elements children based on the parser mapping.
    +     * Any String values are added to attributes and JSON map
    +     * For List, the processList method is called to iterate and process
    +     * For an Object this method is called recursively
    +     * While adding to the attributes the key is prefixed by parent
    +     * @param parent    Parent key for this element, used as a prefix for 
attribute key
    +     * @param element   Element to be processed
    +     * @return          Map of processed data, value can contain String or 
Map of Strings
    +     */
    +    protected Map<String, Object> processElement(String parent, Object 
element) {
    +        long start = System.currentTimeMillis();
    +
    +        Map<String, Object> map = new LinkedHashMap<String, Object>();
    +        String name = element.getClass().getName();
    +        Map<String, String> jexlMap = (HashMap<String, String>) 
processMap.get(name); // get JEXL mappings for this element
    +        getLogger().debug("Processing " + name);
    +
    +        if (jexlMap == null) {
    +            getLogger().info("Missing mapping for element " + name);
    --- End diff --
    
    Seems like this should be a warn() or error(). What's the impact here/what 
happens if this returns null? Seems like it'll mean addElement() never gets 
called so no attributes will be added; i.e., if there's a single missing 
mapping the processing short circuits but it still transfers to REL_SUCCESS. Is 
this right?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to