archivalProfile.php 26.5 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
<?php
/*
 * Copyright (C) 2015 Maarch
 *
 * This file is part of bundle recordsManagement.
 *
 * Bundle recordsManagement 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 recordsManagement 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 recordsManagement.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace bundle\recordsManagement\Controller;
21

Alexandre Morin's avatar
Alexandre Morin committed
22
use core\Exception;
Prosper De Laure's avatar
Prosper De Laure committed
23
24
25
26
27
28
29
30
31

/**
 * Class of adminArchivalProfile
 *
 * @author Alexis Ragot <alexis.ragot@maarch.org>
 */
class archivalProfile
{
    protected $sdoFactory;
32
    protected $csv;
Prosper De Laure's avatar
Prosper De Laure committed
33
34
35
36
37

    protected $lifeCycleJournalController;

    protected $descriptionFields;

38
    protected $profilesDirectory;
Cyril Vazquez's avatar
Cyril Vazquez committed
39
    protected $descriptionSchemeController;
40

Prosper De Laure's avatar
Prosper De Laure committed
41
42
43
    /**
     * Constructor
     * @param \dependency\sdo\Factory $sdoFactory The sdo factory
44
45
     * @param bool                    $notifyModification The state of the fonction of notification modification
     * @param string                  $profilesDirectory  The profile directory
Prosper De Laure's avatar
Prosper De Laure committed
46
     */
47
    public function __construct(\dependency\sdo\Factory $sdoFactory, \dependency\csv\Csv $csv = null, $notifyModification, $profilesDirectory)
Prosper De Laure's avatar
Prosper De Laure committed
48
49
    {
        $this->sdoFactory = $sdoFactory;
50
        $this->csv = $csv;
Prosper De Laure's avatar
Prosper De Laure committed
51
        $this->lifeCycleJournalController = \laabs::newController('lifeCycle/journal');
52
        $this->descriptionSchemeController = \laabs::newController('recordsManagement/descriptionScheme');
Cyril Vazquez's avatar
Cyril Vazquez committed
53
        //$this->descriptionFields = \Laabs::newController('recordsManagement/descriptionField')->index();
54
55
56
57
58
59
60
61

        if (!is_dir($profilesDirectory) && !empty($profilesDirectory)) {
            mkdir($profilesDirectory, 0777, true);
        }

        $this->profilesDirectory = $profilesDirectory;

        $this->notifyModification = $notifyModification;
Prosper De Laure's avatar
Prosper De Laure committed
62
63
64
65
66
    }

    /**
     * List archival profiles
     *
67
     * @param integer $limit Maximal number of results to dispay
68
     * @param string  $query The query filter
69
     *
70
     * @return recordsManagement/archivalProfile[] The list of archival profiles
Prosper De Laure's avatar
Prosper De Laure committed
71
     */
72
    public function index($limit = null, $query = null)
Prosper De Laure's avatar
Prosper De Laure committed
73
    {
74
        $archivalProfiles = $this->sdoFactory->find('recordsManagement/archivalProfile', $query, null, null, null, $limit);
75
76
77
78
79

        foreach ($archivalProfiles as $archivalProfile) {
            $archivalProfile->containedProfiles = $this->getContentsProfiles($archivalProfile->archivalProfileId, true);
        }
        return $archivalProfiles;
Prosper De Laure's avatar
Prosper De Laure committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    }

    /**
     * New empty archival profile with default values
     *
     * @return recordsManagement/archivalProfile The archival profile object
     */
    public function newProfile()
    {
        $archivalProfile = \laabs::newInstance("recordsManagement/archivalProfile");

        return $archivalProfile;
    }

    /**
     * Edit an archival profile
96
     * @param string $archivalProfileId   The archival profile's identifier
97
     * @param bool   $withRelatedProfiles Bring back the contents profiles
98
     * @param bool   $recursively         Get contained archival profiles children
Prosper De Laure's avatar
Prosper De Laure committed
99
100
101
     *
     * @return recordsManagement/archivalProfile The profile object
     */
102
    public function read($archivalProfileId, $withRelatedProfiles = true, $recursively = false)
Prosper De Laure's avatar
Prosper De Laure committed
103
104
105
106
107
    {
        $archivalProfile = $this->sdoFactory->read('recordsManagement/archivalProfile', $archivalProfileId);

        $this->readDetail($archivalProfile);

108
        if ($withRelatedProfiles) {
109
            $archivalProfile->containedProfiles = $this->getContentsProfiles($archivalProfileId, $recursively);
110
111
        }

Prosper De Laure's avatar
Prosper De Laure committed
112
113
114
115
116
        return $archivalProfile;
    }

