ArchiveDeliveryRequest.php 15.3 KB
Newer Older
Prosper De Laure's avatar
Prosper De Laure committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?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;

/**
 * Archive delivery request
 *
 * @author Alexis Ragot <alexis.ragot@maarch.org>
 */
class ArchiveDeliveryRequest extends abstractMessage
{
    /**
     * Get received archive delivery message
     *
     * @return array Array of medona/message object
     */
    public function listReception()
    {
        $currentOrg = \laabs::getToken('ORGANIZATION');
        if (!$currentOrg) {
            $this->view->addContentFile("recordsManagement/welcome/noWorkingOrg.html");

            return $this->view->saveHtml();
        }

        $queryParts = array();
        $registrationNumber = $this->getCurrentRegistrationNumber();

        $queryParts[] = "recipientOrgRegNumber=$registrationNumber";
Alexandre Morin's avatar
Alexandre Morin committed
49
        $queryParts[] = "type='ArchiveDeliveryRequest'";
Prosper De Laure's avatar
Prosper De Laure committed
50
51
52
53
54
55
56
57
        $queryParts[] = "status = 'sent'";
        $queryParts[] = "active=true";

        return $this->sdoFactory->find('medona/message', implode(' and ', $queryParts), null, false, false, 300);
    }

    /**
     * Get transfer history
Alexandre Morin's avatar
Alexandre Morin committed
58
59
60
61
62
63
64
65
66
     *
     * @param string $reference         Reference
     * @param string $archiver          Archiver
     * @param string $originator        Originator
     * @param string $depositor         Depositor
     * @param string $archivalAgreement Archival agreement
     * @param date   $fromDate          From date
     * @param date   $toDate            To date
     * @param string $status            Status
Prosper De Laure's avatar
Prosper De Laure committed
67
68
69
     *
     * @return array Array of medona/message object
     */
Alexandre Morin's avatar
Alexandre Morin committed
70
    public function history($reference = null, $archiver = null, $originator = null, $depositor = null, $archivalAgreement = null, $fromDate = null, $toDate = null, $status = null)
Prosper De Laure's avatar
Prosper De Laure committed
71
    {
Alexandre Morin's avatar
Alexandre Morin committed
72
        return $this->search("ArchiveDelivery", $reference, $archiver, $originator, $depositor, $archivalAgreement, $fromDate, $toDate, $status);
Prosper De Laure's avatar
Prosper De Laure committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    }

    /**
     * Count archive delivery message
     *
     * @return array Number of received and sent messages
     */
    public function count()
    {
        $res = array();
        $queryParts = array();

        $registrationNumber = $this->getCurrentRegistrationNumber();

        $queryParts["registrationNumber"] = "recipientOrgRegNumber=$registrationNumber";
        $queryParts["type"] = "type='ArchiveDeliveryRequest'";
        $queryParts["active"] = "active=true";
        $res['received'] = $this->sdoFactory->count('medona/message', implode(' and ', $queryParts));

        $queryParts["registrationNumber"] = "senderOrgRegNumber=$registrationNumber";
        $res['sent'] = $this->sdoFactory->count('medona/message', implode(' and ', $queryParts));

        return $res;
    }

