forked from content-manager-sdk/Community
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main Window.cs
196 lines (161 loc) · 6.37 KB
/
Main Window.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 System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;
using HP.HPTRIM.SDK;
namespace Record_Update_SDKSample
{
///This class provides an user interface to search a record using a record number.
///The record title, date of creation and assignee details are displayed. An error message is displayed if
///the record doesn't exist.
///The UI allows to update a record and save it.
public partial class SampleForm : Form
{
/// <summary>
/// Retrieves a handle to the desktop window. TRIM UI components require the handle of a parent window
/// </summary>
/// <returns></returns>
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
public static extern IntPtr GetDesktopWindow();
string title;//record title
string recNumber;//record number
TrimDateTime date;//record date of creation
Location location;// record assignee
string locName;// assignee name
string dbid;
string wgs;
public SampleForm()
{
InitializeComponent();
using (Database database = getDatabase())
{
label2.Text = $"{database.Id} / {database.WorkgroupServerName}";
}
}
private Database getDatabase()
{
Database database = new Database();
if (dbid != null)
{
database.Id = dbid;
}
if (wgs != null)
{
database.WorkgroupServerName = wgs;
}
if (!database.IsValid)
{
database.Dispose();
DesktopHelper helper = new DesktopHelper();
database = helper.SelectDatabase(GetDesktopWindow(), false, false);
}
// if the database id and work group server are not set we rely on a default already having been set in the client.
database.Connect(); //connect to the db using DB id and workgroup server
return database;
}
/// <summary>
/// Search Button - used to search a record using a record number. IF the recorfd does not exist
/// an error is displayed.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSearch_Click(object sender, EventArgs e)
{
try
{
using (Database objDB = getDatabase())//create a database object
{
string recnum = RecordNum.Text;
Record rec = new Record(objDB, recnum);//record object
location = new Location(objDB, rec.Assignee.Name);//location object
RecNumVal.Text = rec.Number.ToString();//record number
RecTitleVal.Text = rec.Title;//record title
DateCreatedVal.Text = rec.DateCreated.ToString();//record date of creation
AssigneeVal.Text = location.Name;//record assignee
}
}
// If a record does not exist an exception is thrown and a msgbox is displayed
catch (Exception k)
{
MessageBox.Show(k.Message);
}
}
/// <summary>
/// Save Button - used to save a record. If the assignee is invalid it displays an error.
/// The record the verified before getting saved.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Save_Click(object sender, EventArgs e)
{
try
{
using (Database objDB = getDatabase())//create a database object
{
recNumber = RecNumVal.Text; //record number
title = RecTitleVal.Text; //record title
date = new TrimDateTime(DateCreatedVal.Text); //record date of creation
locName = AssigneeVal.Text; //record assignee
Record rec = new Record(objDB, recNumber);//record object
rec.Title = title;
rec.DateCreated = date;
location = new Location(objDB, rec.Assignee.Name);//location object
if (location.Name != locName) //checking if the assignee name has changed
{
try
{
Location loc = new Location(objDB, locName);
rec.SetAssignee(loc);
}
//An exception is thrown if the location name is invalid
catch(Exception k)
{
System.Diagnostics.Trace.WriteLine(k.Message);
MessageBox.Show("The assingee is invalid or does not exist");
return;
}
}
//Saving the record object
if (rec.Verify(true))
{
rec.Save();
MessageBox.Show("Record Saved");
}
else //Error is displayed if record verification fails
{
MessageBox.Show(rec.ErrorMessage);
}
}
}
// Display any exception thrown in a msgbox
catch (Exception k)
{
MessageBox.Show(k.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
Database database = null;
try
{
DesktopHelper helper = new DesktopHelper();
database = helper.SelectDatabase(GetDesktopWindow(), false, false);
dbid = database?.Id;
wgs = database?.WorkgroupServerName;
label2.Text = $"{dbid} / {wgs}";
}
finally
{
if (database != null)
{
database.Dispose();
}
}
}
}
}