    /**
     * get an archival profile by reference
Matthieu's avatar
Matthieu committed
117
     * @param string $reference The archival profile reference
118
     * @param bool   $withRelatedProfiles      Bring back the contents profiles
Prosper De Laure's avatar
Prosper De Laure committed
119
120
121
     *
     * @return recordsManagement/archivalProfile The profile object
     */
122
    public function getByReference($reference, $withRelatedProfiles = true)
Prosper De Laure's avatar
Prosper De Laure committed
123
    {
124
125
126
        if (is_null($reference)) {
            return null;
        }
Alexandre Morin's avatar
Alexandre Morin committed
127
        try {
Matthieu's avatar
Matthieu committed
128
            $archivalProfile = $this->sdoFactory->read('recordsManagement/archivalProfile', array('reference' => $reference));
129

Alexandre Morin's avatar
Alexandre Morin committed
130
131
132
133
134
135
            $this->readDetail($archivalProfile);

            if ($withRelatedProfiles) {
                $archivalProfile->containedProfiles = $this->getContentsProfiles($archivalProfile->archivalProfileId);
            }
        } catch (\Exception $exception) {
136
            throw new \core\Exception\BadRequestException("Profile %s not found", 404, null, (array) $reference);
137
        }
Prosper De Laure's avatar
Prosper De Laure committed
138
139
140
141
142
143
144
145

        return $archivalProfile;
    }

    /**
     * get array of archival profile by description class
     * @param string $archivalProfileDescriptionClass The archival profile reference
     *
146
     * @return array $archivalProfiles Array of recordsManagement/archivalProfile object
Prosper De Laure's avatar
Prosper De Laure committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
     */
    public function getByDescriptionClass($archivalProfileDescriptionClass)
    {
        $archivalProfiles = $this->sdoFactory->find('recordsManagement/archivalProfile', "descriptionClass='$archivalProfileDescriptionClass'");

        foreach ($archivalProfiles as $archivalProfile) {
            $this->readDetail($archivalProfile);
        }

        return $archivalProfiles;
    }

