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

Commit a40d95d

Browse files
committed
ENH: refs #236. Create item web api method
1 parent 3b1d298 commit a40d95d

File tree

3 files changed

+137
-65
lines changed

3 files changed

+137
-65
lines changed

core/controllers/components/UploadComponent.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function createLinkItem($userDao, $name, $url, $parent = null)
129129
throw new Zend_Exception('Parent permissions errors');
130130
}
131131

132-
Zend_Loader::loadClass("ItemDao", BASE_PATH . '/core/models/dao');
132+
Zend_Loader::loadClass('ItemDao', BASE_PATH.'/core/models/dao');
133133
$item = new ItemDao;
134134
$item->setName($name);
135135
$item->setDescription('');
@@ -232,7 +232,7 @@ public function createUploadedItem($userDao, $name, $path, $parent = null, $lice
232232

233233

234234
// Add bitstreams to the revision
235-
Zend_Loader::loadClass("BitstreamDao", BASE_PATH . '/core/models/dao');
235+
Zend_Loader::loadClass('BitstreamDao', BASE_PATH.'/core/models/dao');
236236
$bitstreamDao = new BitstreamDao;
237237
$bitstreamDao->setName($name);
238238
$bitstreamDao->setPath($path);
@@ -247,7 +247,7 @@ public function createUploadedItem($userDao, $name, $path, $parent = null, $lice
247247
throw new Zend_Exception("Unable to load default assetstore");
248248
}
249249

250-
// Upload the bitstream ifnecessary (based on the assetstore type)
250+
// Upload the bitstream if necessary (based on the assetstore type)
251251
$this->uploadBitstream($bitstreamDao, $assetstoreDao);
252252
$checksum = $bitstreamDao->getChecksum();
253253
$tmpBitstreamDao = $bitstreamModel->getByChecksum($checksum);
@@ -258,7 +258,7 @@ public function createUploadedItem($userDao, $name, $path, $parent = null, $lice
258258
}
259259
$itemRevisionModel->addBitstream($itemRevisionDao, $bitstreamDao);
260260

261-
$this->getLogger()->info(__METHOD__." Upload ok :".$path);
261+
$this->getLogger()->info(__METHOD__.' Upload ok :'.$path);
262262
Zend_Registry::get('notifier')->notifyEvent("EVENT_CORE_UPLOAD_FILE", array($item->toArray(), $itemRevisionDao->toArray()));
263263
return $item;
264264
}//end createUploadedItem

core/models/base/ItemModelBase.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,55 @@ function addRevision($itemdao, $revisiondao)
206206
$this->save($itemdao);//update date
207207
} // end addRevision
208208

209+
/** Create a new empty item */
210+
function createItem($name, $description, $parent, $uuid = '')
211+
{
212+
if(!$parent instanceof FolderDao && !is_numeric($parent))
213+
{
214+
throw new Zend_Exception('Parent should be a folder.');
215+
}
216+
217+
if(empty($name))
218+
{
219+
throw new Zend_Exception('Name cannot be empty.');
220+
}
221+
222+
if(!is_string($name))
223+
{
224+
throw new Zend_Exception('Name should be a string.');
225+
}
226+
227+
if($parent instanceof FolderDao)
228+
{
229+
$parentId = $parent->getFolderId();
230+
}
231+
else
232+
{
233+
$parentId = $parent;
234+
$parent = $this->load($parentId);
235+
}
236+
237+
// Check if an item with the same name already exists for the same parent(s)
238+
$siblings = $parent->getItems();
239+
foreach($siblings as $sibling)
240+
{
241+
if($sibling->getName() == $name)
242+
{
243+
throw new Zend_Exception('An item with the name '.$name.' already exists in that folder');
244+
}
245+
}
246+
247+
$this->loadDaoClass('ItemDao');
248+
$item = new ItemDao();
249+
$item->setName($name);
250+
$item->setDescription($description);
251+
$item->setUuid($uuid);
252+
$this->save($item);
253+
254+
$modelLoad = new MIDAS_ModelLoader();
255+
$folderModel = $modelLoad->loadModel('Folder');
256+
$folderModel->addItem($parent, $item);
257+
$this->copyParentPolicies($item, $parent);
258+
return $item;
259+
}
209260
} // end class ItemModelBase

