File: dlgJob.cpp

package info (click to toggle)
pgadmin3 1.20.0~beta2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 73,704 kB
  • ctags: 18,591
  • sloc: cpp: 193,786; ansic: 18,736; sh: 5,154; pascal: 1,120; yacc: 927; makefile: 516; lex: 421; xml: 126; perl: 40
file content (513 lines) | stat: -rw-r--r-- 13,815 bytes parent folder | download
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// dlgJob.cpp - PostgreSQL Job Property
//
//////////////////////////////////////////////////////////////////////////


// App headers
#include "pgAdmin3.h"
#include "utils/misc.h"
#include "agent/dlgJob.h"
#include "agent/dlgStep.h"
#include "agent/dlgSchedule.h"
#include "agent/pgaJob.h"
#include "agent/pgaStep.h"
#include "agent/pgaSchedule.h"
#include "frm/frmMain.h"


// pointer to controls
#define	txtID				CTRL_TEXT("txtID")
#define chkEnabled          CTRL_CHECKBOX("chkEnabled")
#define cbJobclass          CTRL_COMBOBOX("cbJobclass")
#define txtHostAgent        CTRL_TEXT("txtHostAgent")
#define txtCreated          CTRL_TEXT("txtCreated")
#define txtChanged          CTRL_TEXT("txtChanged")
#define txtNextrun          CTRL_TEXT("txtNextrun")
#define txtLastrun          CTRL_TEXT("txtLastrun")
#define txtLastresult       CTRL_TEXT("txtLastResult")

#define lstSteps            CTRL_LISTVIEW("lstSteps")
#define btnChangeStep       CTRL_BUTTON("btnChangeStep")
#define btnAddStep          CTRL_BUTTON("btnAddStep")
#define btnRemoveStep       CTRL_BUTTON("btnRemoveStep")

#define lstSchedules        CTRL_LISTVIEW("lstSchedules")
#define btnChangeSchedule   CTRL_BUTTON("btnChangeSchedule")
#define btnAddSchedule      CTRL_BUTTON("btnAddSchedule")
#define btnRemoveSchedule   CTRL_BUTTON("btnRemoveSchedule")


BEGIN_EVENT_TABLE(dlgJob, dlgAgentProperty)
	EVT_CHECKBOX(XRCID("chkEnabled"),               dlgProperty::OnChange)
	EVT_COMBOBOX(XRCID("cbJobclass"),               dlgProperty::OnChange)
	EVT_TEXT(XRCID("txtHostAgent"),                 dlgProperty::OnChange)

	EVT_LIST_ITEM_SELECTED(XRCID("lstSteps"),       dlgJob::OnSelChangeStep)
	EVT_BUTTON(XRCID("btnChangeStep"),              dlgJob::OnChangeStep)
	EVT_BUTTON(XRCID("btnAddStep"),                 dlgJob::OnAddStep)
	EVT_BUTTON(XRCID("btnRemoveStep"),              dlgJob::OnRemoveStep)

	EVT_LIST_ITEM_SELECTED(XRCID("lstSchedules"),   dlgJob::OnSelChangeSchedule)
	EVT_BUTTON(XRCID("btnChangeSchedule"),          dlgJob::OnChangeSchedule)
	EVT_BUTTON(XRCID("btnAddSchedule"),             dlgJob::OnAddSchedule)
	EVT_BUTTON(XRCID("btnRemoveSchedule"),          dlgJob::OnRemoveSchedule)

#ifdef __WXMAC__
	EVT_SIZE(                                       dlgJob::OnChangeSize)
#endif
END_EVENT_TABLE();


dlgProperty *pgaJobFactory::CreateDialog(frmMain *frame, pgObject *node, pgObject *parent)
{
	return new dlgJob(this, frame, (pgaJob *)node);
}


dlgJob::dlgJob(pgaFactory *f, frmMain *frame, pgaJob *node)
	: dlgAgentProperty(f, frame, wxT("dlgJob"))
{
	job = node;

	txtID->Disable();
	txtCreated->Disable();
	txtChanged->Disable();
	txtNextrun->Disable();
	txtLastrun->Disable();
	txtLastresult->Disable();
	lstSteps->CreateColumns(0, _("Step"), _("Comment"), 90);
	lstSteps->AddColumn(wxT("cmd"), 0);
	lstSteps->AddColumn(wxT("id"), 0);

	lstSchedules->CreateColumns(0, _("Schedule"), _("Comment"), 90);
	lstSchedules->AddColumn(wxT("cmd"), 0);
	lstSchedules->AddColumn(wxT("id"), 0);
	btnChangeStep->Disable();
	btnRemoveStep->Disable();
	btnChangeSchedule->Disable();
	btnRemoveSchedule->Disable();
}


