public static void Update(ICswResources CswResources, CswNbtConfigurationVariablesPageReturnUpdateSuccess Return, CswNbtDataContractConfigurationVariableResponseContainer Request) { CswNbtResources NbtResources = (CswNbtResources)CswResources; Return.Data = _updateConfigVars(NbtResources, Request); }
//_getConfigVars /// <summary> /// powers the webservice, which handles incoming updated config vars from server /// </summary> /// <param name="NbtResources">NbtResources object</param> /// <param name="varsFromServer">Encapsulates a collection of config vars form server</param> /// <returns></returns> private static CswNbtDataContractConfigurationUpdateSuccessResponse _updateConfigVars(CswNbtResources NbtResources, CswNbtDataContractConfigurationVariableResponseContainer varsFromServer) { CswNbtDataContractConfigurationUpdateSuccessResponse ret = new CswNbtDataContractConfigurationUpdateSuccessResponse(); Dictionary <string, string> updatedConfigVarsFromClient = new Dictionary <string, string>(); //add all the config vars into the dictionary //keyed by name. If the incoming array contains //more than one change to the same config var, //the last change 'wins'. foreach (CswNbtDataContractConfigurationVariableResponse thisResponse in varsFromServer.responseArray) { if (updatedConfigVarsFromClient.ContainsKey(thisResponse.VariableName)) { updatedConfigVarsFromClient[thisResponse.VariableName] = thisResponse.VariableValue; } else { updatedConfigVarsFromClient.Add(thisResponse.VariableName, thisResponse.VariableValue); } } CswTableUpdate CVTableUpdate = NbtResources.makeCswTableUpdate("update config vars", "configuration_variables"); DataTable CVDataTable = CVTableUpdate.getTable(); foreach (DataRow thisRow in CVDataTable.Rows) { string configVarName = thisRow[COL_VARIABLENAME].ToString(); if (updatedConfigVarsFromClient.ContainsKey(configVarName)) { string updatedConfigVarValue = updatedConfigVarsFromClient[configVarName]; string currentConfigVarValue = thisRow[COL_VARIABLEVALUE].ToString(); //make sure config var has been altered if (updatedConfigVarValue != currentConfigVarValue) { string configVarDataType = thisRow[COL_DATATYPE].ToString(); //now do type specific validation switch (configVarDataType) { case DATATYPE_INT: int configValueAsInt = CswConvert.ToInt32(updatedConfigVarValue); int configMinValue = CswConvert.ToInt32(thisRow[COL_CONSTRAINT]); //throw exception if the value is > minvalue if (configValueAsInt <= configMinValue) { throw new CswDniException(CswEnumErrorType.Warning, "The value of " + configVarName + " must be greater than " + configMinValue, "Config varible of INT datatype must be < " + configMinValue); } else { thisRow[COL_VARIABLEVALUE] = updatedConfigVarValue; } break; case DATATYPE_LIST: CswCommaDelimitedString listOptions = new CswCommaDelimitedString(thisRow[COL_CONSTRAINT].ToString()); if (false == listOptions.Contains(updatedConfigVarValue)) { string listOptionsString = listOptions.ToString(); throw new CswDniException(CswEnumErrorType.Warning, configVarName + " must be set to one of these options: " + listOptionsString, "Config variable " + configVarName + " cannot be set to " + currentConfigVarValue + " since it is not one of the predefined options " + listOptionsString); } else { thisRow[COL_VARIABLEVALUE] = updatedConfigVarValue; } break; case DATATYPE_BOOL: if (false == (updatedConfigVarValue == "0" || updatedConfigVarValue == "1")) { throw new CswDniException(CswEnumErrorType.Warning, configVarName + " must be set to 0 or 1", configVarName + " is of type BOOL, therefore it must be set to 0 or 1"); } else { thisRow[COL_VARIABLEVALUE] = updatedConfigVarValue; } break; //for string type there is no config var specific validation default: thisRow[COL_VARIABLEVALUE] = updatedConfigVarValue; break; } //switch by ConfigVarDatatype } // if config var has been updated } //if this config var was returned from client } //for each dataRow //commit changes CVTableUpdate.update(CVDataTable); //if any errors are encountered, the exception will throw an error message //so there is no execution path where we return false ret.success = true; return(ret); }