Skip to content

Commit 4999cd9

Browse files
committed
v1.1.0
- New features -- Photo Download -- Document Upload
1 parent c4df441 commit 4999cd9

File tree

9 files changed

+351
-13
lines changed

9 files changed

+351
-13
lines changed

CLARiNET.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net5.0</TargetFramework>
5+
<TargetFramework>net6.0</TargetFramework>
66
<Authors>Whitley Media</Authors>
77
<PackageIcon>CLARiNET.ico</PackageIcon>
88
<PackageIconUrl />
99
<ApplicationIcon>CLARiNET.ico</ApplicationIcon>
10-
<Version>1.0.9</Version>
10+
<Version>1.1.0</Version>
1111
</PropertyGroup>
1212

1313
<ItemGroup>

CommandLineOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace CLARiNET
99
{
1010
public class Options
1111
{
12-
[Value(index: 0, MetaName = "Command", Required = false, HelpText = "CLARiNET Commands:\n\nCLAR_UPLOAD\nCLAR_DOWNLOAD\nDRIVE_UPLOAD\nDRIVE_TRASH")]
12+
[Value(index: 0, MetaName = "Command", Required = false, HelpText = "CLARiNET Commands:\n\nCLAR_UPLOAD\nCLAR_DOWNLOAD\nDRIVE_UPLOAD\nDRIVE_TRASH\nPHOTO_DOWNLOAD\nDOCUMENT_UPLOAD")]
1313
public string Command { get; set; }
1414

1515
[Value(index: 1, MetaName = "File or Directory", Required = false, HelpText = "Path or Path and file name")]

Document.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Xml;
8+
using System.Xml.Linq;
9+
using System.Xml.XPath;
10+
11+
namespace CLARiNET
12+
{
13+
internal static class Documents
14+
{
15+
public static string Upload(string file, byte[] bytes, string soapUrl, string processedDir, Options options)
16+
{
17+
string xmlData = "";
18+
string result = "";
19+
20+
try
21+
{
22+
xmlData = Resources.Put_Worker_Document_Request;
23+
string[] fileVars = Path.GetFileName(file).Split("~");
24+
if (fileVars.Length > 1)
25+
{
26+
Console.WriteLine("\n\nProcessing {0} for {1}", fileVars[1], fileVars[0]);
27+
string workerId = fileVars[0];
28+
string filename = fileVars[1];
29+
string workerType = "Employee_ID";
30+
string comment = "";
31+
string contentType = "";
32+
if (fileVars.Length > 2)
33+
{
34+
filename = fileVars[2];
35+
if (fileVars[1].ToUpper() == "C")
36+
{
37+
workerType = "Contingent_Worker_ID";
38+
}
39+
}
40+
// worker id ~ filename
41+
// worker id ~ C ~ filename
42+
// TODO: Manifest for comment and content type?
43+
xmlData = xmlData.Replace("{workerId}", workerId)
44+
.Replace("{filename}", filename)
45+
.Replace("{workerIdType}", workerType)
46+
.Replace("{filedata}", Convert.ToBase64String(bytes))
47+
.Replace("{documentCategory}", options.Parameters)
48+
.Replace("{comment}", comment)
49+
.Replace("{contentType}", contentType);
50+
51+
result = WDWebService.CallAPI(options.Username + "@" + options.Tenant, options.Password, soapUrl, xmlData);
52+
if (result.IndexOf("<?xml") == 0)
53+
{
54+
string processedFile = Path.Combine(processedDir, Path.GetFileName(file));
55+
int num = 2;
56+
while (File.Exists(processedFile) && num < 100)
57+
{
58+
processedFile = Path.Combine(processedDir, Path.GetFileNameWithoutExtension(file) + "." + num.ToString("000") + Path.GetExtension(file));
59+
num++;
60+
}
61+
File.Move(file, processedFile);
62+
result = String.Format("Processed {0} for {1}", filename, workerId);
63+
}
64+
}
65+
}
66+
catch (Exception ex)
67+
{
68+
Console.WriteLine("\n\nError: " + ex.Message);
69+
Console.WriteLine("\n");
70+
}
71+
72+
return result;
73+
}
74+
}
75+
}

Extensions.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CLARiNET
8+
{
9+
public static class Extensions
10+
{
11+
public static IEnumerable<IEnumerable<TSource>> Batch<TSource>(
12+
this IEnumerable<TSource> source, int batchSize)
13+
{
14+
var items = new TSource[batchSize];
15+
var count = 0;
16+
17+
foreach (var item in source)
18+
{
19+
items[count++] = item;
20+
if (count == batchSize)
21+
{
22+
yield return items;
23+
24+
items = new TSource[batchSize];
25+
count = 0;
26+
}
27+
}
28+
29+
if (count > 0)
30+
yield return items.Take(count);
31+
}
32+
}
33+
}

Photo.cs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Xml;
8+
using System.Xml.Linq;
9+
using System.Xml.XPath;
10+
11+
namespace CLARiNET
12+
{
13+
internal static class Photos
14+
{
15+
public static string Download(Options options, string file, string soapUrl)
16+
{
17+
string xmlData = "";
18+
string workerRef = Resources.Worker_Reference;
19+
string result = "";
20+
string path = Path.GetDirectoryName(file);
21+
22+
Dictionary<string,string> ids = File.ReadLines(file).Select(line => line.Split(',')).ToDictionary(line => line[0], line => line.Count() > 1 ? line[1] : "");
23+
24+
List<string> workerRefList = new List<string>();
25+
try
26+
{
27+
foreach (var batch in ids.Batch(50))
28+
{
29+
try
30+
{
31+
xmlData = Resources.Get_Worker_Photos_Request;
32+
foreach (var item in batch)
33+
{
34+
if (!String.IsNullOrEmpty(item.Key))
35+
{
36+
string idType = item.Value.Trim();
37+
if (String.IsNullOrEmpty(idType))
38+
{
39+
idType = "Employee_ID";
40+
}
41+
workerRefList.Add(workerRef.Replace("{id}", item.Key.Trim())
42+
.Replace("{id_type}", idType));
43+
}
44+
}
45+
46+
xmlData = xmlData.Replace("{worker_references}", String.Join(" ", workerRefList.ToArray()));
47+
result = WDWebService.CallAPI(options.Username + "@" + options.Tenant, options.Password, soapUrl, xmlData);
48+
if (result.IndexOf("<?xml") == 0)
49+
{
50+
using (StringReader sr = new StringReader(result))
51+
{
52+
XPathDocument doc = new XPathDocument(sr);
53+
XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
54+
ns.AddNamespace("wd", "urn:com.workday/bsvc");
55+
56+
XPathNodeIterator nodes = doc.CreateNavigator().Select("//wd:Worker_Photo", ns);
57+
foreach(XPathNavigator item in nodes)
58+
{
59+
try
60+
{
61+
string emplid = item.SelectSingleNode("wd:Worker_Photo_Data/wd:ID", ns).Value;
62+
string filename = item.SelectSingleNode("wd:Worker_Photo_Data/wd:Filename", ns).Value;
63+
string photo = item.SelectSingleNode("wd:Worker_Photo_Data/wd:File", ns).Value;
64+
string fileOut = Path.Combine(path, emplid + "~" + filename);
65+
byte[] bytes = Convert.FromBase64String(photo);
66+
if (!File.Exists(fileOut) || (File.Exists(fileOut) && bytes.Length != new FileInfo(fileOut).Length))
67+
{
68+
File.WriteAllBytes(fileOut, bytes);
69+
}
70+
}
71+
catch (Exception ex3)
72+
{
73+
Console.WriteLine("\n\nError: " + ex3.Message);
74+
Console.WriteLine("\n");
75+
}
76+
}
77+
}
78+
}
79+
80+
}
81+
catch(Exception batchEx)
82+
{
83+
Console.WriteLine("\n\nError: " + batchEx.Message);
84+
Console.WriteLine("\n");
85+
}
86+
}
87+
result = String.Format("Processed {0:N0} id{1}.", ids.Count(), ids.Count() == 1 ? "" : "s");
88+
}
89+
catch (Exception ex)
90+
{
91+
Console.WriteLine("\n\nError: " + ex.Message);
92+
Console.WriteLine("\n");
93+
}
94+
95+
return result;
96+
}
97+
98+
99+
}
100+
}

Program.cs

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@ static void Main(string[] args)
6464
switch (options.Command)
6565
{
6666
case Command.CLAR_UPLOAD:
67-
case Command.CLAR_DOWNLOAD:
67+
case Command.CLAR_DOWNLOAD:
6868
break;
6969
case Command.DRIVE_UPLOAD:
7070
case Command.DRIVE_TRASH:
71+
case Command.PHOTO_DOWNLOAD:
72+
case Command.DOCUMENT_UPLOAD:
7173
soapUrl = SoapUrlBuild(options.Command);
7274
break;
7375
}
@@ -79,18 +81,32 @@ static void Main(string[] args)
7981

8082
files = Directory.GetFiles(options.Path, searchPattern, new EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive });
8183

82-
// Special file handling
84+
// Special file handling and console update
8385
switch (options.Command)
84-
{
86+
{
87+
case Command.CLAR_UPLOAD:
88+
Console.WriteLine("\n\nDeploying the CLAR and awaiting the result...\n\n");
89+
break;
8590
case Command.CLAR_DOWNLOAD:
91+
Console.WriteLine("\n\nDownloading the CLAR and awaiting the result...\n\n");
8692
files = new string[] { Path.Combine(options.Path, options.Parameters + "." + DateTime.Now.ToString("s").Replace(":", ".") + ".clar") };
8793
break;
94+
case Command.DRIVE_UPLOAD:
95+
Console.WriteLine("\n\nUploading files...\n\n");
96+
break;
8897
case Command.DRIVE_TRASH:
98+
Console.WriteLine("\n\nTrashing files...\n\n");
8999
if (files.Length > 0)
90100
{
91101
files = File.ReadAllLines(files[0]);
92102
}
93103
break;
104+
case Command.PHOTO_DOWNLOAD:
105+
Console.WriteLine("\n\nDownloading photos...\n\n");
106+
break;
107+
case Command.DOCUMENT_UPLOAD:
108+
Console.WriteLine("\n\nUploading documents...\n\n");
109+
break;
94110
default:
95111
break;
96112
}
@@ -103,11 +119,9 @@ static void Main(string[] args)
103119
{
104120
case Command.CLAR_UPLOAD:
105121
bytes = File.ReadAllBytes(file);
106-
Console.WriteLine("\n\nDeploying the CLAR and awaiting the result...\n\n");
107122
result = Encoding.Default.GetString(WDWebService.CallRest(options.Tenant, options.Username + "@" + options.Tenant, options.Password, restUrl, WebRequestMethods.Http.Put, bytes));
108123
break;
109-
case Command.CLAR_DOWNLOAD:
110-
Console.WriteLine("\n\nDownloading the CLAR and awaiting the result...\n\n");
124+
case Command.CLAR_DOWNLOAD:
111125
bytes = WDWebService.CallRest(options.Tenant, options.Username + "@" + options.Tenant, options.Password, restUrl + "?fmt=clar", WebRequestMethods.Http.Get, null);
112126
File.WriteAllBytes(file, bytes);
113127
result = Encoding.Default.GetString(WDWebService.CallRest(options.Tenant, options.Username + "@" + options.Tenant, options.Password, restUrl, WebRequestMethods.Http.Get, null));
@@ -117,13 +131,20 @@ static void Main(string[] args)
117131
xnm.AddNamespace("default", "urn:com.workday/esb/cloud/10.0");
118132
result = "Last Uploaded to Workday: " + DateTime.Parse(xDoc.XPathSelectElement("//default:deployed-since", xnm).Value).ToLocalTime().ToString("s");
119133
break;
120-
case Command.DRIVE_UPLOAD:
134+
case Command.DRIVE_UPLOAD:
121135
bytes = File.ReadAllBytes(file);
122136
result = DriveUpload(file, bytes, soapUrl, options.Username);
123137
break;
124-
case Command.DRIVE_TRASH:
138+
case Command.DRIVE_TRASH:
125139
result = DriveTrash(file, soapUrl, options.Username);
126140
break;
141+
case Command.PHOTO_DOWNLOAD:
142+
result = Photos.Download(options, file, soapUrl);
143+
break;
144+
case Command.DOCUMENT_UPLOAD:
145+
bytes = File.ReadAllBytes(file);
146+
result = Documents.Upload(file, bytes, soapUrl, processedDir, options);
147+
break;
127148
}
128149

129150
if (result.ToLower().IndexOf("<html") >= 0)
@@ -260,8 +281,17 @@ static bool InitOptions(string[] args)
260281
break;
261282
case Command.DRIVE_UPLOAD:
262283
case Command.DRIVE_TRASH:
284+
263285
searchPattern = options.Parameters;
264286
break;
287+
case Command.PHOTO_DOWNLOAD:
288+
searchPattern = options.Parameters;
289+
break;
290+
case Command.DOCUMENT_UPLOAD:
291+
searchPattern = "*.*";
292+
break;
293+
default:
294+
break;
265295
}
266296
}
267297

