From ea36da7ea7acfa52c75bffb7017fd36963b153a3 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Fri, 17 Feb 2023 22:32:47 +0100
Subject: [PATCH] authors wip

---
 .../admin/research/researchers_controller.rb  |  2 +-
 app/models/research/hal/author.rb             | 17 +++++
 app/models/research/hal/publication.rb        |  6 +-
 app/models/university/person/with_research.rb |  5 --
 .../admin/research/researchers/show.html.erb  | 75 ++++++++++---------
 config/locales/research/en.yml                | 12 ++-
 config/locales/research/fr.yml                | 11 ++-
 config/routes/admin/research.rb               |  5 ++
 ...30217205953_create_research_hal_authors.rb | 24 ++++++
 db/schema.rb                                  | 27 ++++++-
 test/fixtures/research/hal/authors.yml        | 30 ++++++++
 test/models/research/hal/author_test.rb       | 21 ++++++
 12 files changed, 182 insertions(+), 53 deletions(-)
 create mode 100644 app/models/research/hal/author.rb
 create mode 100644 db/migrate/20230217205953_create_research_hal_authors.rb
 create mode 100644 test/fixtures/research/hal/authors.yml
 create mode 100644 test/models/research/hal/author_test.rb

diff --git a/app/controllers/admin/research/researchers_controller.rb b/app/controllers/admin/research/researchers_controller.rb
index 9a2bb07e4..4ccb65cd1 100644
--- a/app/controllers/admin/research/researchers_controller.rb
+++ b/app/controllers/admin/research/researchers_controller.rb
@@ -15,7 +15,7 @@ class Admin::Research::ResearchersController < Admin::Research::ApplicationContr
 
   def show
     @papers = @researcher.research_journal_papers.ordered.page(params[:page])
-    @possible_hal_authors = @researcher.possible_hal_authors unless @researcher.hal_identity?
+    @possible_hal_authors = @researcher.possible_hal_authors
     @papers = @researcher.research_journal_papers.ordered.page(params[:page])
     breadcrumb
     add_breadcrumb @researcher
diff --git a/app/models/research/hal/author.rb b/app/models/research/hal/author.rb
new file mode 100644
index 000000000..094f188d2
--- /dev/null
+++ b/app/models/research/hal/author.rb
@@ -0,0 +1,17 @@
+# == Schema Information
+#
+# Table name: research_hal_authors
+#
+#  id                :uuid             not null, primary key
+#  doc_identifier    :string
+#  first_name        :string
+#  form_identifier   :string
+#  full_name         :string
+#  last_name         :string
+#  person_identifier :string
+#  created_at        :datetime         not null
+#  updated_at        :datetime         not null
+#
+class Research::Hal::Author < ApplicationRecord
+  has_and_belongs_to_many :publications
+end
diff --git a/app/models/research/hal/publication.rb b/app/models/research/hal/publication.rb
index 302b61872..2e82ebae6 100644
--- a/app/models/research/hal/publication.rb
+++ b/app/models/research/hal/publication.rb
@@ -25,11 +25,7 @@ class Research::Hal::Publication < ApplicationRecord
 
   DOI_PREFIX = 'http://dx.doi.org/'.freeze
 
-  has_and_belongs_to_many :research_people,
-                          class_name: 'University::Person', 
-                          foreign_key: 'university_person_id',
-                          association_foreign_key: 'research_publication_id'
-  alias :researchers :research_people
+  has_and_belongs_to_many :publications
 
   before_destroy { research_people.clear }
 
diff --git a/app/models/university/person/with_research.rb b/app/models/university/person/with_research.rb
index febdd4246..ade03dda8 100644
--- a/app/models/university/person/with_research.rb
+++ b/app/models/university/person/with_research.rb
@@ -12,12 +12,7 @@ module University::Person::WithResearch
     scope :with_hal_identifier, -> { where.not(hal_form_identifier: [nil,'']) }
   end
 
-  def hal_identity?
-    hal_form_identifier.present?
-  end
-
   def import_research_hal_publications!
-    return unless hal_identity?
     response = HalOpenscience::Document.search  "authIdForm_i:#{hal_form_identifier}",
                                                 fields: ["*"],
                                                 limit: 1000