    /**
     * Read the agragates
     * @param recordsManagement/archivalProfile $archivalProfile
     */
    public function readDetail($archivalProfile)
    {
165
        // Read retention rule
Prosper De Laure's avatar
Prosper De Laure committed
166
167
168
169
        if ($archivalProfile->retentionRuleCode) {
            $archivalProfile->retentionRule = $this->sdoFactory->read('recordsManagement/retentionRule', $archivalProfile->retentionRuleCode);
        }

170
        // Read access rule
Prosper De Laure's avatar
Prosper De Laure committed
171
172
173
174
        if (!empty($archivalProfile->accessRuleCode)) {
            $archivalProfile->accessRule = $this->sdoFactory->read('recordsManagement/accessRule', $archivalProfile->accessRuleCode);
        }

175
        // Read profile description
176
        $archivalProfile->archiveDescription = $this->sdoFactory->readChildren('recordsManagement/archiveDescription', $archivalProfile, null, 'position');
177
        usort($archivalProfile->archiveDescription, function ($a, $b) {
Cyril Vazquez's avatar
Cyril Vazquez committed
178
            return $a->position > $b->position ? 1 : -1;
179
180
        });
        
Cyril Vazquez's avatar
Cyril Vazquez committed
181
182
183
184
        $descriptionFields = $this->descriptionSchemeController->getDescriptionFields($archivalProfile->descriptionClass);
        foreach ($archivalProfile->archiveDescription as $archiveDescription) {
            if (isset($descriptionFields[$archiveDescription->fieldName])) {
                $archiveDescription->descriptionField = $descriptionFields[$archiveDescription->fieldName];
Cyril Vazquez's avatar
Cyril Vazquez committed
185
186
            }
        }
Cyril Vazquez's avatar
Cyril Vazquez committed
187
        
188
189
190
191
        $profileFile = $this->profilesDirectory.DIRECTORY_SEPARATOR.$archivalProfile->reference;
        if (file_exists($profileFile.'.rng') || file_exists($profileFile.'.xsd')) {
            $archivalProfile->profileFile = $profileFile;
        }
Prosper De Laure's avatar
Prosper De Laure committed
192
193
    }

194
195
    /**
     * Get the contents profiles list
196
197
     * @param string $archivalProfileId The parent profile identifier
     * @param bool   $recursively       Get contained archival profiles children
198
199
200
     *
     * @return array The list of contents archival profile
     */
201
    public function getContentsProfiles($archivalProfileId, $recursively = false)
202
    {
Cyril Vazquez's avatar
Cyril Vazquez committed
203
        $containedProfiles = []; 
204
        $contents = $this->sdoFactory->find('recordsManagement/archivalProfileContents', "parentProfileId ='$archivalProfileId'");
205

206
207
208
209
210
        if (count($contents)) {
            foreach ($contents as $content) {
                $containedProfile = $this->sdoFactory->read('recordsManagement/archivalProfile', $content->containedProfileId);

                $this->readDetail($containedProfile);
211
212

                if ($recursively) {
213
                    $containedProfile->containedProfiles = $this->getContentsProfiles($content->containedProfileId, $recursively);
214
215
                }

216
                $containedProfiles[] = $containedProfile;
217
218
219
            }
        }

220
        return $containedProfiles;
221
222
    }

Prosper De Laure's avatar
Prosper De Laure committed
223
224
225
226
227
228
229
230
    /**
     * create an archival profile
     * @param recordsManagement/archivalProfile $archivalProfile The archival profile object
     *
     * @return boolean The result of the request
     */
    public function create($archivalProfile)
    {
231
        $transactionControl = !$this->sdoFactory->inTransaction();
Prosper De Laure's avatar
Prosper De Laure committed
232

233
234
235
        if ($transactionControl) {
            $this->sdoFactory->beginTransaction();
        }
Prosper De Laure's avatar
Prosper De Laure committed
236

237
238
        try {
            $archivalProfile->archivalProfileId = \laabs::newId();
Prosper De Laure's avatar
Prosper De Laure committed
239

240
241
242
            $this->sdoFactory->create($archivalProfile, 'recordsManagement/archivalProfile');

            $this->createDetail($archivalProfile);
243
            
244
245
246
247
248
249
250
251
            // Life cycle journal
            $eventItems = array('archivalProfileReference' => $archivalProfile->reference);
            $this->lifeCycleJournalController->logEvent('recordsManagement/profileCreation', 'recordsManagement/archivalProfile', $archivalProfile->archivalProfileId, $eventItems);
        } catch (\Exception $exception) {
            if ($transactionControl) {
                $this->sdoFactory->rollback();
            }

Dylan's avatar
Dylan committed
252
            throw new \core\Exception("Profile already exist");
253
254
255
256
257
        }

        if ($transactionControl) {
            $this->sdoFactory->commit();
        }
Prosper De Laure's avatar
Prosper De Laure committed
258

259
        return  $archivalProfile->archivalProfileId;
Prosper De Laure's avatar
Prosper De Laure committed
260
261
262
263
264
265
266
267
268
269
    }

