Skip to content

Commit 3b30cab

Browse files
committed
Fixed and updated import manager
1 parent 046fda6 commit 3b30cab

7 files changed

Lines changed: 89 additions & 45 deletions

File tree

src/AbraApi/AbraApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function delete(): CommandBuilders\DeleteQuery {
8686
* Return new ImportQuery command builder
8787
*/
8888
public function import(): CommandBuilders\ImportQuery {
89-
return (new CommandBuilders\ImportQuery(new Callers\ImportQueryResultGetter(new Callers\PostCaller($this))));
89+
return (new CommandBuilders\ImportQuery(new Callers\ImportQueryResultGetter($this)));
9090
}
9191

9292
/**

src/AbraApi/Callers/ImportQueryResultGetter.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,31 @@
55
use AbraApi\Results;
66

77
class ImportQueryResultGetter implements Interfaces\IResultGetter {
8-
/** @var Interfaces\ICaller */
9-
private $caller;
8+
/** @var \AbraApi\AbraApi */
9+
private $abraApi;
10+
/** @var bool */
11+
private $usePutMethod = false;
1012

11-
public function __construct(Interfaces\ICaller $caller) {
12-
$this->caller = $caller;
13+
public function __construct(\AbraApi\AbraApi $abraApi) {
14+
$this->abraApi = $abraApi;
15+
}
16+
17+
public function getCaller(): Interfaces\ICaller {
18+
if($this->usePutMethod)
19+
return (new PutCaller($this->abraApi));
20+
return (new PostCaller($this->abraApi));
21+
}
22+
23+
/**
24+
* Use PUT method to create query
25+
*/
26+
public function usePutMethod() {
27+
$this->usePutMethod = true;
1328
}
1429

1530
public function getResult($url, $body, $optHeaders = array()) {
16-
$resultPlainData = $this->caller->call($url, $body, $optHeaders);
31+
$caller = $this->getCaller();
32+
$resultPlainData = $caller->call($url, $body, $optHeaders);
1733
return (new Results\AbraApiImportResult($resultPlainData["content"], $resultPlainData["headers"], $resultPlainData["httpcode"]));
1834
}
1935
}

src/AbraApi/CommandBuilders/ImportQuery.php

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use AbraApi\Executors\Interfaces\IExecutor,
88
AbraApi\Callers,
9-
AbraApi\Results\Interfaces\IUpdateResult,
9+
AbraApi\Results\Interfaces\IImportResult,
1010
AbraApi\Commands;
1111

1212
class ImportQuery extends Query {
@@ -84,28 +84,41 @@ public function params(...$data): ImportQuery {
8484
* Executes query and returns result of imported document
8585
* If you don´t specify ->select(..), it returns only ID
8686
*/
87-
public function execute(): IUpdateResult {
88-
87+
public function execute(): IImportResult {
88+
if($this->fromBussinessObject === null || $this->intoBussinessObject === null)
89+
throw new \Exception('You must specify ->from(string $bussinessObject, array $documentIds) and ->into(string $bussinessObject, ?string $documentId) to execute import query.');
90+
if(($this->isClsid($this->fromBussinessObject) ^ $this->isClsid($this->intoBussinessObject)))
91+
throw new \Exception("You must specify both left and right importing manager bussiness objects either by ClsID or by API Bussiness object name.");
92+
$resultGetter = $this->resultGetter;
93+
if($this->intoDocumentId !== null && !$this->isClsid($this->fromBussinessObject)) {
94+
$resultGetter->setPutMethod();
95+
}
8996
return $this->resultGetter->getResult($this->getApiEndpoint(), $this->getQuery());
9097
}
9198

9299
/**
93100
* Creates endpoint for query
94101
*/
95-
public function getApiEndpoint() {
96-
return "import?".(QueryHelpers::createSelectUri($this->queryServant));
102+
public function getApiEndpoint(): string {
103+
if($this->isClsid($this->fromBussinessObject)) // both BO´s are defined with CLSID
104+
return "import?".(QueryHelpers::createSelectUri($this->queryServant));
105+
return ($this->intoBussinessObject).
106+
"/import/".
107+
($this->fromBussinessObject).
108+
("?".(QueryHelpers::createSelectUri($this->queryServant)));
97109
}
98110

99111
/**
100112
* Merges all data commands and return it as JSON object
101113
*/
102114
public function getQuery(): string {
103-
if($this->fromBussinessObject === null || $this->intoBussinessObject === null)
104-
throw new \Exception('You must specify ->from(string $bussinessObject, array $documentIds) and ->into(string $bussinessObject, ?string $documentId) to execute import query.');
105-
$query = [
106-
"input_document_clsid" => $this->fromBussinessObject,
107-
"output_document_clsid" => $this->intoBussinessObject
108-
];
115+
$query = [];
116+
if($this->isClsid($this->fromBussinessObject) && $this->isClsid($this->intoBussinessObject)) {
117+
$query = [
118+
"input_document_clsid" => $this->fromBussinessObject,
119+
"output_document_clsid" => $this->intoBussinessObject
120+
];
121+
}
109122
if($this->intoDocumentId !== null)
110123
$query["output_document"] = $this->intoDocumentId;
111124
$mergedCommands = QueryHelpers::mergeCommands($this->queryServant, [ Commands\ParamsCommand::class,
@@ -115,4 +128,11 @@ public function getQuery(): string {
115128
return json_encode(array_merge($mergedCommands, $query));
116129
}
117130

131+
/**
132+
* Returns, if string in parameter is CLSID
133+
*/
134+
private function isClsid(string $clsid) {
135+
return (preg_match('/^[0-9A-Z]{26}$/', $clsid));
136+
}
137+
118138
}

tests/Queries/ImportQueryTest.phpt

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ $import = $abraApi->import()
1111
->into("billsofdelivery")
1212
->params("docqueue_id", "1000000001");
1313

14-
Assert::same('import?select=id', $import->getApiEndpoint());
14+
Assert::same('billsofdelivery/import/receivedorders?select=id', $import->getApiEndpoint());
1515
Assert::equal([
16-
"input_document_clsid" => "receivedorders",
17-
"output_document_clsid" => "billsofdelivery",
1816
"input_documents" => "1000000001",
1917
"params" => [
2018
"docqueue_id" => "1000000001"
@@ -24,8 +22,6 @@ Assert::equal([
2422
$import->params("otherParam", "otherValue");
2523

2624
Assert::equal([
27-
"input_document_clsid" => "receivedorders",
28-
"output_document_clsid" => "billsofdelivery",
2925
"input_documents" => "1000000001",
3026
"params" => [
3127
"docqueue_id" => "1000000001",
@@ -43,11 +39,9 @@ $import = $abraApi->import()
4339
"otherParam" => "otherValue" ])
4440
->outputDocumentData("StoreDocQueue_ID", "1000000001");
4541

46-
Assert::same('import?select=id,DisplayName+as+name', $import->getApiEndpoint());
42+
Assert::same('billsofdelivery/import/receivedorders?select=id,DisplayName+as+name', $import->getApiEndpoint());
4743

4844
Assert::equal([
49-
"input_document_clsid" => "receivedorders",
50-
"output_document_clsid" => "billsofdelivery",
5145
"input_documents" => [
5246
"1000000001",
5347
"2000000001"
@@ -67,8 +61,6 @@ $import->outputDocumentData("SomeColumn", "SomeValue");
6761
$import->outputDocumentData([ "SomeColumn2" => "SomeValue2" ], [ "SomeColumn3" => "SomeValue3" ]);
6862

6963
Assert::equal([
70-
"input_document_clsid" => "receivedorders",
71-
"output_document_clsid" => "billsofdelivery",
7264
"input_documents" => [
7365
"1000000001",
7466
"2000000001"
@@ -88,12 +80,14 @@ Assert::equal([
8880
// import row of some BO into existing row usign CLSID
8981
$import = $abraApi->import()
9082
->from("OBSCO4S1BRD13FY1010DELDFKK", [ "1000000001" ])
91-
->into("billsofdelivery", "9000000001")
83+
->into("OBSCO4S1BRD13FY1010DELDFK2", "9000000001")
9284
->params("docqueue_id", "1000000001");
9385

86+
Assert::same('import?select=id', $import->getApiEndpoint());
87+
9488
Assert::equal([
9589
"input_document_clsid" => "OBSCO4S1BRD13FY1010DELDFKK",
96-
"output_document_clsid" => "billsofdelivery",
90+
"output_document_clsid" => "OBSCO4S1BRD13FY1010DELDFK2",
9791
"output_document" => "9000000001",
9892
"input_documents" => "1000000001",
9993
"params" => [
@@ -106,17 +100,31 @@ Assert::equal([
106100
Assert::exception(function() use($abraApi) {
107101
$abraApi->import()
108102
->into("storecards")
109-
->getQuery();
103+
->execute();
110104
}, \Exception::class, 'You must specify ->from(string $bussinessObject, array $documentIds) and ->into(string $bussinessObject, ?string $documentId) to execute import query.');
111105

112106
Assert::exception(function() use($abraApi) {
113107
$abraApi->import()
114108
->from("storecards", [ "1000000001" ])
115-
->getQuery();
109+
->execute();
116110
}, \Exception::class, 'You must specify ->from(string $bussinessObject, array $documentIds) and ->into(string $bussinessObject, ?string $documentId) to execute import query.');
117111

118112
Assert::exception(function() use($abraApi) {
119113
$abraApi->import()
120114
->from("storecards", [ "10000000" ])
121-
->getQuery();
122-
}, \Exception::class, 'Documents are supposed to be array of Bussiness object ID´s (string with length of 10 characters)');
115+
->execute();
116+
}, \Exception::class, 'Documents are supposed to be array of Bussiness object ID´s (string with length of 10 characters)');
117+
118+
Assert::exception(function() use($abraApi) {
119+
$abraApi->import()
120+
->from("storecards", [ "1000000001" ])
121+
->into("OBSCO4S1BRD13FY1010DELDFK2")
122+
->execute();
123+
}, \Exception::class, 'You must specify both left and right importing manager bussiness objects either by ClsID or by API Bussiness object name.');
124+
125+
Assert::exception(function() use($abraApi) {
126+
$abraApi->import()
127+
->from("OBSCO4S1BRD13FY1010DELDFK2", [ "1000000001" ])
128+
->into("storecards")
129+
->execute();
130+
}, \Exception::class, 'You must specify both left and right importing manager bussiness objects either by ClsID or by API Bussiness object name.');
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
[
2-
'input_documents' => '1',
3-
'params' => ['otherParam' => 'otherValue', 'docqueue_id' => '1000000001'],
4-
'input_document_clsid' => 'receivedorders',
5-
'output_document_clsid' => 'billsofdelivery',
6-
]
1+
API is not responding, try to restart API´s Supervisor and Server.
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
[
2-
'input_document_clsid' => 'receivedorders',
3-
'output_document_clsid' => 'billsofdelivery',
4-
'input_documents' => '1000000001',
5-
'params' => ['docqueue_id' => '1000000001', 'otherParam' => 'otherValue'],
6-
]
1+
You must specify both left and right importing manager bussiness objects either by ClsID or by API Bussiness object name.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
use AbraApi\Results\AbraApiImportResult;
4+
use Tester\Assert;
5+
6+
require __DIR__."/../bootstrap.php";
7+
8+
$result = new AbraApiImportResult(json_encode([ "id" => "100000101" ]), [], 201);
9+
10+
Assert::same("100000101", $result->getId());

0 commit comments

Comments
 (0)