//public override object GetData()
        //{
        //    // Not implemented
        //    return null;
        //}

        public override bool ExecuteStep()
        {
            this.AppendResultDescription(0, "Executing step [{0}] ({1}):", this.StepName, this.StepType);
            this.AppendResultDescription(1, "Context Properties :");
            foreach (ControlProperty prop in this.ContextProps)
            {
                this.AppendResultDescription(2, "{0}/{1} : {2}", prop.Namespace, prop.Property, prop.Value);
                if (!String.IsNullOrEmpty(prop.TargetContextProperty))
                {
                    this.AppendResultDescription(3, "(overrided by context prop '{0}')", prop.TargetContextProperty);
                }
            }
            this.AppendResultDescription(1, "XPath Values :");
            foreach (ControlField field in this.XPathProps)
            {
                this.AppendResultDescription(2, "{0} : {1}", field.XPath, field.Value);
            }


            using (BizTalkDTADbEntities btsDTA = new BizTalkDTADbEntities(System.Configuration.ConfigurationManager.ConnectionStrings["BizTalkDTADbEntities"].ConnectionString))
            {
                TrackingDatabase dta = new TrackingDatabase(BTSTestContext.BizTalkDbServer,
                                                            BTSTestContext.BizTalkDTADb);
                BizTalkOperations       operations = new BizTalkOperations(BTSTestContext.BizTalkDbServer, BTSTestContext.BizTalkMgmtDb);
                List <MatchingPartInfo> partList   = MatchingPartInfo.GetMatchingPartInfo(btsDTA, dta, operations, this.ContextProps, this.XPathProps, this.ProcessName, null);

                if (partList.Count > 0)
                {
                    List <MultipartMessageDefinition> msgs = new List <MultipartMessageDefinition>();
                    foreach (MatchingPartInfo part in partList)
                    {
                        this.AppendResultDescription(1, "[OK] Matching service instance found, messageID={0}", part.MessageInstanceId);
                        IBaseMessage trackedMsg = operations.GetTrackedMessage(part.MessageInstanceId, dta);

                        MultipartMessageDefinition tempMsg = MultipartMessageManager.GenerateFromMessage(trackedMsg);
                        tempMsg.Description = String.Format("Service : {0} - Message instance: {1}", part.ServiceName, part.MessageInstanceId);
                        msgs.Add(tempMsg);
                    }

                    BTSTestContext.AddParam(this.TargetContextProperty, msgs);
                    this.Result = StepResult.OK;
                }
                else
                {
                    this.AppendResultDescription(1, "[KO] No matching service instance found", EventLogEntryType.Error);
                    this.Result = StepResult.Error;
                }
            }

            return(this.Result == StepResult.OK);
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            this.dgMessages.Rows.Clear();

            List <ControlProperty> contextProps = new List <ControlProperty>();
            List <string>          serviceNames = new List <string>();

            foreach (DataGridViewRow dr in this.dgProps.Rows)
            {
                if (dr.Cells[0].Value != null)
                {
                    //ControlProperty cp = new ControlProperty();
                    //cp.Namespace = (string)dr.Cells[1].Value;
                    //cp.Property = (string)dr.Cells[0].Value;
                    //cp.Value = (string)dr.Cells[2].Value;

                    //contextProps.Add(cp);
                    serviceNames.Add((string)dr.Cells[0].Value);
                }
            }

            string entityConnectionStr = String.Empty;

            this.testConnection(out entityConnectionStr, false);

            using (BizTalkDTADbEntities btsDTA = new BizTalkDTADbEntities(entityConnectionStr))
            {
                TrackingDatabase dta = new TrackingDatabase(this.txtDTAServerName.Text,
                                                            this.txtDTADBName.Text);
                BizTalkOperations operations = new BizTalkOperations(this.txtDTAServerName.Text, this.txtMgmtDBName.Text);

                var msgIds = from mioe in btsDTA.dta_MessageInOutEvents
                             join si in btsDTA.dta_ServiceInstances on mioe.uidServiceInstanceId equals si.uidServiceInstanceId
                             join s in btsDTA.dta_Services on si.uidServiceId equals s.uidServiceId
                             join pn in btsDTA.dta_PortName on mioe.nPortId equals pn.nPortId
                             join sn in btsDTA.dta_SchemaName on mioe.nSchemaId equals sn.nSchemaId
                             where mioe.dtTimestamp > this.dtFrom.Value &&
                             mioe.dtTimestamp < this.dtTo.Value &&
                             serviceNames.Contains(s.strServiceName)
                             orderby mioe.dtInsertionTimeStamp descending
                             select new tempMsgInfo
                {
                    strServiceName         = s.strServiceName
                    , uidMessageInstanceId = mioe.uidMessageInstanceId
                    , strPortName          = pn.strPortName
                    , dtTimestamp          = mioe.dtTimestamp
                    , nStatus       = mioe.nStatus
                    , strSchemaName = sn.strSchemaName
                };

                ControlProperty tempCP = contextProps.Find(cp =>
                                                           cp.Namespace == BizWTF.Core.Entities.BTSProperties.messageType.Name.Namespace
                                                           &&
                                                           cp.Property == BizWTF.Core.Entities.BTSProperties.messageType.Name.Name);
                if (tempCP != null)
                {
                    msgIds = msgIds.Where(rec => rec.strSchemaName == tempCP.Value);
                }

                /// Looping through all the messages in order to identify the one we are looking for
                foreach (var msg in msgIds.Take(Int32.Parse(this.txtMaxRecord.Text)))
                {
                    bool match = MatchingPartInfo.PerformMessageLookup(msg.uidMessageInstanceId, contextProps, new List <ControlField>(), dta, operations);

                    if (match)
                    {
                        string status = String.Empty;
                        switch (msg.nStatus)
                        {
                        case 0:
                            status = "Receive";
                            break;

                        case 1:
                            status = "Send";
                            break;

                        case 5:
                            status = "Transmission Failure";
                            break;

                        default:
                            status = "?";
                            break;
                        }
                        this.dgMessages.Rows.Add(status, msg.strServiceName, msg.strPortName, msg.strSchemaName, msg.dtTimestamp, msg.uidMessageInstanceId);
                    }
                }
            }
        }
        //public override List<OrchestrationInstanceInfo> GetData<List<OrchestrationInstanceInfo>>()
        //{
        //    return this.InstanceList;
        //}

        public override bool ExecuteStep()
        {
            this.AppendResultDescription(0, "Executing step [{0}] ({1}):", this.StepName, this.StepType);
            this.AppendResultDescription(1, "Context Properties :");
            foreach (ControlProperty prop in this.ContextProps)
            {
                this.AppendResultDescription(2, "{0}/{1} : {2}", prop.Namespace, prop.Property, prop.Value);
                if (!String.IsNullOrEmpty(prop.TargetContextProperty))
                {
                    this.AppendResultDescription(2, "(overrided by context prop '{0}')", prop.TargetContextProperty);
                }
            }
            this.AppendResultDescription(1, "XPath Values :");
            foreach (ControlField field in this.XPathProps)
            {
                this.AppendResultDescription(2, "{0} : {1}", field.XPath, field.Value);
            }


            using (BizTalkDTADbEntities btsDTA = new BizTalkDTADbEntities(System.Configuration.ConfigurationManager.ConnectionStrings["BizTalkDTADbEntities"].ConnectionString))
            {
                TrackingDatabase dta = new TrackingDatabase(BTSTestContext.BizTalkDbServer,
                                                            BTSTestContext.BizTalkDTADb);
                BizTalkOperations       operations = new BizTalkOperations(BTSTestContext.BizTalkDbServer, BTSTestContext.BizTalkMgmtDb);
                List <MatchingPartInfo> partList   = MatchingPartInfo.GetMatchingPartInfo(btsDTA, dta, operations,
                                                                                          this.ContextProps,
                                                                                          this.XPathProps,
                                                                                          this.ProcessName,
                                                                                          this.SourceContextProperty
                                                                                          ).Distinct <MatchingPartInfo>(new MatchingPartServiceIDComparer()).ToList();

                if (partList.Count > 0)
                {
                    this.AppendResultDescription(1, "[OK] Matching service {0} instances found ", partList.Count);
                    foreach (MatchingPartInfo part in partList)
                    {
                        this.AppendResultDescription(2, "Matching service instance : {0}", part.ServiceInstanceId);

                        OrchestrationInstanceInfo currentInstance = new OrchestrationInstanceInfo(part.txtSymbol);
                        currentInstance.FlowDefinition.ProcessName = this.ProcessName;

                        var filteredMsgIds = from me in btsDTA.dta_MessageInOutEvents
                                             where me.uidServiceInstanceId == part.ServiceInstanceId
                                             orderby me.dtInsertionTimeStamp descending
                                             select new
                        {
                            me.uidMessageInstanceId
                        };
                        foreach (var msg in filteredMsgIds)
                        {
                            currentInstance.Messages.Add(operations.GetTrackedMessage(msg.uidMessageInstanceId, dta));
                        }
                        this.AppendResultDescription(3, "Found {0} attached messages", filteredMsgIds.Count());

                        this.AppendResultDescription(3, "Pre-processing DebugShapes", filteredMsgIds.Count());
                        var debugTraces = from dt in btsDTA.dta_DebugTrace
                                          where dt.uidServiceInstanceId == part.ServiceInstanceId
                                          orderby dt.dtInsertionTimeStamp ascending
                                          select new DebugShape
                        {
                            ShapeID    = dt.vtInstructionId,
                            StartTime  = dt.dtBeginTimeStamp,
                            FinishTime = dt.dtEndTimeStamp
                        };
                        foreach (DebugShape trace in debugTraces)
                        {
                            string    shapePath = String.Empty;
                            ShapeInfo shape     = currentInstance.FlowDefinition.GetShape(trace.ShapeID, ref shapePath);
                            trace.shapeText = shape.shapeText;
                            trace.shapeType = shape.shapeType;
                            trace.ShapePath = shapePath;

                            currentInstance.Trace.TraceDetails.Add(trace);
                            this.AppendResultDescription(4, "Processed shape '{0}' ({1})", trace.shapeText, trace.ShapePath);
                        }
                        this.InstanceList.Add(currentInstance);
                    }

                    this.Result = StepResult.OK;
                }
                else
                {
                    this.AppendResultDescription(1, "No matching service instance found", EventLogEntryType.Error);
                    this.Result = StepResult.Error;
                }
            }

            BTSTestContext.AddParam(this.TargetContextProperty, this.InstanceList);

            return(this.Result == StepResult.OK);
        }