-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API] Add Project endpoint "Dicoms" #6775
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
<?php | ||
/** | ||
* This implements the Dicoms page class under Project | ||
* | ||
* PHP Version 7 | ||
* | ||
* @category API | ||
* @package Loris | ||
* @author Simon Pelletier <simon.pelletier@mcin.ca> | ||
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 | ||
* @link https://github.com/aces/Loris | ||
*/ | ||
namespace LORIS\api\Endpoints\Project; | ||
|
||
use \Psr\Http\Message\ServerRequestInterface; | ||
use \Psr\Http\Message\ResponseInterface; | ||
use \LORIS\api\Endpoint; | ||
|
||
/** | ||
* A class for handling the /projects/$projectname/dicoms endpoint. | ||
* | ||
* @category API | ||
* @package Loris | ||
* @author Simon Pelletier <simon.pelletier@mcin.ca> | ||
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 | ||
* @link https://github.com/aces/Loris | ||
*/ | ||
class Dicoms extends Endpoint implements \LORIS\Middleware\ETagCalculator | ||
{ | ||
/** | ||
* A cache of the results of the endpoint, so that | ||
* it doesn't need to be recalculated for the ETag and handler | ||
*/ | ||
private $_cache; | ||
|
||
/** | ||
* The requested project | ||
*/ | ||
private $_project; | ||
|
||
/** | ||
* Permission checks | ||
* | ||
* @param \User $user The requesting user | ||
* | ||
* @return boolean true if access is permitted | ||
*/ | ||
private function _hasAccess(\User $user) | ||
{ | ||
return $user->hasPermission('dicom_archive_view_allsites'); | ||
} | ||
|
||
/** | ||
* Contructor | ||
* | ||
* @param \Project $project The requested project | ||
*/ | ||
public function __construct(\Project $project) | ||
{ | ||
$this->_project = $project; | ||
} | ||
|
||
/** | ||
* Return which methods are supported by this endpoint. | ||
* | ||
* @return array supported HTTP methods | ||
*/ | ||
protected function allowedMethods() : array | ||
{ | ||
return ['GET']; | ||
} | ||
|
||
/** | ||
* Versions of the LORIS API which are supported by this | ||
* endpoint. | ||
* | ||
* @return array a list of supported API versions. | ||
*/ | ||
protected function supportedVersions() : array | ||
{ | ||
return ["v0.0.4-dev"]; | ||
} | ||
|
||
/** | ||
* Handles a request that starts with /projects/$projectname/candidates | ||
* | ||
* @param ServerRequestInterface $request The incoming PSR7 request | ||
* | ||
* @return ResponseInterface The outgoing PSR7 response | ||
*/ | ||
public function handle(ServerRequestInterface $request) : ResponseInterface | ||
{ | ||
$user = $request->getAttribute('user'); | ||
if ($user instanceof \LORIS\AnonymousUser) { | ||
return new \LORIS\Http\Response\JSON\Unauthorized(); | ||
} | ||
|
||
if (!$this->_hasAccess($user)) { | ||
return new \LORIS\Http\Response\JSON\Forbidden(); | ||
} | ||
|
||
$pathparts = $request->getAttribute('pathparts'); | ||
if (count($pathparts) !== 0) { | ||
return new \LORIS\Http\Response\JSON\NotFound(); | ||
} | ||
|
||
switch ($request->getMethod()) { | ||
case 'GET': | ||
return $this->_handleGET($request); | ||
|
||
case 'OPTIONS': | ||
return (new \LORIS\Http\Response()) | ||
->withHeader('Allow', $this->allowedMethods()); | ||
default: | ||
return new \LORIS\Http\Response\JSON\MethodNotAllowed( | ||
$this->allowedMethods() | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Create an array representation of this endpoint's reponse body | ||
* | ||
* @param ServerRequestInterface $request The incoming PSR7 request | ||
* | ||
* @return ResponseInterface The outgoing PSR7 response | ||
*/ | ||
private function _handleGET(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
if (isset($this->_cache)) { | ||
spell00 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return $this->_cache; | ||
} | ||
|
||
try { | ||
$datestring = $request->getQueryParams()['since'] ?? '1970-01-01'; | ||
$since = new \DateTime($datestring); | ||
} catch (\Exception $e) { | ||
return new \LORIS\Http\Response\JSON\BadRequest( | ||
$e->getMessage() | ||
); | ||
} | ||
|
||
$provisioner = new \LORIS\api\Provisioners\ProjectDicomsObjectProvisioner( | ||
$this->_project, | ||
$since, | ||
'\LORIS\api\Models\ProjectDicoms' | ||
); | ||
|
||
$dicoms = iterator_to_array($provisioner->getAllInstances()); | ||
|
||
$this->_cache = new \LORIS\Http\Response\JsonResponse( | ||
['Dicoms' => $dicoms] | ||
); | ||
|
||
return $this->_cache; | ||
} | ||
|
||
/** | ||
* Implements the ETagCalculator interface | ||
* | ||
* @param ServerRequestInterface $request The PSR7 incoming request. | ||
* | ||
* @return string etag summarizing value of this request. | ||
*/ | ||
public function ETag(ServerRequestInterface $request) : string | ||
{ | ||
return md5(json_encode($this->_handleGET($request)->getBody())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* PHP Version 7 | ||
* | ||
* @category API | ||
* @package Loris | ||
* @author Simon Pelletier <simon.pelletier@mcin.ca> | ||
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 | ||
* @link https://www.github.com/aces/Loris/ | ||
*/ | ||
|
||
namespace LORIS\api\Models; | ||
|
||
/** | ||
* A ProjectDicoms contains values from a Dicom file of a project. | ||
* | ||
* @category API | ||
* @package Loris | ||
* @author Simon Pelletier <simon.pelletier@mcin.ca> | ||
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 | ||
* @link https://www.github.com/aces/Loris/ | ||
*/ | ||
class ProjectDicoms implements \LORIS\Data\DataInstance | ||
{ | ||
/** | ||
* Implements \LORIS\Data\DataInstance interface for this object. | ||
* | ||
* @return array the object data. | ||
*/ | ||
public function jsonSerialize() : array | ||
{ | ||
$obj = get_object_vars($this); | ||
$obj['FileName'] = basename($obj['Archive']); | ||
return $obj; | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
modules/api/php/provisioners/projectdicomsobjectprovisioner.class.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,85 @@ | ||||||
<?php declare(strict_types=1); | ||||||
/** | ||||||
* This file implements a data provisioner to get all dicoms of a project | ||||||
* created since a given date. | ||||||
* | ||||||
* PHP Version 7 | ||||||
* | ||||||
* @category API | ||||||
* @package Loris | ||||||
* @author Simon Pelletier <simon.pelletier@mcin.ca> | ||||||
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 | ||||||
* @link https://www.github.com/aces/Loris/ | ||||||
*/ | ||||||
|
||||||
namespace LORIS\api\Provisioners; | ||||||
|
||||||
use \LORIS\Data\Provisioners\DBObjectProvisioner; | ||||||
/** | ||||||
* This file implements a data provisioner to get all dicoms of a project | ||||||
* created since a given date. | ||||||
* | ||||||
* PHP Version 7 | ||||||
* | ||||||
* @category API | ||||||
* @package Loris | ||||||
* @author Simon Pelletier <simon.pelletier@mcin.ca> | ||||||
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 | ||||||
* @link https://www.github.com/aces/Loris/ | ||||||
*/ | ||||||
class ProjectDicomsObjectProvisioner extends DBObjectProvisioner | ||||||
{ | ||||||
/** | ||||||
* Create a ObjectProvisioner | ||||||
* | ||||||
* @param \Project $project The project from which dicoms are requested | ||||||
* @param \DateTime $since The date from which dicoms are requested | ||||||
* @param string $classname The class name of the returned objects | ||||||
*/ | ||||||
function __construct( | ||||||
\Project $project, | ||||||
\DateTime $since, | ||||||
string $classname='LORIS\api\Models\ProjectDicoms' | ||||||
) { | ||||||
parent::__construct( | ||||||
' | ||||||
SELECT | ||||||
s.CandID as Candidate, | ||||||
c.PSCID as PSCID, | ||||||
c.Entity_type as Entity_type, | ||||||
s.Visit_label as Visit, | ||||||
s.Date_visit as Visit_date, | ||||||
s.CenterID as CenterID, | ||||||
p.Name as Site, | ||||||
t.DateAcquired as date_acquired, | ||||||
t.DateFirstArchived as date_first_archived, | ||||||
t.DateLastArchived as date_last_archived, | ||||||
t.TarchiveID as tarchiveid, | ||||||
t.DicomArchiveID as DicomArchiveID, | ||||||
t.ArchiveLocation as Archive, | ||||||
t.SourceLocation as Source | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
FROM | ||||||
tarchive t | ||||||
LEFT JOIN session s | ||||||
ON (t.SessionID = s.ID) | ||||||
LEFT JOIN candidate c | ||||||
ON (s.CandID = c.CandID) | ||||||
LEFT JOIN psc p | ||||||
ON (s.CenterID = p.CenterID) | ||||||
LEFT JOIN Project project | ||||||
ON (c.RegistrationProjectID = project.ProjectID) | ||||||
WHERE | ||||||
c.Active = \'Y\' AND | ||||||
s.Active = \'Y\' AND | ||||||
project.Name = :v_projectname AND | ||||||
t.DateLastArchived > :v_time | ||||||
ORDER BY t.DateLastArchived ASC; ', | ||||||
[ | ||||||
'v_projectname' => $project->getName(), | ||||||
'v_time' => $since->getTimestamp(), | ||||||
], | ||||||
$classname | ||||||
); | ||||||
} | ||||||
} | ||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there any reason why the following fields are not returned?
Those are fields I could see as being informative to the user accessing the DICOM archives.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I notice that in the JSON response, keys have all kinds of different convention. I would tend to make them consistent (either stick to CamelCase, or _ separated values, consistent casing etc...).
Sorry, my brain gets easily distracted with inconsistencies ;).