pgObject *dlgJob::GetObject()
{
	return job;
}


#ifdef __WXMAC__
void dlgJob::OnChangeSize(wxSizeEvent &ev)
{
	lstSteps->SetSize(wxDefaultCoord, wxDefaultCoord,
	                  ev.GetSize().GetWidth(), ev.GetSize().GetHeight() - 350);
	lstSchedules->SetSize(wxDefaultCoord, wxDefaultCoord,
	                      ev.GetSize().GetWidth(), ev.GetSize().GetHeight() - 350);
	if (GetAutoLayout())
	{
		Layout();
	}
}
#endif


int dlgJob::Go(bool modal)
{
	int returncode;

	pgSet *jcl = connection->ExecuteSet(wxT("SELECT jclname FROM pgagent.pga_jobclass"));
	if (jcl)
	{
		while (!jcl->Eof())
		{
			cbJobclass->Append(jcl->GetVal(0));
			jcl->MoveNext();
		}
		delete jcl;
	}

	if (job)
	{
		// edit mode
		recId = job->GetRecId();
		txtID->SetValue(NumToStr(recId));
		cbJobclass->SetValue(job->GetJobclass());
		chkEnabled->SetValue(job->GetEnabled());
		txtHostAgent->SetValue(job->GetHostAgent());
		txtCreated->SetValue(DateToStr(job->GetCreated()));
		txtChanged->SetValue(DateToStr(job->GetChanged()));
		txtNextrun->SetValue(DateToStr(job->GetNextrun()));
		txtLastrun->SetValue(DateToStr(job->GetLastrun()));
		txtLastresult->SetValue(job->GetLastresult());

		wxCookieType cookie;
		pgObject *data = 0;

		wxTreeItemId item, stepsItem, schedulesItem;
		item = mainForm->GetBrowser()->GetFirstChild(job->GetId(), cookie);
		while (item)
		{
			data = mainForm->GetBrowser()->GetObject(item);
			if (data->GetMetaType() == PGM_STEP)
				stepsItem = item;
			else if (data->GetMetaType() == PGM_SCHEDULE)
				schedulesItem = item;

			item = mainForm->GetBrowser()->GetNextChild(job->GetId(), cookie);
		}

		if (stepsItem)
		{
			pgCollection *coll = (pgCollection *)data;
			// make sure all columns are appended
			coll->ShowTreeDetail(mainForm->GetBrowser());
			// this is the columns collection
			item = mainForm->GetBrowser()->GetFirstChild(stepsItem, cookie);

			// add columns
			while (item)
			{
				data = mainForm->GetBrowser()->GetObject(item);
				if (data->IsCreatedBy(stepFactory))
				{
					pgaStep *step = (pgaStep *)data;
					int pos = lstSteps->AppendItem(stepFactory.GetIconId(), step->GetName(), step->GetComment());
					lstSteps->SetItem(pos, 3, NumToStr((long)step));
					previousSteps.Add(NumToStr((long)step));
				}
				item = mainForm->GetBrowser()->GetNextChild(stepsItem, cookie);
			}
		}

		if (schedulesItem)
		{
			pgCollection *coll = (pgCollection *)data;
			// make sure all columns are appended
			coll->ShowTreeDetail(mainForm->GetBrowser());
			// this is the columns collection
			item = mainForm->GetBrowser()->GetFirstChild(schedulesItem, cookie);

			// add columns
			while (item)
			{
				data = mainForm->GetBrowser()->GetObject(item);
				if (data->IsCreatedBy(scheduleFactory))
				{
					pgaSchedule *schedule = (pgaSchedule *)data;
					int pos = lstSchedules->AppendItem(scheduleFactory.GetIconId(), schedule->GetName(), schedule->GetComment());
					lstSchedules->SetItem(pos, 3, NumToStr((long)schedule));
					previousSchedules.Add(NumToStr((long)schedule));
				}
				item = mainForm->GetBrowser()->GetNextChild(schedulesItem, cookie);
			}
		}
	}
	else
	{
		// create mode
		cbJobclass->SetSelection(0);
		btnChangeStep->Hide();
		btnChangeSchedule->Hide();
	}

	returncode = dlgProperty::Go(modal);

	SetSqlReadOnly(true);

	// This fixes a UI glitch on MacOS X
	// Because of the new layout code, the Columns pane doesn't size itself properly
	SetSize(GetSize().GetWidth() + 1, GetSize().GetHeight());
	SetSize(GetSize().GetWidth() - 1, GetSize().GetHeight());

	return returncode;
}


