public void testCreateMappings() { // Create the root instance Mappings root = new Mappings(); // Create the default set of universal mappings Mappings defaults = new Mappings( root, "Default" ); root.AddChild( defaults ); // Create an ObjectMapping for StudentPersonal ObjectMapping studentMappings = new ObjectMapping( "StudentPersonal" ); defaults.AddRules( studentMappings ); // Add field rules studentMappings.AddRule( new FieldMapping( "FIRSTNAME", "Name[@Type='04']/FirstName" ) ); studentMappings.AddRule( new FieldMapping( "LASTNAME", "Name[@Type='04']/LastName" ) ); // Create a set of mappings for the state of Wisconsin Mappings wisconsin = new Mappings( defaults, "Wisconsin" ); defaults.AddChild( wisconsin ); // Create a set of mappings for the Neillsville School District Mappings neillsville = new Mappings( wisconsin, "Neillsville" ); wisconsin.AddChild( neillsville ); XmlDocument doc = new XmlDocument( ); doc.LoadXml( "<agent/>"); XmlElement n = root.ToDom( doc ); debug( doc ); }
/** * @param mappings * @param direction * @param version * @param elementType */ private MappingsContext( Mappings mappings, MappingDirection direction, SifVersion version, IElementDef elementType) { fMappings = mappings; fDirection = direction; fSIFVersion = version; fElementDef = elementType; }
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; }
private Mappings buildMappings1() { // root Mappings root = new Mappings(); // root.Default Mappings defaults = new Mappings( root, "Default", null, null, null ); root.AddChild( defaults ); // root.Default.StudentPersonal ObjectMapping studentMappings = new ObjectMapping( "StudentPersonal" ); defaults.AddRules( studentMappings ); studentMappings.AddRule( new FieldMapping( "REFID", "@RefId" ) ); // root.Default.MA Mappings massachusetts = new Mappings( defaults, "MA", null, null, null ); defaults.AddChild( massachusetts ); // root.Default.MA.StudentPersonal studentMappings = new ObjectMapping( "StudentPersonal" ); massachusetts.AddRules( studentMappings ); studentMappings.AddRule( new FieldMapping( "LOCALID", "LocalId" ) ); studentMappings.AddRule( new FieldMapping( "FIRSTNAME", "Name[@Type='01']/FirstName" ) ); studentMappings.AddRule( new FieldMapping( "LASTNAME", "Name[@Type='01']/LastName" ) ); // root.Default.MA.Boston Mappings boston = new Mappings( massachusetts, "Boston", null, "Boston", null ); massachusetts.AddChild( boston ); // root.Default.MA.Boston.StudentPersonal studentMappings = new ObjectMapping( "StudentPersonal" ); boston.AddRules( studentMappings ); studentMappings.AddRule( new FieldMapping( "BIRTHDAY", "Demographics/BirthDate" ) ); return root; }
private void dumpMappings( Mappings root ) { string line = root.Id + " (" + root.ChildCount + ") v=" + root.SIFVersionFilterString + " s=" + root.SourceIdFilterString + " z=" + root.ZoneIdFilterString; logger.Debug( INDENT.Substring( 0, indent ) + line ); indent += 2; foreach ( Mappings child in root.Children ) { dumpMappings( child ); } indent -= 2; }
/// <summary> Create a Mappings child </summary> public Mappings CreateChild(string id) { Mappings m = new Mappings(this, id); try { if (fNode != null) { m.fNode = m.ToDom(fNode.OwnerDocument); } } catch (Exception se) { throw new AdkMappingException(se.Message, null, se); } AddChild(m); return m; }
/// <summary> Creates a copy of this Mappings object and adds the copy as a child of /// the specified parent. Note the root Mappings container cannot be copied. /// If this method is called on the root an exception is raised. /// /// This method performs a "deep copy", such that a copy is made of each /// child Mappings, ObjectMapping, FieldMapping, ValueSet, and Property /// instance. /// /// </summary> /// <param name="newParent">The parent Mappings instance /// </param> /// <returns> A "deep copy" of this root Mappings object /// /// </returns> /// <exception cref="OpenADK.Library.Tools.Mapping.AdkMappingException"> AdkMappingException thrown if this method is not called on the /// root Mappings container /// </exception> public Mappings Copy(Mappings newParent) { if (IsRoot()) { throw new AdkMappingException ("Mappings.copy cannot be called on the root Mappings container", null); } // Create a new Mappings instance Mappings m = new Mappings(newParent, fId); // Copy the DOM XmlElement if there is one if (fNode != null && newParent.fNode != null) { m.fNode = (XmlElement) newParent.fNode.OwnerDocument.ImportNode( fNode, false ); } newParent.AddChild( m ); // Copy the filters m.SetSourceIdFilter(SourceIdFilterString); m.SetZoneIdFilter(ZoneIdFilterString); m.SetSIFVersionFilter(SIFVersionFilterString); // Copy all Mappings children if (fChildren != null) { foreach (string key in fChildren.Keys) { Mappings ch = fChildren[key]; m.AddChild(ch.Copy(m)); } } // Copy all ObjectMapping children if (fObjRules != null) { foreach (string key in fObjRules.Keys) { ObjectMapping ch = fObjRules[key]; ObjectMapping copy = ch.Copy(m); m.AddRules(copy, false); // if( m.fNode != null ) // m.fNode.AppendChild( copy.fNode ); } } // Copy fValueSets if (fValueSets != null) { foreach (string key in fValueSets.Keys) { ValueSet vs = fValueSets[key]; m.AddValueSet(vs.Copy(m)); } } // Copy properties if (fProps != null) { foreach (string key in fProps.Keys) { string val = fProps[key]; m.SetProperty(key, val); } } return m; }
/** * Returns a new mappings root, with the passed-in Mappings copied into it * as a child, obtained by using the Mappings.copy() method * * @param mappingSet * The set of mappings to be copied into the new root * @return */ private Mappings getCopy( Mappings mappingSet ) { XmlDocument dom = new XmlDocument(); dom.LoadXml( "<mappings/>" ); Mappings newRoot = new Mappings(); newRoot.XmlElement = dom.DocumentElement; mappingSet.Copy( newRoot ); debug( dom ); return newRoot; }
/// <summary> Creates a copy this ObjectMapping instance. /// /// This method performs a "deep copy", such that a clone is made of each /// child FieldMapping. The parent of the new ObjectMapping will be the /// Mappings object passed to this function. Any DOM Nodes assigned to this /// object or its children are cloned and appended to the parent Mappings's /// DOM XmlElement if one exists. /// /// </summary> /// <returns> A "deep copy" of this object /// </returns> public ObjectMapping Copy(Mappings newParent) { // Create a new ObjectMapping instance ObjectMapping m = new ObjectMapping(fObjType); // Copy the DOM XmlElement if (fNode != null) { if (newParent.fNode != null) { XmlElement newNode = (XmlElement) newParent.fNode.OwnerDocument.ImportNode( fNode, false ); newParent.fNode.AppendChild( newNode ); m.fNode = newNode; } } // Copy fFieldRules if (fFieldRules != null) { if (m.fFieldRules == null) { m.fFieldRules = new List<FieldMapping>(); } for (int i = 0; i < fFieldRules.Count; i++) { FieldMapping copy = (fFieldRules[i]).Copy(m); m.AddRule(copy); } } return m; }
/// <summary> Constructs a child Mappings object with optional filters. /// /// </summary> /// <param name="parent">The root Mappings object /// </param> /// <param name="id">A unique identifier for this Mappings object /// </param> /// <param name="sourceIdFilter">A comma-delimited list of SourceIds or null if no /// SourceId filter should be applied to this Mappings object /// </param> /// <param name="zoneIdFilter">A comma-delimited list of ZoneIds or null if no /// SourceId filter should be applied to this Mappings object /// </param> /// <param name="sifVersionFilter">A comma-delimited list of SIF Versions, in the /// form "1.0r1", or null if no SIF Version filter should be applied to /// this Mappings object /// </param> public Mappings(Mappings parent, string id, string sourceIdFilter, string zoneIdFilter, string sifVersionFilter) { fParent = parent; fId = id; SetSourceIdFilter(sourceIdFilter); SetZoneIdFilter(zoneIdFilter); SetSIFVersionFilter(sifVersionFilter); }
/// <summary> Recursively adds all children of the Mappings to the supplied Vector</summary> private void GroupDescendents(Mappings m, List<Mappings> v) { if (m.fChildren != null) { foreach (Mappings ch in m.fChildren.Values) { v.Add(ch); GroupDescendents(ch, v); } } }
/// <summary> Constructs a child Mappings object with no filters. /// /// </summary> /// <param name="parent">The parent Mappings object /// </param> /// <param name="id">A unique string ID for this Mappings object /// </param> public Mappings(Mappings parent, string id) : this(parent, id, null, null, null) { }
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> Removes a Mappings child</summary> public void RemoveChild(Mappings m) { if (fChildren != null) { fChildren.Remove(m.Id); } // If there is a DOM XmlElement associated with this Mappings, and the new child // also has a XmlElement, detatch it if (fNode != null && m.fNode != null) { fNode.RemoveChild(m.fNode); } }
/** * Returns a new mappings root, with the passed-in Mappings copied into it * as a child, obtained by using the Mappings.copy() method * * @param root * The set of mappings to be copied into the new root * @return */ private Mappings getCopyFromDOM( Mappings mappingSet ) { XmlDocument dom = new XmlDocument(); dom.LoadXml( "<agent/>" ); XmlNode mappingsNode = dom.ImportNode( mappingSet.XmlElement, true ); dom.DocumentElement.AppendChild( mappingsNode ); Mappings newRoot = new Mappings(); newRoot.Populate( dom, (XmlElement) mappingsNode ); return newRoot; }
/** * Creates a MappingsContext instance to handle a set of mappings operations using * the same parameters * @param m The mappings instance to use * @param direction The mappings direction * @param version The version of SIF to use for evaluating rule filters on field mappings * @param elementDef The ElementDef representing the object type being mapped * @return A new MappingsContext, initialized to map using the specified parameters */ public static MappingsContext Create(Mappings m, MappingDirection direction, SifVersion version, IElementDef elementDef) { // Get the rules associated with the element type MappingsContext mc = new MappingsContext(m, direction, version, elementDef); ObjectMapping om = m.GetRules(elementDef.Name, true); mc.AddRules(om); return mc; }
public Candidate(Mappings m) { fMapping = m; // The "restrictiveness" score is based on a count of all filters string[] f = m.GetZoneIdFilter(); if (f != null) { restrictiveness += f.Length; } f = m.GetSourceIdFilter(); if (f != null) { restrictiveness += f.Length; } SifVersion[] v = m.GetSIFVersionFilter(); if (v != null) { restrictiveness += v.Length; } }
/// <summary> Adds a Mappings child</summary> public void AddChild(Mappings m) { if (fChildren == null) { fChildren = new Dictionary<String, Mappings>(); } fChildren[m.Id] = m; // If there is a DOM XmlElement associated with this Mappings, and the new child // also has a XmlElement, attach it if (fNode != null && m.fNode != null) { fNode.AppendChild(m.fNode); } }
/// <summary> Read a configuration file into memory. /// /// </summary> /// <param name="file">The path to the configuration file /// </param> /// <param name="validate">true to validate the configuration file /// /// </param> /// <returns> A DOM Document encapsulating the configuration file. This /// object may also be obtained by calling <c>getDocument</c> /// </returns> /// <exception cref="IOException"> thrown if an error occurs reading the file /// </exception> /// <exception cref="AdkMappingException"> thrown if an error occurs parsing any /// <c><mappings></c> elements /// </exception> /// <exception cref="AdkConfigException"> thrown if the configuration file fails /// to parse or is not valid when validation is turned on /// /// </exception> /// <seealso cref="Document"> /// </seealso> public virtual XmlDocument Read( string file, bool validate ) { try { fSrc = new FileInfo( file ); // TODO: Implement validation if the validate flag is true if ( validate ) { throw new NotImplementedException ( "Document validation is not yet implemented" ); } else { fDoc = new XmlDocument(); fDoc.Load( fSrc.FullName ); } } catch ( Exception ex ) { throw new AdkConfigException( ex.Message, ex ); } // Build the Mappings object fMappings = new Mappings(); fMappings.Populate( fDoc, RootNode ); return fDoc; }
/** * Asserts that the provided set of Mappings matches the one that was * created in createMappings(); */ private void assertMappings( Mappings m ) { Mappings test = m.GetMappings( "Test" ); Assertion.AssertNotNull( "Test mappings is not present", test ); Assertion.AssertEquals( "Should have a single Object Mapping", 1, test.GetObjectMappings().Length ); // TODO: Test the version and sourceId filters more carefully /* * Assertion.AssertEquals( "SifVersion attr should be empty", 0, * test.SifVersionFilter().Length ); Assertion.AssertEquals( "SourceId attr * should be empty", 0, test.SourceIdFilter().Length ); Assertion.AssertEquals( * "Zone attr should be empty", 0, test.ZoneIdFilter().Length ); */ // assert the object mapping ObjectMapping om = test.GetObjectMapping( "StudentPersonal", false ); Assertion.AssertNotNull( "StudentPersonal mappings", om ); Assertion.AssertEquals( "There should be five rules", 5, om.RuleCount ); IList<FieldMapping> rules = om.GetRulesList( false ); // Field 1 Assertion.AssertEquals( "FIELD1 name", "FIELD1", rules[0].FieldName ); Assertion.AssertEquals( "FIELD1 rule", "Name/FirstName", rules[0].GetRule().ToString() ); Assertion.AssertEquals( "FIELD1 ifNull", MappingBehavior.IfNullUnspecified, rules[0].NullBehavior ); // Field 2 Assertion.AssertEquals( "FIELD2 name", "FIELD2", rules[1].FieldName ); Assertion.AssertEquals( "FIELD2 rule", "Name/LastName", rules[1].GetRule().ToString() ); Assertion.AssertEquals( "FIELD2 valueset", "VS1", rules[1].ValueSetID ); Assertion.AssertEquals( "FIELD2 alias", "ALIAS1", rules[1].Alias ); Assertion.AssertEquals( "FIELD2 default", "DEFAULT1", rules[1].DefaultValue ); MappingsFilter filter = rules[1].Filter; Assertion.AssertNotNull( "FIELD2 filter is null", filter ); Assertion.AssertEquals( "filter direction", MappingDirection.Inbound, filter .Direction ); Assertion.AssertEquals( "filter sif version", "=" + SifVersion.SIF11.ToString(), filter.SifVersion ); // Field 3 Assertion.AssertEquals( "FIELD3 name", "FIELD3", rules[2].FieldName ); Assertion.AssertEquals( "FIELD3 rule", "Name/MiddleName", rules[2].GetRule().ToString() ); Assertion.AssertEquals( "FIELD3 valueset", "VS2", rules[2].ValueSetID ); Assertion.AssertEquals( "FIELD3 alias", "ALIAS2", rules[2].Alias ); Assertion.AssertEquals( "FIELD3 default", "DEFAULT2", rules[2].DefaultValue ); Assertion.AssertEquals( "FIELD3 ifNull", MappingBehavior.IfNullDefault, rules[2].NullBehavior ); MappingsFilter filter2 = rules[2].Filter; Assertion.AssertNotNull( "FIELD3 filter is null", filter2 ); Assertion.AssertEquals( "filter2 direction", MappingDirection.Outbound, filter2.Direction ); Assertion.AssertEquals( "filter2 sif version", "=" + SifVersion.SIF15r1.ToString(), filter2.SifVersion ); // Field 4 Assertion.AssertEquals( "FIELD4 name", "FIELD4", rules[3].FieldName ); Assertion.AssertNull( "FIELD4 valueset", rules[3].ValueSetID ); Assertion.AssertNull( "FIELD4 alias", rules[3].Alias ); Assertion.AssertNull( "FIELD4 default", rules[3].DefaultValue ); Assertion.AssertEquals( "FIELD4 ifNull", MappingBehavior.IfNullSuppress, rules[3].NullBehavior ); Rule r = rules[3].GetRule(); Assertion.Assert( "Rule should be OtherIdRule", r is OtherIdRule ); Assertion.AssertEquals( "FIELD5 name", "FIELD5", rules[4].FieldName ); Assertion.AssertEquals( "FIELD5 datatype", SifDataType.Date, rules[4] .DataType ); // TODO: The OtherIdRule doesn't have an API to get at the // OtherIdMapping. For now, just // convert it to a string and assert the results String ruleStr = r.ToString(); Assertion.Assert( "prefix should be BUSROUTE", ruleStr .IndexOf( "prefix='BUSROUTE'" ) > 1 ); Assertion.Assert( "type should be ZZ", ruleStr.IndexOf( "type='ZZ'" ) > 1 ); ValueSet vs = test.GetValueSet( "VS1", false ); Assertion.AssertNotNull( "ValueSet VS1 should not be null", vs ); Assertion.AssertEquals( "VS1 should have 12 entries", 12, vs.Entries.Length ); for ( int a = 0; a < 10; a++ ) { Assertion.AssertEquals( "Mapping by appvalue", "SifValue" + a, vs.Translate( "Value" + a ) ); Assertion.AssertEquals( "Mapping by sifvalue", "Value" + a, vs.TranslateReverse( "SifValue" + a ) ); } // Test the default value entries Assertion.AssertEquals( "Expecting app default value", "AppDefault", vs.TranslateReverse( "abcdefg" ) ); Assertion.AssertEquals( "Expecting app default value", "AppDefault", vs.TranslateReverse( null ) ); Assertion.AssertEquals( "Expecting sif default value", "SifDefault", vs.Translate( "abcdefg" ) ); Assertion.AssertNull( "Expecting NULL value", vs.Translate( null ) ); vs = test.GetValueSet( "VS2", false ); Assertion.AssertNotNull( "ValueSet VS2 should not be null", vs ); Assertion.AssertEquals( "VS2 should have 4 entries", 4, vs.Entries.Length ); for ( int a = 0; a < 3; a++ ) { Assertion.AssertEquals( "Mapping by appvalue", "w" + a, vs.Translate( "q" + a ) ); Assertion.AssertEquals( "Mapping by sifvalue", "q" + a, vs .TranslateReverse( "w" + a ) ); } // Test the default value entries Assertion.AssertEquals( "Expecting app default value", "AppDefault", vs .TranslateReverse( "abcdefg" ) ); Assertion.AssertEquals( "Expecting app default value", "AppDefault", vs .TranslateReverse( null ) ); Assertion.AssertEquals( "Expecting sif default value", "0000", vs .Translate( "abcdefg" ) ); Assertion.AssertEquals( "Expecting sif default value", "0000", vs.Translate( null ) ); }