-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathListFromApi.php
137 lines (116 loc) · 5.07 KB
/
ListFromApi.php
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
<?php
namespace ToolkitApi;
class ListFromApi
{
protected $ToolkitSrvObj;
protected $_requestHandle;
protected $_receiverSize;
protected $_totalRecords = 0;
protected $_nextRecordToRequest = 0;
protected $_receiverDs; // innards of a data structure <data>..</data><data>...</data> that we'll wrap in a receiver variable. At this time it must be an XML string.
// listinfo: totalRecords, firstRecordNumber, requestHandle. if firstRec... < totalRecords then can continue.
// return I5_ERR_BEOF when went past last record. get CPF GUI0006 when used invalid record#.
/**
* @param $requestHandle
* @param $totalRecords
* @param $receiverDs
* @param $lengthOfReceiverVariable
* @param ToolkitInterface $ToolkitSrvObj
*/
public function __construct($requestHandle, $totalRecords, $receiverDs, $lengthOfReceiverVariable, ?ToolkitInterface $ToolkitSrvObj = null)
{
if ($ToolkitSrvObj instanceof Toolkit) {
$this->ToolkitSrvObj = $ToolkitSrvObj;
}
$this->_requestHandle = $requestHandle;
$this->_receiverSize = $lengthOfReceiverVariable;
$this->_totalRecords = $totalRecords;
$this->_nextRecordToRequest = 1; // will request record #1 when someone asks
$this->_receiverDs = $receiverDs; // will request record #1 when someone asks
}
/**
* @return ToolkitInterface
*/
public function getConn()
{
return $this->ToolkitSrvObj;
}
/**
* Call QGYGTLE (get list entry) API using handle and "next record to request."
*
* Note: if get timing problems where no records are returned:
* Embed the call to the QGYGTLE API in a do-loop that loops until a record is returned.
*
* @return bool Return false when run out of records (get GUI0006 error code).
*/
public function getNextEntry()
{
$apiPgm = 'QGYGTLE';
$apiLib = 'QSYS';
$receiverDs = $this->_receiverDs;
$requestHandle = $this->_requestHandle;
$lengthOfReceiverVariable = $this->_receiverSize;
$nextRecordToRequest = $this->_nextRecordToRequest++; // assign to me, then increment for next time
$outputVarname = 'receiver';
$lenLabel= 'size' . $outputVarname;
$paramXml = "<parm io='out' comment='1. receiver data'>
<ds var='$outputVarname' comment='receiver appropriate to whatever API created the list' len='$lenLabel'>
$receiverDs
</ds>
</parm>
<parm io='both' comment='2. Length of receiver variable'>
<data var='receiverLen' type='10i0' setlenx='$lenLabel'>$lengthOfReceiverVariable</data>
</parm>
<parm io='in' comment='3. Request handle'>
<data var='requestHandle' comment='Request handle: binary/hex' type='4b'>$requestHandle</data>
</parm>\n" . $this->ToolkitSrvObj->getListInfoApiXml(4) . "\n" .
"<parm io='in' comment='5. Number of records to return'>
<data var='numRecsDesired' type='10i0'>1</data>
</parm>
<parm io='in' comment='6. Starting record' >
<data var='startingRecordNum' comment='First entry number to put in receiver var. If getting one record at a time, increment this each time.' type='10i0'>$nextRecordToRequest</data>
</parm>\n" .
Toolkit::getErrorDataStructXmlWithCode(7); // param number 7
// was getErrorDataStructXml
// pass param xml directly in.
$retPgmArr = $this->ToolkitSrvObj->PgmCall($apiPgm, $apiLib, $paramXml);
if ($this->ToolkitSrvObj->getErrorCode()) {
return false;
}
/* Even when no error reported by XMLSERVICE (->error),
* we may get a GUI0006 in DS error structure exeption code, since we
* supplied a full error data structure above (getErrorDataStructXmlWithCode).
*/
$apiErrCode = $retPgmArr['io_param']['errorDs']['exceptId'];
if ($apiErrCode != '0000000') {
// Note: caller can check for GUI0006 and GUI0001 (expected when go past last record) vs. any other error (not expected)
$this->ToolkitSrvObj->setErrorCode($apiErrCode);
return false;
}
$retArr = $retPgmArr['io_param'][$outputVarname];
return $retArr;
}
/**
* close the list
*
* @return bool
*/
public function close()
{
// call QGYCLST, the "close list" api.
$apiPgm = 'QGYCLST';
$apiLib = 'QSYS';
$requestHandle = $this->_requestHandle;
$paramXml = "<parm io='in' comment='1. request handle'>
<data var='requestHandle' comment='Request handle: binary/hex' type='4b'>$requestHandle</data>
</parm>\n" . Toolkit::getErrorDataStructXml(2); // param number 2
// pass param xml directly in.
$this->ToolkitSrvObj->PgmCall($apiPgm, $apiLib, $paramXml);
// GUI0006 means end of list
if ($this->ToolkitSrvObj->getErrorCode()) {
return false;
} else {
return true;
}
}
}