    /**
     * update an archival profile
     * @param recordsManagement/archivalProfile $archivalProfile The archival profile object
     *
     * @return boolean The request of the request
     */
    public function update($archivalProfile)
    {
270
        $transactionControl = !$this->sdoFactory->inTransaction();
Prosper De Laure's avatar
Prosper De Laure committed
271

272
273
274
        if ($transactionControl) {
            $this->sdoFactory->beginTransaction();
        }
Prosper De Laure's avatar
Prosper De Laure committed
275

276
        try {
277
278
            $oldArchivalProfile = $this->read($archivalProfile->archivalProfileId);
            $archivalProfile->reference = $oldArchivalProfile->reference;
279
280
281
282
283
284
285
286
287

            $this->deleteDetail($archivalProfile);
            $this->createDetail($archivalProfile);

            // archival profile
            $this->sdoFactory->update($archivalProfile, "recordsManagement/archivalProfile");

            // Life cycle journal
            $eventItems = array('archivalProfileReference' => $archivalProfile->reference);
288
            $this->lifeCycleJournalController->logEvent('recordsManagement/archivalProfileModification', 'recordsManagement/archivalProfile', $archivalProfile->archivalProfileId, $eventItems);
289
290
291
292
        } catch (\Exception $exception) {
            if ($transactionControl) {
                $this->sdoFactory->rollback();
            }
Dylan's avatar
Dylan committed
293
            throw new \core\Exception("Profile already exist");
294
295
296
297
298
299
300
301
        }

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

        return true;
    }
302
 
Prosper De Laure's avatar
Prosper De Laure committed
303
304
305
306
307
308
309
310
311
312
    /**
     * delete an archival profile
     * @param string $archivalProfileId The identifier of the archival profile
     *
     * @return boolean The request of the request
     */
    public function delete($archivalProfileId)
    {
        $archivalProfile = $this->sdoFactory->read('recordsManagement/archivalProfile', $archivalProfileId);

313
        if ($this->isUsed($archivalProfile)) {
Prosper De Laure's avatar
Prosper De Laure committed
314
            throw new \core\Exception\ForbiddenException("The archival profile %s currently in use.", 403, null, [$archivalProfile->reference]);
315
316
        }

Prosper De Laure's avatar
Prosper De Laure committed
317
318
        $this->deleteDetail($archivalProfile);

319
        $archivalProfileContents = $this->sdoFactory->find('recordsManagement/archivalProfileContents', "parentProfileId='$archivalProfileId' OR containedProfileId='$archivalProfileId'");
320
        if (!empty($archivalProfileContents)) {
321
322
323
324
            $this->sdoFactory->deleteCollection($archivalProfileContents, 'recordsManagement/archivalProfileContents');
        }
        
        $organizationController = \laabs::newController('organization/organization');
325
326
327
328
        $archivalProfileAccesses = $organizationController->getArchivalProfileAccess($orgId=null, $archivalProfileId);
        foreach ($archivalProfileAccesses as $archivalProfileAccess) {
            $organizationController->deleteArchivalProfileAccess($archivalProfileAccess->orgId, $archivalProfile->reference);
        }
329

Prosper De Laure's avatar
Prosper De Laure committed
330
331
332
333
334
335
336
337
338
339
340
        $this->sdoFactory->delete($archivalProfile);

        // Life cycle journal
        $eventItems = array('archivalProfileReference' => $archivalProfile->reference);
        $this->lifeCycleJournalController->logEvent('recordsManagement/profileDestruction', 'recordsManagement/archivalProfile', $archivalProfile->archivalProfileId, $eventItems);

        return true;
    }

