File: sakeRequestRead.c

package info (click to toggle)
openmohaa 0.82.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 34,192 kB
  • sloc: cpp: 315,720; ansic: 275,789; sh: 312; xml: 246; asm: 141; makefile: 7
file content (674 lines) | stat: -rw-r--r-- 22,354 bytes parent folder | download | duplicates (2)
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#include "sakeRequestInternal.h"
#include "sakeRequest.h"


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
static SAKEStartRequestResult SAKE_CALL sakeiValidateRequestFieldNames(char **fields, int numFields)
{
	int i;

	for(i = 0 ; i < numFields ; i++)
	{
		if(!fields[i] || !fields[i][0])
			return SAKEStartRequestResult_BAD_FIELD_NAME;
	}

	return SAKEStartRequestResult_SUCCESS;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
static void SAKE_CALL sakeiFillSoapRequestFieldNames(SAKERequest request, char **names, int numFields)
{
	int i;

	gsXmlWriteOpenTag(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "fields");

	for(i = 0 ; i < numFields ; i++)
	{
		gsXmlWriteStringElement(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "string", names[i]);
	}

	gsXmlWriteCloseTag(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "fields");
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
static void SAKE_CALL sakeiFillSoapRequestRecordIds(SAKERequest request, int *recordIds, int numRecordIds)
{
	int i;

	gsXmlWriteOpenTag(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "recordids");

	for(i = 0 ; i < numRecordIds ; i++)
	{
		gsXmlWriteIntElement(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "int", (gsi_u32)recordIds[i]);
	}

	gsXmlWriteCloseTag(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "recordids");
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
static void SAKE_CALL sakeiFillSoapRequestOwnerIds(SAKERequest request, int *ownerIds, int numOwnerIds)
{
	int i;

	gsXmlWriteOpenTag(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "ownerids");

	for(i = 0 ; i < numOwnerIds ; i++)
	{
		gsXmlWriteIntElement(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "int", (gsi_u32)ownerIds[i]);
	}

	gsXmlWriteCloseTag(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "ownerids");
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
static SAKERequestResult SAKE_CALL sakeiReadOutputRecords(SAKERequest request, SAKEField ***outputRecordsPtr, int *numRecords,
                                                          int numFields, char ** fieldNames)

{
	SAKEField **outputRecords;
	SAKEField *outputRecord;
	SAKEField *outputField;
	int recordIndex;
	int fieldIndex;
	size_t size;

	// get to the start of the values
	if(gsi_is_false(gsXmlMoveToChild(request->mSoapResponse, "values")))
		return SAKERequestResult_MALFORMED_RESPONSE;

	// count the number of records
	*numRecords = gsXmlCountChildren(request->mSoapResponse, "ArrayOfRecordValue");

	// check for no records
	if(*numRecords == 0)
	{
		*outputRecordsPtr = NULL;
		return SAKERequestResult_SUCCESS;
	}

	// allocate the array of records
	size = (sizeof(SAKEField*) * *numRecords);
	outputRecords = (SAKEField**)gsimalloc(size);
	if(!outputRecords)
		return SAKERequestResult_OUT_OF_MEMORY;
	memset(outputRecords, 0, size);
	*outputRecordsPtr = outputRecords;

	// loop through the records
	for(recordIndex = 0 ; recordIndex < *numRecords ; recordIndex++)
	{
		// advance to this record
		if(gsi_is_false(gsXmlMoveToNext(request->mSoapResponse, "ArrayOfRecordValue")))
			return SAKERequestResult_MALFORMED_RESPONSE;

		// allocate the array of record fields
		size = (sizeof(SAKEField) * numFields);
		outputRecord = (SAKEField*)gsimalloc(size);
		if(!outputRecord)
			return SAKERequestResult_OUT_OF_MEMORY;
		memset(outputRecord, 0, size);
		outputRecords[recordIndex] = outputRecord;

		// check for the wrong number of fields in the response
		if(gsXmlCountChildren(request->mSoapResponse, "RecordValue") != numFields)
			return SAKERequestResult_MALFORMED_RESPONSE;

		// fill in the array of fields for this record
		for(fieldIndex = 0 ; fieldIndex < numFields ; fieldIndex++)
		{
			// utility pointer
			outputField = &outputRecord[fieldIndex];

			// set the name for this field
			outputField->mName = fieldNames[fieldIndex];

			// move to this field
			if(gsi_is_false(gsXmlMoveToNext(request->mSoapResponse, "RecordValue")))
				return SAKERequestResult_MALFORMED_RESPONSE;

			// set the type and value based on the response field
			if(gsXmlMoveToChild(request->mSoapResponse, "byteValue"))
			{
				int value;
				if(gsi_is_false(gsXmlReadChildAsInt(request->mSoapResponse, "value", &value)))
					return SAKERequestResult_MALFORMED_RESPONSE;
				outputField->mType = SAKEFieldType_BYTE;
				outputField->mValue.mByte = (gsi_u8)value;
			}
			else if(gsXmlMoveToChild(request->mSoapResponse, "shortValue"))
			{
				int value;
				if(gsi_is_false(gsXmlReadChildAsInt(request->mSoapResponse, "value", &value)))
					return SAKERequestResult_MALFORMED_RESPONSE;
				outputField->mType = SAKEFieldType_SHORT;
				outputField->mValue.mShort = (gsi_i16)value;
			}
			else if(gsXmlMoveToChild(request->mSoapResponse, "intValue"))
			{
				int value;
				if(gsi_is_false(gsXmlReadChildAsInt(request->mSoapResponse, "value", &value)))
					return SAKERequestResult_MALFORMED_RESPONSE;
				outputField->mType = SAKEFieldType_INT;
				outputField->mValue.mInt = (gsi_i32)value;
			}
			else if (gsXmlMoveToChild(request->mSoapResponse, "int64Value"))
			{
				gsi_i64 value;
				if (gsi_is_false(gsXmlReadChildAsInt64(request->mSoapResponse, "value", &value)))
					return SAKERequestResult_MALFORMED_RESPONSE;
				outputField->mType = SAKEFieldType_INT64;
				outputField->mValue.mInt64 = value;
			}
			else if(gsXmlMoveToChild(request->mSoapResponse, "floatValue"))
			{
				float value;
				if(gsi_is_false(gsXmlReadChildAsFloat(request->mSoapResponse, "value", &value)))
					return SAKERequestResult_MALFORMED_RESPONSE;
				outputField->mType = SAKEFieldType_FLOAT;
				outputField->mValue.mFloat = value;
			}
			else if(gsXmlMoveToChild(request->mSoapResponse, "asciiStringValue"))
			{
				char *value;
				int len;
				if(gsi_is_false(gsXmlReadChildAsString(request->mSoapResponse, "value", (const char**)&value, &len)))
					return SAKERequestResult_MALFORMED_RESPONSE;
				if(value)
					value[len] = '\0';
				else
					value = "";
				outputField->mType = SAKEFieldType_ASCII_STRING;
				outputField->mValue.mAsciiString = value;
			}
			else if(gsXmlMoveToChild(request->mSoapResponse, "unicodeStringValue"))
			{
				char *value;
				gsi_u16 *valueUnicode;
				int len;
				if(gsi_is_false(gsXmlReadChildAsString(request->mSoapResponse, "value", (const char**)&value, &len)))
					return SAKERequestResult_MALFORMED_RESPONSE;
				if(value)
					value[len] = '\0';
				else
					value = "";
				valueUnicode = UTF8ToUCS2StringAlloc(value);
				if(!valueUnicode)
					return SAKERequestResult_OUT_OF_MEMORY;
				outputField->mType = SAKEFieldType_UNICODE_STRING;
				outputField->mValue.mUnicodeString = valueUnicode;
			}
			else if(gsXmlMoveToChild(request->mSoapResponse, "booleanValue"))
			{
				char *value;
				int len;
				gsi_bool boolval;
				if(gsi_is_false(gsXmlReadChildAsString(request->mSoapResponse, "value", (const char**)&value, &len)))
					return SAKERequestResult_MALFORMED_RESPONSE;	
				if(value)
				{
					value[len] = '\0';
					boolval = (strcmp(value,"true") == 0)?gsi_true:gsi_false;
				}
				else
					boolval = gsi_false; //if returned a NULL value, set bool to false
				outputField->mType = SAKEFieldType_BOOLEAN;
				outputField->mValue.mBoolean = boolval;
			}
			else if(gsXmlMoveToChild(request->mSoapResponse, "dateAndTimeValue"))
			{
				time_t value;
				if(gsi_is_false(gsXmlReadChildAsDateTimeElement(request->mSoapResponse, "value", &value)))
					return SAKERequestResult_MALFORMED_RESPONSE;
				outputField->mType = SAKEFieldType_DATE_AND_TIME;
				outputField->mValue.mDateAndTime = value;
			}
			else if(gsXmlMoveToChild(request->mSoapResponse, "binaryDataValue"))
			{
				gsi_u8 *value;
				int len;
				if(gsi_is_false(gsXmlReadChildAsBase64Binary(request->mSoapResponse, "value", NULL, &len)))
					return SAKERequestResult_MALFORMED_RESPONSE;
				if(len > 0)
				{
					value = (gsi_u8*)gsimalloc((size_t)len);
					if(!value)
						return SAKERequestResult_OUT_OF_MEMORY;
					if(gsi_is_false(gsXmlReadChildAsBase64Binary(request->mSoapResponse, "value", value, &len)))
					{
						gsifree(value);
						return SAKERequestResult_MALFORMED_RESPONSE;
					}
				}
				else
				{
					value = NULL;
					len = 0;
				}
				outputField->mType = SAKEFieldType_BINARY_DATA;
				outputField->mValue.mBinaryData.mLength = len;
				outputField->mValue.mBinaryData.mValue = value;
			}
			else
			{
				GS_FAIL_STR("No recognized field type found in RecordValue");
				return SAKERequestResult_UNKNOWN_ERROR;
			}
		}
	}

	return SAKERequestResult_SUCCESS;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
static void SAKE_CALL sakeiFreeOutputRecord(int numFields, SAKEField *record)
{
	int i;

	if(!record)
		return;


	//Check for binary data or unicode strings and free it if necessary
	for (i = 0; i < numFields; i++)
	{
		if (record[i].mType == SAKEFieldType_BINARY_DATA && record[i].mValue.mBinaryData.mValue != NULL)
			gsifree(record[i].mValue.mBinaryData.mValue);
		if (record[i].mType == SAKEFieldType_UNICODE_STRING)
			gsifree(record[i].mValue.mUnicodeString);
	}
	gsifree(record);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
static void SAKE_CALL sakeiFreeOutputRecords(int numFields, int numRecords, SAKEField **records)
{
	int i,j;

	if(!records)
		return;

	for(i = 0 ; i < numRecords ; i++)
	{
		//Check for binary data or unicode strings and free it if necessary
		for (j = 0; j < numFields; j++)
		{
			if (records[i][j].mType == SAKEFieldType_BINARY_DATA && records[i][j].mValue.mBinaryData.mValue != NULL)
				gsifree(records[i][j].mValue.mBinaryData.mValue);
			if (records[i][j].mType == SAKEFieldType_UNICODE_STRING)
				gsifree(records[i][j].mValue.mUnicodeString);
		}

		gsifree(records[i]);
	}
	gsifree(records);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Search For Records

static SAKEStartRequestResult SAKE_CALL sakeiSearchForRecordsValidateInput(SAKERequest request)
{
	SAKESearchForRecordsInput *input = (SAKESearchForRecordsInput *)request->mInput;

	// check the tableid
	if(!input->mTableId)
		return SAKEStartRequestResult_BAD_TABLEID;

	// check the num fields
	if(input->mNumFields <= 0)
		return SAKEStartRequestResult_BAD_NUM_FIELDS;

	// check for NULL field names
	if(!input->mFieldNames)
		return SAKEStartRequestResult_BAD_FIELDS;

	// check the offset
	if(input->mOffset < 0)
		return SAKEStartRequestResult_BAD_OFFSET;

	// check the max
	if(input->mMaxRecords <= 0)
		return SAKEStartRequestResult_BAD_MAX; 

	// check the field names
	return sakeiValidateRequestFieldNames(input->mFieldNames, input->mNumFields);
}

static SAKEStartRequestResult SAKE_CALL sakeiSearchForRecordsFillSoapRequest(SAKERequest request)
{
	SAKESearchForRecordsInput *input = (SAKESearchForRecordsInput *)request->mInput;

	// write the table id
	gsXmlWriteStringElement(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "tableid", input->mTableId);

	// write the filter
	if(input->mFilter != NULL)
		gsXmlWriteTStringElement(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "filter", input->mFilter);

	// write the sort
	if(input->mSort != NULL)
		gsXmlWriteStringElement(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "sort", input->mSort);

	// write the offset
	gsXmlWriteIntElement(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "offset", (gsi_u32)input->mOffset);

	// write the max
	gsXmlWriteIntElement(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "max", (gsi_u32)input->mMaxRecords);

	// write the target record filter
	if(input->mTargetRecordFilter != NULL)
		gsXmlWriteTStringElement(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "targetfilter", input->mTargetRecordFilter);

	// write the surrounding record count
	gsXmlWriteIntElement(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "surrounding", (gsi_u32)input->mSurroundingRecordsCount);

	// fill in the ownerids
	sakeiFillSoapRequestOwnerIds(request, input->mOwnerIds, input->mNumOwnerIds);

	// write the cache flag
	gsXmlWriteIntElement(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "cacheFlag", (gsi_u32)input->mCacheFlag);


	// fill in the field names
	sakeiFillSoapRequestFieldNames(request, input->mFieldNames, input->mNumFields);

	return SAKEStartRequestResult_SUCCESS;
}

static SAKERequestResult sakeiSearchForRecordsProcessSoapResponse(SAKERequest request)
{
	SAKESearchForRecordsInput *input = (SAKESearchForRecordsInput *)request->mInput;
	SAKESearchForRecordsOutput *output = (SAKESearchForRecordsOutput *)request->mOutput;

	// fill the output records
	return sakeiReadOutputRecords(request, &output->mRecords, &output->mNumRecords, input->mNumFields, input->mFieldNames);
}

static void sakeiSearchForRecordsFreeData(SAKERequest request)
{
	SAKESearchForRecordsInput *input = (SAKESearchForRecordsInput *)request->mInput;
	SAKESearchForRecordsOutput *output = (SAKESearchForRecordsOutput *)request->mOutput;

	if(output)
		sakeiFreeOutputRecords(input->mNumFields, output->mNumRecords, output->mRecords);
}

SAKEStartRequestResult SAKE_CALL sakeiStartSearchForRecordsRequest(SAKERequest request)
{
	static SAKEIRequestInfo info =
	{
		sizeof(SAKESearchForRecordsOutput),
		SAKEI_FUNC_NAME_STRINGS("SearchForRecords"),
		sakeiSearchForRecordsValidateInput,
		sakeiSearchForRecordsFillSoapRequest,
		sakeiSearchForRecordsProcessSoapResponse,
		sakeiSearchForRecordsFreeData
	};

	return sakeiStartRequest(request, &info);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Get My Records

static SAKEStartRequestResult SAKE_CALL sakeiGetMyRecordsValidateInput(SAKERequest request)
{
	SAKEGetMyRecordsInput *input = (SAKEGetMyRecordsInput *)request->mInput;

	// check the tableid
	if(!input->mTableId)
		return SAKEStartRequestResult_BAD_TABLEID;

	// check the num fields
	if(input->mNumFields <= 0)
		return SAKEStartRequestResult_BAD_NUM_FIELDS;

	// check for NULL field names
	if(!input->mFieldNames)
		return SAKEStartRequestResult_BAD_FIELDS;

	// check the field names
	return sakeiValidateRequestFieldNames(input->mFieldNames, input->mNumFields);
}

static SAKEStartRequestResult SAKE_CALL sakeiGetMyRecordsFillSoapRequest(SAKERequest request)
{
	SAKEGetMyRecordsInput *input = (SAKEGetMyRecordsInput *)request->mInput;

	// write the table id
	gsXmlWriteStringElement(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "tableid", input->mTableId);

	// fill in the field names
	sakeiFillSoapRequestFieldNames(request, input->mFieldNames, input->mNumFields);

	return SAKEStartRequestResult_SUCCESS;
}

static SAKERequestResult sakeiGetMyRecordsProcessSoapResponse(SAKERequest request)
{
	SAKEGetMyRecordsInput *input = (SAKEGetMyRecordsInput *)request->mInput;
	SAKEGetMyRecordsOutput *output = (SAKEGetMyRecordsOutput *)request->mOutput;

	// fill the output records
	return sakeiReadOutputRecords(request, &output->mRecords, &output->mNumRecords, input->mNumFields, input->mFieldNames);
}

static void sakeiGetMyRecordsFreeData(SAKERequest request)
{
	SAKEGetMyRecordsInput *input = (SAKEGetMyRecordsInput *)request->mInput;	
	SAKEGetMyRecordsOutput *output = (SAKEGetMyRecordsOutput *)request->mOutput;

	if(output)
		sakeiFreeOutputRecords(input->mNumFields, output->mNumRecords, output->mRecords);
}

SAKEStartRequestResult SAKE_CALL sakeiStartGetMyRecordsRequest(SAKERequest request)
{
	static SAKEIRequestInfo info =
	{
		sizeof(SAKEGetMyRecordsOutput),
		SAKEI_FUNC_NAME_STRINGS("GetMyRecords"),
		sakeiGetMyRecordsValidateInput,
		sakeiGetMyRecordsFillSoapRequest,
		sakeiGetMyRecordsProcessSoapResponse,
		sakeiGetMyRecordsFreeData
	};

	return sakeiStartRequest(request, &info);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Get Specific Records

static SAKEStartRequestResult SAKE_CALL sakeiGetSpecificRecordsValidateInput(SAKERequest request)
{
	SAKEGetSpecificRecordsInput *input = (SAKEGetSpecificRecordsInput *)request->mInput;

	// check the tableid
	if(!input->mTableId)
		return SAKEStartRequestResult_BAD_TABLEID;

	// check the num recordids
	if(input->mNumRecordIds <= 0)
		return SAKEStartRequestResult_BAD_NUM_RECORDIDS;

	// check the recordids
	if(!input->mRecordIds)
		return SAKEStartRequestResult_BAD_RECORDIDS;

	// check the num fields
	if(input->mNumFields <= 0)
		return SAKEStartRequestResult_BAD_NUM_FIELDS;

	// check for NULL field names
	if(!input->mFieldNames)
		return SAKEStartRequestResult_BAD_FIELDS;

	// check the field names
	return sakeiValidateRequestFieldNames(input->mFieldNames, input->mNumFields);
}

static SAKEStartRequestResult SAKE_CALL sakeiGetSpecificRecordsFillSoapRequest(SAKERequest request)
{
	SAKEGetSpecificRecordsInput *input = (SAKEGetSpecificRecordsInput *)request->mInput;

	// write the table id
	gsXmlWriteStringElement(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "tableid", input->mTableId);

	// fill in the recordids
	sakeiFillSoapRequestRecordIds(request, input->mRecordIds, input->mNumRecordIds);

	// fill in the field names
	sakeiFillSoapRequestFieldNames(request, input->mFieldNames, input->mNumFields);

	return SAKEStartRequestResult_SUCCESS;
}

static SAKERequestResult sakeiGetSpecificRecordsProcessSoapResponse(SAKERequest request)
{
	SAKEGetSpecificRecordsInput *input = (SAKEGetSpecificRecordsInput *)request->mInput;
	SAKEGetSpecificRecordsOutput *output = (SAKEGetSpecificRecordsOutput *)request->mOutput;

	// fill the output records
	return sakeiReadOutputRecords(request, &output->mRecords, &output->mNumRecords, input->mNumFields, input->mFieldNames);
}

static void sakeiGetSpecificRecordsFreeData(SAKERequest request)
{
	SAKEGetSpecificRecordsInput *input = (SAKEGetSpecificRecordsInput *)request->mInput;	
	SAKEGetSpecificRecordsOutput *output = (SAKEGetSpecificRecordsOutput *)request->mOutput;

	if(output)
		sakeiFreeOutputRecords(input->mNumFields, output->mNumRecords, output->mRecords);
}

SAKEStartRequestResult SAKE_CALL sakeiStartGetSpecificRecordsRequest(SAKERequest request)
{
	static SAKEIRequestInfo info =
	{
		sizeof(SAKEGetSpecificRecordsOutput),
		SAKEI_FUNC_NAME_STRINGS("GetSpecificRecords"),
		sakeiGetSpecificRecordsValidateInput,
		sakeiGetSpecificRecordsFillSoapRequest,
		sakeiGetSpecificRecordsProcessSoapResponse,
		sakeiGetSpecificRecordsFreeData
	};

	return sakeiStartRequest(request, &info);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Get Random Record

static SAKEStartRequestResult SAKE_CALL sakeiGetRandomRecordValidateInput(SAKERequest request)
{
	SAKEGetRandomRecordInput *input = (SAKEGetRandomRecordInput *)request->mInput;

	// check the tableid
	if(!input->mTableId)
		return SAKEStartRequestResult_BAD_TABLEID;

	// check the num fields
	if(input->mNumFields <= 0)
		return SAKEStartRequestResult_BAD_NUM_FIELDS;

	// check for NULL field names
	if(!input->mFieldNames)
		return SAKEStartRequestResult_BAD_FIELDS;

	// check the field names
	return sakeiValidateRequestFieldNames(input->mFieldNames, input->mNumFields);
}

static SAKEStartRequestResult SAKE_CALL sakeiGetRandomRecordFillSoapRequest(SAKERequest request)
{
	SAKEGetRandomRecordInput *input = (SAKEGetRandomRecordInput *)request->mInput;

	// write the table id
	gsXmlWriteStringElement(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "tableid", input->mTableId);

	// write the filter
	if(input->mFilter != NULL)
		gsXmlWriteTStringElement(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "filter", input->mFilter);

	// write the max
	gsXmlWriteIntElement(request->mSoapRequest, GSI_SAKE_SERVICE_NAMESPACE, "max", 1);

	// fill in the field names
	sakeiFillSoapRequestFieldNames(request, input->mFieldNames, input->mNumFields);

	return SAKEStartRequestResult_SUCCESS;
}

static SAKERequestResult sakeiGetRandomRecordProcessSoapResponse(SAKERequest request)
{
	SAKEGetRandomRecordInput *input = (SAKEGetRandomRecordInput *)request->mInput;
	SAKEGetRandomRecordOutput *output = (SAKEGetRandomRecordOutput *)request->mOutput;
	SAKEField **records;
	int         numRecords;

	// fill the output record
	SAKERequestResult result = sakeiReadOutputRecords(request, &records, &numRecords, input->mNumFields, input->mFieldNames);
	if(result == SAKERequestResult_SUCCESS)
	{
		if((records != NULL) && (numRecords > 0))
			output->mRecord = records[0];
		else
			output->mRecord = NULL;
	}
	//free up the outer record pointer
	gsifree(records);

	return result;
}

static void sakeiGetRandomRecordFreeData(SAKERequest request)
{
	SAKEGetRandomRecordInput *input = (SAKEGetRandomRecordInput *)request->mInput;	
	SAKEGetRandomRecordOutput *output = (SAKEGetRandomRecordOutput *)request->mOutput;

	if(output)
		sakeiFreeOutputRecord(input->mNumFields, output->mRecord);

}

SAKEStartRequestResult SAKE_CALL sakeiStartGetRandomRecordRequest(SAKERequest request)
{
	static SAKEIRequestInfo info =
	{
		sizeof(SAKEGetRandomRecordOutput),
		SAKEI_FUNC_NAME_STRINGS("GetRandomRecords"),
		sakeiGetRandomRecordValidateInput,
		sakeiGetRandomRecordFillSoapRequest,
		sakeiGetRandomRecordProcessSoapResponse,
		sakeiGetRandomRecordFreeData
	};

	return sakeiStartRequest(request, &info);
}