From bfecd191eadf01e4bbf100f1032e2b07461b3153 Mon Sep 17 00:00:00 2001
From: pbarroca <pbarroca@afi-sa.fr>
Date: Fri, 2 Mar 2018 17:22:33 +0100
Subject: [PATCH] dev #17352 : record history with generic versions

---
 .../php/classes/classe_notice_integration.php |  24 ++--
 .../Cosmogramme/Integration/PhaseNotice.php   |   6 -
 .../Cosmogramme/Integration/RawRecord.php     |  59 +++++++++
 .../Cosmogramme/Integration/RecordHistory.php | 120 ------------------
 .../Version/FilePersistence/RawRecord.php     |  43 +++++++
 library/Class/Versions/RawRecord.php          |  41 ++++++
 .../ZendAfi/View/Helper/Notice/Unimarc.php    |   2 +-
 .../RawRecordHistory/RawRecordVersionTest.php |  43 +++++++
 .../RawRecordHistory/audiard_herosdiscret.uni |   1 +
 9 files changed, 196 insertions(+), 143 deletions(-)
 create mode 100644 library/Class/Cosmogramme/Integration/RawRecord.php
 delete mode 100644 library/Class/Cosmogramme/Integration/RecordHistory.php
 create mode 100644 library/Class/Version/FilePersistence/RawRecord.php
 create mode 100644 library/Class/Versions/RawRecord.php
 create mode 100644 tests/scenarios/RawRecordHistory/RawRecordVersionTest.php
 create mode 100644 tests/scenarios/RawRecordHistory/audiard_herosdiscret.uni