diff --git a/app/views/admin/research/researchers/show.html.erb b/app/views/admin/research/researchers/show.html.erb
index dcbd2f426..c6deccf68 100644
--- a/app/views/admin/research/researchers/show.html.erb
+++ b/app/views/admin/research/researchers/show.html.erb
@@ -9,43 +9,44 @@
   <% end %>
 <% end %>
 
-<%= osuny_panel Research::Hal::Publication.model_name.human(count: 2), action: @researcher.hal_publications.count do %>
-  <% if @researcher.hal_identity? %>
-    <div class="table-responsive">
-      <%= render 'admin/research/hal/publications/list', publications: @researcher.hal_publications.ordered %>
-    </div>
-  <% else %>
-    <p><%= t 'research.hal.select_identifier' %><p>
-    <div class="table-responsive">
-      <table class="table">
-        <tbody>
-          <% @possible_hal_authors.each do |author| %>
-            <%
-            path = admin_research_researcher_path @researcher, 
-                                                  hal_doc_identifier: author.docid,
-                                                  hal_form_identifier: author.attributes['form_i'],
-                                                  hal_person_identifier: author.attributes['person_i']
-            %>
-            <tr>
-              <td width="300">
-                Identifiant HAL : <%= author.docid %><br>
-                <small><%= author.fullName_s %></small>
-              </td>
-              <td class="ps-3" width="200"><%= link_to 'Choisir cet identifiant', path, method: :put, class: 'btn btn-primary btn-sm' %><td>
-              <td class="ps-3">
-                <% docs = HalOpenscience::Document.search "authIdFormPerson_s:#{author.docid}", fields: ["*"], limit: 5 %>
-                <ul class="list-unstyled small">
-                  <% docs.results.each do |doc| %>
-                    <li class="mb-2"><%= sanitize doc.citationFull_s %></li>
-                  <% end unless docs.nil? %>
-                </ul>
-              </td>
-            </tr>
-          <% end %>
-        </tbody>
-      </table>
-    </div>
-  <% end %>
+<%= osuny_panel Research::Hal::Author.model_name.human(count: 2), action: @researcher.hal_publications.count do %>
+  <p><%= t 'research.hal.select_identifier' %><p>
+  <div class="table-responsive">
+    <table class="table">
+      <tbody>
+        <% @possible_hal_authors.each do |author| %>
+          <%
+          path = admin_research_researcher_path @researcher, 
+                                                hal_doc_identifier: author.docid,
+                                                hal_form_identifier: author.attributes['form_i'],
+                                                hal_person_identifier: author.attributes['person_i']
+          %>
+          <tr>
+            <td width="300">
+              Identifiant HAL : <%= author.docid %><br>
+              <small><%= author.fullName_s %></small>
+            </td>
+            <td class="ps-3" width="200"><%= link_to 'Choisir cet identifiant', path, method: :put, class: 'btn btn-primary btn-sm' %><td>
+            <td class="ps-3">
+              <% docs = HalOpenscience::Document.search "authIdFormPerson_s:#{author.docid}", fields: ["*"], limit: 5 %>
+              <ul class="list-unstyled small">
+                <% docs.results.each do |doc| %>
+                  <li class="mb-2"><%= sanitize doc.citationFull_s %></li>
+                <% end unless docs.nil? %>
+              </ul>
+            </td>
+          </tr>
+        <% end %>
+      </tbody>
+    </table>
+  </div>
+<% end %>
+
+<%= osuny_panel Research::Hal::Publication.model_name.human do %>
+  <p><%= "#{@researcher.hal_publications.count} #{Research::Hal::Publication.model_name.human(count: @researcher.hal_publications.count).downcase}" %> 
+  <div class="table-responsive">
+    <%= render 'admin/research/hal/publications/list', publications: @researcher.hal_publications.ordered %>
+  </div>
 <% end %>
 
 <% content_for :action_bar_right do %>
diff --git a/config/locales/research/en.yml b/config/locales/research/en.yml
index a6805e9c0..0aac0744d 100644
--- a/config/locales/research/en.yml
+++ b/config/locales/research/en.yml
@@ -2,11 +2,15 @@ en:
   activemodel:
     models:
       research: Research
+      research/hal: HAL
   activerecord:
     models:
       university/person/researcher:
         one: Researcher
         other: Researchers
