/// <summary> Gets all ValueSets for this Mappings instance.</summary> /// <param name="inherit">true to include ValueSets from the Mappings ancestry in /// the returned array, false to include only ValueSets defined by this /// Mappings object /// </param> public ValueSet[] GetValueSets(bool inherit) { Hashtable results = new Hashtable(); Mappings m = this; do { if (m.fValueSets != null) { foreach (ValueSet vs in m.fValueSets.Values) { if (vs != null && !results.ContainsKey(vs.fId)) { results[vs.fId] = vs; } } } m = m.Parent; } while (m != null && inherit); ValueSet[] arr = new ValueSet[results.Count]; results.Values.CopyTo(arr, 0); return arr; }
public virtual ValueSet Copy( Mappings newParent ) { ValueSet copy = new ValueSet( fId, fTitle ); if ( fNode != null && newParent.fNode != null ) { XmlElement newNode = (XmlElement) newParent.fNode.OwnerDocument.ImportNode( fNode, true ); newParent.fNode.AppendChild( newNode ); copy.fNode = newNode; } // Copy the ValueSetEntry's ValueSetEntry[] entries = Entries; for ( int i = 0; i < entries.Length; i++ ) { copy.Define( entries[i].Name, entries[i].Value, entries[i].Title ); } if (fDefaultAppEntry != null) { copy.SetAppDefault(fDefaultAppEntry.Name, fRenderAppDefaultIfNull); } if (fDefaultSifEntry != null) { copy.SetSifDefault(fDefaultSifEntry.Value, fRenderSifDefaultIfNull); } return copy; }
/// <summary> Add a ValueSet to this Mappings instance</summary> public void AddValueSet(ValueSet vset) { if (fValueSets == null) { fValueSets = new Dictionary<String, ValueSet>(); } fValueSets[vset.fId] = vset; if (vset.fNode == null && fNode != null) { // Create a new <valueset> vset.fNode = fNode.OwnerDocument.CreateElement("valueset"); vset.fNode.SetAttribute("id", vset.fId); if (vset.fTitle != null) { vset.fNode.SetAttribute("title", vset.fTitle); } fNode.AppendChild(vset.fNode); // Add <value> elements to the <valueset>... ValueSetEntry[] entries = vset.Entries; for (int i = 0; i < entries.Length; i++) { entries[i].Node = fNode.OwnerDocument.CreateElement("value"); entries[i].Node.SetAttribute("name", entries[i].Name); if (entries[i].Title != null) { entries[i].Node.SetAttribute("title", entries[i].Title); } entries[i].Node.InnerText = entries[i].Value; vset.fNode.AppendChild(entries[i].Node); } } }
/// <summary> Parse the <c><value></c> children of a <c><valueset></c> /// element. /// /// </summary> /// <param name="element">A DOM <i>XmlElement</i> encapsulating a <c><valueset></c> element /// </param> /// <param name="parent">The parent ValueSet instance /// </param> protected internal void PopulateValueSet(XmlElement element, ValueSet parent) { foreach (XmlElement n in new XmlUtils.XmlElementEnumerator(element)) { if( String.Compare( "value", n.Name, true ) == 0 ) { String name = GetAttribute( n, "name"); if(name==null) { throw new AdkConfigException( "<value> name= attribute is required" ); } String sifValue = n.InnerText; if (String.IsNullOrEmpty(sifValue)) { throw new AdkConfigException("<value>SIF Value is Required</value>"); } String title = n.GetAttribute("title"); parent.Define(name, sifValue, title, n); String def = n.GetAttribute( "default"); if(!string.IsNullOrEmpty( def ) ) { String ifNull = n.GetAttribute( "ifnull"); bool renderIfDefault = false; if (!String.IsNullOrEmpty( ifNull)) { // cool phrase! renderIfDefault = String.Compare( ifNull, "default", true) == 0; } bool inboundDefault = false; bool outboundDefault = false; if ( String.Compare( def, "both", true ) == 0 || String.Compare( def, "true", true ) == 0 ) { inboundDefault = true; outboundDefault = true; } if (!inboundDefault && String.Compare(def, "inbound", true) == 0 ) { inboundDefault = true; } if (!outboundDefault && String.Compare(def, "outbound", true) == 0) { outboundDefault = true; } if (inboundDefault) { parent.SetAppDefault(name, renderIfDefault); } if (outboundDefault) { parent.SetSifDefault(sifValue, renderIfDefault); } } } } }
protected internal void Populate(XmlElement node, Mappings parent) { // TODO: We should probably be using a GetElementsby name query here // I think the java implementation might have been doing a recursive search here foreach (XmlElement n in new XmlUtils.XmlElementEnumerator(node)) { if (n.Name.ToUpper() == XML_MAPPINGS.ToUpper()) { // Get the ID string id = n.GetAttribute("id"); // Create a new child Mappings object Mappings mappings = new Mappings(parent, id); mappings.XmlElement = n; if (parent.fChildren == null) { parent.fChildren = new Dictionary<String, Mappings>(); } parent.fChildren[mappings.Id] = mappings; // Set a SifVersion filter if present string ver = n.GetAttribute("sifVersion"); if (ver.Trim().Length > 0) { mappings.SetSIFVersionFilter(ver); } // Set a ZoneId filter if present string zoneIds = n.GetAttribute("zoneId"); if (zoneIds.Trim().Length > 0) { mappings.SetZoneIdFilter(zoneIds); } // Set a SourceId filter if present string sourceIds = n.GetAttribute("sourceId"); if (sourceIds.Trim().Length > 0) { mappings.SetSourceIdFilter(sourceIds); } // Populate the Mappings object with rules Populate(n, mappings); } else { if (n.Name.ToUpper() == XML_OBJECT.ToUpper()) { if (n.ParentNode.Name == XML_MAPPINGS) { string obj = n.GetAttribute(XML_OBJECT); if (obj == null) { throw new AdkConfigException ("<object> element must have an object attribute"); } ObjectMapping om = new ObjectMapping(obj); om.XmlElement = n; parent.AddRules(om, false); PopulateObject(n, om); } } else { if (n.Name.ToUpper() == "PROPERTY") { if (n.ParentNode.Name == XML_MAPPINGS) { parent.SetProperty (n.GetAttribute(AdkXmlConstants.Property.NAME), n.GetAttribute(AdkXmlConstants.Property.VALUE)); } } else { if (n.Name.ToUpper() == "VALUESET") { if (n.ParentNode.Name == XML_MAPPINGS) { ValueSet set_Renamed = new ValueSet (n.GetAttribute("id"), n.GetAttribute("title"), n); parent.AddValueSet(set_Renamed); PopulateValueSet(n, set_Renamed); } } else { Populate(n, parent); } } } } } }
/// <summary> Remove a ValueSet from this Mappings instance. /// /// If the ValueSet has a DOM XmlElement attached to it, it is removed from its /// parent XmlElement and dereferenced. /// /// </summary> /// <param name="vset">The ValueSet to remove /// </param> public void RemoveValueSet(ValueSet vset) { if (fValueSets != null) { if (vset.fNode != null) { // Remove the ValueSet's node from its parent's DOM XmlElement vset.fNode.ParentNode.RemoveChild(vset.fNode); // Remove all children and dereference the entrys' Nodes ValueSetEntry[] entries = vset.Entries; for (int i = 0; i < entries.Length; i++) { if (entries[i].Node != null && entries[i].Node.ParentNode != null) { entries[i].Node.ParentNode.RemoveChild(entries[i].Node); } entries[i].Node = null; } } // Remove from the lookup table fValueSets.Remove(vset.fId); // Dereference the node vset.fNode = null; } }
/** * Creates a set of mappings that operations can be applied to, such as * saving to a DOM or Agent.cfg. The results can be asserted by calling * {@see #assertMappings(Mappings)}. * * NOTE: This method returns an AgentConfig instance instead of a mappings * instance because there is no way set the Mappings instance on * AgentConfig. This might change in the future * * @return */ private AgentConfig createMappings() { Mappings root = fCfg.Mappings; // Remove the mappings being used root.RemoveChild( root.GetMappings( "Default" ) ); root.RemoveChild( root.GetMappings( "TestID" ) ); Mappings newMappings = root.CreateChild( "Test" ); // Add an object mapping ObjectMapping objMap = new ObjectMapping( "StudentPersonal" ); // Currently, the Adk code requires that an Object Mapping be added // to it's parent before fields are added. // We should re-examine this and perhaps fix it, if possible newMappings.AddRules( objMap ); objMap.AddRule( new FieldMapping( "FIELD1", "Name/FirstName" ) ); // Field 2 FieldMapping field2 = new FieldMapping( "FIELD2", "Name/LastName" ); field2.ValueSetID = "VS1"; field2.Alias = "ALIAS1"; field2.DefaultValue = "DEFAULT1"; MappingsFilter mf = new MappingsFilter(); mf.Direction = MappingDirection.Inbound; mf.SifVersion = SifVersion.SIF11.ToString(); field2.Filter = mf; objMap.AddRule( field2 ); // Field 3 test setting the XML values after it's been added to the // parent object (the code paths are different) FieldMapping field3 = new FieldMapping( "FIELD3", "Name/MiddleName" ); objMap.AddRule( field3 ); field3.ValueSetID = "VS2"; field3.Alias = "ALIAS2"; field3.DefaultValue = "DEFAULT2"; MappingsFilter mf2 = new MappingsFilter(); mf2.Direction = MappingDirection.Outbound; mf2.SifVersion = SifVersion.SIF15r1.ToString(); field3.Filter = mf2; field3.NullBehavior = MappingBehavior.IfNullDefault; OtherIdMapping oim = new OtherIdMapping( "ZZ", "BUSROUTE" ); FieldMapping field4 = new FieldMapping( "FIELD4", oim ); objMap.AddRule( field4 ); field4.DefaultValue = "Default"; field4.ValueSetID = "vs"; field4.Alias = "alias"; field4.DefaultValue = null; field4.ValueSetID = null; field4.Alias = null; field4.NullBehavior = MappingBehavior.IfNullSuppress; // Field4 tests the new datatype attribute FieldMapping field5 = new FieldMapping( "FIELD5", "Demographics/BirthDate" ); objMap.AddRule( field5 ); field5.DataType = SifDataType.Date; // Add a valueset translation ValueSet vs = new ValueSet( "VS1" ); newMappings.AddValueSet( vs ); // Add a few definitions for ( int a = 0; a < 10; a++ ) { vs.Define( "Value" + a, "SifValue" + a, "Title" + a ); } vs.Define( "AppDefault", "0000", "Default App Value" ); vs.SetAppDefault( "AppDefault", true ); vs.Define( "0000", "SifDefault", "Default Sif Value" ); vs.SetSifDefault( "SifDefault", false ); // Add a valueset translation vs = new ValueSet( "VS2" ); newMappings.AddValueSet( vs ); // Add a few definitions for ( int a = 0; a < 3; a++ ) { vs.Define( "q" + a, "w" + a, "t" + a ); } vs.Define( "AppDefault", "0000", "Default Value" ); vs.SetAppDefault( "AppDefault", true ); vs.SetSifDefault( "0000", true ); return fCfg; }