diff --git a/app/models/plan.rb b/app/models/plan.rb index 2684ddc..3be6e6b 100644 --- a/app/models/plan.rb +++ b/app/models/plan.rb @@ -40,6 +40,9 @@ include ValidationValues prepend Dmpopidor::Models::Plan + after_create :create_plan_fragments + after_update :update_plan_fragments + # ============= # = Constants = # ============= diff --git a/app/models/research_output.rb b/app/models/research_output.rb index 6a0ff05..b5d3f6d 100644 --- a/app/models/research_output.rb +++ b/app/models/research_output.rb @@ -22,6 +22,8 @@ class ResearchOutput < ActiveRecord::Base include ValidationMessages + + after_save :create_or_update_fragments # ================ # = Associations = @@ -77,6 +79,36 @@ self.answers.select { |answer| answer.question_id.in?(Section.find(section_id).questions.pluck(:id)) } end + def json_fragment + Fragment::ResearchOutput.where("(data->>'research_output_id')::int = ?", id).first + end + + def create_or_update_fragments + fragment = self.json_fragment() + dmp_fragment = self.plan.json_fragment() + if fragment.nil? + fragment = Fragment::ResearchOutput.create( + data: { + "research_output_id" => self.id, + "title" => self.abbreviation, + "description" => self.fullname, + "type" => self.type.label + }, + dmp_id: dmp_fragment.id, + parent_id: dmp_fragment.id + ) + else + fragment.update( + data: { + "research_output_id" => self.id, + "title" => self.abbreviation, + "description" => self.fullname, + "type" => self.type.label + } + ) + end + end + ## # deep copy the given research output # @@ -84,5 +116,6 @@ def self.deep_copy(research_output) research_output.dup end + end diff --git a/lib/dmpopidor/models/plan.rb b/lib/dmpopidor/models/plan.rb index 57eac1a..291f51e 100644 --- a/lib/dmpopidor/models/plan.rb +++ b/lib/dmpopidor/models/plan.rb @@ -48,6 +48,75 @@ def num_research_outputs research_outputs.count end + + + def json_fragment + Fragment::Dmp.where("(data->>'plan_id')::int = ?", id).first + end + + def create_project_json + { + "title" => self.title, + "grantId" => { + "value" => self.grant_number ? self.grant_number : "" + }, + "principalInvestigator" => { + "dbId" => json_fragment().project.principalInvestigator.id + } + } + end + + def create_meta_json + { + "creationDate" => self.created_at, + "lastModifiedDate" => self.updated_at + } + end + + + private + def create_plan_fragments + research_output = self.research_outputs.first + + dmp_fragment = Fragment::Dmp.create( + data: { + "plan_id" => self.id + } + ) + principal_investigator_fragment = Fragment::Person.create( + data: { + "lastName" => self.principal_investigator ? self.principal_investigator : "", + "firstName" => "", + "mbox" => self.principal_investigator_email ? self.principal_investigator_email : "", + "personId" => self.principal_investigator_identifier ? self.principal_investigator_identifier : "", + }, + dmp_id: dmp_fragment.id + ) + + project_fragment = Fragment::Project.create( + data: create_project_json(), + dmp_id: dmp_fragment.id, + parent_id: dmp_fragment.id + ) + + meta_fragment = Fragment::Meta.create( + data: create_meta_json(), + dmp_id: dmp_fragment.id, + parent_id: dmp_fragment.id + ) + + end + + def update_plan_fragments + dmp_fragment = self.json_fragment() + + dmp_fragment.meta.update( + data: create_meta_json() + ) + dmp_fragment.project.update( + data: create_project_json() + ) + end end end end \ No newline at end of file diff --git a/spec/factories/structured_answers.rb b/spec/factories/structured_answers.rb index a7e4796..db775a5 100644 --- a/spec/factories/structured_answers.rb +++ b/spec/factories/structured_answers.rb @@ -9,6 +9,8 @@ # created_at :datetime not null # updated_at :datetime not null # classname :string +# dmp_id :integer +# parent_id :integer # # Indexes #