+      research/hal/author:
+        one: Author
+        other: Authors
       research/hal/publication:
         one: Publication
         other: Publications
@@ -32,6 +36,10 @@ en:
         one: Thesis
         other: Theses
     attributes:
+      research/hal/author:
+        first_name: First name
+        last_name: Last name
+        full_name: Full name
       research/hal/publication:
         description: Publications scientifiques importées automatiquement de HAL
         docid: Identifier
@@ -93,8 +101,8 @@ en:
       source: Serge Haroche, Prix Nobel de physique 2012
     hal:
       title: HAL
-      select_identifier: You have no HAL identifier yet. Is it one of these possibilities? If there are several, please choose the most complete one (without -0 at the end).
-    manage_researchers: Managege researchers
+      select_identifier: Select all the HAL authors that correspond to this researcher. Use the publications to be sure it's the same person.
+    manage_researchers: Manage researchers
     researchers:
       one: Researcher
       other: Researchers
diff --git a/config/locales/research/fr.yml b/config/locales/research/fr.yml
index f2eb4e9f3..0cde7ec46 100644
--- a/config/locales/research/fr.yml
+++ b/config/locales/research/fr.yml
@@ -2,11 +2,15 @@ fr:
   activemodel:
     models:
       research: Recherche
+      research/hal: HAL
   activerecord:
     models:
       university/person/researcher:
         one: Chercheur·e
         other: Chercheur·e·s
+      research/hal/author:
+        one: Auteur·e
+        other: Auteur·e·s
       research/hal/publication:
         one: Publication
         other: Publications
@@ -32,6 +36,10 @@ fr:
         one: Thèse
         other: Thèses
     attributes:
+      research/hal/author:
+        first_name: Prénom
+        last_name: Nom
+        full_name: Nom complet
       research/hal/publication:
         description: Publications scientifiques importées automatiquement de HAL
         docid: Identifiant
@@ -92,8 +100,7 @@ fr:
       text: La recherche scientifique est, à la fois, une démarche créatrice de connaissances motivée par la curiosité pure et une activité génératrice d’innovations qui augmentent les moyens d’action et de diagnostic sur la nature, l’homme et la société. Ces deux aspects de la recherche, le fondamental et l’appliqué, loin de s’opposer, sont complémentaires l’un de l’autre. La recherche fondamentale crée le socle de connaissances à partir duquel naissent les applications et, inversement, les avancées technologiques procurent les outils d’investigation de plus en plus perfectionnés qui conduisent à approfondir nos connaissances fondamentales.
       source: Serge Haroche, Prix Nobel de physique 2012
     hal:
-      title: HAL
-      select_identifier: Vous n'avez pas d'identifiant HAL. Est-ce l'une des possibilités suivantes ? S'il y a plusieurs possibilités, choisissez l'identifiant le plus complet (sans 0 à la fin).
+      select_identifier: Choisissez tou·te·s les auteur·e·s HAL correspondant à cette personne. Appuyez-vous sur les publications pour vérifier qu'il s'agit de la bonne personne.
     manage_researchers: Gérer les chercheur·e·s
     researchers:
       one: Chercheur·e
diff --git a/config/routes/admin/research.rb b/config/routes/admin/research.rb
index 9bf9266f4..7792d706e 100644
--- a/config/routes/admin/research.rb
+++ b/config/routes/admin/research.rb
@@ -1,6 +1,11 @@
 namespace :research do
   resources :researchers, only: [:index, :show, :update]
   namespace :hal do
+    resources :authors, only: [:index, :show, :destroy] do
+      member do
+        get :static
+      end
+    end
     resources :publications, only: [:index, :show, :destroy] do
       member do
         get :static
