Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit b83988a

Browse files
author
mgrauer
committed
Add item rest addmetadata endpoint and initial test
1 parent 9190dbe commit b83988a

File tree

5 files changed

+221
-0
lines changed

5 files changed

+221
-0
lines changed

core/controllers/api/ItemController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public function putAction()
9595
'removepolicyuser' => 'itemRemovePolicyuser',
9696
'setmetadata' => 'itemSetmetadata',
9797
'setmultiplemetadata' => 'itemSetmultiplemetadata',
98+
'addmetadata' => 'itemAddmetadata',
9899
'deletemetadata' => 'itemDeletemetadata',
99100
'deletemetadataall' => 'itemDeletemetadataAll',
100101
);

core/controllers/components/ApiitemComponent.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,71 @@ public function itemSetmultiplemetadata($args)
170170
return true;
171171
}
172172

173+
/**
174+
* Set multiple metadata fields on an item, will overwrite any existing metadatum with
175+
* the new value; requires passing the metadata in the request body as JSON
176+
* with a key of 'metadata' mapping to a list; each element of the 'metadata' list is
177+
* a metadatum on the item; each metadatum requires an 'element' and 'value' key, with
178+
* 'qualifier' and 'type' being optional keys on the metadatum, 'type' defaults to MIDAS_METADATA_TEXT.
179+
* @path /item/addmetadata/{id}
180+
* @http PUT
181+
* @param id The id of the item
182+
* @param revision (Optional) Item Revision number to set metadata on, defaults to latest revision.
183+
* @return true on success,
184+
* will fail if there are no revisions or the specified revision is not found.
185+
*
186+
* @param array $args parameters
187+
* @throws Exception
188+
*/
189+
public function itemAddmetadata($args)
190+
{
191+
/** @var ApihelperComponent $apihelperComponent */
192+
$apihelperComponent = MidasLoader::loadComponent('Apihelper');
193+
$apihelperComponent->validateParams($args, array('id'));
194+
$userDao = $apihelperComponent->getUser($args);
195+
196+
$apihelperComponent->requirePolicyScopes(array(MIDAS_API_PERMISSION_SCOPE_WRITE_DATA));
197+
198+
/** @var ItemModel $itemModel */
199+
$itemModel = MidasLoader::loadModel('Item');
200+
$item = $itemModel->load($args['id']);
201+
if ($item === false || !$itemModel->policyCheck($item, $userDao, MIDAS_POLICY_WRITE)) {
202+
throw new Exception("This item doesn't exist or you don't have write permission.", MIDAS_INVALID_POLICY);
203+
}
204+
$revisionNumber = array_key_exists('revision', $args) ? (int) $args['revision'] : null;
205+
$revision = $apihelperComponent->getItemRevision($item, $revisionNumber);
206+
207+
if (!array_key_exists(0, $args)) {
208+
throw new Exception("Missing request body data.", MIDAS_INVALID_PARAMETER);
209+
}
210+
$json_body = json_decode($args[0]);
211+
if (NULL === $json_body) {
212+
throw new Exception("Request body data must be valid JSON.", MIDAS_INVALID_PARAMETER);
213+
}
214+
if (!array_key_exists('metadata', $json_body)) {
215+
throw new Exception("Request body data missing key 'metadata'.", MIDAS_INVALID_PARAMETER);
216+
}
217+
$metadata = $json_body->metadata;
218+
foreach ($metadata as $metadatum) {
219+
if (!isset($metadatum->element) || !isset($metadatum->value)) {
220+
throw new Exception("All metadata must have 'element' and 'value' keys.", MIDAS_INVALID_PARAMETER);
221+
}
222+
}
223+
224+
foreach ($metadata as $metadatum) {
225+
$apihelperComponent->setMetadata(
226+
$item,
227+
isset($metadatum->type) ? $metadatum->type : MIDAS_METADATA_TEXT,
228+
$metadatum->element,
229+
isset($metadatum->qualifier) ? $metadatum->qualifier : '',
230+
$metadatum->value,
231+
$revision
232+
);
233+
}
234+
235+
return true;
236+
}
237+
173238
/**
174239
* Delete a metadata tuple (element, qualifier, type) from a specific item revision,
175240
* defaults to the latest revision of the item.

core/tests/controllers/api/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919

2020
add_midas_test(CoreRestCallUserMethodsTest RestCallUserMethodsTest.php)
2121
add_midas_test(CoreRestKeyControllerTest RestKeyControllerTest.php)
22+
add_midas_test(CoreRestCallItemMethodsTest RestCallItemMethodsTest.php)
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
/*=========================================================================
3+
Midas Server
4+
Copyright Kitware SAS, 26 rue Louis Guérin, 69100 Villeurbanne, France.
5+
All rights reserved.
6+
For more information visit http://www.kitware.com/.
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
21+
require_once BASE_PATH.'/core/tests/controllers/api/RestCallMethodsTestCase.php';
22+
23+
/** Tests the functionality of the web API Rest Item methods */
24+
class Core_RestCallItemMethodsTest extends RestCallMethodsTestCase
25+
{
26+
/** set up tests */
27+
public function setUp()
28+
{
29+
parent::setUp();
30+
}
31+
32+
/** Set metadata on the item. */
33+
public function testItemAddmetadata()
34+
{
35+
$itemsFile = $this->loadData('Item', 'default');
36+
$itemDao = $this->Item->load($itemsFile[1]->getKey());
37+
38+
$apipath = '/item/addmetadata/' . $itemDao->getItemId();
39+
40+
// No user will fail.
41+
$this->resetAll();
42+
$resp = $this->_callRestApi('PUT', $apipath);
43+
$this->_assertStatusFail($resp);
44+
45+
$usersFile = $this->loadData('User', 'default');
46+
$userDao = $this->User->load($usersFile[0]->getKey());
47+
48+
// Lack of request body will fail.
49+
$this->resetAll();
50+
$this->params['useSession'] = 'true';
51+
$resp = $this->_callRestApi('PUT', $apipath, $userDao);
52+
$this->_assertStatusFail($resp);
53+
54+
// Request body without a 'metadata' key will fail.
55+
$this->resetAll();
56+
$this->params['useSession'] = 'true';
57+
$this->params[0] = json_encode(array('murkydata' => array()));
58+
$resp = $this->_callRestApi('PUT', $apipath, $userDao);
59+
$this->_assertStatusFail($resp);
60+
61+
// Metadatum needs 'value' key.
62+
$this->resetAll();
63+
$this->params['useSession'] = 'true';
64+
$this->params[0] = json_encode(array('metadata' => array('element' => 'key1')));
65+
$resp = $this->_callRestApi('PUT', $apipath, $userDao);
66+
$this->_assertStatusFail($resp);
67+
68+
// Metadatum needs 'element' key.
69+
$this->resetAll();
70+
$this->params['useSession'] = 'true';
71+
$this->params[0] = json_encode(array('metadata' => array('value' => 'val1')));
72+
$resp = $this->_callRestApi('PUT', $apipath, $userDao);
73+
$this->_assertStatusFail($resp);
74+
75+
// Write some metadata correctly.
76+
$this->resetAll();
77+
$this->params['useSession'] = 'true';
78+
$this->params[0] = json_encode(array('metadata' => array(
79+
array('element' => 'key1', 'value' => 'val1'),
80+
array('element' => 'key2', 'value' => 'val2')
81+
)));
82+
$resp = $this->_callRestApi('PUT', $apipath, $userDao);
83+
$this->_assertStatusOk($resp);
84+
85+
/** @var ItemModel $itemModel */
86+
$itemModel = MidasLoader::loadModel('Item');
87+
$revisionDao = $itemModel->getLastRevision($itemDao);
88+
/** @var ItemRevisionModel $itemRevisionModel */
89+
$itemRevisionModel = MidasLoader::loadModel('ItemRevision');
90+
$metadata = $itemRevisionModel->getMetadata($revisionDao);
91+
$metadataArray = array();
92+
$key1 = false;
93+
$key2 = false;
94+
foreach ($metadata as $metadatum) {
95+
if ($metadatum->element === 'key1' && $metadatum->value === 'val1') {
96+
$key1 = true;
97+
}
98+
if ($metadatum->element === 'key2' && $metadatum->value === 'val2') {
99+
$key2 = true;
100+
}
101+
}
102+
$this->assertTrue($key1 && $key2, 'Metadata incorrectly set');
103+
104+
// Update key1, add key3, leave key2 alone.
105+
$this->resetAll();
106+
$this->params['useSession'] = 'true';
107+
$this->params[0] = json_encode(array('metadata' => array(
108+
array('element' => 'key1', 'value' => 'newval1'),
109+
array('element' => 'key3', 'value' => 'val3')
110+
)));
111+
$resp = $this->_callRestApi('PUT', $apipath, $userDao);
112+
$this->_assertStatusOk($resp);
113+
114+
$metadata = $itemRevisionModel->getMetadata($revisionDao);
115+
$metadataArray = array();
116+
$key1 = false;
117+
$key2 = false;
118+
$key3 = false;
119+
foreach ($metadata as $metadatum) {
120+
if ($metadatum->element === 'key1' && $metadatum->value === 'newval1') {
121+
$key1 = true;
122+
}
123+
if ($metadatum->element === 'key2' && $metadatum->value === 'val2') {
124+
$key2 = true;
125+
}
126+
if ($metadatum->element === 'key3' && $metadatum->value === 'val3') {
127+
$key3 = true;
128+
}
129+
}
130+
$this->assertTrue($key1 && $key2 && $key3, 'Metadata incorrectly set');
131+
}
132+
}

