require "csv" class Doctrine < ApplicationRecord has_many :doctrine_jurisprudences, dependent: :destroy has_many :jurisprudences, class_name: "Cdao::Jurisprudence", through: :doctrine_jurisprudences has_many :doctrine_subjects, dependent: :destroy has_many :subjects, through: :doctrine_subjects has_many :annotations, dependent: :destroy validates :content, presence: true before_validation do self.content_fingerprint = Digest::SHA256.hexdigest(content) end def doctrine_subjects DoctrineSubject.where(doctrine_id: self.id) end def jurisprudences Cdao::Jurisprudence.where(id: doctrine_jurisprudences.map(&:jurisprudence_id)).order(year: :desc, docdate: :desc) end def jurisprudence jurisprudences.first end def subjects Cdao::Subject.where(id: doctrine_subjects.map(&:subject_id)) end def subject_ids subjects.map(&:id) end def subject_ids=(ids) ids = ids.map(&:to_i) ids.each do |subject_id| record = DoctrineSubject.find_or_initialize_by(doctrine_id: self.id, subject_id: subject_id) next if record.persisted? record.save end DoctrineSubject.where(doctrine_id: self.id).each do |record| record.destroy unless ids.member?(record.subject_id) end subject_ids end def plain_content Sanitize.clean(content).strip end def self.parse_csv_file(parent_id, path) ::CSV.foreach(path).each_with_index do |data, index| row_no = index + 1 subject_name = data[0].strip headnote = data[1].strip content = data[2].strip doc_url = data[6].strip next if [headnote, content, doc_url].any?(&:blank?) parent = Cdao::Subject.find(parent_id) subject = parent.descendants.find_or_initialize_by(name: subject_name) if subject.new_record? subject.save subject.library_ids = [27] end logger = Logger.new(Rails.root.join("log/import.doctrines.log")) if subject.blank? logger.info "[#{row_no}] - no subject found" next end subject_id = subject.id doc_id = doc_url.split("?").first.split("/").map(&:strip).last record = self.find_or_initialize_by(headnote: headnote, content: ["
", content, "
"].join) if record.save record.doctrine_subjects.find_or_create_by(subject_id: subject_id) record.doctrine_jurisprudences.find_or_create_by(jurisprudence_id: doc_id) else logger.info "[#{row_no}] - failed to save #{record.errors.inspect}" end end end def self.parse_csv_file_new(parent_id, path) ::CSV.foreach(path).each_with_index do |data, index| row_no = index + 1 subject_name = data[0].strip headnote = "" content = data[1].strip doc_url = data[8].strip # Annotations annomark_name = (data[2] || "").strip editor_note = (data[4] || "").strip phil_rep = "" doc_urls = (data[5] || "").strip.split("citing").map(&:strip) if doc_urls.present? anno_doc_url = doc_urls.first citing_doc_url = doc_urls.count > 1 ? doc_urls.last : "" end next if [content, doc_url].any?(&:blank?) parent = Cdao::Subject.find(parent_id) subject = parent.descendants.find_or_initialize_by(name: subject_name) if subject.new_record? subject.save subject.library_ids = [27] end logger = Logger.new(Rails.root.join("log/import.doctrines.log")) if subject.blank? logger.info "[#{row_no}] - no subject found" next end subject_id = subject.id doc_id = doc_url.split("?").first.split("/").map(&:strip).last if anno_doc_url.present? anno_doc_args = anno_doc_url.split("?").first.split("/").map(&:strip).last(2) anno_doc_type = anno_doc_args.first.classify anno_doc_id = anno_doc_args.last annotation_document = Cdao::Document.find_by(doc_type: anno_doc_type, doc_id: anno_doc_id) end if citing_doc_url.present? citing_doc_args = citing_doc_url.split("?").first.split("/").map(&:strip).last(2) citing_doc_type = citing_doc_args.first.classify citing_doc_id = citing_doc_args.last citing_document = Cdao::Document.find_by(doc_type: citing_doc_type, doc_id: citing_doc_id) end record = self.find_or_initialize_by(headnote: headnote, content: ["
", content, "
"].join) if record.save record.doctrine_subjects.find_or_create_by(subject_id: subject_id) record.doctrine_jurisprudences.find_or_create_by(jurisprudence_id: doc_id) # Annotations phil_rep = "" annomark = Annomark.find_by(name: annomark_name) annotation = record.annotations.new annotation.annomark_ids = [annomark.id] if annomark.present? annotation.document = annotation_document annotation.phil_rep = phil_rep || annotation_document.phil_rep annotation.editor_notes = editor_note if annotation.save annotation.add_document(citing_document) if citing_document.present? end else logger.info "[#{row_no}] - failed to save #{record.errors.inspect}" end end end searchable do text :content integer :id integer :subject_ids, multiple: true integer :user_ids, multiple: true do paper_trail.originator.to_i end integer :search_year do jurisprudence.year.present? && jurisprudence.year > 0 ? jurisprudence.year : (jurisprudence.doc_date.try :year) end date :search_doc_date do jurisprudence.doc_date.presence || Date.new(jurisprudence.year.presence || 0) end date :created_at string :headnote end end