modules/api/controllers/components/ApiComponent.php

Lines changed: 82 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -94,62 +94,6 @@ function login($args)
9494
return $data;
9595
}
9696

97-
/**
98-
* Get the UUID of a resource
99-
* @param id The id of the resource (numeric)
100-
* @param type The type of the resource (numeric)
101-
* @return Universal identifier
102-
*/
103-
public function uuidGet($args)
104-
{
105-
$this->_validateParams($args, array('id', 'type'));
106-
107-
$id = $args['id'];
108-
$type = $args['type'];
109-
$modelLoad = new MIDAS_ModelLoader();
110-
switch($type)
111-
{
112-
case MIDAS_RESOURCE_ASSETSTORE:
113-
$model = $modelLoad->loadModel('Assetstore');
114-
break;
115-
case MIDAS_RESOURCE_BITSTREAM:
116-
$model = $modelLoad->loadModel('Bitstream');
117-
break;
118-
case MIDAS_RESOURCE_ITEM:
119-
$model = $modelLoad->loadModel('Item');
120-
break;
121-
case MIDAS_RESOURCE_COMMUNITY:
122-
$model = $modelLoad->loadModel('Community');
123-
break;
124-
case MIDAS_RESOURCE_REVISION:
125-
$model = $modelLoad->loadModel('ItemRevision');
126-
break;
127-
case MIDAS_RESOURCE_FOLDER:
128-
$model = $modelLoad->loadModel('Folder');
129-
break;
130-
case MIDAS_RESOURCE_USER:
131-
$model = $modelLoad->loadModel('User');
132-
break;
133-
default :
134-
throw new Zend_Exception("Undefined type");
135-
}
136-
$dao = $model->load($id);
137-
138-
if($dao == false)
139-
{
140-
throw new Exception('Invalid resource type or id.', MIDAS_INVALID_PARAMETER);
141-
}
142-
143-
$uuid = $dao->getUuid();
144-
145-
if($uuid == false)
146-
{
147-
throw new Exception('Invalid resource type or id.', MIDAS_INVALID_PARAMETER);
148-
}
149-
150-
return $uuid;
151-
}
152-
15397
/**
15498
* Get a resource by its UUID
15599
* @param uuid Universal identifier for the resource
@@ -387,7 +331,7 @@ function uploadPerform($args)
387331
}
388332

389333
/**
390-
* Create a new community
334+
* Create a new community or update an existing one using the uuid
391335
* @param token Authentication token
392336
* @param name The community name
393337
* @param description (Optional) The community description
@@ -461,7 +405,7 @@ function communityCreate($args)
461405

462406
if($communityDao === false)
463407
{
464-
throw new Exception('Request failed', MIDAS_INTERNAL_ERROR);
408+
throw new Exception('Create community failed', MIDAS_INTERNAL_ERROR);
465409
}
466410

467411
return $communityDao->toArray();
@@ -591,7 +535,7 @@ function communityDelete($args)
591535
}
592536

593537
/**
594-
* Create a folder
538+
* Create a folder or update an existing one if one exists by the uuid passed
595539
* @param token Authentication token
596540
* @param name The name of the folder to create
597541
* @param description (Optional) The description of the folder
@@ -654,7 +598,7 @@ function folderCreate($args)
654598
$new_folder = $folderModel->createFolder($name, $description, $folder, $uuid);
655599
if($new_folder === false)
656600
{
657-
throw new Exception('Request failed', MIDAS_INTERNAL_ERROR);
601+
throw new Exception('Create folder failed', MIDAS_INTERNAL_ERROR);
658602
}
659603
$policyGroup = $folder->getFolderpolicygroup();
660604
$policyUser = $folder->getFolderpolicyuser();
@@ -778,6 +722,78 @@ function folderDownload($args)
778722
$this->controller->redirect('/download/?folders='.$folder->getKey());
779723
}
780724

725+
/**
726+
* Create an item or update an existing one if one exists by the uuid passed
727+
* @param token Authentication token
728+
* @param name The name of the item to create
729+
* @param description (Optional) The description of the item
730+
* @param uuid (Optional) Uuid of the item. If none is passed, will generate one.
731+
* @param privacy (Optional) Default 'Public'.
732+
* @param parentid The id of the parent folder
733+
* @return The item object that was created
734+
*/
735+
function itemCreate($args)
736+
{
737+
$this->_validateParams($args, array('name'));
738+
$userDao = $this->_getUser($args);
739+
if($userDao == false)
740+
{
741+
throw new Exception('Cannot create item anonymously', MIDAS_INVALID_POLICY);
742+
}
743+
744+
$modelLoader = new MIDAS_ModelLoader();
745+
$itemModel = $modelLoader->loadModel('Item');
746+
$name = $args['name'];
747+
$description = $args['description'];
748+
749+
$uuid = isset($args['uuid']) ? $args['uuid'] : '';
750+
$record = false;
751+
if(!empty($uuid))
752+
{
753+
$componentLoader = new MIDAS_ComponentLoader();
754+
$uuidComponent = $componentLoader->loadComponent('Uuid');
755+
$record = $uuidComponent->getByUid($uuid);
756+
}
757+
if($record != false && $record instanceof ItemDao)
758+
{
759+
if(!$itemModel->policyCheck($record, $userDao, MIDAS_POLICY_WRITE))
760+
{
761+
throw new Exception('Invalid policy', MIDAS_INVALID_POLICY);
762+
}
763+
$record->setName($name);
764+
if(isset($args['description']))
765+
{
766+
$record->setDescription($args['description']);
767+
}
768+
if(isset($args['privacy']))
769+
{
770+
$record->setPrivacy($args['privacy']);
771+
}
772+
$itemModel->save($record);
773+
return $record->toArray();
774+
}
775+
else
776+
{
777+
if(!array_key_exists('parentid', $args))
778+
{
779+
throw new Exception('Parameter parentid is not defined', MIDAS_INVALID_PARAMETER);
780+
}
781+
$folderModel = $modelLoader->loadModel('Folder');
782+
$folder = $folderModel->load($args['parentid']);
783+
if($folder == false)
784+
{
785+
throw new Exception('Parent folder doesn\'t exist', MIDAS_INVALID_PARAMETER);
786+
}
787+
$item = $itemModel->createItem($name, $description, $folder, $uuid);
788+
if($item === false)
789+
{
790+
throw new Exception('Create new item failed', MIDAS_INTERNAL_ERROR);
791+
}
792+
793+
return $item->toArray();
794+
}
795+
}
796+
781797
/**
782798
* Get an item's information
783799
* @param token (Optional) Authentication token
@@ -805,7 +821,7 @@ function itemGet($args)
805821
$owningFolders = $item->getFolders();
806822
if(count($owningFolders) > 0)
807823
{
808-
$itemArray['folder_id'] = $owningFolders[0]->parent_id;
824+
$itemArray['folder_id'] = $owningFolders[0]->getKey();
809825
}
810826

811827
$revisionsArray = array();
@@ -817,8 +833,13 @@ function itemGet($args)
817833
{
818834
$revisions = $item->getRevisions();
819835
}
836+
820837
foreach($revisions as $revision)
821838
{
839+
if(!$revision)
840+
{
841+
continue;
842+
}
822843
$bitstreamArray = array();
823844
$bitstreams = $revision->getBitstreams();
824845
foreach($bitstreams as $b)

0 commit comments

Comments
 (0)