@@ -319,7 +349,9 @@ static void CommandOption()
319349
case Command.CLAR_UPLOAD:
320350
case Command.CLAR_DOWNLOAD:
321351
case Command.DRIVE_UPLOAD:
322-
case Command.DRIVE_TRASH:
352+
case Command.DRIVE_TRASH:
353+
case Command.PHOTO_DOWNLOAD:
354+
case Command.DOCUMENT_UPLOAD:
323355
break;
324356
default:
325357
throw new Exception("Invalid command. Please use --help for a list of valid commands.");
@@ -345,6 +377,14 @@ static void PathOption()
345377
case Command.DRIVE_TRASH:
346378
options.Path = appDir;
347379
break;
380+
case Command.PHOTO_DOWNLOAD:
381+
options.Path = appDir;
382+
break;
383+
case Command.DOCUMENT_UPLOAD:
384+
options.Path = inboundDir;
385+
break;
386+
default:
387+
break;
348388
}
349389
}
350390
Console.WriteLine("Processing: " + options.Path + "\n");
@@ -376,6 +416,15 @@ static void ParameterOption()
376416
options.Parameters = "*trash*";
377417
searchPattern = options.Parameters;
378418
break;
419+
case Command.PHOTO_DOWNLOAD:
420+
Console.WriteLine("Enter the id file name:\n");
421+
options.Parameters = Console.ReadLine().Trim();
422+
Console.WriteLine("");
423+
searchPattern = options.Parameters;
424+
break;
425+
case Command.DOCUMENT_UPLOAD:
426+
searchPattern = "*.*";
427+
break;
379428
}
380429
}
381430

@@ -492,6 +541,12 @@ static string SoapUrlBuild(string command)
492541
case Command.DRIVE_TRASH:
493542
soapUrl += "/{tenant}/Drive/{version}";
494543
break;
544+
case Command.PHOTO_DOWNLOAD:
545+
soapUrl += "/{tenant}/Human_Resources/{version}";
546+
break;
547+
case Command.DOCUMENT_UPLOAD:
548+
soapUrl += "/{tenant}/Staffing/{version}";
549+
break;
495550
}
496551

497552
return soapUrl;

0 commit comments

Comments
 (0)