forked from EncompassRest/EncompassRest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchemas.cs
204 lines (174 loc) · 7.23 KB
/
Schemas.cs
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using EncompassREST.Exceptions;
using EncompassREST.HelperClasses;
using EncompassREST.JSONHelpers;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace EncompassREST
{
public class Schemas
{
private string API_PATH = "encompass/v1/schema";
private Session _Session;
public Schemas(Session Session)
{
_Session = Session;
}
public Session Session
{
get { return _Session; }
}
public async Task<string> GetSchemaAsync()
{
return await GetSchemaAsync(null, true);
}
public async Task<string> GetSchemaAsync(IList<string> entities,bool includeFieldExtensions)
{
RequestParameters rp = new RequestParameters();
if (entities != null &&
entities.Count > 0)
{
rp.Add("entities", String.Join(",", entities));
}
rp.Add("includeFieldExtensions", includeFieldExtensions.ToString());
HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Get, string.Format(API_PATH + "/loan{0}",rp.ToString()));
var response = await _Session.RESTClient.SendAsync(message);
//await _Session.RESTClient.GetAsync(API_PATH + "/loan" + rp.ToString());
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
return await response.Content.ReadAsStringAsync();
}
else
{
throw new RESTException("getSchema", response);
}
}
public async Task<string> GetSchemaFieldAsync(string FieldID)
{
HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Get, string.Format(API_PATH + "/loan/{0}", FieldID));
//var response = await _Session.RESTClient.GetAsync(API_PATH + "/loan/" + FieldID);
var response = await _Session.RESTClient.SendAsync(message);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
return await response.Content.ReadAsStringAsync();
}
else
{
throw new RESTException("GetSchemaField", response);
}
}
public async Task<string> GetFieldPathAsync(string FieldID)
{
string schemaJson;
StringBuilder ReturnPath = new StringBuilder();
try
{
schemaJson = await GetSchemaFieldAsync(FieldID);
}
catch (RESTException re)
{
throw new RESTException("GetSchemaFieldAsync", re.Response);
}
JObject jsonMain = JObject.Parse(schemaJson);
var entityTypes = jsonMain["entity_types"];
foreach (var token in entityTypes)
{
var p = (JProperty)token;
ReturnPath.Append(p.Name + ".");
}
//jsonMain["entity_types"].Last["properties"]
var fieldIDTokens = jsonMain.FindTokens("field_id");
if (fieldIDTokens.Count == 1)
{
var path = fieldIDTokens.FirstOrDefault().Path;
var itemsInPath = path.Split('.');
ReturnPath.Append(itemsInPath.GetValue(itemsInPath.Count() - 2));
}
else
{
var fieldInstanceTokens = jsonMain.FindTokens("field_instances");
var path = fieldInstanceTokens.FirstOrDefault().Path;
var itemsInPath = path.Split('.');
ReturnPath.Append(itemsInPath.GetValue(itemsInPath.Count() - 2));
}
return ReturnPath.ToString();
}
public async Task GenerateClassFilesFromSchemaAsync(string DestinationPath,string Namespace)
{
string RawSchema = await GetSchemaAsync();
JToken jo = JToken.Parse(RawSchema);
var entities = jo["entity_types"];
foreach (JToken jt in entities.Children())
await GenerateClassFileFromSchemaAsync(DestinationPath,Namespace,((JProperty)jt).Name,jo);
}
private async Task GenerateClassFileFromSchemaAsync(string DestinationPath, string Namespace, string Section, JToken SchemaToken)
{
string entity;
StringBuilder sb = new StringBuilder();
string partial = (Section == "Loan") ? " partial " : "";
sb.Append(
@"using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace " + Namespace + @"
{
public " + partial + "class " + Section + @"
{"
);
var section = SchemaToken["entity_types"][Section]["properties"];
foreach (JToken SectionToken in section.Children())
{
JProperty VariableProperty = (JProperty)SectionToken;
string vName = VariableProperty.Name;
vName = vName.Substring(0, 1).ToLower() + vName.Substring(1); //set proper case
var vType = SchemaToken["entity_types"][Section]["properties"][VariableProperty.Name]["type"];
switch(vType.ToString())
{
case "string":
case "uuid":
sb.AppendLine(" public string " + vName + " { get; set; }");
break;
case "decimal":
case "bool":
case "int":
sb.AppendLine(" public " + vType.ToString() +"? " + vName + " { get; set; }");
break;
case "date":
case "datetime":
sb.AppendLine(" public DateTime? " + vName + " { get; set; }");
break;
//validate reserialization of these elements
case "set":
case "list":
entity = SchemaToken["entity_types"][Section]["properties"][VariableProperty.Name]["element_type"].ToString();
entity = entity.Substring(0, 1).ToUpper() + entity.Substring(1);
sb.AppendLine(" public List<"+entity + "> " + vName + " { get; set; }");
break;
case "entity":
entity = SchemaToken["entity_types"][Section]["properties"][VariableProperty.Name]["element_type"].ToString();
entity = entity.Substring(0, 1).ToUpper() + entity.Substring(1);
sb.AppendLine(" public " + entity + " " + vName + " { get; set; }");
break;
default:
sb.AppendLine(" public PROBLEM<" + VariableProperty.Name + "> " + vName + " { get; set; }");
break;
}
}
sb.Append(
@" }
}"
);
using (StreamWriter sw = new StreamWriter(Path.Combine(DestinationPath, Section + ".cs")))
{
await sw.WriteAsync(sb.ToString());
}
}
}
}