-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
378 lines (364 loc) · 16.4 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
using System;
using System.IO;
using System.Configuration;
using SC.API.ComInterop;
using SC.API.ComInterop.Models;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Net;
using System.Threading;
using OfficeOpenXml;
using System.Linq;
// Grabs story info from Sharpcloud and converts the data into a relationship sheet and item sheet.
namespace SCExporter
{
class Program
{
public class Sharp
{
public SharpCloudApi sc { get; set; }
public Story story { get; set; }
public ExcelWorksheet sheet { get; set; }
public int order { get; set; }
public int attCount { get; set; }
public List<SC.API.ComInterop.Models.Attribute> attList {get; set;}
public int sheetLine { get; set; }
}
static Thread firstHalf,downloader;
static void Main(string[] args)
{
var fileLocation = System.IO.Directory.GetParent
(System.IO.Directory.GetParent(Environment.CurrentDirectory)
.ToString()).ToString();
// Loads user setting from config file
var teamstoryid = ConfigurationManager.AppSettings["teamstoryid"];
var portfolioid = ConfigurationManager.AppSettings["portfolioid"];
var templateid = ConfigurationManager.AppSettings["templateid"];
var userid = ConfigurationManager.AppSettings["user"];
var passwd = ConfigurationManager.AppSettings["pass"];
var URL = ConfigurationManager.AppSettings["URL"];
var storyID = ConfigurationManager.AppSettings["story"];
MatchCollection matchUrl = Regex.Matches(storyID, @"story\/(.+)\/view");
string[] matchGroup = null;
string storyUrl = "";
if(matchUrl.Count > 0)
{
matchGroup = matchUrl[0].ToString().Split('/');
storyUrl = matchGroup[1 ];
}
else
{
storyUrl = storyID;
}
// Login and get story data from Sharpcloud
var sc = new SharpCloudApi(userid, passwd, URL);
var story = sc.LoadStory(storyUrl);
//Create EPPlus and create a workbook with 2 spreadsheets
FileInfo newFile = new FileInfo(fileLocation + "\\combine.xlsx");
ExcelPackage pck = new ExcelPackage(newFile);
//Add the Content sheets
var itemSheet = pck.Workbook.Worksheets.FirstOrDefault(x => x.Name == "Items");
if (itemSheet == null)
{
itemSheet = pck.Workbook.Worksheets.Add("Items");
}
var relationshipSheet = pck.Workbook.Worksheets.First();
if (pck.Workbook.Worksheets.Count > 1)
relationshipSheet = pck.Workbook.Worksheets.ElementAt(1);
if (relationshipSheet == itemSheet)
{
relationshipSheet = pck.Workbook.Worksheets.Add("RelationshipSheet");
}
//Setting up the threads
object a, b;
Sharp first = new Sharp();
Sharp second = new Sharp();
first.sc = sc; first.story = story; first.sheet = itemSheet;
first.order = 0;
second.story = story;
second.order = 1;
a = first;
b = second;
// Initial Item Header list
var headList = new List<string> { "Name", "Description", "Category", "Start", "Duration",
"Resources", "Tags", "Panels", "Subcategory", "AttCount", "cat_color", "file_path" };
// Filters the default attributes from the story
var attData = story.Attributes;
var attList = new List<SC.API.ComInterop.Models.Attribute>();
Regex regex = new Regex(@"none|None|Sample");
var attCount = 0;
foreach (var att in attData)
{
// Checks to see if attribute header is a default attritube.
Match match = regex.Match(att.Name);
if (!match.Success)
{
// Adds non-default attribute to the List and to the header line
attList.Add(att);
attCount++;
headList.Add(att.Name + "|" + att.Type + "|" + att.Description);
}
}
var go = 1;
foreach (var head in headList)
{
itemSheet.Cells[1, go].Value = head;
go++;
}
first.attList = attList;
first.sheetLine = 2;
first.attCount = attCount;
// Insert data into 2 spreadsheets
firstHalf = new Thread(new ParameterizedThreadStart(downloadFiles));
downloader = new Thread(new ParameterizedThreadStart(downloadFiles));
firstHalf.Start(a);
downloader.Start(b);
ItemSheet(a);
RelationshipSheet(story, relationshipSheet);
pck.SaveAs(newFile);
/*
Console.WriteLine("Resource Tags");
foreach (var resTag in story.ResourceTags)
{
Console.WriteLine(resTag.Name);
}
*/
}
// Grabs all relationship data between 2 items
private static void RelationshipSheet(Story Story, OfficeOpenXml.ExcelWorksheet relationshipSheet)
{
// file path variable
// Header Line
relationshipSheet.Cells["A1"].Value = "Item 1";
relationshipSheet.Cells["B1"].Value = "Item 2";
relationshipSheet.Cells["C1"].Value = "Direction";
relationshipSheet.Cells["D1"].Value = "Tags";
relationshipSheet.Cells["E1"].Value = "Comment";
var goAtt = 6;
foreach(var att in Story.RelationshipAttributes)
{
relationshipSheet.Cells[1, goAtt].Value = att.Name + "|" + att.Type;
goAtt++;
}
var count = 2;
// Parse through relationship data
foreach (var line in Story.Relationships)
{
relationshipSheet.Cells[count, 1].Value = line.Item1.Name;
relationshipSheet.Cells[count, 2].Value = line.Item2.Name;
relationshipSheet.Cells[count, 3].Value = line.Direction.ToString();
var tagLine = "";
foreach(var lineTag in line.Tags)
{
tagLine += lineTag.Text + "|";
}
relationshipSheet.Cells[count, 4].Value = tagLine;
relationshipSheet.Cells[count, 5].Value = line.Comment;
var go = 6;
foreach(var att in Story.RelationshipAttributes)
{
relationshipSheet.Cells[count, go].Value = line.GetAttributeValueAsText(Story.RelationshipAttribute_FindByName(att.Name));
go++;
}
count++;
}
//Write data to file
Console.WriteLine("Relationship sheet written");
}
//Grabs all item data with their attributes
private static void ItemSheet(object Sharp)
{
var fileLocation = System.IO.Directory.GetParent
(System.IO.Directory.GetParent(Environment.CurrentDirectory)
.ToString()).ToString();
Sharp sharp = Sharp as Sharp;
var story = sharp.story;
var sc = sharp.sc;
var catData = story.Categories;
var attList = sharp.attList;
var attCount = sharp.attCount;
var itemSheet = sharp.sheet;
var order = sharp.order;
var sheetLine = sharp.sheetLine;
// Goes through items in category order
foreach (var cat in catData)
{
foreach(var item in story.Items)
{
// check to see if category matches item category
if (item.Category.Name == cat.Name)
{
// Creates the initial list for the item
var itemList = new List<string> { item.Name, item.Description, item.Category.Name, item.StartDate.ToString(), item.DurationInDays.ToString() };
//Goes through the item's resources
var resLine = "";
if (item.Resources.Length > 0)
{
foreach (var res in item.Resources)
{
//downloads resource file if there is a file extension to file
if (res.FileExtension != null)
{
resLine += res.Name + "~" + res.Name + "*" + res.FileExtension + "|";
}
// Gets the url for a website
else
{
resLine += res.Name + "~" + res.Url + "|";
}
}
}
// Item has no resources
else
{
resLine = "null";
}
// Add the resource data to list
itemList.Add(resLine);
// Adds the tags to the list
var tagLine = "";
if (item.Tags.Length > 0)
{
foreach (var tag in item.Tags)
{
tagLine += tag.Text + "|";
}
}
else
{
tagLine = "null";
}
itemList.Add(tagLine);
// Adds the panels to the list
var panLine = "";
// Check to see if item has any panels containing data
var dataCount = 0;
foreach (var pan in item.Panels)
{
// Check to see if panel data is empty
if (pan.Data.ToString() != "_EMPTY_")
{
dataCount++;
panLine += pan.Title + "@" + pan.Type + "@" + pan.Data + "|";
}
}
if (dataCount == 0)
{
panLine = "null";
}
itemList.Add(panLine);
// adds the sub category to the item
var subLine = "";
// checks to see if item has a subcategory
try
{
subLine = item.SubCategory.Name;
}
catch
{
subLine = "null";
}
itemList.Add(subLine);
itemList.Add(attCount.ToString());
//Adds the color of the category to the list
var colors = (cat.Color.A + "|" + cat.Color.R + "|" + cat.Color.G + "|" + cat.Color.B).ToString();
itemList.Add(colors);
// check to see if item has a image based off the sharpcloud image url
Regex zeroImage = new Regex(@"00000000");
Match zeroMatch = zeroImage.Match(item.ImageUri.ToString());
// Downloads image to folder if url is not all 0s
itemList.Add(fileLocation + "\\" + "Files" + "\\");
string[] itemLine = itemList.ToArray();
var go = 1;
foreach (var itemCell in itemLine)
{
itemSheet.Cells[sheetLine, go].Value = itemCell;
if (go == 5)
{
itemSheet.Cells[sheetLine, go].Value = Double.Parse(itemCell);
itemSheet.Cells[sheetLine, go].Style.Numberformat.Format = "#";
}
if (go == 10)
{
itemSheet.Cells[sheetLine, go].Value = Double.Parse(itemCell);
}
go++;
}
// Adds the attributes to the item
foreach (var att in attList)
{
switch (att.Type.ToString())
{
case "Text":
itemSheet.Cells[sheetLine, go].Value = item.GetAttributeValueAsText(att);
break;
case "Numeric":
itemSheet.Cells[sheetLine, go].Value = item.GetAttributeValueAsDouble(att);
itemSheet.Cells[sheetLine, go].Style.Numberformat.Format = "0.00";
break;
case "Date":
itemSheet.Cells[sheetLine, go].Value = item.GetAttributeValueAsDate(att);
itemSheet.Cells[sheetLine, go].Style.Numberformat.Format = "mm/dd/yyyy hh:mm:ss AM/PM";
break;
case "List":
itemSheet.Cells[sheetLine, go].Value = item.GetAttributeValueAsText(att);
break;
case "Location":
itemSheet.Cells[sheetLine, go].Value = item.GetAttributeValueAsText(att);
break;
}
go++;
}
// Adds entire list to the row for the item.
sheetLine++;
}
}
}
// Writes file to disk
Console.WriteLine("ItemSheet Written");
}
private static void downloadFiles(object Sharp)
{
var fileLocation = System.IO.Directory.GetParent
(System.IO.Directory.GetParent(Environment.CurrentDirectory)
.ToString()).ToString();
Sharp sharp = Sharp as Sharp;
var story = sharp.story;
int order = sharp.order;
var a = 0;
var b = (story.Items.Length / 2) - 1;
if(order > 0)
{
a = story.Items.Length / 2;
b = story.Items.Length;
}
for (var i = a; i < b;i++)
{
//Goes through the item's resources
if (story.Items[i].Resources.Length > 0)
{
foreach (var res in story.Items[i].Resources)
{
//downloads resource file if there is a file extension to file
if (res.FileExtension != null)
{
res.DownloadFile(fileLocation + "\\Files\\" + res.Name + res.FileExtension);
}
}
}
// check to see if item has a image based off the sharpcloud image url
Regex zeroImage = new Regex(@"00000000");
Match zeroMatch = zeroImage.Match(story.Items[i].ImageUri.ToString());
// Downloads image to folder if url is not all 0s
if (!zeroMatch.Success)
{
using (WebClient client = new WebClient())
{
client.DownloadFile(story.Items[i].ImageUri, (fileLocation + "\\" + "Files" + "\\" + story.Items[i].Name + ".jpg"));
}
}
}
Console.WriteLine("Files Downloaded");
}
}
}