    /**
     * Deliver an archive
Alexandre Morin's avatar
Alexandre Morin committed
100
101
102
103
     * @param mixed  $archiveIds    The identifier of archive or a list of identifiers
     * @param string $identifier    The medona message reference
     * @param boolean $derogation   Ask for an authorization
     * @param string $comment       The message comment
Prosper De Laure's avatar
Prosper De Laure committed
104
105
106
107
108
     *
     * @return array Array of message
     *
     * @throws \bundle\recordsManagement\Exception\notCommunicableException
     */
Alexandre Morin's avatar
Alexandre Morin committed
109
    public function requestDelivery($archiveIds, $identifier = null, $derogation = false, $comment = null)
Prosper De Laure's avatar
Prosper De Laure committed
110
    {
111
112
        $requesterOrg = \laabs::getToken('ORGANIZATION');
        if (!$requesterOrg) {
Alexis Ragot's avatar
Alexis Ragot committed
113
114
            throw \laabs::newException('medona/invalidMessageException', "No current organization choosen");
        }
115
        $requesterOrgRegNumber = $requesterOrg->registrationNumber;
Alexis Ragot's avatar
Alexis Ragot committed
116

Prosper De Laure's avatar
Prosper De Laure committed
117
118
119
120
121
122
123
124
        if (!is_array($archiveIds)) {
            $archiveIds = array($archiveIds);
        }

        $archivesByOriginator = array();
        $messages = array();

        foreach ($archiveIds as $archiveId) {
125
            $archive = $this->archiveController->retrieve($archiveId, $withBinary = false, $checkAccess = true, $isCommunication = true);
126
            
127
            if (!isset($archivesByOriginator[$archive->originatorOrgRegNumber])) {
Prosper De Laure's avatar
Prosper De Laure committed
128
129
130
131
132
133
                $archivesByOriginator[$archive->originatorOrgRegNumber] = array();
            }

            $archivesByOriginator[$archive->originatorOrgRegNumber][] = $archive;
        }

134
135
136
        if (!$identifier) {
            $identifier = "archiveDeliveryRequest_".date("Y-m-d-H-i-s");
        }
Prosper De Laure's avatar
Prosper De Laure committed
137

138
        $reference = $identifier;
Prosper De Laure's avatar
Prosper De Laure committed
139
        foreach ($archivesByOriginator as $originatorOrgRegNumber => $archives) {
140
141
142
143
144
145
146
147
148
149
150
151
152
            $i = 1;
            
            $unique = array(
                'type' => 'ArchiveDeliveryRequest',
                'senderOrgRegNumber' => $requesterOrgRegNumber,
                'reference' => $reference,
            );

            while ($this->sdoFactory->exists("medona/message", $unique)) {
                $i++;
                $unique['reference'] = $reference = $identifier.'_'.$i;
            }

153
            $archiverOrgRegNumber = $archives[0]->archiverOrgRegNumber;
154
155

            $message = $this->send($reference, $archives, $derogation, $comment, $requesterOrgRegNumber, $archiverOrgRegNumber);
Prosper De Laure's avatar
Prosper De Laure committed
156
157
158
159
160
161
162
163
164
165
166
167
168

            $messages[] = $message;
        }

        return $messages;
    }

    /**
     * Send a new a new delivery request
     * @param string  $reference             The message identifier
     * @param array   $archives              An array of archives
     * @param boolean $derogation            Ask for an authorization
     * @param string  $comment               The request comment
169
170
     * @param object  $requesterOrgRegNumber The requesting org reg number
     * @param string  $archiverOrgRegNumber  The archiver org registration number
171
     * @param string  $userName              The requester user name
Prosper De Laure's avatar
Prosper De Laure committed
172
173
174
     *
     * @return The reply message generated
     */
175
    public function send($reference, $archives, $derogation = false, $comment = false, $requesterOrgRegNumber = false, $archiverOrgRegNumber = false, $userName = false)
Prosper De Laure's avatar
Prosper De Laure committed
176
177
178
179
180
181
182
183
184
    {
        if (!is_array($archives)) {
            $archives = array($archives);
        }

        $message = \laabs::newInstance('medona/message');
        $message->messageId = \laabs::newId();

        $schema = "medona";
185
        if (\laabs::hasBundle('seda')) {
Prosper De Laure's avatar
Prosper De Laure committed
186
187
188
189
190
191
192
193
194
195
196
            $schema = "seda";
        }
        $message->schema = $schema;
        $message->type = "ArchiveDeliveryRequest";
        $message->status = 'new';
        $message->date = \laabs::newDatetime(null, "UTC");
        $message->receptionDate = $message->date;
        $message->reference = $reference;

        $message->comment[] = $comment;

197
198
        $message->senderOrgRegNumber = $requesterOrgRegNumber;
        $message->recipientOrgRegNumber = $archiverOrgRegNumber;
Prosper De Laure's avatar
Prosper De Laure committed
199
200
201
202

        // read org names, addresses, communications, contacts
        $this->readOrgs($message);

203
204
205
206
207
208
209
        $message->derogation = $derogation;

        if ($derogation) {
            $message->status = "sent";
        } else {
            $message->status = "accepted";
        }
Prosper De Laure's avatar
Prosper De Laure committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228

        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;
        }

