123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- //------------------------------------------------------------------------------
- // <copyright file="MessageQueueCriteria.cs" company="Microsoft">
- // Copyright (c) Microsoft Corporation. All rights reserved.
- // </copyright>
- //------------------------------------------------------------------------------
- using Experimental.System.Messaging.Interop;
- using System;
- using System.ComponentModel;
- using System.Globalization; //for CultureInfo
- namespace Experimental.System.Messaging
- {
- /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria"]/*' />
- /// <devdoc>
- /// <para>
- /// This class
- /// is used to filter MessageQueues when performing a
- /// query in the network, through MessageQueue.GetPublicQueues method.
- /// </para>
- /// </devdoc>
- public class MessageQueueCriteria
- {
- private DateTime createdAfter;
- private DateTime createdBefore;
- private string label;
- private string machine;
- private DateTime modifiedAfter;
- private DateTime modifiedBefore;
- private Guid category;
- private CriteriaPropertyFilter filter = new CriteriaPropertyFilter();
- private Restrictions restrictions;
- private Guid machineId;
- private static DateTime minDate = new DateTime(1970, 1, 1);
- private static DateTime maxDate = new DateTime(2038, 1, 19);
- /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.CreatedAfter"]/*' />
- /// <devdoc>
- /// Specifies the lower bound of the interval
- /// that will be used as the queue creation time
- /// search criteria.
- /// </devdoc>
- public DateTime CreatedAfter
- {
- get
- {
- if (!this.filter.CreatedAfter)
- throw new InvalidOperationException(Res.GetString(Res.CriteriaNotDefined));
- return this.createdAfter;
- }
- set
- {
- if (value < MessageQueueCriteria.minDate || value > MessageQueueCriteria.maxDate)
- throw new ArgumentException(Res.GetString(Res.InvalidDateValue, MessageQueueCriteria.minDate.ToString(CultureInfo.CurrentCulture), MessageQueueCriteria.maxDate.ToString(CultureInfo.CurrentCulture)));
- this.createdAfter = value;
- if (this.filter.CreatedBefore && this.createdAfter > this.createdBefore)
- this.createdBefore = this.createdAfter;
- this.filter.CreatedAfter = true;
- }
- }
- /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.CreatedBefore"]/*' />
- /// <devdoc>
- /// Specifies the upper bound of the interval
- /// that will be used as the queue creation time
- /// search criteria.
- /// </devdoc>
- public DateTime CreatedBefore
- {
- get
- {
- if (!this.filter.CreatedBefore)
- throw new InvalidOperationException(Res.GetString(Res.CriteriaNotDefined));
- return this.createdBefore;
- }
- set
- {
- if (value < MessageQueueCriteria.minDate || value > MessageQueueCriteria.maxDate)
- throw new ArgumentException(Res.GetString(Res.InvalidDateValue, MessageQueueCriteria.minDate.ToString(CultureInfo.CurrentCulture), MessageQueueCriteria.maxDate.ToString(CultureInfo.CurrentCulture)));
- this.createdBefore = value;
- if (this.filter.CreatedAfter && this.createdAfter > this.createdBefore)
- this.createdAfter = this.createdBefore;
- this.filter.CreatedBefore = true;
- }
- }
- internal bool FilterMachine
- {
- get
- {
- return this.filter.MachineName;
- }
- }
- /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.Label"]/*' />
- /// <devdoc>
- /// Specifies the label that that will be used as
- /// the criteria to search queues in the network.
- /// </devdoc>
- public string Label
- {
- get
- {
- if (!this.filter.Label)
- throw new InvalidOperationException(Res.GetString(Res.CriteriaNotDefined));
- return this.label;
- }
- set
- {
- if (value == null)
- throw new ArgumentNullException("value");
- this.label = value;
- this.filter.Label = true;
- }
- }
- /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.MachineName"]/*' />
- /// <devdoc>
- /// <para>
- /// Specifies the machine name that will be used
- /// as the criteria to search queues in the network.
- /// </para>
- /// </devdoc>
- public string MachineName
- {
- get
- {
- if (!this.filter.MachineName)
- throw new InvalidOperationException(Res.GetString(Res.CriteriaNotDefined));
- return this.machine;
- }
- set
- {
- if (!SyntaxCheck.CheckMachineName(value))
- throw new ArgumentException(Res.GetString(Res.InvalidProperty, "MachineName", value));
- try
- {
- this.machineId = MessageQueue.GetMachineId(value);
- }
- finally
- {
- }
- this.machine = value;
- this.filter.MachineName = true;
- }
- }
- /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.ModifiedAfter"]/*' />
- /// <devdoc>
- /// Specifies the lower bound of the interval
- /// that will be used as the queue modified time
- /// search criteria.
- /// </devdoc>
- public DateTime ModifiedAfter
- {
- get
- {
- if (!this.filter.ModifiedAfter)
- throw new InvalidOperationException(Res.GetString(Res.CriteriaNotDefined));
- return this.modifiedAfter;
- }
- set
- {
- if (value < MessageQueueCriteria.minDate || value > MessageQueueCriteria.maxDate)
- throw new ArgumentException(Res.GetString(Res.InvalidDateValue, MessageQueueCriteria.minDate.ToString(CultureInfo.CurrentCulture), MessageQueueCriteria.maxDate.ToString(CultureInfo.CurrentCulture)));
- this.modifiedAfter = value;
- if (this.filter.ModifiedBefore && this.modifiedAfter > this.modifiedBefore)
- this.modifiedBefore = this.modifiedAfter;
- this.filter.ModifiedAfter = true;
- }
- }
- /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.ModifiedBefore"]/*' />
- /// <devdoc>
- /// Specifies the upper bound of the interval
- /// that will be used as the queue modified time
- /// search criteria.
- /// </devdoc>
- public DateTime ModifiedBefore
- {
- get
- {
- if (!this.filter.ModifiedBefore)
- throw new InvalidOperationException(Res.GetString(Res.CriteriaNotDefined));
- return this.modifiedBefore;
- }
- set
- {
- if (value < MessageQueueCriteria.minDate || value > MessageQueueCriteria.maxDate)
- throw new ArgumentException(Res.GetString(Res.InvalidDateValue, MessageQueueCriteria.minDate.ToString(CultureInfo.CurrentCulture), MessageQueueCriteria.maxDate.ToString(CultureInfo.CurrentCulture)));
- this.modifiedBefore = value;
- if (this.filter.ModifiedAfter && this.modifiedAfter > this.modifiedBefore)
- this.modifiedAfter = this.modifiedBefore;
- this.filter.ModifiedBefore = true;
- }
- }
- /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.Reference"]/*' />
- /// <internalonly/>
- internal Restrictions.MQRESTRICTION Reference
- {
- get
- {
- int size = 0;
- if (this.filter.CreatedAfter)
- ++size;
- if (this.filter.CreatedBefore)
- ++size;
- if (this.filter.Label)
- ++size;
- if (this.filter.ModifiedAfter)
- ++size;
- if (this.filter.ModifiedBefore)
- ++size;
- if (this.filter.Category)
- ++size;
- restrictions = new Restrictions(size);
- if (this.filter.CreatedAfter)
- restrictions.AddI4(NativeMethods.QUEUE_PROPID_CREATE_TIME, Restrictions.PRGT, ConvertTime(this.createdAfter));
- if (this.filter.CreatedBefore)
- restrictions.AddI4(NativeMethods.QUEUE_PROPID_CREATE_TIME, Restrictions.PRLE, ConvertTime(this.createdBefore));
- if (this.filter.Label)
- restrictions.AddString(NativeMethods.QUEUE_PROPID_LABEL, Restrictions.PREQ, this.label);
- if (this.filter.ModifiedAfter)
- restrictions.AddI4(NativeMethods.QUEUE_PROPID_MODIFY_TIME, Restrictions.PRGT, ConvertTime(this.modifiedAfter));
- if (this.filter.ModifiedBefore)
- restrictions.AddI4(NativeMethods.QUEUE_PROPID_MODIFY_TIME, Restrictions.PRLE, ConvertTime(this.modifiedBefore));
- if (this.filter.Category)
- restrictions.AddGuid(NativeMethods.QUEUE_PROPID_TYPE, Restrictions.PREQ, this.category);
- return this.restrictions.GetRestrictionsRef();
- }
- }
- /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.Category"]/*' />
- /// <devdoc>
- /// Specifies the Category that will be used
- /// as the criteria to search queues in the network.
- /// </devdoc>
- public Guid Category
- {
- get
- {
- if (!this.filter.Category)
- throw new InvalidOperationException(Res.GetString(Res.CriteriaNotDefined));
- return this.category;
- }
- set
- {
- this.category = value;
- this.filter.Category = true;
- }
- }
- /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.ClearAll"]/*' />
- /// <devdoc>
- /// Resets all the current instance settings.
- /// </devdoc>
- public void ClearAll()
- {
- this.filter.ClearAll();
- }
- /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.ConvertTime"]/*' />
- /// <internalonly/>
- private int ConvertTime(DateTime time)
- {
- time = time.ToUniversalTime();
- return (int)(time - MessageQueueCriteria.minDate).TotalSeconds;
- }
- /// <include file='doc\MessageQueueCriteria.uex' path='docs/doc[@for="MessageQueueCriteria.CriteriaPropertyFilter"]/*' />
- /// <internalonly/>
- private class CriteriaPropertyFilter
- {
- public bool CreatedAfter;
- public bool CreatedBefore;
- public bool Label;
- public bool MachineName;
- public bool ModifiedAfter;
- public bool ModifiedBefore;
- public bool Category;
- public void ClearAll()
- {
- this.CreatedAfter = false;
- this.CreatedBefore = false;
- this.Label = false;
- this.MachineName = false;
- this.ModifiedAfter = false;
- this.ModifiedBefore = false;
- this.Category = false;
- }
- }
- }
- }
|