Added RecordReferenceCollection utility method

This commit is contained in:
Benoît Burnichon
2016-03-04 15:27:00 +01:00
parent a894faf1fa
commit 25fd47fa7b
7 changed files with 150 additions and 134 deletions

View File

@@ -20,6 +20,7 @@ use Alchemy\Phrasea\Model\Entities\Order;
use Alchemy\Phrasea\Order\OrderElementTransformer; use Alchemy\Phrasea\Order\OrderElementTransformer;
use Alchemy\Phrasea\Order\OrderFiller; use Alchemy\Phrasea\Order\OrderFiller;
use Alchemy\Phrasea\Order\OrderTransformer; use Alchemy\Phrasea\Order\OrderTransformer;
use Alchemy\Phrasea\Record\RecordReferenceCollection;
use Assert\Assertion; use Assert\Assertion;
use Assert\InvalidArgumentException; use Assert\InvalidArgumentException;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
@@ -45,8 +46,7 @@ class OrderController extends Controller
{ {
$data = $this->decodeJsonBody($request, 'orders.json#/definitions/order_request'); $data = $this->decodeJsonBody($request, 'orders.json#/definitions/order_request');
$requestedRecords = $this->toRequestedRecordsArray($data->data->records); $availableRecords = $this->toRequestedRecords($data->data->records);
$availableRecords = $this->fetchRecords($requestedRecords);
$records = $this->filterOrderableRecords($availableRecords); $records = $this->filterOrderableRecords($availableRecords);
$recordRequest = new RecordsRequest($records, new ArrayCollection($availableRecords), null, RecordsRequest::FLATTEN_YES); $recordRequest = new RecordsRequest($records, new ArrayCollection($availableRecords), null, RecordsRequest::FLATTEN_YES);
@@ -132,9 +132,9 @@ class OrderController extends Controller
/** /**
* @param array $records * @param array $records
* @return array * @return \record_adapter[]
*/ */
private function toRequestedRecordsArray(array $records) private function toRequestedRecords(array $records)
{ {
$requestedRecords = []; $requestedRecords = [];
@@ -145,38 +145,7 @@ class OrderController extends Controller
]; ];
} }
return $requestedRecords; return RecordReferenceCollection::fromArrayOfArray($requestedRecords)->toRecords($this->getApplicationBox());
}
/**
* @param array $recordIds
* @return \record_adapter[]
*/
private function fetchRecords(array $recordIds)
{
$perDataboxRecords = [];
foreach ($recordIds as $index => $record) {
if (!isset($perDataboxRecords[$record['databox_id']])) {
$perDataboxRecords[$record['databox_id']] = [];
}
$perDataboxRecords[$record['databox_id']][$record['record_id']] = $index;
}
$records = [];
foreach ($perDataboxRecords as $databoxId => $recordIndexes) {
$repository = $this->findDataboxById($databoxId)->getRecordRepository();
foreach ($repository->findByRecordIds(array_keys($perDataboxRecords[$databoxId])) as $record) {
$records[$recordIndexes[$record->getRecordId()]] = $record;
}
}
ksort($records);
return $records;
} }
/** /**
@@ -187,9 +156,15 @@ class OrderController extends Controller
{ {
$acl = $this->getAclForUser(); $acl = $this->getAclForUser();
return array_filter($records, function (\record_adapter $record) use ($acl) { $filtered = [];
return $acl->has_right_on_base($record->getBaseId(), 'cancmd');
}); foreach ($records as $index => $record) {
if ($acl->has_right_on_base($record->getBaseId(), 'cancmd')) {
$filtered[$index] = $record;
}
}
return $filtered;
} }
/** /**

View File

@@ -53,11 +53,11 @@ use Alchemy\Phrasea\Model\Repositories\FeedEntryRepository;
use Alchemy\Phrasea\Model\Repositories\FeedRepository; use Alchemy\Phrasea\Model\Repositories\FeedRepository;
use Alchemy\Phrasea\Model\Repositories\LazaretFileRepository; use Alchemy\Phrasea\Model\Repositories\LazaretFileRepository;
use Alchemy\Phrasea\Model\Repositories\TaskRepository; use Alchemy\Phrasea\Model\Repositories\TaskRepository;
use Alchemy\Phrasea\Record\RecordReferenceCollection;
use Alchemy\Phrasea\SearchEngine\SearchEngineInterface; use Alchemy\Phrasea\SearchEngine\SearchEngineInterface;
use Alchemy\Phrasea\SearchEngine\SearchEngineLogger; use Alchemy\Phrasea\SearchEngine\SearchEngineLogger;
use Alchemy\Phrasea\SearchEngine\SearchEngineOptions; use Alchemy\Phrasea\SearchEngine\SearchEngineOptions;
use Alchemy\Phrasea\SearchEngine\SearchEngineResult; use Alchemy\Phrasea\SearchEngine\SearchEngineResult;
use Alchemy\Phrasea\SearchEngine\SearchEngineResultToRecordsConverter;
use Alchemy\Phrasea\SearchEngine\SearchEngineSuggestion; use Alchemy\Phrasea\SearchEngine\SearchEngineSuggestion;
use Alchemy\Phrasea\Status\StatusStructure; use Alchemy\Phrasea\Status\StatusStructure;
use Alchemy\Phrasea\TaskManager\LiveInformation; use Alchemy\Phrasea\TaskManager\LiveInformation;
@@ -1065,10 +1065,10 @@ class V1Controller extends Controller
$ret['results'] = ['records' => [], 'stories' => []]; $ret['results'] = ['records' => [], 'stories' => []];
$converter = new SearchEngineResultToRecordsConverter($this->getApplicationBox());
/** @var SearchEngineResult $search_result */ /** @var SearchEngineResult $search_result */
foreach ($converter->convert($search_result->getResults()) as $record) { $references = new RecordReferenceCollection($search_result->getResults());
foreach ($references->toRecords($this->getApplicationBox()) as $record) {
if ($record->isStory()) { if ($record->isStory()) {
$ret['results']['stories'][] = $this->listStory($request, $record); $ret['results']['stories'][] = $this->listStory($request, $record);
} else { } else {

View File

@@ -2,13 +2,8 @@
namespace Alchemy\Phrasea\Model; namespace Alchemy\Phrasea\Model;
interface RecordInterface interface RecordInterface extends RecordReferenceInterface
{ {
/**
* @return string
*/
public function getId();
/** /**
* The unique id of the collection where belong the record. * The unique id of the collection where belong the record.
* *
@@ -26,13 +21,6 @@ interface RecordInterface
/** @return \DateTime */ /** @return \DateTime */
public function getCreated(); public function getCreated();
/**
* The id of the databox where belong the record.
*
* @return integer
*/
public function getDataboxId();
/** @return boolean */ /** @return boolean */
public function isStory(); public function isStory();
@@ -42,9 +30,6 @@ interface RecordInterface
/** @return string */ /** @return string */
public function getOriginalName(); public function getOriginalName();
/** @return integer */
public function getRecordId();
/** @return string */ /** @return string */
public function getSha256(); public function getSha256();

View File

@@ -0,0 +1,29 @@
<?php
/**
* This file is part of Phraseanet
*
* (c) 2005-2016 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Model;
interface RecordReferenceInterface
{
/**
* @return string
*/
public function getId();
/**
* @return int
*/
public function getDataboxId();
/**
* @return int
*/
public function getRecordId();
}

View File

@@ -10,9 +10,10 @@
namespace Alchemy\Phrasea\Record; namespace Alchemy\Phrasea\Record;
use Alchemy\Phrasea\Model\RecordReferenceInterface;
use Assert\Assertion; use Assert\Assertion;
class RecordReference class RecordReference implements RecordReferenceInterface
{ {
/** /**
* @var int * @var int

View File

@@ -0,0 +1,100 @@
<?php
/**
* This file is part of Phraseanet
*
* (c) 2005-2016 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Record;
use Assert\Assertion;
class RecordReferenceCollection implements \IteratorAggregate
{
/**
* @param array<int|string,array> $records
* @return RecordReferenceCollection
*/
public static function fromArrayOfArray($records)
{
Assertion::allIsArrayAccessible($records);
$references = [];
foreach ($records as $index => $record) {
if (isset($record['id'])) {
$references[$index] = RecordReference::createFromRecordReference($record['id']);
} elseif (isset($record['databox_id']) && isset($record['record_id'])) {
$references[$index] = RecordReference::createFromDataboxIdAndRecordId($record['databox_id'], $record['record_id']);
}
}
return new self($references);
}
/**
* @var RecordReference[]
*/
private $references = [];
/**
* @param RecordReference[] $references
*/
public function __construct($references)
{
Assertion::allIsInstanceOf($references, RecordReference::class);
$this->references = $references;
}
public function getIterator()
{
return new \ArrayIterator($this->references);
}
/**
* @return array<int,array<int,int>>
*/
public function groupPerDataboxId()
{
$groups = [];
foreach ($this->references as $index => $reference) {
$databoxId = $reference->getDataboxId();
if (!isset($groups[$databoxId])) {
$groups[$databoxId] = [];
}
$groups[$databoxId][$reference->getRecordId()] = $index;
}
return $groups;
}
/**
* @param \appbox $appbox
* @return \record_adapter[]
*/
public function toRecords(\appbox $appbox)
{
$groups = $this->groupPerDataboxId();
$records = [];
foreach ($groups as $databoxId => $recordIds) {
$databox = $appbox->get_databox($databoxId);
foreach ($databox->getRecordRepository()->findByRecordIds(array_keys($recordIds)) as $record) {
$records[$recordIds[$record->getRecordId()]] = $record;
}
}
ksort($records);
return array_values($records);
}
}

View File

@@ -1,74 +0,0 @@
<?php
/**
* This file is part of Phraseanet
*
* (c) 2005-2016 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\SearchEngine;
use Alchemy\Phrasea\Model\RecordInterface;
use Assert\Assertion;
class SearchEngineResultToRecordsConverter
{
/**
* @var \appbox
*/
private $appbox;
public function __construct(\appbox $appbox)
{
$this->appbox = $appbox;
}
/**
* @param RecordInterface[] $records
* @return \record_adapter[]
*/
public function convert($records)
{
Assertion::allIsInstanceOf($records, RecordInterface::class);
$perDataboxRecordIds = $this->groupRecordIdsPerDataboxId($records);
$records = [];
foreach ($perDataboxRecordIds as $databoxId => $recordIds) {
$databox = $this->appbox->get_databox($databoxId);
foreach ($databox->getRecordRepository()->findByRecordIds(array_keys($recordIds)) as $record) {
$records[$recordIds[$record->getRecordId()]] = $record;
}
}
ksort($records);
return $records;
}
/**
* @param RecordInterface[] $records
* @return array[]
*/
private function groupRecordIdsPerDataboxId($records)
{
$number = 0;
$perDataboxRecordIds = [];
foreach ($records as $record) {
$databoxId = $record->getDataboxId();
if (!isset($perDataboxRecordIds[$databoxId])) {
$perDataboxRecordIds[$databoxId] = [];
}
$perDataboxRecordIds[$databoxId][$record->getRecordId()] = $number++;
}
return $perDataboxRecordIds;
}
}