pgObject *dlgJob::CreateObject(pgCollection *collection)
{
	pgObject *obj = jobFactory.CreateObjects(collection, 0, wxT("   WHERE jobid=") + NumToStr(recId) + wxT("\n"));
	return obj;
}


void dlgJob::CheckChange()
{
	bool enable = true;
	wxString name = GetName();
	if (job)
	{
		enable  =  txtComment->GetValue() != job->GetComment()
		           || name != job->GetName()
		           || chkEnabled->GetValue() != job->GetEnabled()
		           || txtHostAgent->GetValue() != job->GetHostAgent();
		if (!enable)
		{
			enable = !GetUpdateSql().IsEmpty();
		}
	}

	if (statusBar)
		statusBar->SetStatusText(wxEmptyString);

	CheckValid(enable, !txtName->GetValue().IsEmpty(), _("Please specify name."));

	EnableOK(enable);
}


void dlgJob::OnChangeStep(wxCommandEvent &ev)
{
	long pos = lstSteps->GetSelection();
	pgaStep *obj = (pgaStep *) StrToLong(lstSteps->GetText(pos, 3));

	dlgStep step(&stepFactory, mainForm, obj, job);
	step.CenterOnParent();
	step.SetConnection(connection);

	if (step.Go(true) != wxID_CANCEL)
	{
		lstSteps->SetItem(pos, 0, step.GetName());
		lstSteps->SetItem(pos, 1, step.GetComment());

		if (lstSteps->GetText(pos, 3).IsEmpty())
		{
			wxString *stepSql = new wxString(step.GetInsertSql());
			lstSteps->SetItemData(pos, (long)stepSql);
		}
		else
		{
			wxString *stepSql = new wxString(step.GetUpdateSql());
			lstSteps->SetItemData(pos, (long)stepSql);
		}

		CheckChange();
	}
}


void dlgJob::OnSelChangeStep(wxListEvent &ev)
{
	btnChangeStep->Enable();
	btnRemoveStep->Enable();
}


void dlgJob::OnAddStep(wxCommandEvent &ev)
{
	dlgStep step(&stepFactory, mainForm, NULL, job);
	step.CenterOnParent();
	step.SetConnection(connection);
	if (step.Go(true) != wxID_CANCEL)
	{
		int pos = lstSteps->AppendItem(stepFactory.GetIconId(), step.GetName(), step.GetComment());
		wxString *stepSql = new wxString(step.GetInsertSql());
		lstSteps->SetItemData(pos, (long)stepSql);
		CheckChange();
	}
}


void dlgJob::OnRemoveStep(wxCommandEvent &ev)
{
	delete (wxString *)lstSteps->GetItemData(lstSteps->GetSelection());
	lstSteps->DeleteCurrentItem();

	btnChangeStep->Disable();
	btnRemoveStep->Disable();

	CheckChange();
}


void dlgJob::OnSelChangeSchedule(wxListEvent &ev)
{
	btnChangeSchedule->Enable();
	btnRemoveSchedule->Enable();
}


void dlgJob::OnChangeSchedule(wxCommandEvent &ev)
{
	long pos = lstSchedules->GetSelection();
	pgaSchedule *obj = (pgaSchedule *) StrToLong(lstSchedules->GetText(pos, 3));

	dlgSchedule schedule(&scheduleFactory, mainForm, obj, job);
	schedule.CenterOnParent();
	schedule.SetConnection(connection);

	if (schedule.Go(true) != wxID_CANCEL)
	{
		lstSchedules->SetItem(pos, 0, schedule.GetName());
		lstSchedules->SetItem(pos, 1, schedule.GetComment());

		if (lstSchedules->GetText(pos, 3).IsEmpty())
		{
			wxString *scheduleSql = new wxString(schedule.GetInsertSql());
			lstSchedules->SetItemData(pos, (long)scheduleSql);
		}
		else
		{
			wxString *scheduleSql = new wxString(schedule.GetUpdateSql());
			lstSchedules->SetItemData(pos, (long)scheduleSql);
		}

		CheckChange();
	}
}


void dlgJob::OnAddSchedule(wxCommandEvent &ev)
{
	dlgSchedule schedule(&scheduleFactory, mainForm, NULL, job);
	schedule.CenterOnParent();
	schedule.SetConnection(connection);
	if (schedule.Go(true) != wxID_CANCEL)
	{
		int pos = lstSchedules->AppendItem(scheduleFactory.GetIconId(), schedule.GetName(), schedule.GetComment());
		wxString *scheduleSql = new wxString(schedule.GetInsertSql());
		lstSchedules->SetItemData(pos, (long)scheduleSql);
		CheckChange();
	}
}


