ArchiveDestructionRequest.php 11.8 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
49
50
<?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;

/**
 * Destruction request message
 *
 * @author Prosper DE LAURE <prosper.delaure@maarch.org>
 */
class ArchiveDestructionRequest extends abstractMessage
{
     /**
     * Get received archive delivery message
     *
     * @return array Array of medona/message object
     */
    public function listReception()
    {
        $registrationNumber = $this->getCurrentRegistrationNumber();

        $queryParts = [];
        $queryParts[] = "recipientOrgRegNumber=$registrationNumber";
        $queryParts[] = "type='ArchiveDestructionRequest'";
        $queryParts[] = "active=true";
        $queryParts[] = "status != 'processed' AND status != 'error' AND status != 'sent' AND status != 'validated' AND status != 'rejected'";

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

    /**
     * Get transfer history
Alexandre Morin's avatar
Alexandre Morin committed
51
52
53
54
55
56
57
58
59
     *
     * @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
60
61
62
     *
     * @return array Array of medona/message object
     */
Alexandre Morin's avatar
Alexandre Morin committed
63
    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
64
    {
Alexandre Morin's avatar
Alexandre Morin committed
65
        return $this->search("ArchiveDestruction", $reference, $archiver, $originator, $depositor, $archivalAgreement, $fromDate, $toDate, $status);
Prosper De Laure's avatar
Prosper De Laure committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    }

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

        $queryParts["type"] = "type='ArchiveDestructionRequest'";
        $queryParts["registrationNumber"] = "recipientOrgRegNumber=$registrationNumber";
        $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;
    }

    /**
     * Send a new a new delivery request
92
93
94
95
96
97
     * @param string $reference              The message identifier
     * @param array  $archives               An array of archives
     * @param string $comment                The request comment
     * @param string $requesterOrgRegNumber  The requesting org reg number
     * @param string $archiverOrgRegNumber   The recipient archiver org reg number
     * @param string $originatorOrgRegNumber The originator org reg number
Prosper De Laure's avatar
Prosper De Laure committed
98
99
100
     *
     * @return The request message generated
     */
101
    public function send($reference, $archives, $comment = null, $requesterOrgRegNumber = null, $archiverOrgRegNumber = null, $originatorOrgRegNumber=null)
Prosper De Laure's avatar
Prosper De Laure committed
102
103
104
105
106
107
108
109
    {
        if (!is_array($archives)) {
            $archives = array($archives);
        }

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

110
        $schema = "medona";
Alexis Ragot's avatar
Alexis Ragot committed
111
        if (\laabs::hasBundle('seda') && $archives[0]->descriptionClass == 'archivesPubliques/content') {
Prosper De Laure's avatar
Prosper De Laure committed
112
113
114
115
116
117
118
119
120
121
122
            $schema = "seda";
        }
        $message->schema = $schema;
        $message->type = "ArchiveDestructionRequest";
        $message->status = 'sent';
        $message->date = \laabs::newDatetime(null, "UTC");
        $message->receptionDate = $message->date;
        $message->reference = $reference;

        $message->comment[] = $comment;

123
124
        $message->senderOrgRegNumber = $requesterOrgRegNumber;
        $message->recipientOrgRegNumber = $archiverOrgRegNumber;
Prosper De Laure's avatar
Prosper De Laure committed
125
126
127
128
129
130
131
132
133
134
135
136
137
        $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;
            }

138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
            if ($message->schema != 'medona') {
                $archiveDestructionRequestController = \laabs::newController($message->schema.'/ArchiveDestructionRequest');
                $archiveDestructionRequestController->send($message);
            } else {
                $archiveDestructionRequest = $this->sendMessage($message);
                $message->object = $archiveDestructionRequest;

                $archiveDestructionRequest->originatingAgency = $this->sendOrganization($message->senderOrg);
                $archiveDestructionRequest->archivalAgency = $this->sendOrganization($message->recipientOrg);

                $message->object->unitIdentifier = $message->unitIdentifier;

                $this->generate($message);
                $this->save($message);
            }
153
            $operationResult = true;
Prosper De Laure's avatar
Prosper De Laure committed
154
155
        } catch (\Exception $e) {
            $message->status = "error";
156
            $operationResult = false;
Prosper De Laure's avatar
Prosper De Laure committed
157
158
159
160

            throw $e;
        }

161
162
163
164
165
166
167
168
169
        $event = $this->lifeCycleJournalController->logEvent(
            'medona/sending',
            'medona/message',
            $message->messageId,
            $message,
            $operationResult
        );

        $this->create($message);
170
171
        $senderOrg = \laabs::getToken('ORGANIZATION');

172
        // Requested by archiver: send auth request to originator
