/// <summary> /// Constructs the event filter for the subscription. /// </summary> /// <returns>The event filter.</returns> public EventFilter ConstructFilter(Session session) { EventFilter filter = new EventFilter(); // the select clauses specify the values returned with each event notification. filter.SelectClauses = SelectClauses; // the where clause restricts the events returned by the server. // it works a lot like the WHERE clause in a SQL statement and supports // arbitrary expession trees where the operands are literals or event fields. ContentFilter whereClause = new ContentFilter(); // the code below constructs a filter that looks like this: // (Severity >= X OR LastSeverity >= X) AND (SuppressedOrShelved == False) AND (OfType(A) OR OfType(B)) // add the severity. ContentFilterElement element1 = null; ContentFilterElement element2 = null; if (Severity > EventSeverity.Min) { // select the Severity property of the event. SimpleAttributeOperand operand1 = new SimpleAttributeOperand(); operand1.TypeDefinitionId = ObjectTypeIds.BaseEventType; operand1.BrowsePath.Add(BrowseNames.Severity); operand1.AttributeId = Attributes.Value; // specify the value to compare the Severity property with. LiteralOperand operand2 = new LiteralOperand(); operand2.Value = new Variant((ushort)Severity); // specify that the Severity property must be GreaterThanOrEqual the value specified. element1 = whereClause.Push(FilterOperator.GreaterThanOrEqual, operand1, operand2); } // add the suppressed or shelved. if (!IgnoreSuppressedOrShelved) { // select the SuppressedOrShelved property of the event. SimpleAttributeOperand operand1 = new SimpleAttributeOperand(); operand1.TypeDefinitionId = ObjectTypeIds.BaseEventType; operand1.BrowsePath.Add(BrowseNames.SuppressedOrShelved); operand1.AttributeId = Attributes.Value; // specify the value to compare the Severity property with. LiteralOperand operand2 = new LiteralOperand(); operand2.Value = new Variant(false); // specify that the Severity property must Equal the value specified. element2 = whereClause.Push(FilterOperator.Equals, operand1, operand2); // chain multiple elements together with an AND clause. if (element1 != null) { element1 = whereClause.Push(FilterOperator.And, element1, element2); } else { element1 = element2; } } // add the event types. if (EventTypes != null && EventTypes.Count > 0) { element2 = null; // save the last element. for (int ii = 0; ii < EventTypes.Count; ii++) { // for this example uses the 'OfType' operator to limit events to thoses with specified event type. LiteralOperand operand1 = new LiteralOperand(); operand1.Value = new Variant(EventTypes[ii]); ContentFilterElement element3 = whereClause.Push(FilterOperator.OfType, operand1); // need to chain multiple types together with an OR clause. if (element2 != null) { element2 = whereClause.Push(FilterOperator.Or, element2, element3); } else { element2 = element3; } } // need to link the set of event types with the previous filters. if (element1 != null) { whereClause.Push(FilterOperator.And, element1, element2); } } filter.WhereClause = whereClause; // return filter. return filter; }
/// <summary> /// Returns the subscription filter to use. /// </summary> public EventFilter GetFilter() { ContentFilter whereClause = new ContentFilter(); ContentFilterElement element1 = whereClause.Push(FilterOperator.OfType, EventTypeId); EventFilter filter = new EventFilter(); for (int ii = 0; ii < Fields.Count; ii++) { filter.SelectClauses.Add(Fields[ii].Operand); if (Fields[ii].FilterValue != Variant.Null) { LiteralOperand operand = new LiteralOperand(); operand.Value = Fields[ii].FilterValue; ContentFilterElement element2 = whereClause.Push(Fields[ii].FilterOperator, Fields[ii].Operand, operand); element1 = whereClause.Push(FilterOperator.And, element1, element2); } } filter.WhereClause = whereClause; return filter; }