        try {
            if ($message->schema != 'medona') {
                $archiveDeliveryRequestController = \laabs::newController($message->schema.'/ArchiveDeliveryRequest');
                $archiveDeliveryRequestController->send($message);
            } else {
                $archiveDeliveryRequest = $this->sendMessage($message);
                $message->object = $archiveDeliveryRequest;

                $archiveDeliveryRequest->requester = $this->sendOrganization($message->senderOrg);
229
230
231
232
                if ($userName) {
                    $archiveDeliveryRequest->requester->userName = $userName;
                }
                
Prosper De Laure's avatar
Prosper De Laure committed
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
                $archiveDeliveryRequest->archivalAgency = $this->sendOrganization($message->recipientOrg);

                $message->object->unitIdentifier = $message->unitIdentifier;
            }
            $operationResult = true;

            $this->create($message);
        } 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;
    }

    /**
     * Send an authorization request
     * @param string $messageId The message identifier
     */
    public function sendAuthorizationRequest($messageId)
    {
        $message = $this->sdoFactory->read('medona/message', array('messageId' => $messageId));

        if ($message->derogation) {
            $unitIdentifier = $this->sdoFactory->readChildren('medona/unitIdentifier', $message);
            $originatorOrgRegNumber = $this->archiveController->read($unitIdentifier[0]->objectId)->originatorOrgRegNumber;

            $authorizationControlAuthorityRequestController = \laabs::newController('medona/AuthorizationControlAuthorityRequest');
            $authorizationControlAuthorityRequestController->send($messageId, $originatorOrgRegNumber);
        }
    }

Alexandre Morin's avatar
Alexandre Morin committed
276
277
278
279
280
281
282
283
284
285
286
287
288
289
    /**
     * Derogation archive delivery request message
     * @param string $messageId The message identifier
     */
    public function derogation($messageId)
    {
        $this->changeStatus($messageId, "derogation");

        $message = $this->sdoFactory->read('medona/message', array('messageId' => $messageId));

        $message->derogation = "true";
        $this->sdoFactory->update($message);

        $event = $this->lifeCycleJournalController->logEvent(
290
            'medona/authorization',
Alexandre Morin's avatar
Alexandre Morin committed
291
292
293
294
295
296
297
298
299
300
301
302
303
304
            'medona/message',
            $message->messageId,
            $message,
            true
        );

        $controlAutorityControler = \laabs::newController("medona/ControlAuthority");
        if (count($controlAutorityControler->index()) > 0) {
            $this->sendAuthorizationRequest((string) $message->messageId);
        } else {
            $this->accept((string) $message->messageId);
        }
    }