    /**
     * Get form of teh description class
341
     * @param string $archivalProfileReference The reference of the archival profile
Prosper De Laure's avatar
Prosper De Laure committed
342
     *
343
     * @return recordsManagement/descriptionClass Object The description class object parsed with the profile descriptions
Prosper De Laure's avatar
Prosper De Laure committed
344
345
346
347
348
349
350
351
352
353
     */
    public function descriptionForm($archivalProfileReference)
    {
        $archivalProfile = $this->getByReference($archivalProfileReference);
        $descriptionObject = \laabs::newController($archivalProfile->descriptionClass)->form($archivalProfile->archiveDescription);
        $descriptionObject->descriptionClass =  $archivalProfile->descriptionClass;

        return $descriptionObject;
    }

354
355
    /**
     * Check if archival
356
     * @param recordsManagement/archivalProfile $archivalProfile The archival profile object
357
358
359
360
361
     *
     * @return bool The result of the operation
     */
    public function isUsed($archivalProfile)
    {
362
        $archivalProfileReference = $archivalProfile->reference;
Cyril Vazquez's avatar
Cyril Vazquez committed
363
364
365
        $archivalAgreementController = \laabs::newController('medona/archivalAgreement');
        $archivalAgreement = $archivalAgreementController->getByProfileReference($archivalProfile->reference);
        if (!empty($archivalAgreement)) {
366
367
368
369
370
371
372
            return true;
        }

        $archivalProfileAccessController = \laabs::newController('organization/organization');
        $archivalProfileAccess = $archivalProfileAccessController->getArchivalProfileAccess(null,$archivalProfileReference);
        if (!empty($archivalProfileAccess)) {
            return true;
373
374
        }

375
376
377
378
379
380
381
382
        if((bool) $this->sdoFactory->count('recordsManagement/archivalProfileContents', "containedProfileId = '$archivalProfile->archivalProfileId'")) {
            return true;
        }                

        if((bool) $this->sdoFactory->count('recordsManagement/archive', "archivalProfileReference = '$archivalProfileReference'")) {
            return true;
        }
        return false; 
383
384
    }

385
386
387
388
389
    /**
     * Create a detail
     * @param recordsManagement/archivalProfile $archivalProfile The archival profile object
     *
     */
Prosper De Laure's avatar
Prosper De Laure committed
390
391
392
    protected function createDetail($archivalProfile)
    {
        if (!empty($archivalProfile->archiveDescription)) {
393
            $position = 0;
Prosper De Laure's avatar
Prosper De Laure committed
394
395
            foreach ($archivalProfile->archiveDescription as $description) {
                $description->archivalProfileId = $archivalProfile->archivalProfileId;
396
397
398
                $description->position = $position;
                $position++;

Prosper De Laure's avatar
Prosper De Laure committed
399
400
401
                $this->sdoFactory->create($description, 'recordsManagement/archiveDescription');
            }
        }
402

403
        // Contents profiles       
404
405
406
407
408
        if (!empty($archivalProfile->containedProfiles)) {
            foreach ($archivalProfile->containedProfiles as $containedProfileId) {
                try {
                    $profile = $this->sdoFactory->read("recordsManagement/archivalProfile", $containedProfileId);
                } catch (\Exception $e) {
Prosper De Laure's avatar
Prosper De Laure committed
409
                    throw new \core\Exception\NotFoundException("%s can't be found.", 404, null, [$containedProfileId]);
410
411
412
                }
                
                $archivalProfileContents = \laabs::newInstance('recordsManagement/archivalProfileContents');
413
414
415
416
417
418
419
420
                $contentProfiles = $this->getContentsProfiles($containedProfileId);
                if (!empty($contentProfiles)) {
                    foreach ($contentProfiles as $contentProfile) {
                        if ($contentProfile->archivalProfileId == $archivalProfile->archivalProfileId) {
                            throw new \core\Exception\BadRequestException("%s cannot be recursively called.", 404, null, [$contentProfile->name]);
                        }
                    }
                }
421
                $archivalProfileContents->parentProfileId = $archivalProfile->archivalProfileId;
422
423
424
425
426
                $archivalProfileContents->containedProfileId = $containedProfileId;

                $this->sdoFactory->create($archivalProfileContents, 'recordsManagement/archivalProfileContents');
            }
        }
Prosper De Laure's avatar
Prosper De Laure committed
427
428
    }

429
430
431
432
433
    /**
     * Delete a detail
     * @param recordsManagement/archivalProfile $archivalProfile The archival profile object
     *
     */
Prosper De Laure's avatar
Prosper De Laure committed
434
435
    protected function deleteDetail($archivalProfile)
    {
436
        $this->sdoFactory->deleteChildren('recordsManagement/archiveDescription', $archivalProfile, 'recordsManagement/archivalProfile');
Prosper De Laure's avatar
Prosper De Laure committed
437

438
        $this->sdoFactory->deleteChildren('recordsManagement/archivalProfileContents', $archivalProfile, 'recordsManagement/archivalProfile');
Prosper De Laure's avatar
Prosper De Laure committed
439
    }
440

441
442
443
444
445
446
447
448
449
450
    /**
     * Upload a profile file
     * @param string $profileReference The profile reference
     * @param string $archivalProfile  The profile binary file
     * @param string $format           The profile file format
     *
     * @return boolean The result of the operation
     */
    public function uploadArchivalProfile($profileReference, $archivalProfile, $content, $format = 'rng')
    {
451
452
453
454
        if (empty($format)) {
            $format = 'rng';
        }

455
456
457
458
459
460
461
462
463
464
465
466
        $profilesDirectory = $this->profilesDirectory;
        $profilesDirectory .= DIRECTORY_SEPARATOR.$profileReference.'.'.$format ;
        $content = base64_decode($content);

        if (!$archivalProfile->archivalProfileId) {
            $archivalProfileController = \laabs::newController('recordsManagement/archivalProfile');
            $archivalProfileController->create($archivalProfile);
        }

        file_put_contents($profilesDirectory, $content);

        $archivalProfile = $this->getByReference($profileReference);
467
468
469
470
471
472

        if (strpos($content, "fr:gouv:culture:archivesdefrance:seda:v1") > -1) {
            $archivalProfile->descriptionSchema = "seda";
        } elseif (strpos($content, "fr:gouv:culture:archivesdefrance:seda:v2") > -1) {
            $archivalProfile->descriptionSchema = "seda2";
        }
473
474

        $this->sdoFactory->update($archivalProfile, "recordsManagement/archivalProfile");
475

476
477
        return true;
    }
478

479
480
481
482
483
484
    /**
     * Export profile file
     * @param string $profileReference
     *
     * @return string
     */
485
    public function exportFile($profileReference)
486
487
488
489
    {
        $profilesDirectory = $this->profilesDirectory;
        $fileDirectory = $profilesDirectory.DIRECTORY_SEPARATOR.$profileReference.".rng";

490
491
492
        $file = file_get_contents($fileDirectory);

        return $file;
493
    }
494

495
    public function exportCsv($limit = null)
496
497
498
499
500
501
502
503
    {
        $archivalProfiles = $this->sdoFactory->find('recordsManagement/archivalProfile', null, null, null, null, $limit);
        foreach ($archivalProfiles as $key => $archivalProfile) {
            $containedProfiles = $this->getContentsProfiles($archivalProfile->archivalProfileId);
            $archiveDescription = $this->sdoFactory->readChildren('recordsManagement/archiveDescription', $archivalProfile, null, 'position');

            $archivalProfile = \laabs::castMessage($archivalProfile, 'recordsManagement/archivalProfileImportExport');
            if ($containedProfiles) {
nicolas.diril's avatar
nicolas.diril committed
504
                $lastIndex = count($containedProfiles) -1;
505
                foreach ($containedProfiles as $index => $containedProfile) {
506
507
                    $archivalProfile->childrenProfiles .= $containedProfile->reference;

508
                    if ($lastIndex !== $index) {
509
510
511
512
513
514
515
516
517
                        $archivalProfile->childrenProfiles .= ";";
                    }
                }
            }

            $archivalProfile->archiveDescriptions = json_encode($archiveDescription);

            $archivalProfiles[$key] = $archivalProfile;
        }
518

nicolas.diril's avatar
nicolas.diril committed
519
520
521
        $handler = fopen('php://temp', 'w+');
        $this->csv->writeStream($handler, (array) $archivalProfiles, 'recordsManagement/archivalProfileImportExport', true);
        return $handler;
522
    }
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546

