From 3745e10b6a816f501df6c85c25885b880564327b Mon Sep 17 00:00:00 2001 From: Dimitris Efstathiou Date: Wed, 23 May 2018 23:59:14 +0300 Subject: [PATCH] pkp/pkp-lib#3261 Query/Note export filters added --- .../native/filter/NoteNativeXmlFilter.inc.php | 166 ++++++++++++++++ .../filter/QueryNativeXmlFilter.inc.php | 180 ++++++++++++++++++ .../filter/SubmissionNativeXmlFilter.inc.php | 24 +++ plugins/importexport/native/pkp-native.xsd | 65 +++++++ 4 files changed, 435 insertions(+) create mode 100644 plugins/importexport/native/filter/NoteNativeXmlFilter.inc.php create mode 100644 plugins/importexport/native/filter/QueryNativeXmlFilter.inc.php diff --git a/plugins/importexport/native/filter/NoteNativeXmlFilter.inc.php b/plugins/importexport/native/filter/NoteNativeXmlFilter.inc.php new file mode 100644 index 00000000000..c0ddfdd1d81 --- /dev/null +++ b/plugins/importexport/native/filter/NoteNativeXmlFilter.inc.php @@ -0,0 +1,166 @@ +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 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; + } +} + +?> diff --git a/plugins/importexport/native/filter/QueryNativeXmlFilter.inc.php b/plugins/importexport/native/filter/QueryNativeXmlFilter.inc.php new file mode 100644 index 00000000000..0a84c1fb2e3 --- /dev/null +++ b/plugins/importexport/native/filter/QueryNativeXmlFilter.inc.php @@ -0,0 +1,180 @@ +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 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; + } +} + +?> diff --git a/plugins/importexport/native/filter/SubmissionNativeXmlFilter.inc.php b/plugins/importexport/native/filter/SubmissionNativeXmlFilter.inc.php index 29664aed035..308a3e3e7cc 100644 --- a/plugins/importexport/native/filter/SubmissionNativeXmlFilter.inc.php +++ b/plugins/importexport/native/filter/SubmissionNativeXmlFilter.inc.php @@ -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; } @@ -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 // diff --git a/plugins/importexport/native/pkp-native.xsd b/plugins/importexport/native/pkp-native.xsd index ffd972a1fad..25a6f625ad5 100644 --- a/plugins/importexport/native/pkp-native.xsd +++ b/plugins/importexport/native/pkp-native.xsd @@ -232,6 +232,7 @@ + @@ -284,6 +285,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +