1+ using Microsoft . Xrm . Sdk ;
2+ using System ;
3+ using System . Collections . Generic ;
4+ using ParamType = Rappen . XTB . CAT . Customapirequestparameter . Type_OptionSet ;
5+
6+ namespace Rappen . XTB . CAT
7+ {
8+ public class CATRequest
9+ {
10+ public ExecutionInfo Execution { get ; set ; }
11+ public string Name { get ; set ; }
12+ public EntityReference Target { get ; set ; }
13+ public List < CATRequestParameter > Parameters { get ; set ; }
14+
15+ internal CATRequest ( )
16+ { }
17+
18+ public CATRequest ( OrganizationRequest request )
19+ {
20+ Execution = new ExecutionInfo ( ) ;
21+ Name = request . RequestName ;
22+ if ( request . Parameters != null )
23+ {
24+ if ( request . Parameters . ContainsKey ( "Target" ) )
25+ {
26+ Target = request [ "Target" ] as EntityReference ;
27+ }
28+ foreach ( var parameter in request . Parameters )
29+ {
30+ if ( parameter . Key != "Target" )
31+ {
32+ if ( Parameters == null )
33+ {
34+ Parameters = new List < CATRequestParameter > ( ) ;
35+ }
36+ Parameters . Add ( new CATRequestParameter
37+ {
38+ Name = parameter . Key ,
39+ Type = ValueToParamType ( parameter . Value ) ,
40+ Value = ValueToString ( parameter . Value )
41+ } ) ;
42+ }
43+ }
44+ }
45+ }
46+
47+ public override string ToString ( ) => Execution ? . RunTime . ToString ( "HH:mm:ss" ) + " " + Name ;
48+
49+ private ParamType ValueToParamType ( object value )
50+ {
51+ if ( value is string ) return ParamType . String ;
52+ if ( value is bool ) return ParamType . Boolean ;
53+ if ( value is DateTime ) return ParamType . DateTime ;
54+ if ( value is decimal ) return ParamType . Decimal ;
55+ if ( value is Entity ) return ParamType . Entity ;
56+ if ( value is EntityCollection ) return ParamType . EntityCollection ;
57+ if ( value is EntityReference ) return ParamType . EntityReference ;
58+ if ( value is float ) return ParamType . Float ;
59+ if ( value is int ) return ParamType . Integer ;
60+ if ( value is Money ) return ParamType . Money ;
61+ if ( value is OptionSetValue ) return ParamType . Picklist ;
62+ if ( value is string ) return ParamType . String ;
63+ if ( value is string [ ] ) return ParamType . StringArray ;
64+ if ( value is Guid ) return ParamType . GuId ;
65+ throw new Exception ( $ "Incorrect value type: { value . GetType ( ) } value: { value } ") ;
66+ }
67+
68+ public OrganizationRequest Request
69+ {
70+ get
71+ {
72+ var request = new OrganizationRequest ( Name ) ;
73+ if ( Target != null )
74+ {
75+ request [ "Target" ] = Target ;
76+ }
77+ Parameters . ForEach ( p => request . Parameters . Add ( p . Name , StringToParamValue ( p . Type , p . Value ) ) ) ;
78+ return request ;
79+ }
80+ }
81+
82+ public static ParamType StringToParamType ( string type )
83+ {
84+ type = type
85+ . Replace ( "OptionSetValue" , "Picklist" )
86+ . Replace ( "Int32" , "Integer" )
87+ . Replace ( "Int64" , "Integer" )
88+ . Replace ( "Double" , "Float" ) ;
89+ if ( Enum . TryParse ( type , out ParamType paramtype ) )
90+ {
91+ return paramtype ;
92+ }
93+ throw new Exception ( $ "Incorrect type: { type } ") ;
94+ }
95+
96+ public static string ValueToString ( object value )
97+ {
98+ if ( value is OptionSetValue valueosv ) return valueosv . Value . ToString ( ) ;
99+ if ( value is EntityReference valueer ) return $ "{ valueer . LogicalName } :{ valueer . Id } ";
100+ else return value . ToString ( ) ;
101+ }
102+
103+ public static object StringToParamValue ( ParamType type , string value )
104+ {
105+ switch ( type )
106+ {
107+ case ParamType . String :
108+ return value ;
109+
110+ case ParamType . Integer :
111+ return Convert . ToInt64 ( value ) ;
112+
113+ case ParamType . Float :
114+ return Convert . ToSingle ( value ) ;
115+
116+ case ParamType . Decimal :
117+ return Convert . ToDecimal ( value ) ;
118+
119+ case ParamType . Money :
120+ return new Money ( Convert . ToDecimal ( value ) ) ;
121+
122+ case ParamType . GuId :
123+ return Guid . Parse ( value ) ;
124+
125+ case ParamType . DateTime :
126+ return Convert . ToDateTime ( value ) ;
127+
128+ case ParamType . Picklist :
129+ return new OptionSetValue ( Convert . ToInt16 ( value ) ) ;
130+
131+ case ParamType . EntityReference :
132+ var entrefstr = value . Split ( ':' ) ;
133+ return new EntityReference ( entrefstr [ 0 ] , Guid . Parse ( entrefstr [ 1 ] ) ) ;
134+
135+ case ParamType . Entity :
136+ case ParamType . EntityCollection :
137+ case ParamType . StringArray :
138+ throw new Exception ( $ "{ type } not supported") ;
139+
140+ default :
141+ throw new Exception ( $ "Unknown type: { type } ") ;
142+ }
143+ }
144+ }
145+
146+ public class ExecutionInfo
147+ {
148+ public DateTime RunTime { get ; set ; } = DateTime . Now ;
149+ public long Duration { get ; set ; }
150+ public string ErrorMessage { get ; set ; }
151+ public object Result { get ; set ; }
152+ }
153+
154+ public class CATRequestParameter
155+ {
156+ public ParamType Type { get ; set ; }
157+ public string Name { get ; set ; }
158+ public string Value { get ; set ; }
159+ }
160+ }
0 commit comments