    public function import($data, $isReset = false)
    {
        $transactionControl = !$this->sdoFactory->inTransaction();

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

        try {
            if ($isReset) {
                $archivalProfiles = $this->index();

                foreach ($archivalProfiles as $archivalProfile) {
                    $this->sdoFactory->deleteChildren('recordsManagement/archivalProfileContents', $archivalProfile, 'recordsManagement/archivalProfile');
                    $this->sdoFactory->deleteChildren('recordsManagement/archiveDescription', $archivalProfile, 'recordsManagement/archivalProfile');
                    $archivalProfileContents = $this->sdoFactory->find('recordsManagement/archivalProfileContents', "parentProfileId='$archivalProfile->archivalProfileId' OR containedProfileId='$archivalProfile->archivalProfileId'");
                    if (!empty($archivalProfileContents)) {
                        $this->sdoFactory->deleteCollection($archivalProfileContents, 'recordsManagement/archivalProfileContents');
                    }
                    $this->sdoFactory->delete($archivalProfile, 'recordsManagement/archivalProfile');
                }
            }

nicolas.diril's avatar
nicolas.diril committed
547
            $archivalProfiles = $this->csv->readStream($data, 'recordsManagement/archivalProfileImportExport', true);
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
            $archivalProfileContents = [];
            $archivalProfilesReferences = [];
            foreach ($archivalProfiles as $key => $archivalProfile) {
                if ($isReset
                    || !$this->sdoFactory->exists('recordsManagement/archivalProfile', array('reference' => $archivalProfile->reference))
                ) {
                    $archiveProfileId = $this->importArchivalProfile($archivalProfile, 'create');
                } else {
                    $archiveProfileId = $this->importArchivalProfile($archivalProfile, 'update');
                }

                if ($archivalProfile->childrenProfiles) {
                    $archivalProfileContents[$archivalProfile->reference] = explode(';', $archivalProfile->childrenProfiles);
                }
                $archivalProfilesReferences[$archivalProfile->reference] = (string) $archiveProfileId;
            }

            foreach ($archivalProfileContents as $parentProfileReference => $containedProfileReferences) {
                foreach ($containedProfileReferences as $containedProfileReference) {
                    $archivalProfileContent = \laabs::newInstance('recordsManagement/archivalProfileContents');
                    $archivalProfileContent->parentProfileId = $archivalProfilesReferences[$parentProfileReference];
                    $archivalProfileContent->containedProfileId = $archivalProfilesReferences[$containedProfileReference];

                    $this->sdoFactory->create($archivalProfileContent, 'recordsManagement/archivalProfileContents');
                }
            }
        } catch (\Exception $e) {
            if ($transactionControl) {
                $this->sdoFactory->rollback();
            }
            throw $e;
        }

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

        return true;
    }

