-
Notifications
You must be signed in to change notification settings - Fork 0
/
CROGC - 334 - Convert Supporting Docs to Image.cs
196 lines (165 loc) · 9.73 KB
/
CROGC - 334 - Convert Supporting Docs to Image.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
using System;
using System.Collections.Generic;
using Hyland.Unity;
using UnityFileConversions;
namespace UnityScripts
{
/// <summary>
/// Description
/// </summary>
public class DBPR_UnityScript_275 : IWorkflowScript
{
#region User-Configurable Script Settings
// Script name for diagnostics logging
private const string ScriptName = "334 - CROGC - Convert Supporting Docs to Image";
// Diagnostics logging level - set to Verbose for testing, Error for production
private const Diagnostics.DiagnosticsLevel DiagLevel = Diagnostics.DiagnosticsLevel.Verbose;
// Date/Time format for diagnostics logging
private const string DateTimeFormat = "MM-dd-yyyy HH:mm:ss.fff";
private const string CRD_DOCUMENT_TYPE = "OGC - CR - Board Case File (PRECONV)";
private const long IMAGE_FILE_TYPE = 2;
private const long WORD_FILE_TYPE = 12;
private const long PDF_FILE_TYPE = 16;
private const string TEMP_DIRECTORY = @"C:\Temp\Unity\";
private const string CONVERTER_DIRECTORY = @"C:\Converter\FileConverter.exe";
private string strFilePath = "";
#endregion
/***********************************************
* USER/SE: PLEASE DO NOT EDIT BELOW THIS LINE *
***********************************************/
#region IWorkflowScript
/// <summary>
/// Implementation of <see cref="IWorkflowScript.OnWorkflowScriptExecute" />.
/// <seealso cref="IWorkflowScript" />
/// </summary>
/// <param name="app">Unity Application object</param>
/// <param name="args">Workflow event arguments</param>
public void OnWorkflowScriptExecute(Application app, WorkflowEventArgs args)
// public void OnWorkflowScriptExecute(Application app, WorkflowEventArgs args = null)
{
try
{
app.Diagnostics.WriteIf(Diagnostics.DiagnosticsLevel.Info, string.Format("{0} - Start Script - [{1}]", DateTime.Now.ToString(DateTimeFormat), ScriptName));
// Get the active document
Document objCurrentDocument = args.Document;
app.Diagnostics.WriteIf(Diagnostics.DiagnosticsLevel.Info, string.Format("Processing document: {0}", objCurrentDocument.ID.ToString()));
// Get the default rendition
Rendition objFormRendition = objCurrentDocument.DefaultRenditionOfLatestRevision;
DocumentType objCrdDocType = app.Core.DocumentTypes.Find(CRD_DOCUMENT_TYPE);
// Validate the document type
//DocumentType objCrdDocType = app.Core.DocumentTypes.Find(CRD_DOCUMENT_TYPE);
if (objCrdDocType == null)
{
throw new InvalidProgramException(string.Format("Document type \"{0}\" does not exist!", CRD_DOCUMENT_TYPE));
}
//If the doc is already an image, pull it in its default format and save the new copy
if (objFormRendition.FileType.ID == IMAGE_FILE_TYPE)
{
app.Diagnostics.WriteIf(Diagnostics.DiagnosticsLevel.Info, "Document is an image, save copy to new Doc Type");
DefaultDataProvider objDefaultProvider = app.Core.Retrieval.Default;
PageData objDefaultImageData = objDefaultProvider.GetDocument(objFormRendition);
StoreNewDocumentProperties objDocProps = app.Core.Storage.CreateStoreNewDocumentProperties(objCrdDocType, app.Core.FileTypes.Find(IMAGE_FILE_TYPE));
foreach (KeywordRecord objKeyRecord in objCurrentDocument.KeywordRecords)
{
if (objKeyRecord.KeywordRecordType.RecordType == RecordType.StandAlone || objKeyRecord.KeywordRecordType.RecordType == RecordType.SingleInstance)
{
foreach (Keyword objKeyword in objKeyRecord.Keywords)
{
if (objCrdDocType.KeywordRecordTypes.FindKeywordType(objKeyword.KeywordType.ID) != null)
{
objDocProps.AddKeyword(objKeyword);
}
}
}
else
{
EditableKeywordRecord objEditRecord = objKeyRecord.CreateEditableKeywordRecord();
objDocProps.AddKeywordRecord(objEditRecord);
}
}
// Store the new document
Document objNewDoc = null;
objNewDoc = app.Core.Storage.StoreNewDocument(objDefaultImageData, objDocProps);
if (objNewDoc == null)
{
throw new InvalidProgramException("Failed to store new document");
}
app.Diagnostics.WriteIf(Diagnostics.DiagnosticsLevel.Info, string.Format("Stored new document: {0}", objNewDoc.ID.ToString()));
// Clean up
objDefaultImageData.Dispose();
// If we got here, the script was successful
args.ScriptResult = true;
}
//If the Doc is Word or Excel, convert it with the conversion utility and save it
else if (objFormRendition.FileType.ID == WORD_FILE_TYPE || objFormRendition.FileType.ID == PDF_FILE_TYPE)
{
ConversionUtilities unityConverter = new ConversionUtilities(app, TEMP_DIRECTORY, CONVERTER_DIRECTORY);
app.Diagnostics.Write("Word/PDF file conversion");
List<FileDefinition> files = unityConverter.Convert(args.Document, UnityFileConversions.ImxFileType.Image, UnityFileConversions.ImportType.Document, app.Core.DocumentTypes.Find(CRD_DOCUMENT_TYPE));
unityConverter.CleanupFiles();
args.ScriptResult = true;
}
else
{
app.Diagnostics.WriteIf(Diagnostics.DiagnosticsLevel.Info, "Document is not image/Word/PDF, convert to new Doc Type");
ImageDataProvider objImageProvider = app.Core.Retrieval.Image;
PageData objImageData = objImageProvider.GetDocument(objFormRendition);
StoreNewDocumentProperties objDocProps = app.Core.Storage.CreateStoreNewDocumentProperties(objCrdDocType, app.Core.FileTypes.Find(IMAGE_FILE_TYPE));
foreach (KeywordRecord objKeyRecord in objCurrentDocument.KeywordRecords)
{
if (objKeyRecord.KeywordRecordType.RecordType == RecordType.StandAlone || objKeyRecord.KeywordRecordType.RecordType == RecordType.SingleInstance)
{
foreach (Keyword objKeyword in objKeyRecord.Keywords)
{
if (objCrdDocType.KeywordRecordTypes.FindKeywordType(objKeyword.KeywordType.ID) != null)
{
objDocProps.AddKeyword(objKeyword);
}
}
}
else
{
EditableKeywordRecord objEditRecord = objKeyRecord.CreateEditableKeywordRecord();
objDocProps.AddKeywordRecord(objEditRecord);
}
}
// Store the new document
Document objNewDoc = null;
objNewDoc = app.Core.Storage.StoreNewDocument(objImageData, objDocProps);
if (objNewDoc == null)
{
throw new InvalidProgramException("Failed to store new document");
}
app.Diagnostics.WriteIf(Diagnostics.DiagnosticsLevel.Info, string.Format("Stored new document: {0}", objNewDoc.ID.ToString()));
// Clean up
objImageData.Dispose();
// If we got here, the script was successful
args.ScriptResult = true;
}
}
catch (InvalidProgramException ex)
{
app.Diagnostics.WriteIf(Diagnostics.DiagnosticsLevel.Error, string.Format("Invalid Program Exception: {0}", ex.Message));
app.Diagnostics.WriteIf(Diagnostics.DiagnosticsLevel.Error, string.Format("Stack Trace: {0}", ex.StackTrace));
args.ScriptResult = false;
}
catch (UnityAPIException ex)
{
app.Diagnostics.WriteIf(Diagnostics.DiagnosticsLevel.Error, string.Format("Unity API Exception: {0}", ex.Message));
app.Diagnostics.WriteIf(Diagnostics.DiagnosticsLevel.Error, string.Format("Stack Trace: {0}", ex.StackTrace));
args.ScriptResult = false;
}
catch (Exception ex)
{
app.Diagnostics.WriteIf(Diagnostics.DiagnosticsLevel.Error, string.Format("General Exception: {0}", ex.Message));
app.Diagnostics.WriteIf(Diagnostics.DiagnosticsLevel.Error, string.Format("Stack Trace: {0}", ex.StackTrace));
args.ScriptResult = false;
}
finally
{
app.Diagnostics.WriteIf(Diagnostics.DiagnosticsLevel.Info, string.Format("End Script - [{0}]", ScriptName));
}
}
#endregion
}
}