// Check the validity of a single field of a schema based on the supplied context. The value of this key should be in the context as well // as any other properties needed to evaluation validity. If the schema or field do no exist, false will be returned. // @param schemaId schema identifier // @param key input key // @param context Map of keys/values to validate against // @return a boolean indicating whether the code exists for the the passed schema field public bool isContextValid(String schemaId, String key, Dictionary <String, String> context) { // first get the algorithm StagingSchema schema = getSchema(schemaId); if (schema == null) { return(false); } // get the table id from the schema IInput input = null; if (!schema.getInputMap().TryGetValue(key, out input)) { input = null; } if (input == null) { return(false); } // missing context will always return false if (context == null || context.Count == 0) { return(false); } // all context input needs to be trimmed Dictionary <String, String> testContext = new Dictionary <String, String>(20, StringComparer.Ordinal); foreach (KeyValuePair <String, String> entry in context) { testContext[entry.Key] = (entry.Value != null ? entry.Value.Trim() : ""); } // if the input specifies a table for validation, test against it if (input.getTable() != null) { ITable table = getTable(input.getTable()); return(table != null && (DecisionEngineFuncs.matchTable(table, testContext) != null)); } return(true); }
public void verifyInputs() { HashSet <String> errors = new HashSet <String>(); foreach (String id in _STAGING.getSchemaIds()) { StagingSchema schema = _STAGING.getSchema(id); // loop over all the inputs returned by processing the schema and make sure they are all part of the main list of inputs on the schema foreach (String input in _STAGING.getInputs(schema)) { if (!schema.getInputMap().ContainsKey(input)) { errors.Add("Error processing schema " + schema.getId() + ": Table input '" + input + "' not in master list of inputs"); } } } assertNoErrors(errors, "input values"); }
// Stage the passed case. // @param data all input values are passed through this database // @return the same StagingData with output values filled in public StagingData stage(StagingData data) { // first clear out schema/output/errors/path data.setSchemaId(null); data.setOutput(new Dictionary <String, String>(100, StringComparer.Ordinal)); data.setErrors(new List <Error>(100)); data.setPath(new List <String>(100)); // make sure site and histology are supplied if (data.getInput(StagingData.PRIMARY_SITE_KEY) == null || data.getInput(StagingData.HISTOLOGY_KEY) == null) { data.setResult(StagingData.Result.FAILED_MISSING_SITE_OR_HISTOLOGY); return(data); } // get the schema; if a single schema is not found, return right away with an error List <StagingSchema> schemas = lookupSchema(new SchemaLookup(data.getInput())); if (schemas.Count != 1) { if (schemas.Count == 0) { data.setResult(StagingData.Result.FAILED_NO_MATCHING_SCHEMA); } else { data.setResult(StagingData.Result.FAILED_MULITPLE_MATCHING_SCHEMAS); } return(data); } StagingSchema schema = null; if (schemas.Count > 0) { schema = schemas[0]; } // add schema id to result data.setSchemaId(schema.getId()); // copy the input into a new context Dictionary <String, String> context = new Dictionary <String, String>(data.getInput(), StringComparer.Ordinal); // make sure all supplied inputs are defined in the definition foreach (KeyValuePair <String, String> entry in context) { if (!schema.getInputMap().ContainsKey(entry.Key)) { data.addError(new Error.ErrorBuilder(Error.Type.UNKNOWN_INPUT).message("Unknown input key supplied: " + entry.Key).key(entry.Key).build()); } } if (data.getErrors().Count > 0) { data.setResult(StagingData.Result.FAILED_INVALID_INPUT); return(data); } // add context variables addContextKeys(context); // check that year of DX is valid if (!isContextValid(schema.getId(), StagingData.YEAR_DX_KEY, context)) { data.setResult(StagingData.Result.FAILED_INVALID_YEAR_DX); return(data); } // perform the staging Result result = _engine.process(schemas[0].getId(), context); // remove the context variables removeContextKeys(context); // set the staging data result based on the Result returned from the DecisionEngine if (Result.Type.FAILED_INPUT == result.getType()) { data.setResult(StagingData.Result.FAILED_INVALID_INPUT); } else { data.setResult(StagingData.Result.STAGED); } // remove the original input keys from the resulting context; in addition, we want to remove any input keys // from the resulting context that were set with a default value; to accomplish this remove all keys that are // defined as input in the selected schema foreach (KeyValuePair <String, String> entry in data.getInput()) { context.Remove(entry.Key); } foreach (StagingSchemaInput input in schemas[0].getInputs()) { context.Remove(input.getKey()); } // add the results to the data card data.setOutput(result.getContext()); data.setErrors(result.getErrors()); data.setPath(result.getPath()); return(data); }
// Initialize a schema. // @param schema schema entity // @return initialized schema entity public static StagingSchema initSchema(StagingSchema schema) { // parse the schema selection ranges if (schema.getSchemaSelectionTable() == null) { throw new System.InvalidOperationException("Schemas must have a schema selection table."); } // store the inputs in a Map that can searched more efficiently if (schema.getInputs() != null) { Dictionary <String, IInput> parsedInputMap = new Dictionary <String, IInput>(); foreach (StagingSchemaInput input in schema.getInputs()) { // verify that all inputs contain a key if (input.getKey() == null) { throw new System.InvalidOperationException("All input definitions must have a 'key' defined."); } parsedInputMap[input.getKey()] = input; } schema.setInputMap(parsedInputMap); } // store the outputs in a Map that can searched more efficiently if (schema.getOutputs() != null) { Dictionary <String, IOutput> parsedOutputMap = new Dictionary <String, IOutput>(); foreach (StagingSchemaOutput output in schema.getOutputs()) { // verify that all inputs contain a key if (output.getKey() == null) { throw new System.InvalidOperationException("All output definitions must have a 'key' defined."); } parsedOutputMap[output.getKey()] = output; } schema.setOutputMap(parsedOutputMap); } // make sure that the mapping initial context does not set a value for an input field if (schema.getMappings() != null) { foreach (StagingMapping mapping in schema.getMappings()) { if (mapping.getInitialContext() != null) { foreach (StagingKeyValue kv in mapping.getInitialContext()) { if (schema.getInputMap().ContainsKey(kv.getKey())) { throw new System.InvalidOperationException("The key '" + kv.getKey() + "' is defined in an initial context, but that is not allowed since it is also defined as an input."); } } } } } return(schema); }