tethys/app/Http/Controllers/Publish/DoiController.php

306 lines
10 KiB
PHP
Raw Normal View History

2021-02-26 16:02:07 +00:00
<?php
namespace App\Http\Controllers\Publish;
2021-02-26 16:02:07 +00:00
use App\Http\Controllers\Controller;
use App\Interfaces\DoiInterface;
2021-02-26 16:02:07 +00:00
use App\Models\Dataset;
use App\Models\DatasetIdentifier;
use Illuminate\Http\Request;
use App\Models\Oai\OaiModelError;
use App\Exceptions\OaiModelException;
use Illuminate\Support\Facades\View;
use App\Exceptions\GeneralException;
use App\Library\Search\SolariumAdapter;
use \Exception;
2021-02-26 16:02:07 +00:00
class DoiController extends Controller
{
protected $doiService;
protected $LaudatioUtils;
/**
2021-02-26 16:02:07 +00:00
* Holds xml representation of document information to be processed.
*
* @var \DomDocument Defaults to null.
*/
protected $xml = null;
/**
2021-02-26 16:02:07 +00:00
* Holds the stylesheet for the transformation.
*
* @var \DomDocument Defaults to null.
*/
protected $xslt = null;
/**
* Holds the xslt processor.
*
* @var \XSLTProcessor Defaults to null.
*/
protected $proc = null;
/**
* DOIController constructor.
* @param DoiInterface $DOIService
*/
public function __construct(DoiInterface $DoiClient)
{
$this->doiClient = $DoiClient;
2021-02-26 16:02:07 +00:00
$this->xml = new \DomDocument();
$this->proc = new \XSLTProcessor();
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\View\View
2021-02-26 16:02:07 +00:00
*/
// public function index()
// {
// //
// }
public function index(): \Illuminate\Contracts\View\View
2021-02-26 16:02:07 +00:00
{
$datasets = Dataset::query()
2021-05-25 12:15:02 +00:00
->has('identifier')
->orderBy('server_date_modified', 'desc')
->get();
return View::make('workflow.doi.index', [
'datasets' => $datasets,
]);
2021-02-26 16:02:07 +00:00
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create(Request $request)
{
//
2021-02-26 16:02:07 +00:00
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$dataId = $request->input('publish_id');
2021-02-26 16:02:07 +00:00
// Setup stylesheet
$this->loadStyleSheet(public_path() . '/prefixes/doi_datacite.xslt');
2021-02-26 16:02:07 +00:00
// set timestamp
2021-02-26 16:02:07 +00:00
$date = new \DateTime();
$unixTimestamp = $date->getTimestamp();
$this->proc->setParameter('', 'unixTimestamp', $unixTimestamp);
$prefix = config('tethys.datacite_prefix');
$this->proc->setParameter('', 'prefix', $prefix);
$repIdentifier = "tethys";
$this->proc->setParameter('', 'repIdentifier', $repIdentifier);
$this->xml->appendChild($this->xml->createElement('Datasets'));
$dataset = Dataset::where('publish_id', '=', $dataId)->firstOrFail();
if (is_null($dataset)) {
throw new OaiModelException('Dataset is not available for registering DOI!', OaiModelError::NORECORDSMATCH);
}
$dataset->fetchValues();
$xmlModel = new \App\Library\Xml\XmlModel();
$xmlModel->setModel($dataset);
$xmlModel->excludeEmptyFields();
$cache = ($dataset->xmlCache) ? $dataset->xmlCache : new \App\Models\XmlCache();
$xmlModel->setXmlCache($cache);
$domNode = $xmlModel->getDomDocument()->getElementsByTagName('Rdr_Dataset')->item(0);
$node = $this->xml->importNode($domNode, true);
$this->addSpecInformation($node, 'data-type:' . $dataset->type);
$this->xml->documentElement->appendChild($node);
$xmlMeta = $this->proc->transformToXML($this->xml);
// Log::alert($xmlMeta);
//create doiValue and correspunfing landingpage of tehtys
2021-02-26 16:02:07 +00:00
$doiValue = $prefix . '/tethys.' . $dataset->publish_id;
$appUrl = config('app.url');
$landingPageUrl = $appUrl . "/dataset/" . $dataset->publish_id;
$response = $this->doiClient->registerDoi($doiValue, $xmlMeta, $landingPageUrl);
// if operation successful, store dataste identifier
2021-02-26 16:02:07 +00:00
if ($response->getStatusCode() == 201) {
$doi = new DatasetIdentifier();
$doi['value'] = $doiValue;
$doi['dataset_id'] = $dataset->id;
$doi['type'] = "doi";
$doi['status'] = "registered";
$doi->save();
2021-02-26 16:02:07 +00:00
}
}
/**
* Display the specified resource.
*
* @param \App\Models\DatasetIdentifier $doi
* @return \Illuminate\Http\Response
*/
public function show(DatasetIdentifier $doi)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\DatasetIdentifier $doi
* @return \Illuminate\Http\Response
*/
public function edit($id)
2021-02-26 16:02:07 +00:00
{
$dataset = Dataset::query()
->with([
'titles',
'persons' => function ($query) {
$query->wherePivot('role', 'author');
},
])->findOrFail($id);
2021-05-25 12:15:02 +00:00
return View::make('workflow.doi.edit', [
'dataset' => $dataset,
2021-05-25 12:15:02 +00:00
]);
2021-02-26 16:02:07 +00:00
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\DatasetIdentifier $doi
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $publish_id)
2021-02-26 16:02:07 +00:00
{
$dataId = $publish_id; //$request->input('publish_id');
// Setup stylesheet
$this->loadStyleSheet(public_path() . '/prefixes/doi_datacite.xslt');
// set timestamp
$date = new \DateTime();
$unixTimestamp = $date->getTimestamp();
$this->proc->setParameter('', 'unixTimestamp', $unixTimestamp);
$prefix = "";
$base_domain = "";
$datacite_environment = config('tethys.datacite_environment');
if ($datacite_environment == "debug") {
$prefix = config('tethys.datacite_test_prefix');
2021-05-25 12:15:02 +00:00
$base_domain = config('tethys.test_base_domain');
} elseif ($datacite_environment == "production") {
$prefix = config('tethys.datacite_prefix');
$base_domain = config('tethys.base_domain');
}
// $prefix = config('tethys.datacite_prefix');
$this->proc->setParameter('', 'prefix', $prefix);
$repIdentifier = "tethys";
$this->proc->setParameter('', 'repIdentifier', $repIdentifier);
$this->xml->appendChild($this->xml->createElement('Datasets'));
$dataset = Dataset::where('publish_id', '=', $dataId)->firstOrFail();
if (is_null($dataset)) {
throw new OaiModelException('Dataset is not available for registering DOI!', OaiModelError::NORECORDSMATCH);
}
$dataset->fetchValues();
$xmlModel = new \App\Library\Xml\XmlModel();
$xmlModel->setModel($dataset);
$xmlModel->excludeEmptyFields();
$cache = ($dataset->xmlCache) ? $dataset->xmlCache : new \App\Models\XmlCache();
$xmlModel->setXmlCache($cache);
$domNode = $xmlModel->getDomDocument()->getElementsByTagName('Rdr_Dataset')->item(0);
$node = $this->xml->importNode($domNode, true);
$this->addSpecInformation($node, 'data-type:' . $dataset->type);
$this->xml->documentElement->appendChild($node);
$newXmlMeta = $this->proc->transformToXML($this->xml);
// Log::alert($xmlMeta);
//create doiValue and correspunfing landingpage of tehtys
$doiValue = $prefix . '/tethys.' . $dataset->publish_id;
$response = $this->doiClient->updateMetadataForDoi($doiValue, $newXmlMeta);
// if operation successful, store dataste identifier
if ($response->getStatusCode() == 201) {
2021-05-25 12:15:02 +00:00
$doi = $dataset->identifier;
// $doi['value'] = $doiValue;
// $doi['type'] = "doi";
// $doi['status'] = "findable";
// $doi->save();
$doi->touch();
try {
$service = new SolariumAdapter("solr", config('solarium'));
$service->addDatasetsToIndex($dataset);
} catch (Exception $e) {
$this->error(__METHOD__ . ': ' . 'Indexing document ' . $dataset->id . ' failed: ' . $e->getMessage());
}
return redirect()
->route('publish.workflow.doi.index')
->with('flash_message', 'You have successfully updated a DOI for the dataset!');
// if ($doi->save()) {
// // update server_date_modified for triggering nex xml cache (doi interface)
// $time = new \Illuminate\Support\Carbon();
// $dataset->server_date_modified = $time;
// $dataset->save();
// return redirect()
// ->route('publish.workflow.editor.index')
// ->with('flash_message', 'You have successfully created a DOI for the dataset!');
// }
} else {
$message = 'unexpected DataCite MDS response code ' . $response->getStatusCode();
// $this->log($message, 'err');
throw new GeneralException($message);
}
2021-02-26 16:02:07 +00:00
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\DatasetIdentifier $doi
* @return \Illuminate\Http\Response
*/
public function destroy(DatasetIdentifier $doi)
{
//
}
2021-02-26 16:02:07 +00:00
/**
* Load an xslt stylesheet.
*
* @return void
*/
private function loadStyleSheet($stylesheet)
{
$this->xslt = new \DomDocument;
$this->xslt->load($stylesheet);
$this->proc->importStyleSheet($this->xslt);
if (isset($_SERVER['HTTP_HOST'])) {
$this->proc->setParameter('', 'host', $_SERVER['HTTP_HOST']);
}
//$this->proc->setParameter('', 'server', $this->getRequest()->getBaseUrl());
}
private function addSpecInformation(\DOMNode $document, $information)
{
$setSpecAttribute = $this->xml->createAttribute('Value');
$setSpecAttributeValue = $this->xml->createTextNode($information);
$setSpecAttribute->appendChild($setSpecAttributeValue);
$setSpecElement = $this->xml->createElement('SetSpec');
//$setSpecElement =new \DOMElement("SetSpec");
$setSpecElement->appendChild($setSpecAttribute);
$document->appendChild($setSpecElement);
}
}