Prosper De Laure's avatar
Prosper De Laure committed
305
306
307
308
309
310
311
312
313
    /**
     * Accept archive delivery request message
     * @param string $messageId The message identifier
     */
    public function accept($messageId)
    {
        $this->changeStatus($messageId, "accepted");

        $message = $this->sdoFactory->read('medona/message', array('messageId' => $messageId));
Alexandre Morin's avatar
Alexandre Morin committed
314

Prosper De Laure's avatar
Prosper De Laure committed
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
        $event = $this->lifeCycleJournalController->logEvent(
            'medona/acceptance',
            'medona/message',
            $message->messageId,
            $message,
            true
        );
    }

    /**
     * Reject the message identifier
     * @param string $messageId The message identifier
     * @param string $comment   A comment
     *
     * @return object The reply message
     */
    public function reject($messageId, $comment = null)
    {
        $this->changeStatus($messageId, "rejected");

Alexandre Morin's avatar
Alexandre Morin committed
335
        //$archiveDeliveryRequestReplyController = \laabs::newController('medona/ArchiveDeliveryRequestReply');
Prosper De Laure's avatar
Prosper De Laure committed
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396

        $message = $this->sdoFactory->read('medona/message', array('messageId' => $messageId));

        $event = $this->lifeCycleJournalController->logEvent(
            'medona/rejection',
            'medona/message',
            $message->messageId,
            $message,
            true
        );
    }

    /**
     * Process the messages
     *
     * @return medona/message $message
     */
    public function processBatch()
    {
        $results = array();

        $messages = $this->sdoFactory->find("medona/message", "status='accepted' AND type='ArchiveDeliveryRequest' AND active=true");
        foreach ($messages as $message) {
            $this->changeStatus($message->messageId, "processing");
            $this->readOrgs($message);

            if (!empty($message->data)) {
                $message->object = json_decode($message->data);
            }

            $message->unitIdentifier = $this->sdoFactory->readChildren("medona/unitIdentifier", $message);

            try {
                $results[(string) $message->messageId] = $this->process($message);
            } catch (\Exception $e) {
                $results[(string) $message->messageId] = $e;
            }
        }

        return $results;
    }

    /**
     * Validate message against schema and rules
     * @param medona/message $message
     * @param string         $replyCode
     *
     * @return the result of process
     */
    public function process($message, $replyCode = "000")
    {
        if (is_scalar($message)) {
            $messageId = $message;
            $message = $this->sdoFactory->read('medona/message', $messageId);
            $message->unitIdentifier = $this->sdoFactory->readChildren("medona/unitIdentifier", $message);
        }

        $this->changeStatus($message->messageId, "processing");
        $archives = array();

        foreach ((array) $message->unitIdentifier as $unitIdentifier) {
397
            $archives[] = $this->archiveController->communicate($unitIdentifier->objectId);
Prosper De Laure's avatar
Prosper De Laure committed
398
399
        }

400
401
402
        $logMessage = ["message" => "%s archives are communicated", "variables"=> count($archives)];
        \laabs::notify(\bundle\audit\AUDIT_ENTRY_OUTPUT, $logMessage);

Prosper De Laure's avatar
Prosper De Laure committed
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
        try {
            $archiveDeliveryRequestReplyController = \laabs::newController('medona/ArchiveDeliveryRequestReply');
            $archiveDeliveryRequestReplyController->send($message, $archives, $replyCode);
            $operationResult = true;
        } catch (\Exception $e) {
            $message->status = "error";
            $operationResult = false;
            $this->sdoFactory->update($message);
            throw $e;
        }

        $this->lifeCycleJournalController->logEvent(
            'medona/processing',
            'medona/message',
            $message->messageId,
            $message,
            $operationResult
        );

        $message->status = "processed";

        return $this->sdoFactory->update($message);
    }

    /**
     * Export archive delivery
     * @param string $messageId The message identifier
     *
     * @return the result of process
     */
    public function export($messageId)
    {
        $message = $this->sdoFactory->read('medona/message', $messageId);
Alexandre Morin's avatar
Alexandre Morin committed
436

Prosper De Laure's avatar
Prosper De Laure committed
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
        $exportResult = \laabs::newController("medona/message")->export($messageId);

        $this->lifeCycleJournalController->logEvent(
            'medona/reception',
            'medona/message',
            $message->messageId,
            $message,
            true
        );

        $this->changeStatus($messageId, "received");

        return $exportResult;
    }
}