modules/api/controllers/components/ApiComponent.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,28 @@ public function itemSetmultiplemetadata($args)
767767
return $this->_callCoreApiMethod($args, 'itemSetmultiplemetadata', 'item');
768768
}
769769

770+
/**
771+
* Set multiple metadata fields on an item, will overwrite any existing metadatum with
772+
* the new value; requires passing the metadata in the request body as JSON
773+
* with a key of 'metadata' mapping to a list; each element of the 'metadata' list is
774+
* a metadatum on the item; each metadatum requires an 'element' and 'value' key, with
775+
* 'qualifier' and 'type' being optional keys on the metadatum, 'type' defaults to MIDAS_METADATA_TEXT.
776+
*
777+
* @param token Authentication token
778+
* @param itemid The id of the item
779+
* @param revision (Optional) Item Revision number to set metadata on, defaults to latest revision.
780+
* @return true on success,
781+
* will fail if there are no revisions or the specified revision is not found.
782+
*/
783+
public function itemAddmetadata($args)
784+
{
785+
/** @var ApihelperComponent $ApihelperComponent */
786+
$ApihelperComponent = MidasLoader::loadComponent('Apihelper');
787+
$ApihelperComponent->renameParamKey($args, 'itemid', 'id');
788+
789+
return $this->_callCoreApiMethod($args, 'itemAddmetadata', 'item');
790+
}
791+
770792
/**
771793
* Delete a metadata tuple (element, qualifier, type) from a specific item revision,
772794
* defaults to the latest revision of the item.

0 commit comments

Comments
 (0)