-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
301 lines (272 loc) · 13.1 KB
/
Program.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.IO;
using CommandLine;
using System.Reflection;
using System.Net;
using System.Text;
using System.Xml.XPath;
using System.Xml;
using HtmlAgilityPack;
namespace CLARiNET
{
class Program
{
static List<XElement> envs = null;
static Options options = new Options();
static string appDir = null;
static string inboundDir = null;
static string processedDir = null;
static string[] files = null;
static string searchPattern = "*.clar";
static string host = "";
static string cloudCollection = "";
const string ccxUrl = "https://{host}/ccx";
const string cloudRepoUrl = ccxUrl + "/cc-cloud-repo/collections/";
static void Main(string[] args)
{
string soapUrl = "";
string restUrl = "";
appDir = AppDomain.CurrentDomain.BaseDirectory;
inboundDir = Path.Combine(appDir, "inbound");
processedDir = Path.Combine(appDir, "processed");
try
{
Console.WriteLine("\n** CLARiNET by Whitley Media **\n\n");
InitDirectories(inboundDir, processedDir);
if (!InitOptions(args))
{
return;
}
if (options.Encrypt)
{
CommandLineAuth.OptionsEncrypt();
return;
}
if (options.PrintEnvironments)
{
CommandLineEnv.PrintEnvironments(envs);
return;
}
if (options != null)
{
// Command
options.Command = CommandLineCommand.CommandGet(options.Command);
options.Command = CommandLineCommand.CommandValidate(options.Command);
// Path
options.Path = CommandLinePath.PathOption(options.Path, options.Command);
CommandLinePath.PathValidate(options.Path);
// Parameters
(options.Parameters, cloudCollection, searchPattern) = CommandLineParameters.ParameterOption(options.Command, options.Parameters, cloudCollection, searchPattern);
(options.Parameters, cloudCollection, searchPattern) = CommandLineParameters.ParameterValidate(options.Command, options.Parameters, cloudCollection, searchPattern);
// Files Check
files = CommandLineFiles.FilesCheck(options.Command, options.Path, searchPattern);
// Environment
(options.EnvNum, host) = CommandLineEnv.EnvironmentOption(options.EnvNum, envs);
// Tenant
options.Tenant = CommandLineTenant.TenantOption(options.Tenant);
// Username
options.Username = CommandLineAuth.UsernameOption(options.Username);
// Password
options.Password = CommandLineAuth.PasswordOption(options.Password);
}
string quote = "'";
if (OperatingSystem.IsWindows())
{
quote = "\"";
}
Console.WriteLine("\n\n");
Console.WriteLine("clarinet {0} {1} {2} {3} {4} {5} {6}",
options.Command,
quote + options.Path + quote,
quote + options.Parameters + quote,
host,
options.Tenant,
options.Username,
Crypto.Protect(options.Password));
Console.WriteLine("\n\n");
if (options.PrintCommandline)
{
return;
}
// Post Init and UI Option Handling
switch (options.Command.ToEnum<Command>())
{
case Command.CLAR_UPLOAD:
case Command.CLAR_DOWNLOAD:
break;
case Command.DRIVE_UPLOAD:
case Command.DRIVE_TRASH:
case Command.PHOTO_DOWNLOAD:
case Command.PHOTO_UPLOAD:
case Command.DOCUMENT_UPLOAD:
case Command.CANDIDATE_ATTACHMENT_UPLOAD:
soapUrl = SoapUrlBuild(options.Command);
break;
}
// API Call
string result = "";
restUrl = cloudRepoUrl.Replace("{host}", host) + cloudCollection;
soapUrl = soapUrl.Replace("{tenant}", options.Tenant).Replace("{version}", "v37.0");
files = Directory.GetFiles(options.Path, searchPattern, new EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive });
// Special file handling and console update
switch (options.Command.ToEnum<Command>())
{
case Command.CLAR_UPLOAD:
Console.WriteLine("\n\nDeploying the CLAR and awaiting the result...\n\n");
break;
case Command.CLAR_DOWNLOAD:
Console.WriteLine("\n\nDownloading the CLAR and awaiting the result...\n\n");
files = new string[] { Path.Combine(options.Path, options.Parameters + "." + DateTime.Now.ToString("s").Replace(":", ".") + ".clar") };
break;
case Command.DRIVE_UPLOAD:
Console.WriteLine("\n\nUploading files...\n\n");
WDContentType.Load();
break;
case Command.DRIVE_TRASH:
Console.WriteLine("\n\nTrashing files...\n\n");
if (files.Length > 0)
{
files = File.ReadAllLines(files[0]);
}
break;
case Command.PHOTO_DOWNLOAD:
Console.WriteLine("\n\nDownloading photos...\n\n");
break;
case Command.PHOTO_UPLOAD:
Console.WriteLine("\n\nUploading photos...\n\n");
break;
case Command.DOCUMENT_UPLOAD:
Console.WriteLine("\n\nUploading documents...\n\n");
WDContentType.Load();
break;
case Command.CANDIDATE_ATTACHMENT_UPLOAD:
Console.WriteLine("\n\nUploading attachments...\n\n");
WDContentType.Load();
break;
default:
break;
}
foreach (string file in files)
{
Byte[] bytes;
switch (options.Command.ToEnum<Command>())
{
case Command.CLAR_UPLOAD:
bytes = File.ReadAllBytes(file);
result = Encoding.Default.GetString(WDWebService.CallRest(options.Tenant, options.Username + "@" + options.Tenant, options.Password, restUrl, WebRequestMethods.Http.Put, bytes));
break;
case Command.CLAR_DOWNLOAD:
bytes = WDWebService.CallRest(options.Tenant, options.Username + "@" + options.Tenant, options.Password, restUrl + "?fmt=clar", WebRequestMethods.Http.Get, null);
File.WriteAllBytes(file, bytes);
result = Encoding.Default.GetString(WDWebService.CallRest(options.Tenant, options.Username + "@" + options.Tenant, options.Password, restUrl, WebRequestMethods.Http.Get, null));
File.WriteAllText(file.Replace(".clar", ".xml"), result);
XDocument xDoc = XDocument.Parse(result);
XmlNamespaceManager xnm = new XmlNamespaceManager(new NameTable());
xnm.AddNamespace("default", "urn:com.workday/esb/cloud/10.0");
result = "Last Uploaded to Workday: " + DateTime.Parse(xDoc.XPathSelectElement("//default:deployed-since", xnm).Value).ToLocalTime().ToString("s");
break;
case Command.DRIVE_UPLOAD:
bytes = File.ReadAllBytes(file);
result = DriveProcess.DriveUpload(options, processedDir, file, bytes, soapUrl, options.Username);
break;
case Command.DRIVE_TRASH:
result = DriveProcess.DriveTrash(options, file, soapUrl, options.Username);
break;
case Command.PHOTO_DOWNLOAD:
result = Photos.Download(options, file, soapUrl);
break;
case Command.PHOTO_UPLOAD:
bytes = File.ReadAllBytes(file);
result = Photos.Upload(file, bytes, soapUrl, processedDir, options);
break;
case Command.DOCUMENT_UPLOAD:
bytes = File.ReadAllBytes(file);
result = Documents.Upload(file, bytes, soapUrl, processedDir, options);
break;
case Command.CANDIDATE_ATTACHMENT_UPLOAD:
bytes = File.ReadAllBytes(file);
result = CandidateAttachments.Upload(file, bytes, soapUrl, processedDir, options);
break;
}
if (result.ToLower().IndexOf("<html") >= 0)
{
Console.WriteLine("No XML response detected. Workday may be unavailable or your parameters are incorrect.");
}
else
{
if (!String.IsNullOrEmpty(result))
{
Console.WriteLine("Result for " + file + "\n");
Console.WriteLine(result);
Console.WriteLine("\n\n");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("\n\n" + ex.Message);
Console.WriteLine("\n");
}
}
static void InitDirectories(string inboundDir, string outboundDir)
{
// Create inbound/outbount directories
if (!Directory.Exists(inboundDir))
{
Directory.CreateDirectory(inboundDir);
}
if (!Directory.Exists(outboundDir))
{
Directory.CreateDirectory(outboundDir);
}
}
static bool InitOptions(string[] args)
{
// Parse Environments
XDocument xDoc = XDocument.Parse(ResourceFile.Read("WDEnvironments.xml"));
envs = new List<XElement>(xDoc.Descendants(XName.Get("env")));
// Custom Host Node
XElement custom = new XElement("env", new XElement("e2-endpoint") { Value = "{custom}"});
custom.SetAttributeValue("name", "Enter a Custom Host Name");
custom.SetAttributeValue("type", "impl");
envs.Add(custom);
// Parse Arguments
ParserResult <Options> pResult = Parser.Default.ParseArguments<Options>(args)
.WithParsed<Options>(o =>
{
options = o;
});
if (pResult.Tag == ParserResultType.NotParsed)
{
return false;
}
return true;
}
static string SoapUrlBuild(string command)
{
string soapUrl = ccxUrl.Replace("{host}", host);
soapUrl = WDWebService.GetServiceURL(soapUrl, options.Tenant, options.Username, options.Password);
switch (options.Command.ToEnum<Command>())
{
case Command.DRIVE_UPLOAD:
case Command.DRIVE_TRASH:
soapUrl += "/{tenant}/Drive/{version}";
break;
case Command.PHOTO_UPLOAD:
case Command.PHOTO_DOWNLOAD:
soapUrl += "/{tenant}/Human_Resources/{version}";
break;
case Command.DOCUMENT_UPLOAD:
soapUrl += "/{tenant}/Staffing/{version}";
break;
case Command.CANDIDATE_ATTACHMENT_UPLOAD:
soapUrl += "/{tenant}/Recruiting/{version}";
break;
}
return soapUrl;
}
}
}