Add search and list_story methods

This commit is contained in:
Romain Neutron
2012-12-19 17:10:04 +01:00
parent a25c841391
commit ee215ce63c
2 changed files with 269 additions and 48 deletions

View File

@@ -837,20 +837,69 @@ class API_V1_adapter extends API_V1_Abstract
);
}
/**
* Search for results
*
* @param Request $request
* @return \API_V1_result
*/
public function search(Request $request)
{
$result = new API_V1_result($request, $this);
list($ret, $search_result) = $this->prepare_search_request($request);
$ret['results'] = array('records' => array(), 'stories' => array());
foreach ($search_result->get_datas()->get_elements() as $record) {
if ($record->is_grouping()) {
$ret['results']['stories'][] = $this->list_story($record);
} else {
$ret['results']['records'][] = $this->list_record($record);
}
}
/**
* @todo donner des highlights
*/
$result->set_datas($ret);
return $result;
}
/**
* Get an API_V1_result containing the results of a records search
*
* Deprecated in favor of search
*
* @param Request $request
* @param int $databox_id
* @param string $response_type
* @return API_V1_result
*/
public function search_records(Request $request)
{
$result = new API_V1_result($request, $this);
list($ret, $search_result) = $this->prepare_search_request($request);
foreach ($search_result->get_datas()->get_elements() as $record) {
$ret['results'][] = $this->list_record($record);
}
/**
* @todo donner des highlights
*/
$result->set_datas($ret);
return $result;
}
private function prepare_search_request(Request $request)
{
$session = $this->appbox->get_session();
$user = User_Adapter::getInstance($session->get_usr_id(), $this->appbox);
$registry = $this->appbox->get_registry();
$result = new API_V1_result($request, $this);
$search_type = ($request->get('search_type')
&& in_array($request->get('search_type'), array(0, 1))) ?
@@ -948,16 +997,7 @@ class API_V1_adapter extends API_V1_Abstract
'query' => $search_engine->get_query(),
);
foreach ($search_result->get_datas()->get_elements() as $record) {
$ret['results'][] = $this->list_record($record);
}
/**
* @todo donner des highlights
*/
$result->set_datas($ret);
return $result;
return array($ret, $search_result);
}
/**
@@ -973,17 +1013,25 @@ class API_V1_adapter extends API_V1_Abstract
{
$result = new API_V1_result($request, $this);
$containers = $this->appbox
$that = $this;
$baskets = array_map(function ($basket) use ($that) {
return $that->list_basket($basket);
}, (array) $this->appbox
->get_databox($databox_id)
->get_record($record_id)
->get_container_baskets();
->get_container_baskets()
);
$ret = array();
foreach ($containers as $basket) {
$ret[] = $this->list_basket($basket);
}
$record = $this->appbox->get_databox($databox_id)->get_record($record_id);
$result->set_datas(array("baskets" => $ret));
$stories = array_map(function ($story) use ($that) {
return $that->list_story($story);
}, $record->get_grouping_parents()->get_elements());
$result->set_datas(array(
"baskets" => $baskets,
"stories" => $stories,
));
return $result;
}
@@ -1857,6 +1905,65 @@ class API_V1_adapter extends API_V1_Abstract
);
}
/**
* Retrieve detailled informations about one story
*
* @param record_adapter $story
* @return array
*/
protected function list_story(record_adapter $story, $includeChildren = true)
{
if (!$story->is_grouping()) {
throw new \API_V1_exception_notfound('Story not found');
}
$that = $this;
$records = array_map(function (\record_adapter $record) use ($that) {
return $that->list_record($record);
}, array_values($story->get_children()->get_elements()));
$caption = $story->get_caption();
$format = function(caption_record $caption, $dcField) {
$field = $caption->get_dc_field($dcField);
if (!$field) {
return null;
}
return $field->get_serialized_values();
};
return array(
'databox_id' => $story->get_sbas_id(),
'story_id' => $story->get_record_id(),
'updated_on' => $story->get_modification_date()->format(DATE_ATOM),
'created_on' => $story->get_creation_date()->format(DATE_ATOM),
'collection_id' => phrasea::collFromBas($story->get_base_id()),
'thumbnail' => $this->list_embedable_media($story->get_thumbnail(), registry::get_instance()),
'uuid' => $story->get_uuid(),
'metadatas' => array(
'dc:contributor' => $format($caption, databox_Field_DCESAbstract::Contributor),
'dc:coverage' => $format($caption, databox_Field_DCESAbstract::Coverage),
'dc:creator' => $format($caption, databox_Field_DCESAbstract::Creator),
'dc:date' => $format($caption, databox_Field_DCESAbstract::Date),
'dc:description' => $format($caption, databox_Field_DCESAbstract::Description),
'dc:format' => $format($caption, databox_Field_DCESAbstract::Format),
'dc:identifier' => $format($caption, databox_Field_DCESAbstract::Identifier),
'dc:language' => $format($caption, databox_Field_DCESAbstract::Language),
'dc:publisher' => $format($caption, databox_Field_DCESAbstract::Publisher),
'dc:relation' => $format($caption, databox_Field_DCESAbstract::Relation),
'dc:rights' => $format($caption, databox_Field_DCESAbstract::Rights),
'dc:source' => $format($caption, databox_Field_DCESAbstract::Source),
'dc:subject' => $format($caption, databox_Field_DCESAbstract::Subject),
'dc:title' => $format($caption, databox_Field_DCESAbstract::Title),
'dc:type' => $format($caption, databox_Field_DCESAbstract::Type),
),
'records' => $records,
);
}
/**
* List all databoxes of the current appbox
*

View File

@@ -163,24 +163,163 @@ class API_V1_adapterTest extends PhraseanetPHPUnitAuthenticatedAbstract
}
}
public function testSearch_records()
public function testSearch_recordsWithRecords()
{
$request = new Request(array('record_type' => "image"), array(), array(), array(), array(), array('HTTP_Accept' => 'application/json'));
static::$records['record_1'];
$request = new Request(array('record_type' => "image", 'search_type' => 0), array(), array(), array(), array(), array('HTTP_Accept' => 'application/json'));
$result = $this->object->search_records($request);
$this->assertEquals(200, $result->get_http_code());
$this->assertEquals('application/json', $result->get_content_type());
$this->assertTrue(is_array(json_decode($result->format(), true)));
$data = json_decode($result->format(), true);
$found = false;
foreach ($data['response']['results'] as $retRecord) {
if($retRecord['record_id'] == static::$records['record_1']->get_record_id() && $retRecord['databox_id'] == static::$records['record_1']->get_sbas_id()) {
$found = true;
break;
}
}
if (!$found) {
$this->fail('unable to find the record back');
}
}
public function testSearch_recordsWithStories()
{
$story = \record_adapter::createStory(self::$collection);
$story->appendChild(static::$records['record_1']);
$request = new Request(array('search_type' => 1), array(), array(), array(), array(), array('HTTP_Accept' => 'application/json'));
$result = $this->object->search_records($request);
$this->assertEquals(200, $result->get_http_code());
$this->assertEquals('application/json', $result->get_content_type());
$this->assertTrue(is_array(json_decode($result->format(), true)));
$data = json_decode($result->format(), true);
$found = false;
foreach ($data['response']['results'] as $retStory) {
if($retStory['record_id'] == $story->get_record_id() && $retStory['databox_id'] == $story->get_sbas_id()) {
$found = true;
break;
}
}
if (!$found) {
$this->fail('unable to find the story back');
}
}
public function testSearchWithStories()
{
$story = \record_adapter::createStory(self::$collection);
$story->appendChild(static::$records['record_1']);
$request = new Request(array('search_type' => 1), array(), array(), array(), array(), array('HTTP_Accept' => 'application/json'));
$result = $this->object->search($request);
$this->assertEquals(200, $result->get_http_code());
$this->assertEquals('application/json', $result->get_content_type());
$this->assertTrue(is_array(json_decode($result->format(), true)));
$data = json_decode($result->format(), true);
$this->assertArrayHasKey('records', $data['response']['results']);
$this->assertArrayHasKey('stories', $data['response']['results']);
$found = false;
foreach ($data['response']['results']['stories'] as $retStory) {
if($retStory['story_id'] == $story->get_record_id() && $retStory['databox_id'] == $story->get_sbas_id()) {
$found = true;
break;
}
}
if (!$found) {
$this->fail('unable to find the story back');
}
}
public function testSearchWithRecords()
{
static::$records['record_1'];
$request = new Request(array('search_type' => 0), array(), array(), array(), array(), array('HTTP_Accept' => 'application/json'));
$result = $this->object->search($request);
$this->assertEquals(200, $result->get_http_code());
$this->assertEquals('application/json', $result->get_content_type());
$this->assertTrue(is_array(json_decode($result->format(), true)));
$data = json_decode($result->format(), true);
$this->assertArrayHasKey('records', $data['response']['results']);
$this->assertArrayHasKey('stories', $data['response']['results']);
$found = false;
foreach ($data['response']['results']['records'] as $retRecord) {
if($retRecord['record_id'] == static::$records['record_1']->get_record_id() && $retRecord['databox_id'] == static::$records['record_1']->get_sbas_id()) {
$found = true;
break;
}
}
if (!$found) {
$this->fail('unable to find the record back');
}
}
public function testGet_record_related()
{
$appbox = appbox::get_instance(\bootstrap::getCore());
$basketElement = $this->insertOneBasketElement();
$basketElement->setRecord(static::$records['record_1']);
$story = \record_adapter::createStory(self::$collection);
$story->appendChild(static::$records['record_1']);
self::$core['EM']->flush($basketElement);
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_Accept' => 'application/json'));
$result = $this->object->get_record_related($request, static::$records['record_1']->get_sbas_id(), static::$records['record_1']->get_record_id());
$this->assertEquals(200, $result->get_http_code());
$this->assertEquals('application/json', $result->get_content_type());
$this->assertTrue(is_array(json_decode($result->format(), true)));
$data = json_decode($result->format(), true);
$this->assertArrayHasKey('baskets', $data['response']);
$this->assertArrayHasKey('stories', $data['response']);
$found = false;
foreach ($data['response']['baskets'] as $bask) {
if ($bask['basket_id'] == $basketElement->getBasket()->getId()) {
$found = true;
break;
}
}
if (!$found) {
$this->fail('unable to find the basket back');
}
$found = false;
foreach ($data['response']['stories'] as $retStory) {
if ($retStory['story_id'] == $story->get_record_id() && $retStory['databox_id'] == $story->get_sbas_id()) {
$found = true;
break;
}
}
if (!$found) {
$this->fail('unable to find the story back');
}
}
public function testGet_record_metadatas()
@@ -233,7 +372,6 @@ class API_V1_adapterTest extends PhraseanetPHPUnitAuthenticatedAbstract
$caption_field_value = caption_Field_Value::create(databox_field::get_instance($databox, 1), static::$records['record_1'], 'my value');
}
//valide metas
$metadatas = array();
foreach (static::$records['record_1']->get_databox()->get_meta_structure()->get_elements() as $field) {
@@ -499,30 +637,6 @@ class API_V1_adapterTest extends PhraseanetPHPUnitAuthenticatedAbstract
$feed->delete();
}
public function testSearch_users()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
public function testGet_user_acces()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
public function testAdd_user()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
protected function checkResponseField(API_V1_result $result, $field, $type)
{
$response = json_decode($result->format(), true);