|
| 1 | +using Microsoft.Xrm.Sdk; |
| 2 | +using Microsoft.Xrm.Sdk.Query; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | + |
| 7 | +namespace Rappen.XTB.CAT |
| 8 | +{ |
| 9 | + class CATTool : ICATTool |
| 10 | + { |
| 11 | + public string Name => $"{Target} Tester"; |
| 12 | + |
| 13 | + public string Target => "Custom Action"; |
| 14 | + |
| 15 | + public string MessageIdentifierColumn => "M.name"; |
| 16 | + |
| 17 | + public string ParameterIdentifierColumn => "name"; |
| 18 | + |
| 19 | + public QueryExpression GetActionQuery(Guid solutionid) |
| 20 | + { |
| 21 | + var qx = new QueryExpression("workflow"); |
| 22 | + qx.ColumnSet.AddColumns("name", "uniquename", "createdby", "primaryentity", "scope", "mode", "ismanaged", "iscustomizable", "istransacted", "iscustomprocessingstepallowedforotherpublishers", "inputparameters", "description"); |
| 23 | + qx.AddOrder("ismanaged", OrderType.Descending); |
| 24 | + qx.AddOrder("name", OrderType.Ascending); |
| 25 | + qx.Criteria.AddCondition("category", ConditionOperator.Equal, 3); |
| 26 | + qx.Criteria.AddCondition("type", ConditionOperator.Equal, 1); |
| 27 | + qx.Criteria.AddCondition("componentstate", ConditionOperator.Equal, 0); |
| 28 | + qx.Criteria.AddCondition("statuscode", ConditionOperator.Equal, 2); |
| 29 | + var qxsdk = qx.AddLink("sdkmessage", "sdkmessageid", "sdkmessageid", JoinOperator.LeftOuter); |
| 30 | + qxsdk.EntityAlias = "M"; |
| 31 | + qxsdk.Columns.AddColumns("name", "workflowsdkstepenabled"); |
| 32 | + if (!solutionid.Equals(Guid.Empty)) |
| 33 | + { |
| 34 | + var solcomp = qx.AddLink("solutioncomponent", "workflowid", "objectid"); |
| 35 | + solcomp.LinkCriteria.AddCondition("solutionid", ConditionOperator.Equal, solutionid); |
| 36 | + } |
| 37 | + return qx; |
| 38 | + } |
| 39 | + |
| 40 | + public QueryExpression GetInputQuery(Guid actionid) |
| 41 | + { |
| 42 | + var qx = new QueryExpression("sdkmessagerequestfield"); |
| 43 | + qx.Distinct = true; |
| 44 | + qx.ColumnSet.AddColumns("name", "position", "parameterbindinginformation", "optional", "parser", "fieldmask"); |
| 45 | + qx.AddOrder("position", OrderType.Ascending); |
| 46 | + var req = qx.AddLink("sdkmessagerequest", "sdkmessagerequestid", "sdkmessagerequestid"); |
| 47 | + var pair = req.AddLink("sdkmessagepair", "sdkmessagepairid", "sdkmessagepairid"); |
| 48 | + var msg = pair.AddLink("sdkmessage", "sdkmessageid", "sdkmessageid"); |
| 49 | + var wf = msg.AddLink("workflow", "sdkmessageid", "sdkmessageid"); |
| 50 | + wf.LinkCriteria.AddCondition("workflowid", ConditionOperator.Equal, actionid); |
| 51 | + return qx; |
| 52 | + } |
| 53 | + |
| 54 | + public QueryExpression GetOutputQuery(Guid actionid) |
| 55 | + { |
| 56 | + var qx = new QueryExpression("sdkmessageresponsefield"); |
| 57 | + qx.Distinct = true; |
| 58 | + qx.ColumnSet.AddColumns("name", "position", "parameterbindinginformation", "formatter", "publicname"); |
| 59 | + qx.AddOrder("position", OrderType.Ascending); |
| 60 | + var resp = qx.AddLink("sdkmessageresponse", "sdkmessageresponseid", "sdkmessageresponseid"); |
| 61 | + var req = resp.AddLink("sdkmessagerequest", "sdkmessagerequestid", "sdkmessagerequestid"); |
| 62 | + var pair = req.AddLink("sdkmessagepair", "sdkmessagepairid", "sdkmessagepairid"); |
| 63 | + var msg = pair.AddLink("sdkmessage", "sdkmessageid", "sdkmessageid"); |
| 64 | + var wf = msg.AddLink("workflow", "sdkmessageid", "sdkmessageid"); |
| 65 | + wf.LinkCriteria.AddCondition("workflowid", ConditionOperator.Equal, actionid); |
| 66 | + return qx; |
| 67 | + } |
| 68 | + |
| 69 | + public void PreProcessParams(EntityCollection records, IEnumerable<EntityMetadataProxy> entities) |
| 70 | + { |
| 71 | + foreach (var record in records.Entities.Where(e => !e.Contains("type"))) |
| 72 | + { |
| 73 | + var attribute = record.Contains("parser") ? "parser" : "formatter"; |
| 74 | + if (record.TryGetAttributeValue(attribute, out string parser)) |
| 75 | + { |
| 76 | + parser = parser.Split(',')[0]; |
| 77 | + while (parser.Contains(".")) |
| 78 | + { |
| 79 | + parser = parser.Substring(parser.IndexOf('.') + 1); |
| 80 | + } |
| 81 | + record["type"] = parser; |
| 82 | + } |
| 83 | + } |
| 84 | + var otcrecords = records.Entities.Where(r => r.Contains("parameterbindinginformation")); |
| 85 | + if (otcrecords.Count() > 0) |
| 86 | + { |
| 87 | + var siblingrecords = new List<Entity>(); |
| 88 | + foreach (var otcrecord in otcrecords) |
| 89 | + { |
| 90 | + var siblingrecord = records.Entities.FirstOrDefault(r => r["name"].ToString() == otcrecord["name"].ToString() && !r.Contains("parameterbindinginformation")); |
| 91 | + if (siblingrecord == null) |
| 92 | + { |
| 93 | + continue; |
| 94 | + } |
| 95 | + var binding = otcrecord["parameterbindinginformation"].ToString(); |
| 96 | + var otcstr = binding.Replace("OTC:", "").Trim(); |
| 97 | + if (int.TryParse(otcstr, out int otc)) |
| 98 | + { |
| 99 | + if (entities.FirstOrDefault(e => e.Metadata.ObjectTypeCode == otc) is EntityMetadataProxy meta) |
| 100 | + { |
| 101 | + otcrecord["entity"] = meta; |
| 102 | + } |
| 103 | + } |
| 104 | + siblingrecords.Add(siblingrecord); |
| 105 | + } |
| 106 | + siblingrecords.ForEach(s => records.Entities.Remove(s)); |
| 107 | + } |
| 108 | + records.Entities.Where(e => !e.Contains("isoptional") && e.Contains("optional")).ToList().ForEach(e => e["isoptional"] = e["optional"]); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments