Commit 6ae9dba9 authored by Alexandre Morin's avatar Alexandre Morin
Browse files

Merge branch 'develop' into 'master'

Develop 2.1

See merge request maarch/thirdPartyArchiving!10
parents e34fb8bc abb791ee
......@@ -121,7 +121,7 @@ class ArchiveDeliveryRequest extends abstractMessage
foreach ($archiveIds as $archiveId) {
$archive = $this->archiveController->retrieve($archiveId);
if (isset($archivesByOriginator[$archive->originatorOrgRegNumber])) {
if (!isset($archivesByOriginator[$archive->originatorOrgRegNumber])) {
$archivesByOriginator[$archive->originatorOrgRegNumber] = array();
}
......@@ -254,11 +254,11 @@ class ArchiveDeliveryRequest extends abstractMessage
$operationResult
);
if ($message->status == "accepted") {
/*if ($message->status == "accepted") {
$replyMessage = $this->process($message);
$message->replyMessage = $replyMessage;
}
}*/
return $message;
}
......@@ -298,9 +298,9 @@ class ArchiveDeliveryRequest extends abstractMessage
true
);
$replyMessage = $this->process((string) $messageId);
/*$replyMessage = $this->process((string) $messageId);
$message->replyMessage = $replyMessage;
$message->replyMessage = $replyMessage;*/
}
/**
......
......@@ -161,7 +161,7 @@ class ArchiveDestructionRequest extends abstractMessage
$senderOrg = \laabs::getToken('ORGANIZATION');
// Requested by archiver: send auth request to originator
if ($senderOrg->registrationNumber == $message->recipientOrg) {
if ($senderOrg->registrationNumber == $message->recipientOrgRegNumber) {
$authorizationOriginatingAgencyRequestController = \laabs::newController('medona/AuthorizationOriginatingAgencyRequest');
$authorizationOriginatingAgencyRequestController->send($message, $originatorOrgRegNumber);
......
......@@ -27,6 +27,7 @@ namespace ext\thirdPartyArchiving\bundle\medona\Controller;
class ArchiveModification
{
protected $sdoFactory;
protected $archiveController;
/**
* Constructor
......
......@@ -30,10 +30,17 @@ class ArchiveNotification extends abstractMessage
{
/**
* Get received notification messages
* @param string $reference
* @param string $archiver
* @param string $originator
* @param string $depositor
* @param string $archivalAgreement
* @param date $fromDate
* @param date $toDate
*
* @return array Array of medona/message object
*/
public function listReception()
public function listReception($reference = null, $archiver = null, $originator = null, $depositor = null, $archivalAgreement = null, $fromDate = null, $toDate = null)
{
$registrationNumber = $this->getCurrentRegistrationNumber();
......@@ -45,6 +52,23 @@ class ArchiveNotification extends abstractMessage
return $this->sdoFactory->find('medona/message', '('.implode(') and (', $queryParts).')', null, false, false, 300);
}
/**
* Get transfer history
* @param string $reference
* @param string $archiver
* @param string $originator
* @param string $depositor
* @param string $archivalAgreement
* @param date $fromDate
* @param date $toDate
*
* @return array Array of medona/message object
*/
public function history($reference = null, $archiver = null, $originator = null, $depositor = null, $archivalAgreement = null, $fromDate = null, $toDate = null)
{
return $this->search("ArchiveNotification", $reference, $archiver, $originator, $depositor, $archivalAgreement, $fromDate, $toDate, false);
}
/**
* Count notification message
*
......
......@@ -387,7 +387,7 @@ class ArchiveRestitution extends abstractMessage
$requestMessage->unitIdentifier = $this->sdoFactory->readChildren('medona/unitIdentifier', $requestMessage);
foreach ($requestMessage->unitIdentifier as $unitIdentifier) {
$this->archiveController->setStatus($unitIdentifier->objectId, 'disposed');
$this->archiveController->setStatus($unitIdentifier->objectId, 'restituted');
}
$this->changeStatus($messageId, "validated");
......
......@@ -435,8 +435,9 @@ class ArchiveRestitutionRequest extends abstractMessage
$removedArchiveIds = [];
foreach ($restitutionRequest->unitIdentifier as $unitIdentifier) {
$archive = $this->archiveController->destruct($unitIdentifier->objectId);
$removedArchiveIds[] = (string) $archive["success"][0]->archiveId;
if (count($archive["success"]) > 0) {
$removedArchiveIds[] = (string)$archive["success"][0]->archiveId;
}
}
return $removedArchiveIds;
......
......@@ -48,7 +48,8 @@ class ArchiveTransfer extends abstractMessage
$queryParts[] = "recipientOrgRegNumber=$registrationNumber";
$queryParts[] = "type='ArchiveTransfer'";
$queryParts[] = "active=true";
$queryParts[] = "status != 'processed' AND status != 'error' AND status != 'invalid' AND status !='draft' AND status !='template'" ;
$queryParts[] = "isIncoming=true";
$queryParts[] = "status != 'processed' AND status != 'error' AND status != 'invalid' AND status !='draft' AND status !='template' AND status !='rejected' AND status !='acknowledge'" ;
return $this->sdoFactory->find('medona/message', implode(' and ', $queryParts), null, false, false, 300);
}
......@@ -61,11 +62,13 @@ class ArchiveTransfer extends abstractMessage
public function listSending()
{
$registrationNumber = $this->getCurrentRegistrationNumber();
$accountToken = \laabs::getToken("AUTH");
$queryParts = [];
$queryParts[] = "senderOrgRegNumber=$registrationNumber";
$queryParts[] = "(accountId= '$accountToken->accountId' OR senderOrgRegNumber=$registrationNumber)";
$queryParts[] = "type='ArchiveTransfer'";
$queryParts[] = "active=true";
$queryParts[] = "isIncoming=true";
$queryParts[] = "status=['sent', 'received','accepted']";
return $this->sdoFactory->find("medona/message", implode(' and ', $queryParts), null, false, false, 300);
......@@ -85,7 +88,7 @@ class ArchiveTransfer extends abstractMessage
*/
public function history($reference = null, $archiver = null, $originator = null, $depositor = null, $archivalAgreement = null, $fromDate = null, $toDate = null)
{
return $this->search("ArchiveTransfer", $reference, $archiver, $originator, $depositor, $archivalAgreement, $fromDate, $toDate);
return $this->search("ArchiveTransfer", $reference, $archiver, $originator, $depositor, $archivalAgreement, $fromDate, $toDate, true);
}
/**
......@@ -252,6 +255,7 @@ class ArchiveTransfer extends abstractMessage
throw \laabs::newException('medona/invalidMessageException', "Invalid message", 400);
}
$message->isIncoming = true;
$message->status = "received";
$this->create($message);
} catch (\Exception $e) {
......@@ -501,6 +505,71 @@ class ArchiveTransfer extends abstractMessage
return true;
}
/**
* Validate message against schema and rules for a draft message with no status change nor reply
* @param mixed $messageId The message identifier
*
* @return boolean
*/
public function validateDraft($messageId)
{
$this->errors = array();
$this->replyCode = null;
$message = $this->sdoFactory->read('medona/message', $messageId);
$this->loadData($message);
$this->loadXmlFile($message);
try {
// Check sender (depositor) roles
$this->validateDepositor($message);
// Check recipient (archiver) roles
$this->validateArchiver($message);
if (isset($message->archivalAgreementReference)) {
$this->validateArchivalAgreement($message);
}
// Get the validator for schema (medona/seda/other)
if ($message->schema != 'medona') {
$archiveTransferController = \laabs::newController($message->schema.'/ArchiveTransfer');
$archiveTransferController->validate($message, $this->currentArchivalAgreement);
$this->errors = array_merge($this->errors, $archiveTransferController->errors);
$this->infos = array_merge($this->infos, $archiveTransferController->infos);
} else {
// Validation du profil métier
$this->validateProfile($message, $this->currentArchivalAgreement);
// Contrôle des documents attachés
$this->validateAttachments($message, $this->currentArchivalAgreement);
// Validation of archive relationship.
$this->validateArchiveRelationships($message->object->dataObjectPackage->descriptiveMetadata);
}
} catch (\Exception $exception) {
$this->errors[] = new \core\Error($exception->getMessage());
$exception = \laabs::newException('medona/invalidMessageException', "Invalid message", 400);
$exception->errors = $this->errors;
throw $exception;
}
// Non blocking errors
if (count($this->errors) > 0) {
$exception = \laabs::newException('medona/invalidMessageException', "Invalid message", 400);
$exception->errors = $this->errors;
throw $exception;
}
return true;
}
protected function sendValidationError($message)
{
$message->status = "invalid";
......@@ -632,10 +701,10 @@ class ArchiveTransfer extends abstractMessage
$this->sendError("300", "L'accord de versement n'est pas actif.");
}
$dateOnDay = $today->sub(new \core\Type\Duration("P1D"));
$dateOnWeek = $today->sub(new \core\Type\Duration("P7D"));
$dateOnMonth = $today->sub(new \core\Type\Duration("P1M"));
$dateOnYear = $today->sub(new \core\Type\Duration("P1Y"));
$dateOnDay = $today->shift(new \core\Type\Duration("P1D"));
$dateOnWeek = $today->shift(new \core\Type\Duration("P7D"));
$dateOnMonth = $today->shift(new \core\Type\Duration("P1M"));
$dateOnYear = $today->shift(new \core\Type\Duration("P1Y"));
$dateArchivalAgreement = $this->currentArchivalAgreement->beginDate;
if ($message->size > ($this->currentArchivalAgreement->maxSizeTransfer*1048576) && $this->currentArchivalAgreement->maxSizeTransfer > 0) {
......@@ -1059,20 +1128,19 @@ class ArchiveTransfer extends abstractMessage
$operationResult = true;
} catch (\Exception $e) {
$message->status = "error";
$operationResult = false;
$this->sdoFactory->update($message);
$this->lifeCycleJournalController->logEvent(
'medona/processing',
'medona/message',
$message->messageId,
$message,
false
);
throw $e;
}
$this->lifeCycleJournalController->logEvent(
'medona/processing',
'medona/message',
$message->messageId,
$message,
$operationResult
);
$transactionControl = !$this->sdoFactory->inTransaction();
if ($transactionControl) {
......@@ -1140,9 +1208,17 @@ class ArchiveTransfer extends abstractMessage
}
$message->status = "error";
$operationResult = false;
$this->sdoFactory->update($message);
$this->lifeCycleJournalController->logEvent(
'medona/processing',
'medona/message',
$message->messageId,
$message,
false
);
throw $e;
}
......@@ -1150,6 +1226,14 @@ class ArchiveTransfer extends abstractMessage
$this->sdoFactory->commit();
}
$this->lifeCycleJournalController->logEvent(
'medona/processing',
'medona/message',
$message->messageId,
$message,
true
);
$archiveTransferReplyController = \laabs::newController('medona/ArchiveTransferReply');
$replyMessage = $archiveTransferReplyController->send($message, "000");
......
......@@ -34,11 +34,11 @@ class ArchiveTransferReply extends abstractMessage
* Send a new transfer reply
* @param string $transferMessage The request message identifier
* @param string $replyCode The reply code
* @param array $archives The archives to send
* @param string $comment The comment
*
* @return The reply message generated
*/
public function send($transferMessage, $replyCode = "OK", $archives = array())
public function send($transferMessage, $replyCode = "OK", $comment = null)
{
if (is_scalar($transferMessage)) {
$messageId = $transferMessage;
......@@ -61,6 +61,10 @@ class ArchiveTransferReply extends abstractMessage
$message->comment[] = $lifeCycleEvent->description;
}
if ($comment) {
$message->comment[] = $comment;
}
$message->requestReference = $transferMessage->reference;
$message->operationDate = \laabs::newDatetime(null, "UTC");
......@@ -78,7 +82,7 @@ class ArchiveTransferReply extends abstractMessage
$message->senderOrg = $recipientOrg;
}
if ($archives) {
/*if ($archives) {
foreach ($archives as $archive) {
$unitIdentifier = \laabs::newInstance("medona/unitIdentifier");
$unitIdentifier->messageId = $message->messageId;
......@@ -89,7 +93,7 @@ class ArchiveTransferReply extends abstractMessage
}
}
$message->archive = $archives;
$message->archive = $archives;*/
/*$message->lifeCycleEventId = \laabs::newTokenList();
foreach ($archives as $archive) {
......
<?php
/*
* Copyright (C) 2015 Maarch
*
* This file is part of bundle medona
*
* Bundle medona is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bundle medona is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with bundle medona. If not, see <http://www.gnu.org/licenses/>.
*/
namespace ext\thirdPartyArchiving\bundle\medona\Controller;
use core\Encoding\json;
/**
* Trait for archive restitution
*
* @author Alexandre Morin <alexandre.morin@maarch.org>
**/
class ArchiveTransferSending extends abstractMessage
{
/**
* Get received archive tranfer message
*
* @return array Array of medona/message object
*/
public function listReception()
{
$registrationNumber = $this->getCurrentRegistrationNumber();
$queryParts = [];
$queryParts[] = "recipientOrgRegNumber=$registrationNumber";
$queryParts[] = "type='ArchiveTransfer'";
$queryParts[] = "active=true";
$queryParts[] = "isIncoming=false";
$queryParts[] = "status != 'processed' AND status != 'error' AND status != 'invalid' AND status !='draft' AND status !='template' AND status !='rejected' AND status !='acknowledge'";
return $this->sdoFactory->find('medona/message', implode(' and ', $queryParts), null, false, false, 300);
}
/**
* Get process restitution message
*
* @return array Array of medona/message object
*/
public function processList()
{
$registrationNumber = $this->getCurrentRegistrationNumber();
$queryParts = [];
$queryParts[] = "type='ArchiveTransfer'";
$queryParts[] = "senderOrgRegNumber=$registrationNumber";
$queryParts[] = "status='acknowledge'";
$queryParts[] = "active=true";
$queryParts[] = "isIncoming=false";
return $this->sdoFactory->find('medona/message', implode(' and ', $queryParts), null, false, false, 300);
}
/**
* Get transfer history
* @param string $reference
* @param string $archiver
* @param string $originator
* @param string $depositor
* @param string $archivalAgreement
* @param date $fromDate
* @param date $toDate
*
* @return array Array of medona/message object
*/
public function history($reference = null, $archiver = null, $originator = null, $depositor = null, $archivalAgreement = null, $fromDate = null, $toDate = null)
{
return $this->search("ArchiveTransfer", $reference, $archiver, $originator, $depositor, $archivalAgreement, $fromDate, $toDate, false);
}
/**
* Flag for transfer
* @param array $archiveIds Array of archive identifier
* @param string $archiverOrgRegNumber The archival agency
* @param string $comment The message comment
* @param string $identifier The medona message reference
*
* @return array The result of the operation
*/
public function setForTransfer($archiveIds, $archiverOrgRegNumber, $comment, $identifier = null)
{
$senderOrg = \laabs::getToken('ORGANIZATION');
if (!$senderOrg) {
throw \laabs::newException('medona/invalidMessageException', "No current organization choosen");
}
if (!is_array($archiveIds)) {
$archiveIds = array($archiveIds);
}
$senderOrgRegNumber = $senderOrg->registrationNumber;
$user = \laabs::getToken('AUTH');
$userName = false;
if($user) {
$user = \laabs::callService('auth/userAccount/read_userAccountId_', $user->accountId);
$userName = $user->accountName;
}
$archives = [];
$result = array('success' => array(), 'error' => array());
$counter = 0;
foreach ($archiveIds as $archiveId) {
$archive = $this->archiveController->retrieve($archiveId);
$transferable = $this->isTransferable($archive);
if (!$transferable) {
$result["error"][] = $archive->archiveId;
} else {
$result["success"][] = $archive->archiveId;
$archives[] = $archive;
}
}
if (!empty($archives)) {
$messageIdentifier = $identifier ? $identifier : "archiveTransfer_".\laabs::newId();
$recipientOrgReqNumber = $archiverOrgRegNumber;
$this->send($messageIdentifier, $archives, $senderOrgRegNumber, $recipientOrgReqNumber, $comment);
}
return $result;
}
public function send($reference, $archives, $senderOrg, $recipientOrg, $comment = null)
{
$message = \laabs::newInstance('medona/message');
$message->messageId = \laabs::newId();
$message->schema = "medona";
if (\laabs::hasBundle('seda') && $archives[0]->descriptionClass == 'archivesPubliques/content') {
$message->schema = "seda";
}
$message->type = "ArchiveTransfer";
$message->date = \laabs::newDatetime(null, "UTC");
$message->receptionDate = $message->date;
$message->reference = $reference;
$message->status = "sent";
$message->comment[] = $comment;
$message->senderOrgRegNumber = $senderOrg;
$message->recipientOrgRegNumber = $recipientOrg;
$this->readOrgs($message); // read org names, addresses, communications, contacts
try {
$message->unitIdentifier = array();
foreach ($archives as $archive) {
$unitIdentifier = \laabs::newInstance("medona/unitIdentifier");
$unitIdentifier->messageId = $message->messageId;
$unitIdentifier->objectClass = "recordsManagement/archive";
$unitIdentifier->objectId = (string) $archive->archiveId;
$message->unitIdentifier[] = $unitIdentifier;
$archive->lifeCycleEvent = $this->lifeCycleJournalController->getObjectEvents($archive->archiveId, 'recordsManagement/archive');
$this->archiveController->setStatus($archive->archiveId, 'transferable');
}
$message->archive = $archives;
$message->dataObjectCount = count($archives);
if ($message->schema != 'medona') {
$archiveTransferController = \laabs::newController($message->schema.'/ArchiveTransfer');
$archiveTransferController->send($message);
} else {
$archiveTransfer = $this->sendMessage($message);
$message->object = $archiveTransfer;
$archiveTransfer->transferringAgency = $this->sendOrganization($message->senderOrg);
$archiveTransfer->archivalAgency = $this->sendOrganization($message->recipientOrg);
$this->generate($message);
$this->save($message);
}
$this->create($message);
$operationResult = true;
} catch (\Exception $e) {
$message->status = "invalid";
$this->create($message);
$operationResult = false;
throw $e;
}
$this->lifeCycleJournalController->logEvent(
'medona/sending',
'medona/message',
$message->messageId,
$message,
$operationResult
);
return $message;
}
/**
* Export outgoing archive
* @param string $messageId The message identifier
*
* @return the result of process
*/
public function export($messageId)
{
$message = $this->sdoFactory->read('medona/message', $messageId);
$exportResult = \laabs::newController("medona/message")->export($messageId);
$this->lifeCycleJournalController->logEvent(
'medona/reception',
'medona/message',
$message->messageId,
$message,
true
);
$this->changeStatus($messageId, "downloaded");
return $exportResult;
}
/**
* Validate archive transfer message
* @param string $messageId The message identifier
*/
public function validate($messageId)
{
$this->changeStatus($messageId, "accepted");