private bool AddConditions(Query q, String whereClause ) { InitializeComparisonList(); bool added = true; whereClause = whereClause.Trim(); if( whereClause.Length == 0 ){ return added; } string[] whereConditions = Regex.Split(whereClause, "[aA][nN][dD]"); ComparisonOperators cmpOperator = ComparisonOperators.EQ; string[] fields = null; if (whereConditions.Length > 0) { foreach (String condition in whereConditions) { fields = null; foreach (KeyValuePair<string, ComparisonOperators> kvp in supportedComparisons) { string cmpString = kvp.Key; cmpOperator = kvp.Value; if (cmpOperator == ComparisonOperators.EQ) { int index = condition.LastIndexOf(cmpString); fields = new string[2]; if (index > 0) { fields[0] = condition.Substring(0, index); fields[1] = condition.Substring((index + 1)); } else { fields[0] = condition; } }//end if if (fields == null) { fields = Regex.Split(condition, cmpString); } if (fields[0] == condition) { //Means no match found using that current comparison operator //so skip this condition fields = null; continue; } if (fields.Length != 2) { Console.WriteLine("ERROR: Unsupported where clause: " + whereClause); PrintSQLHelp(); added = false; break; } string fieldExpr = fields[0].Trim(); IElementDef def = Adk.Dtd.LookupElementDefBySQP(q.ObjectType, fieldExpr ); if (def == null) { Console.WriteLine("ERROR: Unrecognized field in where clause: " + fieldExpr ); PrintSQLHelp(); added = false; break; } else { if (fieldExpr.IndexOf('[') > 0) { // If there is a square bracket in the field syntax, use the raw XPath, // rather then the ElementDef because using ElementDef restrictions // does not work for XPath expressions that contain predicates // Note that using raw XPath expressions works fine, but the ADK is no longer // going to be able to use version-independent rendering of the query q.AddCondition(fieldExpr, cmpOperator, fields[1].Trim()); } else { q.AddCondition( def, cmpOperator, fields[1].Trim() ); } //our condition has been found, no need to check the other comparison //operators for a match so move to the next condition break; } }//end foreach }//end foreach } return added; }
private void _processSIF_ZoneStatus(SIF_ZoneStatus zoneStatus, IZone zone) { if (zoneStatus == null) { return; } bool sync = getChameleonProperty(zone, "sync", false); bool events = getChameleonProperty(zone, "logEvents", true); bool logEntry = getChameleonProperty(zone, "sifLogEntrySupport", false); ArrayList objectDefs = new ArrayList(); SIF_Providers providers = zoneStatus.SIF_Providers; if (providers != null) { foreach (SIF_Provider p in providers) { foreach (SIF_Object obj in p.SIF_ObjectList) { // Lookup the topic for each provided object in the zone IElementDef def = Adk.Dtd.LookupElementDef(obj.ObjectName); if (def != null) { objectDefs.Add(def); ITopic topic = TopicFactory.GetInstance(def); if (topic.GetSubscriber() == null) { if (events) { topic.SetSubscriber(fLogger, new SubscriptionOptions( )); } if (sync) { topic.SetQueryResults(fLogger); } } } } } } if (logEntry) { ITopic sifLogEntryTopic = TopicFactory.GetInstance(InfraDTD.SIF_LOGENTRY); sifLogEntryTopic.SetSubscriber(fLogger, new SubscriptionOptions( )); } foreach (ITopic topic in TopicFactory.GetAllTopics( SifContext.DEFAULT )) { try { // Join the topic to each zone ( causes the agent to subscribe to the joined objects ) // TODO: Add an "isJoinedTo()" API to topic so that it doesn't throw an exception if (topic.ObjectType != InfraDTD.SIF_ZONESTATUS.Name) { topic.Join(zone); } } catch (Exception ex) { zone.Log.Error(ex.Message, ex); } } if (sync) { if (objectDefs.Count == 0) { zone.ServerLog.Log (LogLevel.WARNING, "No objects are being provided in this zone", null, "1001"); } string syncObjects = zone.Properties.GetProperty("chameleon.syncObjects"); foreach (IElementDef def in objectDefs) { if (def.IsSupported(Adk.SifVersion)) { if (syncObjects == null || (syncObjects.Length > 0 && syncObjects.IndexOf(def.Name) > -1)) { Query q = new Query(def); // Query by specific parameters string condition = zone.Properties.GetProperty ("chameleon.syncConditions." + def.Name); if (condition != null && condition.Length > 0) { // The condition should be in the format "path=value" e.g "@RefId=123412341...1234|@Name=asdfasdf" String[] queryConditions = condition.Split('|'); foreach (String cond in queryConditions) { string[] conds = cond.Split('='); if (conds.Length == 2) { q.AddCondition(conds[0], "EQ", conds[1]); } } } if (logEntry) { zone.ServerLog.Log (LogLevel.INFO, "Requesting " + q.ObjectType.Name + " from the zone", q.ToXml(Adk.SifVersion), "1002"); } zone.Query(q); } } else { String debug = "Will not request " + def.Name + " because it is not supported in " + Adk.SifVersion.ToString(); Console.WriteLine(debug); zone.ServerLog.Log(LogLevel.WARNING, debug, null, "1001"); } } } }
/// <summary> /// Filter the SIF Request to a student whose SIF RefId is 7C834EA9EDA12090347F83297E1C290C. /// </summary> /// <param name="query">SIF Query that contains the filter condition.</param> /// <param name="zone">Zone to make the SIF Request on.</param> protected override void AddToBroadcastRequestQuery(Query query, IZone zone) { if (log.IsDebugEnabled) log.Debug("Added a condition to the request query for StudentPersonal SIF RefId of 7C834EA9EDA12090347F83297E1C290C."); query.AddCondition(StudentDTD.STUDENTPERSONAL_REFID, ComparisonOperators.EQ, "7C834EA9EDA12090347F83297E1C290C"); }
private Query testResolveBySQP( IElementDef objectDef, String sqp, SifVersion version, IElementDef resolvedNestedElement ) { Adk.SifVersion = version; Query q = new Query( objectDef ); q.AddCondition( sqp, ComparisonOperators.EQ, "foo" ); String sifQueryXML = q.ToXml(); Console.WriteLine( sifQueryXML ); String searchFor = "<SIF_Element>" + sqp + "</SIF_Element>"; // The .Net ADK doesn't encode apostrophes when they are in // element content, so the following line is different than // the java test //searchFor = searchFor.Replace( "'", "'" ); Assert.IsTrue( sifQueryXML.Contains( searchFor ), "SQP in XML" ); SifParser parser = SifParser.NewInstance(); SIF_Request sifR = (SIF_Request) parser.Parse( "<SIF_Request>" + sifQueryXML + "</SIF_Request>", null ); Query newQuery = new Query( sifR.SIF_Query ); Condition cond = newQuery.HasCondition( sqp ); Assert.IsNotNull( cond, "hasCondition" ); Assert.AreEqual( sqp, cond.GetXPath(), "SQP" ); Assert.AreEqual( sqp, cond.GetXPath( newQuery, version ), "Version-Specific SQP" ); return newQuery; }
/** * Test SIF Query Pattern support in the ADK * * @param objectDef * The IElementDef representing the root SIF Object * @param def * The IElementDef representing the field being queried (e.g. * CommonDTD.NAME_FIRSTNAME) * @param sqp * The expected SIF Query Pattern (e.g. "Name/FirstName") for the * above field def * @param version * The version of SIF to test */ private Query testSQP( IElementDef objectDef, IElementDef def, String sqp, SifVersion version ) { Adk.SifVersion = version; IElementDef lookedUp = Adk.Dtd.LookupElementDefBySQP( objectDef, sqp ); Assert.AreEqual( def.Name, lookedUp.Name, "IElementDef" ); testResolveBySQP( objectDef, sqp, version, def ); Query q = new Query( objectDef ); q.AddCondition( def, ComparisonOperators.EQ, "foo" ); String sifQueryXML = q.ToXml(); Console.WriteLine( sifQueryXML ); String searchFor = "<SIF_Element>" + sqp + "</SIF_Element>"; Assert.IsTrue( sifQueryXML.Contains( searchFor ), "SQP in XML" ); SifParser parser = SifParser.NewInstance(); SIF_Request sifR = (SIF_Request) parser.Parse( "<SIF_Request>" + sifQueryXML + "</SIF_Request>", null ); Query newQuery = new Query( sifR.SIF_Query ); Condition cond = newQuery.HasCondition( sqp ); Assert.IsNotNull( cond, "hasCondition" ); Assert.AreEqual( sqp, cond.GetXPath(), "SQP" ); Assert.AreEqual( def, cond.Field, "IElementDef" ); return newQuery; }
public void testSQP050() { Query q = new Query( StudentDTD.STUDENTPERSONAL ); q.AddCondition( "OtherIdList/OtherId[@Type='ZZ']", ComparisonOperators.EQ, "SCHOOL:997" ); Condition c = q.HasCondition( CommonDTD.OTHERID ); Assert.IsNotNull( c ); String xPath = c.GetXPath( q, SifVersion.SIF15r1 ); Assert.AreEqual( xPath, "OtherId[@Type='ZZ']", "SIF 1.5 XPath" ); xPath = c.GetXPath( q, SifVersion.SIF20 ); Assert.AreEqual( "OtherIdList/OtherId[@Type='ZZ']", xPath, "SIF 2.0 XPath" ); }
public void testSQP060() { // TT 217 Presumptive Query Syntax support Query q = new Query( ReportingDTD.STUDENTLOCATOR ); q.AddCondition( "RequestingAgencyId/@Type", ComparisonOperators.EQ, "LEA" ); q.AddCondition( "RequestingAgencyId", ComparisonOperators.EQ, "0001" ); q = SaveToXMLAndReparse( q, SifVersion.LATEST ); Condition c = q.HasCondition( ReportingDTD.REQUESTINGAGENCYID_TYPE ); Assert.IsNotNull( c ); String xPath = c.GetXPath( q, SifVersion.LATEST ); Assert.AreEqual( "RequestingAgencyId/@Type", xPath, "RequestingAgencyID/@Type XPath" ); c = q.HasCondition( ReportingDTD.REQUESTINGAGENCYID ); Assert.IsNotNull( c ); xPath = c.GetXPath( q, SifVersion.LATEST ); Assert.AreEqual( "RequestingAgencyId", xPath, "RequestingAgencyIDe XPath" ); }
public void testSimpleRefidFilterFail() { Authentication auth = BuildAuthentication(); Query q = new Query( InfrastructureDTD.AUTHENTICATION ); q.AddCondition( InfrastructureDTD.AUTHENTICATION_REFID, ComparisonOperators.EQ, "FAIL" ); Assert.IsFalse( q.Evaluate( auth ) ); }
public void testSQP040() { Query q = new Query( StudentDTD.STUDENTSCHOOLENROLLMENT ); q.AddCondition( StudentDTD.STUDENTSCHOOLENROLLMENT_SCHOOLYEAR, ComparisonOperators.EQ, "2001" ); Condition c = q .HasCondition( StudentDTD.STUDENTSCHOOLENROLLMENT_SCHOOLYEAR ); Assert.IsNotNull( c ); String xPath = c.GetXPath( q, SifVersion.SIF15r1 ); Assert.AreEqual( "SchoolYear", xPath, "SIF 1.5 XPath" ); xPath = c.GetXPath( q, SifVersion.SIF20 ); Assert.AreEqual( "@SchoolYear", xPath, "SIF 2.0 XPath" ); }
public void testSimpleOrFilter() { Authentication auth = BuildAuthentication(); Query q = new Query( InfrastructureDTD.AUTHENTICATION, GroupOperator.Or ); q.AddCondition( InfrastructureDTD.AUTHENTICATIONINFO_DISTINGUISHEDNAME, ComparisonOperators.EQ, "foo" ); q.AddCondition( InfrastructureDTD.AUTHENTICATION_REFID, ComparisonOperators.EQ, REFID_GUID ); Assert.IsTrue( q.Evaluate( auth ) ); }
public void testSimpleLTFilter() { StudentPersonal sp = new StudentPersonal( Adk.MakeGuid(), new Name( NameType.BIRTH, "E", "Sally" ) ); Query q = new Query( StudentDTD.STUDENTPERSONAL ); q.AddCondition( CommonDTD.NAME_LASTNAME, ComparisonOperators.LT, "G" ); Assert.IsTrue( q.Evaluate( sp ) ); q = new Query( StudentDTD.STUDENTPERSONAL ); q.AddCondition( CommonDTD.NAME_LASTNAME, ComparisonOperators.LT, "E" ); Assert.IsFalse( q.Evaluate( sp ) ); }
public void TestQueryCompare() { Query query = new Query(StudentDTD.STUDENTSCHOOLENROLLMENT, GroupOperator.Or); query.AddCondition(StudentDTD.STUDENTSCHOOLENROLLMENT_TIMEFRAME, ComparisonOperators.EQ, TimeFrame.CURRENT.Value); query.AddCondition(StudentDTD.STUDENTSCHOOLENROLLMENT_TIMEFRAME, ComparisonOperators.EQ, TimeFrame.FUTURE.Value); StudentSchoolEnrollment studentSchoolEnrollment = new StudentSchoolEnrollment(); studentSchoolEnrollment.TimeFrame = TimeFrame.HISTORICAL.Value; Assert.IsFalse(query.Evaluate(studentSchoolEnrollment)); }
public void testQuery010() { Query q = new Query( StudentDTD.STUDENTPERSONAL ); q.AddCondition( "Demographics/Ethnicity", ComparisonOperators.EQ, "W" ); Console.WriteLine( q.ToXml() ); q = SaveToXMLAndReparse( q, SifVersion.SIF15r1 ); Condition c = q.HasCondition( "Demographics/Ethnicity" ); Assert.IsNotNull( c, "Condition didn't resolve" ); }
public void testElementWithNullValue() { StudentPersonal sp = new StudentPersonal( Adk.MakeGuid(), new Name( NameType.BIRTH, null, "Sally" ) ); Query q = new Query( StudentDTD.STUDENTPERSONAL ); q.AddCondition( CommonDTD.NAME_LASTNAME, ComparisonOperators.GT, "E" ); Assert.IsFalse( q.Evaluate( sp ) ); q = new Query( StudentDTD.STUDENTPERSONAL ); q.AddCondition( CommonDTD.NAME_LASTNAME, ComparisonOperators.EQ, "E" ); Assert.IsFalse( q.Evaluate( sp ) ); q = new Query( StudentDTD.STUDENTPERSONAL ); q.AddCondition( CommonDTD.NAME_LASTNAME, ComparisonOperators.NE, "E" ); Assert.IsTrue( q.Evaluate( sp ) ); }