diff --git a/cosmogramme/php/classes/classe_notice_integration.php b/cosmogramme/php/classes/classe_notice_integration.php
index efa41ccbb55..ac0c46bd383 100644
--- a/cosmogramme/php/classes/classe_notice_integration.php
+++ b/cosmogramme/php/classes/classe_notice_integration.php
@@ -75,7 +75,7 @@ class notice_integration {
 
   protected $_codif_provider;
 
-  protected $_record_history;
+  protected $_raw_data;
 
 
   public function __construct() {
@@ -149,6 +149,8 @@ class notice_integration {
   public function traiteNotice($data) {
     global $sql;
 
+    $this->_raw_data = $data;
+
     $id_bib = $this->id_bib;
 
     if(!$this->analyseur)
@@ -493,9 +495,6 @@ class notice_integration {
     $notice = Class_Notice::newInstance($this->noticeToDBEnreg());
     $notice->save();
 
-    if ($this->_record_history)
-      $this->_record_history->setRecord($notice);
-
     $this->statut = static::RECORD_INSERT;
     return $notice->getId();
   }
@@ -639,9 +638,6 @@ class notice_integration {
     if ($this->statut == static::RECORD_UPDATE)
       return $id_notice;
 
-    if ($this->_record_history)
-      $this->_record_history->setRecord($existing_notice);
-
     return $this->_realUpdate($existing_notice);
   }
 
@@ -753,12 +749,14 @@ class notice_integration {
     foreach($exemplaires as $exemplaire)
       $exemplaire->save();
 
-    if ($this->_record_history)
-      $this->_record_history->setItems($exemplaires);
-
     if($record = Class_Notice::find($id_notice))
       $record->setDateMaj(dateDuJour(2))->save();
 
+    (new Class_Cosmogramme_Integration_RawRecord($this->_raw_data,
+                                                 $this->notice['id_origine'],
+                                                 $this->id_int_bib))
+      ->save();
+
     Class_Exemplaire::clearCache();
     Class_Notice::clearCache();
   }
@@ -1401,12 +1399,6 @@ class notice_integration {
     $this->_codif_provider = $provider;
     return $this;
   }
-
-
-  public function setRecordHistory($history) {
-    $this->_record_history = $history;
-    return $this;
-  }
 }
 
 
diff --git a/library/Class/Cosmogramme/Integration/PhaseNotice.php b/library/Class/Cosmogramme/Integration/PhaseNotice.php
index 8d282aef1b3..df330376e27 100644
--- a/library/Class/Cosmogramme/Integration/PhaseNotice.php
+++ b/library/Class/Cosmogramme/Integration/PhaseNotice.php
@@ -47,13 +47,7 @@ class Class_Cosmogramme_Integration_PhaseNotice
                                       $integration->getProfil(),
                                       $integration->getTypeDoc());
     $integrator->setServiceRunner(static::$_service_runner);
-
-    $history = new Class_Cosmogramme_Integration_RecordHistory($data, $integration);
-    $integrator->setRecordHistory($history);
-
     $integrator->traiteNotice($data);
-    $history->save();
-
     $this->_incrementCount($integrator->getStatut());
   }
 
diff --git a/library/Class/Cosmogramme/Integration/RawRecord.php b/library/Class/Cosmogramme/Integration/RawRecord.php
new file mode 100644
index 00000000000..86650811ef2
--- /dev/null
+++ b/library/Class/Cosmogramme/Integration/RawRecord.php
@@ -0,0 +1,59 @@
+<?php
+/**
+ * Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH 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 AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with BOKEH; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+
+class Class_Cosmogramme_Integration_RawRecord {
+  use Trait_Versionable;
+
+  protected
+    $_library_id,
+    $_data,
+    $_id;
+
+  public static function hasVersionFor($item) {
+    return (new static('', $item->getIdOrigine(), $item->getIdIntBib()))
+      ->hasVersion();
+  }
+
+
+  public function __construct($data, $id, $library_id) {
+    $this->_library_id = $library_id;
+    $this->_data = $data;
+    $this->_id = $id;
+  }
+
+
+  public function getId() {
+    return $this->_id;
+  }
+
+
+  public function getLibraryId() {
+    return $this->_library_id;
+  }
+
+
+  public function save() {
+    $this->newVersionWith(['id' => $this->_id,
+                           'library_id' => $this->_library_id,
+                           'data' => $this->_data]);
+  }
+}
diff --git a/library/Class/Cosmogramme/Integration/RecordHistory.php b/library/Class/Cosmogramme/Integration/RecordHistory.php
deleted file mode 100644
index cf523642ae7..00000000000
--- a/library/Class/Cosmogramme/Integration/RecordHistory.php
+++ /dev/null
@@ -1,120 +0,0 @@
-<?php
-/**
- * Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
- *
- * BOKEH is free software; you can redistribute it and/or modify
- * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
- * the Free Software Foundation.
- *
- * There are special exceptions to the terms and conditions of the AGPL as it
- * is applied to this software (see README file).
- *
- * BOKEH 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 AFFERO GENERAL PUBLIC LICENSE for more details.
- *
- * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
- * along with BOKEH; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
- */
-
-
-class Class_Cosmogramme_Integration_RecordHistory {
-  use Trait_StaticFileSystem, Trait_TimeSource;
-
-  const BASE_PATH = 'records';
-
-  protected
-    $_data,
-    $_folder_manager,
-    $_library_id,
-    $_record_id,
-    $_item_ids = [];
-
-
-  public static function hasHistory($item) {
-    if (!$id = $item->getId())
-      return false;
-
-    if (!$id_bib = $item->getIdIntBib())
-      return false;
-
-    return (new static('', new Class_Entity(['IdBib' => $id_bib])))
-      ->itemHasHistory($id);
-  }
-
-
-
-  public function __construct($data, $integration) {
-    $this->_data = $data;
-    $this->_library_id = $integration->getIdBib();
-  }
-
-
-  public function itemHasHistory($id) {
-    if (!$path = $this->_getPathOf($id))
-      return false;
-
-    return $this->getFileSystem()->file_exists($path);
-  }
-
-
-  public function setRecord($record) {
-    $this->_record_id = $record->getId();
-    return $this;
-  }
-
-
-  public function setItems($items) {
-    $this->_item_ids = array_map(function($item) { return $item->getId(); },
-                                 $items);
-    return $this;
-  }
-
-
-  public function save() {
-    foreach($this->_item_ids as $item_id)
-      $this->_saveForItem($item_id);
-
-    return $this;
-  }
-
-
-  protected function _saveForItem($id) {
-    if (!$id)
-      return $this;
-
-    if (!$path = $this->_getPathOf($id))
-      return $this;
-
-    $this->getFileSystem()
-         ->file_put_contents($path, $this->_data);
-
-    return $this;
-  }
-
-
-  protected function _getPathOf($id) {
-    if (!$this->getFolderManager()
-        ->ensure($folder = $this->_getFolderPath()))
-      return;
-
-    return $folder . '/'. $id;
-  }
-
-
-  protected function _getFolderPath() {
-    return PATH_TEMP . implode('/', [static::BASE_PATH,
-                                     $this->_library_id,
-                                     $this->_record_id]);
-  }
-
-
-  public function getFolderManager() {
-    if (null === $this->_folder_manager)
-      $this->_folder_manager = Class_Folder_Manager::newInstanceLimitedTo(PATH_TEMP);
-
-    return $this->_folder_manager;
-  }
-}
diff --git a/library/Class/Version/FilePersistence/RawRecord.php b/library/Class/Version/FilePersistence/RawRecord.php
new file mode 100644
index 00000000000..c1eb7d049da
--- /dev/null
+++ b/library/Class/Version/FilePersistence/RawRecord.php
@@ -0,0 +1,43 @@
+<?php
+/**
+ * Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH 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 AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with BOKEH; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+
+class Class_Version_FilePersistence_RawRecord extends Class_Version_FilePersistence {
+  protected function _getMaxStack() {
+    return 1;
+  }
+
+
+  protected function _getFolderPath($versions) {
+    return $this->_getRootPath()
+      . implode('/', [static::BASE_PATH,
+                      $this->_sanitize($versions->getName()),
+                      $this->_sanitize($versions->getLibraryId()),
+                      $this->_sanitize($versions->getKey())]);
+  }
+
+
+  protected function _fileName() {
+    return
+      $this->getTimeSource()->dateFormat('Y-m-d_His')
+      . '.json';
+  }
+}
diff --git a/library/Class/Versions/RawRecord.php b/library/Class/Versions/RawRecord.php
new file mode 100644
index 00000000000..b09950195f1
--- /dev/null
+++ b/library/Class/Versions/RawRecord.php
@@ -0,0 +1,41 @@
+<?php
+/**
+ * Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH 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 AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with BOKEH; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+
+class Class_Versions_RawRecord extends Class_Versions {
+  protected static
+    $_file_persistence = 'Class_Version_FilePersistence_RawRecord';
+
+  protected
+    $_name = 'raw_record',
+    $_library_id;
+
+
+  public function __construct($model) {
+    parent::__construct($model);
+    $this->_library_id = $model->getLibraryId();
+  }
+
+
+  public function getLibraryId() {
+    return $this->_library_id;
+  }
+}
diff --git a/library/ZendAfi/View/Helper/Notice/Unimarc.php b/library/ZendAfi/View/Helper/Notice/Unimarc.php
index 0e6b6123194..d0fc9699269 100644
--- a/library/ZendAfi/View/Helper/Notice/Unimarc.php
+++ b/library/ZendAfi/View/Helper/Notice/Unimarc.php
@@ -142,7 +142,7 @@ class ZendAfi_View_Helper_Notice_Unimarc extends Zend_View_Helper_HtmlElement {
 
 
   protected function renderItemDownload($item) {
-    if (!Class_Cosmogramme_Integration_RecordHistory::hasHistory($item))
+    if (!Class_Cosmogramme_Integration_RawRecord::hasVersionFor($item))
       return '';
 
     $download_url = $this->view->url(['controller' => 'recherche',
diff --git a/tests/scenarios/RawRecordHistory/RawRecordVersionTest.php b/tests/scenarios/RawRecordHistory/RawRecordVersionTest.php
new file mode 100644
index 00000000000..5475cc61236
--- /dev/null
+++ b/tests/scenarios/RawRecordHistory/RawRecordVersionTest.php
@@ -0,0 +1,43 @@
+<?php
+/**
+ * Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH 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 AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with BOKEH; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+
+class RawRecordUnimarcVersionTest extends ModelTestCase {
+  protected
+    $_storm_default_to_volatile = true,
+    $_record;
+
+  public function setUp() {
+    parent::setUp();
+    Class_Versions_RawRecord::defaultToFile();
+
+    $marc = file_get_contents(__DIR__ . '/audiard_herosdiscret.uni');
+
+    $this->_record = new Class_Cosmogramme_Integration_RawRecord($marc, '694479', 12);
+    $this->_record->save();
+  }
+
+
+  /** @test */
+  public function shouldHaveCreatedOneVersion() {
+    $this->assertEquals(1, $this->_record->numberOfVersions());
+  }
+}
diff --git a/tests/scenarios/RawRecordHistory/audiard_herosdiscret.uni b/tests/scenarios/RawRecordHistory/audiard_herosdiscret.uni
new file mode 100644
index 00000000000..602bdcce715
--- /dev/null
+++ b/tests/scenarios/RawRecordHistory/audiard_herosdiscret.uni
@@ -0,0 +1 @@
+01518ngm0 2200349   450 001000700000071002700007073001800034100001300052200004500065210001900110215001600129300013500145345001800280606002200298686001300320686004100333686003500374700007300409702005300482702005200535702004800587702004500635702004900680702005300729702003400782702003300816702003800849801001700887932001000904992012500914995012901039694479  a826030-9bStudio Canal  a5050582603095  a201411061 aUn héros très discretfJacques Audiard  cAliceleod1996  a1 DVD, 1h41  aD'après le roman de Jean-François Deniau. - Bonus : interview du réalisateur et Alain Le Henry. Scènes commentées. Making of.  b826030-9cDVD  acinéma français  aF2PCDM4  aFictiontFiction2Code_Statistique_1  aFilmtFilm2Code_Statistique_2 1aAudiardbJacques31000251106g3006Metteur en scène ou réalisateur 1aDesplatbAlexandre3100025081142306Compositeur 1aDeniaubJean-François3100013496740706Auteur 1aKassovitzbMathieu3100021986940056Acteur 1aGrinbergbAnouk3130350006040056Acteur 1aKiberlainbSandrine310003627044005gActeur 1aTrintignantbJean-Louis3100022982340056Acteur 1aDupontelbAlbert40056Acteur 1aBarentinbNadia40056Acteur 1aLe HenrybAlain40006Intervenant 2aFrc20150829  aDrame  uhttp://ecx.images-amazon.com/images/I/51%2BE27GCzjL._SL160_.jpgvhttp://ecx.images-amazon.com/images/I/51%2BE27GCzjL.jpg  aMédiathèquef31879003220456kF AUDm20150829qdrgfopv12[DISPO][Disponible][0][1][][0][0][0][0]42015-02-1367778197
\ No newline at end of file
-- 
GitLab