diff --git a/db/migrate/20230217205953_create_research_hal_authors.rb b/db/migrate/20230217205953_create_research_hal_authors.rb
new file mode 100644
index 000000000..c182ed216
--- /dev/null
+++ b/db/migrate/20230217205953_create_research_hal_authors.rb
@@ -0,0 +1,24 @@
+class CreateResearchHalAuthors < ActiveRecord::Migration[7.0]
+  def change
+    create_table :research_hal_authors, id: :uuid do |t|
+      t.string :doc_identifier
+      t.string :form_identifier
+      t.string :person_identifier
+      t.string :first_name
+      t.string :last_name
+      t.string :full_name
+
+      t.timestamps
+    end
+
+    create_join_table :research_hal_authors, :research_hal_publications, column_options: {type: :uuid} do |t|
+      t.index [:research_hal_author_id, :research_hal_publication_id], name: 'hal_author_publication'
+      t.index [:research_hal_publication_id, :research_hal_author_id], name: 'hal_publication_author'
+    end
+
+    create_join_table :research_hal_authors, :university_persons, column_options: {type: :uuid} do |t|
+      t.index [:research_hal_author_id, :university_person_id], name: 'hal_author_person'
+      t.index [:university_person_id, :research_hal_author_id], name: 'hal_person_author'
+    end
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index be3d3b1ef..018e3421e 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema[7.0].define(version: 2023_02_17_202815) do
+ActiveRecord::Schema[7.0].define(version: 2023_02_17_205953) do
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
   enable_extension "plpgsql"
@@ -592,6 +592,31 @@ ActiveRecord::Schema[7.0].define(version: 2023_02_17_202815) do
     t.string "summernote_locale"
   end
 
+  create_table "research_hal_authors", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
+    t.string "doc_identifier"
+    t.string "form_identifier"
+    t.string "person_identifier"
+    t.string "first_name"
+    t.string "last_name"
+    t.string "full_name"
+    t.datetime "created_at", null: false
+    t.datetime "updated_at", null: false
+  end
+
+  create_table "research_hal_authors_publications", id: false, force: :cascade do |t|
+    t.uuid "research_hal_author_id", null: false
+    t.uuid "research_hal_publication_id", null: false
+    t.index ["research_hal_author_id", "research_hal_publication_id"], name: "hal_author_publication"
+    t.index ["research_hal_publication_id", "research_hal_author_id"], name: "hal_publication_author"
+  end
+
+  create_table "research_hal_authors_university_persons", id: false, force: :cascade do |t|
+    t.uuid "research_hal_author_id", null: false
+    t.uuid "university_person_id", null: false
+    t.index ["research_hal_author_id", "university_person_id"], name: "hal_author_person"
+    t.index ["university_person_id", "research_hal_author_id"], name: "hal_person_author"
+  end
+
   create_table "research_hal_publications", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
     t.string "docid"
     t.jsonb "data"
diff --git a/test/fixtures/research/hal/authors.yml b/test/fixtures/research/hal/authors.yml
new file mode 100644
index 000000000..29f9027fd
--- /dev/null
+++ b/test/fixtures/research/hal/authors.yml
@@ -0,0 +1,30 @@
+# == Schema Information
+#
+# Table name: research_hal_authors
+#
+#  id                :uuid             not null, primary key
+#  doc_identifier    :string
+#  first_name        :string
+#  form_identifier   :string
+#  full_name         :string
+#  last_name         :string
+#  person_identifier :string
+#  created_at        :datetime         not null
+#  updated_at        :datetime         not null
+#
+
+one:
+  doc_identifier: MyString
+  form_identifier: MyString
+  person_identifier: MyString
+  first_name: MyString
+  last_name: MyString
+  full_name: MyString
+
+two:
+  doc_identifier: MyString
+  form_identifier: MyString
+  person_identifier: MyString
+  first_name: MyString
+  last_name: MyString
+  full_name: MyString
diff --git a/test/models/research/hal/author_test.rb b/test/models/research/hal/author_test.rb
new file mode 100644
index 000000000..bda90df48
--- /dev/null
+++ b/test/models/research/hal/author_test.rb
@@ -0,0 +1,21 @@
+# == Schema Information
+#
+# Table name: research_hal_authors
+#
+#  id                :uuid             not null, primary key
+#  doc_identifier    :string
+#  first_name        :string
+#  form_identifier   :string
+#  full_name         :string
+#  last_name         :string
+#  person_identifier :string
+#  created_at        :datetime         not null
+#  updated_at        :datetime         not null
+#
+require "test_helper"
+
+class Research::Hal::AuthorTest < ActiveSupport::TestCase
+  # test "the truth" do
+  #   assert true
+  # end
+end
-- 
GitLab