173
        if ($senderOrg->registrationNumber == $message->recipientOrgRegNumber) {
Prosper De Laure's avatar
Prosper De Laure committed
174
            $authorizationOriginatingAgencyRequestController = \laabs::newController('medona/AuthorizationOriginatingAgencyRequest');
175
            $authorizationOriginatingAgencyRequestController->send($message, $originatorOrgRegNumber);
Prosper De Laure's avatar
Prosper De Laure committed
176

177
            $message->status == "originator_authorization_wait";
Prosper De Laure's avatar
Prosper De Laure committed
178
179
180
181
182
183
        } else {
            // Requested by originator
            $controlAuthorities = $this->orgController->getOrgsByRole('controlAuthority');

            // Check if control authority is set on system
            if (count($controlAuthorities)) {
184
                $message->status == "control_authorization_wait";
Prosper De Laure's avatar
Prosper De Laure committed
185
                $authorizationControlAuthorityRequestController = \laabs::newController('medona/AuthorizationControlAuthorityRequest');
186
                $authorizationControlAuthorityRequestController->send($message, $originatorOrgRegNumber);
Prosper De Laure's avatar
Prosper De Laure committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
            }
        }

        return $message;
    }

    /**
     * Validate
     * @param object $messageId
     */
    public function validate($messageId)
    {
        if (is_scalar($messageId)) {
            $message = $this->read($messageId);
        } else {
            $message = $messageId;
        }

        $message->status = "validated";

        foreach ($message->unitIdentifier as $unitIdentifier) {
208
            $this->archiveController->eliminate((string) $unitIdentifier->objectId);
Prosper De Laure's avatar
Prosper De Laure committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
        }

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

    /**
     * Accept
     * @param object $message
     */
    public function accept($message)
    {
        if (is_scalar($message)) {
            $message = $this->read($message);
        }

        $message->status = "accepted";

226
227
228
229
230
231
232
233
        $event = $this->lifeCycleJournalController->logEvent(
            'medona/acceptance',
            'medona/message',
            $message->messageId,
            $message,
            true
        );

Prosper De Laure's avatar
Prosper De Laure committed
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
        $this->sdoFactory->update($message);
    }

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

        $message = $this->read($messageId);

        $archiveIds = [];
        foreach ($message->unitIdentifier as $unitIdentifier) {
            $archiveIds[] = (string) $unitIdentifier->objectId;
        }
        $this->archiveController->setStatus($archiveIds, 'preserved');

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

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

    /**
     * Process all archive destructions
     *
     * @return the result of process
     */
    public function processAll()
    {
        $results = array();

        $index = $this->sdoFactory->index('medona/message', array('messageId'), 'type = "ArchiveDestructionRequest" AND status = "validated"');

        foreach ($index as $messageId) {
            $results[(string) $messageId] = $this->process($messageId);
        }

        return $results;
    }

    /**
     * Process archive destruction
     * @param medona/message $messageId
     *
     * @return the result of process
     */
    public function process($messageId)
    {
        if (is_scalar($messageId)) {
            $message = $this->sdoFactory->read('medona/message', $messageId);
        } else {
            $message = $messageId;
        }

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

        $archiveIds = [];
        foreach ($message->unitIdentifier as $unitIdentifier) {
            $archiveIds[] = (string) $unitIdentifier->objectId;
        }

        $transactionControl = !$this->sdoFactory->inTransaction();

        if ($transactionControl) {
            $this->sdoFactory->beginTransaction();
        }

        try {
            $archives = $this->archiveController->destruct($archiveIds);

314
315
316
            $logMessage = ["message" => "%s archives are deleted", "variables"=> count($archives)];
            \laabs::notify(\bundle\audit\AUDIT_ENTRY_OUTPUT, $logMessage);

Prosper De Laure's avatar
Prosper De Laure committed
317
318
319
            $message->status = "processed";
            $message->operationDate = \laabs::newDatetime(null, "UTC");

320
            $operationResult = true;
Prosper De Laure's avatar
Prosper De Laure committed
321
322
            $this->update($message);
        } catch (\Exception $e) {
323
            $operationResult = false;
Prosper De Laure's avatar
Prosper De Laure committed
324
325
326
327
328
329
330
            throw $e;
        }

        if ($transactionControl) {
            $this->sdoFactory->commit();
        }

331
332
333
334
335
336
337
338
        $this->lifeCycleJournalController->logEvent(
            'medona/processing',
            'medona/message',
            $message->messageId,
            $message,
            $operationResult
        );

Prosper De Laure's avatar
Prosper De Laure committed
339
340
341
342
343
344
345
346
        $archiveDestructionNotificationController = \laabs::newController("medona/ArchiveDestructionNotification");

        // TO DO : Write destruction failures in comments
        $replyMessage = $archiveDestructionNotificationController->send($message, $archives['success']);

        return $replyMessage->messageId;
    }
}