File size: 2,930 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Management.Automation;

using Microsoft.Management.Infrastructure;

using Dbg = System.Management.Automation.Diagnostics;

namespace Microsoft.PowerShell.Cmdletization.Cim
{
    /// <summary>
    /// Base job for queries.
    /// </summary>
    internal abstract class QueryJobBase : CimChildJobBase<CimInstance>
    {
        private readonly CimQuery _cimQuery;

        internal QueryJobBase(CimJobContext jobContext, CimQuery cimQuery)
                : base(jobContext)
        {
            Dbg.Assert(cimQuery != null, "Caller should verify cimQuery != null");
            _cimQuery = cimQuery;
        }

        public override void OnNext(CimInstance item)
        {
            this.ExceptionSafeWrapper(
                    delegate
                    {
                        Dbg.Assert(item != null, "When OnNext is called from our IObservable, item parameter should always be != null");
                        if (item == null)
                        {
                            return;
                        }

                        if (!_cimQuery.IsMatchingResult(item))
                        {
                            return;
                        }

                        this.WriteObject(item);
                    });
        }

        public override void OnCompleted()
        {
            this.ExceptionSafeWrapper(
                    delegate
                    {
                        foreach (ClientSideQuery.NotFoundError notFoundError in _cimQuery.GenerateNotFoundErrors())
                        {
                            string errorId = "CmdletizationQuery_NotFound";
                            if (!string.IsNullOrEmpty(notFoundError.PropertyName))
                            {
                                errorId = errorId + "_" + notFoundError.PropertyName;
                            }

                            CimJobException cimJobException = CimJobException.CreateWithFullControl(
                                this.JobContext,
                                notFoundError.ErrorMessageGenerator(this.Description, this.JobContext.ClassName),
                                errorId,
                                ErrorCategory.ObjectNotFound);
                            if (!string.IsNullOrEmpty(notFoundError.PropertyName))
                            {
                                cimJobException.ErrorRecord.SetTargetObject(notFoundError.PropertyValue);
                            }

                            this.WriteError(cimJobException.ErrorRecord);
                        }
                    });
            base.OnCompleted();
        }

        internal override CimCustomOptionsDictionary CalculateJobSpecificCustomOptions()
        {
            return CimCustomOptionsDictionary.Create(_cimQuery.queryOptions);
        }
    }
}