Skip to content

Commit

Permalink
pkp#3261 Query/Note export filters added
Browse files Browse the repository at this point in the history
  • Loading branch information
defstat committed May 23, 2018
1 parent 204c36e commit 3745e10
Show file tree
Hide file tree
Showing 4 changed files with 435 additions and 0 deletions.
166 changes: 166 additions & 0 deletions plugins/importexport/native/filter/NoteNativeXmlFilter.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php

/**
* @file plugins/importexport/native/filter/NoteNativeXmlFilter.inc.php
*
* Copyright (c) 2014-2018 Simon Fraser University
* Copyright (c) 2000-2018 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class NoteNativeXmlFilter
* @ingroup plugins_importexport_native
*
* @brief Base class that converts a set of notes to a Native XML document
*/

import('lib.pkp.plugins.importexport.native.filter.NativeExportFilter');

class NoteNativeXmlFilter extends NativeExportFilter {
/**
* Constructor
* @param $filterGroup FilterGroup
*/
function __construct($filterGroup) {
$this->setDisplayName('Native XML Notes export');
parent::__construct($filterGroup);
}


//
// Implement template methods from PersistableFilter
//
/**
* @copydoc PersistableFilter::getClassName()
*/
function getClassName() {
return 'lib.pkp.plugins.importexport.native.filter.NoteNativeXmlFilter';
}


//
// Implement template methods from Filter
//
/**
* @see Filter::process()
* @param $notes array Array of Notes
* @return DOMDocument
*/
function &process(&$notes) {
// Create the XML document
$doc = new DOMDocument('1.0');
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$deployment = $this->getDeployment();

// Wrap in a <notes> element
$rootNode = $doc->createElementNS($deployment->getNamespace(), 'notes');
foreach ($notes as $note) {
$rootNode->appendChild($this->createNoteNode($doc, $note));
}
$doc->appendChild($rootNode);
$rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$rootNode->setAttribute('xsi:schemaLocation', $deployment->getNamespace() . ' ' . $deployment->getSchemaFilename());

return $doc;
}

//
// Conversion functions
//
/**
* Create and return a note node.
* @param $doc DOMDocument
* @param $note Note
* @return DOMElement
*/
function createNoteNode($doc, $note) {
$deployment = $this->getDeployment();
$context = $deployment->getContext();

// Create the note node
$noteNode = $doc->createElementNS($deployment->getNamespace(), 'note');

$noteNode->setAttribute('assoc_type', $note->getAssocType());
$noteNode->setAttribute('title', $note->getTitle());
$noteNode->setAttribute('contents', $note->getContents());

$authorUser = $note->getUser();
assert(isset($authorUser));
$noteNode->setAttribute('author', $authorUser->getUsername());

if ($dateCreated = $note->getDateCreated()) {
$noteNode->setAttribute('date_created', strftime('%Y-%m-%d', strtotime($dateCreated)));
}

if ($dateModified = $note->getDateModified()) {
$noteNode->setAttribute('date_modified', strftime('%Y-%m-%d', strtotime($dateModified)));
}

$this->addFiles($doc, $noteNode, $note);

return $noteNode;
}

/**
* Add the Files for a note to its DOM element.
* @param $doc DOMDocument
* @param $noteNode DOMElement
* @param $note Note
*/
function addFiles($doc, $noteNode, $note) {
$fileDao = DAORegistry::getDAO('SubmissionFileDAO');

$noteFiles = $fileDao->getAllRevisionsByAssocId(ASSOC_TYPE_NOTE, $note->getId());

$noteFilesNode = $this->processFiles($noteFiles, $note);
if ($noteFilesNode->documentElement instanceof DOMElement) {
$clone = $doc->importNode($noteFilesNode->documentElement, true);
$noteNode->appendChild($clone);
}
}

/**
* Create nodeFiles Node
* @param $noteFiles array of SubmissionFiles
* @param $note Note
* @return DOMDocument
*/
function processFiles($noteFiles, $note) {
$doc = new DOMDocument('1.0');
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$deployment = $this->getDeployment();

$rootNode = $doc->createElementNS($deployment->getNamespace(), 'noteFiles');
foreach ($noteFiles as $noteFile) {
$rootNode->appendChild($this->createFileNode($doc, $noteFile));
}
$doc->appendChild($rootNode);
$rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$rootNode->setAttribute('xsi:schemaLocation', $deployment->getNamespace() . ' ' . $deployment->getSchemaFilename());

return $doc;
}

/**
* Create note file node.
* @param $doc DOMDocument
* @param $file SubmissionFile
* @return DOMElement
*/
function createFileNode($doc, $file) {
$deployment = $this->getDeployment();
$context = $deployment->getContext();

// Create the file node
$fileNode = $doc->createElementNS($deployment->getNamespace(), 'noteFile');

if ($file) {
$fileNode->setAttribute('oldFileId', $file->getFileId());
}

return $fileNode;
}
}