void dlgJob::OnRemoveSchedule(wxCommandEvent &ev)
{
	delete (wxString *)lstSchedules->GetItemData(lstSchedules->GetSelection());
	lstSchedules->DeleteCurrentItem();

	btnChangeSchedule->Disable();
	btnRemoveSchedule->Disable();

	CheckChange();
}


wxString dlgJob::GetInsertSql()
{
	wxString sql;

	if (!job)
	{
		sql = wxT("INSERT INTO pgagent.pga_job (jobid, jobjclid, jobname, jobdesc, jobenabled, jobhostagent)\n")
		      wxT("SELECT <JobId>, jcl.jclid, ") + qtDbString(GetName()) +
		      wxT(", ") + qtDbString(txtComment->GetValue()) + wxT(", ") + BoolToStr(chkEnabled->GetValue()) +
		      wxT(", ") + qtDbString(txtHostAgent->GetValue()) + wxT("\n")
		      wxT("  FROM pgagent.pga_jobclass jcl WHERE jclname=") + qtDbString(cbJobclass->GetValue()) + wxT(";\n");
	}
	return sql;
}


wxString dlgJob::GetUpdateSql()
{
	wxString sql, name;
	name = GetName();

	if (job)
	{
		// edit mode
		wxString vars;

		if (name != job->GetName())
		{
			if (!vars.IsEmpty())
				vars.Append(wxT(", "));
			vars.Append(wxT("jobname = ") + qtDbString(name));
		}
		if (cbJobclass->GetValue().Trim() != job->GetJobclass())
		{
			if (!vars.IsEmpty())
				vars.Append(wxT(", "));
			vars.Append(wxT("jobjclid= (SELECT jclid FROM pgagent.pga_jobclass WHERE jclname=") + qtDbString(cbJobclass->GetValue()) + wxT(")"));
		}
		if (chkEnabled->GetValue() != job->GetEnabled())
		{
			if (!vars.IsEmpty())
				vars.Append(wxT(", "));
			vars.Append(wxT("jobenabled = ") + BoolToStr(chkEnabled->GetValue()));
		}
		if (txtHostAgent->GetValue() != job->GetHostAgent())
		{
			if (!vars.IsEmpty())
				vars.Append(wxT(", "));
			vars.Append(wxT("jobhostagent = ") + qtDbString(txtHostAgent->GetValue()));
		}
		if (txtComment->GetValue() != job->GetComment())
		{
			if (!vars.IsEmpty())
				vars.Append(wxT(", "));
			vars.Append(wxT("jobdesc = ") + qtDbString(txtComment->GetValue()));
		}

		if (!vars.IsEmpty())
			sql = wxT("UPDATE pgagent.pga_job SET ") + vars + wxT("\n")
			      wxT(" WHERE jobid=") + NumToStr(recId) + wxT(";\n");

	}
	else
	{
		// create mode
		// done by GetInsertSql
	}

	int pos, index;

	wxArrayString tmpSteps = previousSteps;
	for (pos = 0 ; pos < lstSteps->GetItemCount() ; pos++)
	{
		wxString str = lstSteps->GetText(pos, 3);
		if (!str.IsEmpty())
		{
			index = tmpSteps.Index(str);
			if (index >= 0)
				tmpSteps.RemoveAt(index);
		}

		if(lstSteps->GetItemData(pos))
		{
			str = *(wxString *)lstSteps->GetItemData(pos);
			if (!str.IsEmpty())
				sql += str;
		}
	}

	for (index = 0 ; index < (int)tmpSteps.GetCount() ; index++)
	{
		sql += wxT("DELETE FROM pgagent.pga_jobstep WHERE jstid=")
		       + NumToStr(((pgaStep *)StrToLong(tmpSteps.Item(index)))->GetRecId()) + wxT(";\n");
	}

	wxArrayString tmpSchedules = previousSchedules;
	for (pos = 0 ; pos < lstSchedules->GetItemCount() ; pos++)
	{
		wxString str = lstSchedules->GetText(pos, 3);
		if (!str.IsEmpty())
		{
			index = tmpSchedules.Index(str);
			if (index >= 0)
				tmpSchedules.RemoveAt(index);
		}
		if(lstSchedules->GetItemData(pos))
		{
			str = *(wxString *)lstSchedules->GetItemData(pos);
			if (!str.IsEmpty())
				sql += str;
		}
	}

	for (index = 0 ; index < (int)tmpSchedules.GetCount() ; index++)
	{
		sql += wxT("DELETE FROM pgagent.pga_schedule WHERE jscid=")
		       + NumToStr(((pgaStep *)StrToLong(tmpSchedules.Item(index)))->GetRecId()) + wxT(";\n");
	}

	return sql;

}