    private function importArchivalProfile($importArchivalProfile, $type) {
        if ($type === 'create') {
            $archivalProfile = \laabs::newInstance('recordsManagement/archivalProfile');
            $archivalProfile->archivalProfileId = \laabs::newId();
        } else {
            $archivalProfile = $this->sdoFactory->find('recordsManagement/archivalProfile', "reference ='".$importArchivalProfile->reference."'")[0];
        }

        $archivalProfile->reference = $importArchivalProfile->reference;
        $archivalProfile->name = $importArchivalProfile->name;
        $archivalProfile->description = $importArchivalProfile->description;
        $archivalProfile->descriptionSchema = $importArchivalProfile->descriptionSchema;
        $archivalProfile->descriptionClass = $importArchivalProfile->descriptionClass;
        $archivalProfile->retentionStartDate = $importArchivalProfile->retentionStartDate;
        $archivalProfile->retentionRuleCode = $importArchivalProfile->retentionRuleCode;
        $archivalProfile->accessRuleCode = $importArchivalProfile->accessRuleCode;
        $archivalProfile->acceptUserIndex = $importArchivalProfile->acceptUserIndex;
        $archivalProfile->acceptArchiveWithoutProfile = $importArchivalProfile->acceptArchiveWithoutProfile;
        $archivalProfile->fileplanLevel = $importArchivalProfile->fileplanLevel;

        if ($type === 'create') {
            $this->sdoFactory->create($archivalProfile, 'recordsManagement/archivalProfile');
        } else {
            $this->sdoFactory->update($archivalProfile, 'recordsManagement/archivalProfile');

            $this->sdoFactory->deleteChildren('recordsManagement/archivalProfileContents', $archivalProfile, 'recordsManagement/archivalProfile');
            $this->sdoFactory->deleteChildren('recordsManagement/archiveDescription', $archivalProfile, 'recordsManagement/archivalProfile');
        }

        if ($importArchivalProfile->archiveDescriptions) {
            $archiveDescriptions = json_decode($importArchivalProfile->archiveDescriptions);
            foreach ($archiveDescriptions as $archiveDescription) {
                $newArchivalDescription = \laabs::newInstance('recordsManagement/archiveDescription');
                $newArchivalDescription->archivalProfileId = (string) $archivalProfile->archivalProfileId;
                $newArchivalDescription->fieldName = $archiveDescription->fieldName;
                $newArchivalDescription->required = $archiveDescription->required;
                $newArchivalDescription->position = $archiveDescription->position;
                $newArchivalDescription->isImmutable = $archiveDescription->isImmutable;
                $newArchivalDescription->isRetained = $archiveDescription->isRetained;
                $newArchivalDescription->isInList = $archiveDescription->isInList;

                $this->sdoFactory->create($newArchivalDescription,'recordsManagement/archiveDescription');
            }
        }
            
        return $archivalProfile->archivalProfileId;
    }
Prosper De Laure's avatar
Prosper De Laure committed
635
}