?>
180 changes: 180 additions & 0 deletions plugins/importexport/native/filter/QueryNativeXmlFilter.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php

/**
* @file plugins/importexport/native/filter/QueryNativeXmlFilter.inc.php
*
* Copyright (c) 2014-2018 Simon Fraser University
* Copyright (c) 2000-2018 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class QueryNativeXmlFilter
* @ingroup plugins_importexport_native
*
* @brief Base class that converts a set of Queries to a Native XML document
*/

import('lib.pkp.plugins.importexport.native.filter.NativeExportFilter');

class QueryNativeXmlFilter extends NativeExportFilter {
/**
* Constructor
* @param $filterGroup FilterGroup
*/
function __construct($filterGroup) {
$this->setDisplayName('Native XML Queries export');
parent::__construct($filterGroup);
}


//
// Implement template methods from PersistableFilter
//
/**
* @copydoc PersistableFilter::getClassName()
*/
function getClassName() {
return 'lib.pkp.plugins.importexport.native.filter.QueryNativeXmlFilter';
}


//
// Implement template methods from Filter
//
/**
* @see Filter::process()
* @param $reviewRounds array Array of Queries
* @return DOMDocument
*/
function &process(&$queries) {
// Create the XML document
$doc = new DOMDocument('1.0');
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$deployment = $this->getDeployment();

// Wrap in a <queries> element
$rootNode = $doc->createElementNS($deployment->getNamespace(), 'queries');
foreach ($queries as $query) {
$rootNode->appendChild($this->createQueryNode($doc, $query));
}
$doc->appendChild($rootNode);
$rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$rootNode->setAttribute('xsi:schemaLocation', $deployment->getNamespace() . ' ' . $deployment->getSchemaFilename());

return $doc;
}

//
// Conversion functions
//
/**
* Create and return a Query node.
* @param $doc DOMDocument
* @param $query Query
* @return DOMElement
*/
function createQueryNode($doc, $query) {
$deployment = $this->getDeployment();
$context = $deployment->getContext();

// Create the query node
$queryNode = $doc->createElementNS($deployment->getNamespace(), 'query');

$queryNode->setAttribute('assoc_type', $query->getAssocType());
$queryNode->setAttribute('stage_id', $query->getStageId());
$queryNode->setAttribute('seq', $query->getSequence());
$queryNode->setAttribute('closed', $query->getIsClosed());

$this->addNotes($doc, $queryNode, $query);
$this->addQueryParticipants($doc, $queryNode, $query);

return $queryNode;
}

/**
* Add the Notes for a query to its DOM element.
* @param $doc DOMDocument
* @param $queryNode DOMElement
* @param $query Query
*/
function addNotes($doc, $queryNode, $query) {
$filterDao = DAORegistry::getDAO('FilterDAO');
$nativeExportFilters = $filterDao->getObjectsByGroup('note=>native-xml');
assert(count($nativeExportFilters)==1); // Assert only a single serialization filter
$exportFilter = array_shift($nativeExportFilters);
$exportFilter->setDeployment($this->getDeployment());

$notesDao = DAORegistry::getDAO('NoteDAO');
$notes = $notesDao->getByAssoc(ASSOC_TYPE_QUERY, $query->getId())->toArray();

$notesDoc = $exportFilter->execute($notes);
if ($notesDoc->documentElement instanceof DOMElement) {
$clone = $doc->importNode($notesDoc->documentElement, true);
$queryNode->appendChild($clone);
}
}

/**
* Add the QueryParticipants for a Query to its DOM element.
* @param $doc DOMDocument
* @param $reviewRoundNode DOMElement
* @param $query Query
*/
function addQueryParticipants($doc, $queryNode, $query) {
$queryDao = DAORegistry::getDAO('QueryDAO');

$participantIds = $queryDao->getParticipantIds($query->getId());

$participantsNode = $this->processPraticipantIds($participantIds, $query);
if ($participantsNode->documentElement instanceof DOMElement) {
$clone = $doc->importNode($participantsNode->documentElement, true);
$queryNode->appendChild($clone);
}
}

/**
* Create participants node
* @param $participantIds array of int
* @param $query Query
* @return DOMDocument
*/
function processPraticipantIds($participantIds, $query) {
$doc = new DOMDocument('1.0');
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$deployment = $this->getDeployment();

$rootNode = $doc->createElementNS($deployment->getNamespace(), 'participants');
foreach ($participantIds as $participantId) {
$rootNode->appendChild($this->createParticipantNode($doc, $participantId, $query));
}
$doc->appendChild($rootNode);
$rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$rootNode->setAttribute('xsi:schemaLocation', $deployment->getNamespace() . ' ' . $deployment->getSchemaFilename());

return $doc;
}

/**
* Create and return a review round file node.
* @param $doc DOMDocument
* @param $participantId int
* @param $query Query
* @return DOMElement
*/
function createParticipantNode($doc, $participantId, $query) {
$deployment = $this->getDeployment();
$context = $deployment->getContext();

$participantNode = $doc->createElementNS($deployment->getNamespace(), 'participant');

$userDao = DAORegistry::getDAO('UserDAO');
$participantUser = $userDao->getById($participantId);
assert(isset($participantUser));
$participantNode->setAttribute('username', $participantUser->getUsername());

return $participantNode;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ function createSubmissionNode($doc, $submission) {
$this->addFiles($doc, $submissionNode, $submission);
$this->addRepresentations($doc, $submissionNode, $submission);
$this->addReviewRounds($doc, $submissionNode, $submission);
$this->addQueries($doc, $submissionNode, $submission);

return $submissionNode;
}
Expand Down Expand Up @@ -337,6 +338,29 @@ function addReviewRounds($doc, $submissionNode, $submission) {
}
}

/**
* Add the Queries for a submission to its DOM element.
* @param $doc DOMDocument
* @param $submissionNode DOMElement
* @param $submission Submission
*/
function addQueries($doc, $submissionNode, $submission) {
$filterDao = DAORegistry::getDAO('FilterDAO');
$nativeExportFilters = $filterDao->getObjectsByGroup('query=>native-xml');
assert(count($nativeExportFilters)==1); // Assert only a single serialization filter
$exportFilter = array_shift($nativeExportFilters);
$exportFilter->setDeployment($this->getDeployment());

$queryDao = DAORegistry::getDAO('QueryDAO');
$queries = $queryDao->getByAssoc(ASSOC_TYPE_SUBMISSION, $submission->getId())->toArray();

$queriesDoc = $exportFilter->execute($queries);
if ($queriesDoc->documentElement instanceof DOMElement) {
$clone = $doc->importNode($queriesDoc->documentElement, true);
$submissionNode->appendChild($clone);
}
}

//
// Abstract methods for subclasses to implement
//
Expand Down
Loading

0 comments on commit 3745e10

Please sign in to comment.