/// <summary> /// Save the current value of class properties to overwrite the record of variables identified by the parameters key. /// It uses the configured <see cref="IValuesWriter">writer</see>. /// </summary> /// <param name="parametersKey">Parameters key of the record to save</param> public string SaveParameters(string parametersKey) { string returnMessage = null; if (_containingObject == null) { throw new Exception("The ContainingObject is not configured"); } //if (_parametersSet == null) throw new Exception("The parameters set is not defined: invoke 'LoadParameters()' first."); if (_writer == null) { throw new Exception("The writer is not configured"); } if (_reader == null) { throw new Exception("The reader is not configured"); } _parametersSet = _reader.ReadValues(); IKeyValue keyValue = _parametersSet.Values.Keys.Where(k => k.Name.Equals(parametersKey)).SingleOrDefault(); if (keyValue == null) { int maxId = 0; if (_parametersSet.Values.Count > 0) { maxId = _parametersSet.Values.Keys.Select(k => k.Id).Max(); } keyValue = DataTypeFactory.NewKeyValue(maxId + 1, parametersKey, parametersKey); _parametersSet.Values.Add(keyValue, new Dictionary <VarInfo, List <string> >()); foreach (var varInfo in _parametersSet.Parameters) { _parametersSet.Values[keyValue].Add(varInfo, null); } } else { returnMessage = "Overwriting key value '" + parametersKey + "'."; } foreach (var varInfo in _parametersSet.Parameters) { PropertyInfo propertyInfo = _containingObject.PropertiesDescription[varInfo.Name]; if (propertyInfo.PropertyType != varInfo.ValueType.TypeForCurrentValue) { throw new Exception("Type for property '" + propertyInfo.Name + "' (" + propertyInfo.PropertyType + ") is not coherent with the corresponding " + "VarInfo type (" + varInfo.ValueType.TypeForCurrentValue + ")"); } MethodInfo getMethod = propertyInfo.GetGetMethod(); object propertyValue = getMethod.Invoke(_containingObject, new object[0]); List <string> serializedValue = varInfo.ValueType.Converter.ParseValueForMPE(propertyValue); _parametersSet.Values[keyValue][varInfo] = serializedValue; } _writer.WriteValues(_parametersSet); return(returnMessage); }
/// <summary> /// Reads the values of parameter set from the xml file. /// </summary> /// <returns>parameters set</returns> /// <exception cref="InvalidOperationException">Xml path has not been /// defined.</exception> /// <exception cref="InvalidDataException">The data have not the correct /// format.</exception> /// <exception cref="IOException">An I/O error is occured.</exception> //public IParametersSet ReadValues() protected override IParametersSet InternalReadValues() { // Check source existence if ((FilePath == null) || (!File.Exists(FilePath))) { throw new InvalidOperationException( "Xml file missing or not specified"); } // Read the stream /* 8/6/2012 - DFa - refactoring MPE - DCC - begin */ /* old - begin */ //IParametersSet ps = DataTypeFactory.NewParametersSet(null, null, null); //ISetDescriptor sd = DataTypeFactory.NewSetDescriptor(null, null, null, null, null); //ps.Descriptor = sd; /* old - end */ IParametersSet ps; ISetDescriptor sd; string descriptorComponent = null; string descriptorModel = null; string descriptorKeyType = null; string descriptorURL = null; string descriptorDescription = null; List <VarInfo> tmpViList = new List <VarInfo>(); Dictionary <IKeyValue, Dictionary <VarInfo, List <string> > > temporaryValues = new Dictionary <IKeyValue, Dictionary <VarInfo, List <string> > >(); /* 8/6/2012 - DFa - refactoring MPE - DCC - end */ // Create the parser // ----------------- // Create the XmlSchemaSet class XmlSchemaSet sc = new XmlSchemaSet(); // Add the schema to the collection string schemaPath = Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar + SCHEMA_FOLDER + Path.DirectorySeparatorChar + "ValuesXmlSchema.xsd"; if (!File.Exists(schemaPath)) { schemaPath = Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar + "ValuesXmlSchema.xsd"; if (!File.Exists(schemaPath)) { throw new IOException("Could not find ValuesXmlSchema.xsd"); } } sc.Add("http://CRA.ParameterEditor.org/ValuesXmlSchema.xsd", schemaPath); // Set the validation settings XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.Schema; settings.Schemas = sc; // Create the XmlReader and XmlDocument objects XmlReader reader = XmlReader.Create(FilePath, settings); XmlDocument doc = new XmlDocument(); try { // Map // Parameter name - parameter Dictionary <string, VarInfo> nameParameterMap = new Dictionary <string, VarInfo>(); // Read the template doc.Load(reader); // Fill data structure foreach (XmlNode elem in doc.DocumentElement.ChildNodes) { if (elem.NodeType != XmlNodeType.Element) { continue; } switch (elem.Name) { // Description element case E_DESCRIPTION: { foreach (XmlNode descChild in elem.ChildNodes) { if (descChild.NodeType != XmlNodeType.Element) { continue; } switch (descChild.Name) { /* 8/6/2012 - DFa - refactoring MPE - DCC - begin */ /* old - begin */ //case E_DESC_E_NAMESPACE: // sd.Component = descChild.InnerText; // break; //case E_DESC_E_TYPENAME: // sd.Model = descChild.InnerText; // break; //case E_DESC_E_PARAMETER_KEY: // sd.KeyType = descChild.InnerText; // break; //case E_DESC_E_URL: // sd.URL = descChild.InnerText; // break; //case E_DESC_E_DESCRIPTION: // sd.Description = descChild.InnerText; // break; /* old - end */ case E_DESC_E_NAMESPACE: descriptorComponent = descChild.InnerText; break; case E_DESC_E_TYPENAME: descriptorModel = descChild.InnerText; break; case E_DESC_E_PARAMETER_KEY: descriptorKeyType = descChild.InnerText; break; case E_DESC_E_URL: descriptorURL = descChild.InnerText; break; case E_DESC_E_DESCRIPTION: descriptorDescription = descChild.InnerText; break; /* 8/6/2012 - DFa - refactoring MPE - DCC - end */ } } } break; // Definitions element case E_VARINFO_ATTRIBUTES: { // Temporary variables VarInfo tmpVi = null; /* 8/6/2012 - DFa - refactoring MPE - DCC - begin */ /* old - begin */ //List<VarInfo> tmpViList = new List<VarInfo>(); /* old - end */ /* 8/6/2012 - DFa - refactoring MPE - DCC - end */ int index = 0; foreach (XmlNode viAttChild in elem.ChildNodes) { if (viAttChild.NodeType != XmlNodeType.Element) { continue; } switch (viAttChild.Name) { case E_VIATT_E_VARINFO: { tmpVi = new VarInfo(); tmpVi.Id = index++; tmpVi.Name = viAttChild.Attributes[ E_VIATT_E_VARINFO_A_NAME].Value; foreach (XmlNode vInfoChild in viAttChild.ChildNodes) { if (vInfoChild.NodeType != XmlNodeType.Element) { continue; } switch (vInfoChild.Name) { case E_VIATT_E_DESCRIPTION: tmpVi.Description = vInfoChild.InnerText; break; case E_VIATT_E_MAXVALUE: tmpVi.MaxValue = double.Parse(vInfoChild.InnerText, NumberFormatInfo.InvariantInfo); break; case E_VIATT_E_MINVALUE: tmpVi.MinValue = double.Parse(vInfoChild.InnerText, NumberFormatInfo.InvariantInfo); break; case E_VIATT_E_DEFAULTVALUE: tmpVi.DefaultValue = double.Parse(vInfoChild.InnerText, NumberFormatInfo.InvariantInfo); break; case E_VIATT_E_TYPE: VarInfo.ParseValueType(vInfoChild.InnerText, tmpVi); break; case E_VIATT_E_UNITS: tmpVi.Units = vInfoChild.InnerText; break; case E_VIATT_E_URL: tmpVi.URL = vInfoChild.InnerText; break; } } tmpViList.Add(tmpVi); nameParameterMap.Add(tmpVi.Name, tmpVi); } break; } } /* 8/6/2012 - DFa - refactoring MPE - DCC - begin */ /* old - begin */ //ps.Parameters = tmpViList.ToArray(); /* old - end */ /* 8/6/2012 - DFa - refactoring MPE - DCC - end */ } break; // Values element case E_VALUES: { // Temporary variables /* 8/6/2012 - DFa - refactoring MPE - DCC - begin */ /* old - begin */ //ps.Values = new Dictionary<IKeyValue, Dictionary<VarInfo, List<string>>>(); /* old - end */ /* 8/6/2012 - DFa - refactoring MPE - DCC - end */ Dictionary <VarInfo, List <string> > tmpVals = null; IKeyValue tmpKv = null; VarInfo tmpParam = null; List <string> tmpValuesList = null; int index = 0; foreach (XmlNode valsChild in elem.ChildNodes) { if (valsChild.NodeType != XmlNodeType.Element) { continue; } switch (valsChild.Name) { case E_VALS_E_KEYVALUE: { /* 8/6/2012 - DFa - refactoring MPE - DCC - begin */ /* old - begin */ //tmpKv = DataTypeFactory.NewKeyValue(0, null, null); //tmpKv.Id = index++; //tmpKv.Name = valsChild.Attributes[ // E_VALS_E_KEYVALUE_A_NAME].Value; /* old - end */ if (tmpKv != null) { tmpKv = DataTypeFactory.NewKeyValue(index++, valsChild.Attributes[E_VALS_E_KEYVALUE_A_NAME].Value, tmpKv.Description); } else { tmpKv = DataTypeFactory.NewKeyValue(index++, valsChild.Attributes[E_VALS_E_KEYVALUE_A_NAME].Value, null); } /* 8/6/2012 - DFa - refactoring MPE - DCC - end */ tmpVals = new Dictionary <VarInfo, List <string> >(); foreach (XmlNode keyChild in valsChild.ChildNodes) { if (keyChild.NodeType != XmlNodeType.Element) { continue; } switch (keyChild.Name) { case E_VALS_E_KV_E_DESCRIPTION: /* 8/6/2012 - DFa - refactoring MPE - DCC - begin */ /* old - begin */ //tmpKv.Description = keyChild.InnerText; /* old - end */ if (tmpKv != null) { tmpKv = DataTypeFactory.NewKeyValue(tmpKv.Id, tmpKv.Name, keyChild.InnerText); } else { tmpKv = DataTypeFactory.NewKeyValue(0, null, keyChild.InnerText); } /* 8/6/2012 - DFa - refactoring MPE - DCC - end */ break; case E_VALS_E_KV_E_PARAMETER: { tmpValuesList = new List <string>(); try { tmpParam = nameParameterMap[keyChild.Attributes[E_VALS_E_KV_E_PARAM_A_NAME].Value]; }catch (Exception) { throw new Exception("Parameter not found:'" + keyChild.Attributes[E_VALS_E_KV_E_PARAM_A_NAME].Value + "'"); } foreach (XmlNode paramChild in keyChild.ChildNodes) { if (paramChild.NodeType != XmlNodeType.Element) { continue; } switch (paramChild.Name) { case E_VALS_E_KV_E_VALUE: { string s = paramChild.InnerText; if (paramChild.Attributes.GetNamedItem(E_VALS_E_KV_E_KEY) != null) { // s = s + EditorEngine.E_VALS_E_KV_E_KEY_SEPARATOR + paramChild.Attributes.GetNamedItem(E_VALS_E_KV_E_KEY).Value; s = VarInfoValueTypes. ConcatenateKeyAndValue( paramChild.Attributes. GetNamedItem( E_VALS_E_KV_E_KEY).Value, s); } tmpValuesList.Add(s); break; } } } tmpVals.Add(tmpParam, tmpValuesList); } break; } } /* 8/6/2012 - DFa - refactoring MPE - DCC - begin */ /* old - begin */ //ps.Values.Add(tmpKv, tmpVals); /* old - end */ temporaryValues.Add(tmpKv, tmpVals); /* 8/6/2012 - DFa - refactoring MPE - DCC - end */ } break; } } } break; } } /* 8/6/2012 - DFa - refactoring MPE - DCC - begin */ sd = DataTypeFactory.NewSetDescriptor(descriptorComponent, descriptorModel, descriptorKeyType, descriptorURL, descriptorDescription); ps = DataTypeFactory.NewParametersSet(sd, tmpViList.ToArray(), temporaryValues); /* 8/6/2012 - DFa - refactoring MPE - DCC - end */ return(ps); } catch (Exception ex) { throw new InvalidDataException(ex.Message, ex); } finally { if (reader != null) { reader.Close(); } } }