Skip to content
This repository has been archived by the owner on Jan 5, 2019. It is now read-only.

Special report form #197

Merged
merged 16 commits into from
Apr 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/actors/hydranorth/generic_file/actor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ def update_visibility(visibility)
interpret_visibility
end

def create_metadata_with_resource_type(batch_id, resource_type)

if resource_type
generic_file.resource_type = [resource_type]
else
ActiveFedora::Base.logger.warn "unable to find the resource type it sets to"
end

create_metadata(batch_id)
end

end
end
end
55 changes: 54 additions & 1 deletion app/controllers/concerns/hydranorth/batch_controller_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,61 @@ module BatchControllerBehavior
include Sufia::BatchControllerBehavior

included do
class_attribute :edit_form_class
class_attribute :edit_form_class, :cstr_edit_form_class, :ser_edit_form_class
self.edit_form_class = Hydranorth::Forms::BatchEditForm
self.cstr_edit_form_class = Hydranorth::Forms::CstrBatchEditForm
self.ser_edit_form_class = Hydranorth::Forms::SerBatchEditForm
end

def edit
@batch = Batch.find_or_create(params[:id])
@form = edit_form
@form[:resource_type] = @batch.generic_files.map(&:resource_type).flatten
end


def update
authenticate_user!
@batch = Batch.find_or_create(params[:id])
@batch.status = ["processing"]
@batch.save
resource_type = @batch.generic_files.map(&:resource_type).flatten
if resource_type.include? Sufia.config.special_reports['cstr']
@collection = Collection.find(Sufia.config.cstr_collection_id)
add_to_collection
elsif resource_type.include? Sufia.config.special_reports['ser']
@collection = Collection.find(Sufia.config.ser_collection_id)
add_to_collection
end
file_attributes = Hydranorth::Forms::BatchEditForm.model_attributes(params[:generic_file])
Sufia.queue.push(BatchUpdateJob.new(current_user.user_key, params[:id], params[:title], params[:trid], params[:ser], file_attributes, params[:visibility]))
flash[:notice] = 'Your files are being processed by ' + t('sufia.product_name') + ' in the background. The metadata and access controls you specified are being applied. Files will be marked <span class="label label-danger" title="Private">Private</span> until this process is complete (shouldn\'t take too long, hang in there!). You may need to refresh your dashboard to see these updates.'
if uploading_on_behalf_of? @batch
redirect_to sufia.dashboard_shares_path
else
redirect_to sufia.dashboard_files_path
end
end

protected
def add_to_collection
@batch.generic_files.each do |gf|
@collection.member_ids = @collection.member_ids.push(gf.id)
@collection.save
end

end
def edit_form
generic_file = ::GenericFile.new(creator: [current_user.name], title: @batch.generic_files.map(&:label))
resource_type = @batch.generic_files.map(&:resource_type).flatten
if resource_type.include? Sufia.config.special_reports['cstr']
cstr_edit_form_class.new(generic_file)
elsif resource_type.include? Sufia.config.special_reports['ser']
ser_edit_form_class.new(generic_file)
else
edit_form_class.new(generic_file)
end

end


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ def redirect_to_return_controller
redirect_to sufia.dashboard_index_path
end
end
protected
def terms
Hydranorth::Forms::BatchEditForm.terms
end
def generic_file_params
Hydranorth::Forms::BatchEditForm.model_attributes(params[:generic_file])
end

end
end
51 changes: 51 additions & 0 deletions app/controllers/concerns/hydranorth/files_controller_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,58 @@ def attributes
attributes = params
end

def presenter

if @generic_file[:resource_type].include? Sufia.config.special_reports['cstr']
Hydranorth::CstrPresenter.new(@generic_file)
elsif @generic_file[:resource_type].include? Sufia.config.special_reports['ser']
Hydranorth::SerPresenter.new(@generic_file)
else
Hydranorth::GenericFilePresenter.new(@generic_file)
end
end

def edit_form

if @generic_file[:resource_type].include? Sufia.config.special_reports['cstr']
Hydranorth::Forms::CstrEditForm.new(@generic_file)
elsif @generic_file[:resource_type].include? Sufia.config.special_reports['ser']
Hydranorth::Forms::SerEditForm.new(@generic_file)
else
Hydranorth::Forms::GenericFileEditForm.new(@generic_file)
end
end
def process_file(file)

Batch.find_or_create(params[:batch_id])

update_metadata_from_upload_screen
update_resource_type_from_upload_screen
if params[:resource_type].present?
actor.create_metadata_with_resource_type(params[:batch_id], params[:resource_type])
else
actor.create_metadata(params[:batch_id])
end
if actor.create_content(file, file.original_filename, file_path, file.content_type)
respond_to do |format|
format.html {
render 'jq_upload', formats: 'json', content_type: 'text/html'
}
format.json {
render 'jq_upload'
}
end
else
msg = @generic_file.errors.full_messages.join(', ')
flash[:error] = msg
json_error "Error creating generic file: #{msg}"
end
end
def update_resource_type_from_upload_screen
# Relative path is set by the jquery uploader when uploading a directory
@generic_file.resource_type = [Sufia.config.special_reports['cstr']] if params[:resource_type] == Sufia.config.special_reports['cstr']
@generic_file.resource_type = [Sufia.config.special_reports['ser']] if params[:resource_type] == Sufia.config.special_reports['ser']
end
end

end
6 changes: 6 additions & 0 deletions app/forms/hydranorth/forms/cstr_batch_edit_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Hydranorth
module Forms
class CstrBatchEditForm < CstrEditForm
end
end
end
16 changes: 16 additions & 0 deletions app/forms/hydranorth/forms/cstr_edit_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Hydranorth
module Forms
class CstrEditForm < CstrPresenter
include HydraEditor::Form
self.required_fields = [:title, :creator, :subject, :license, :trid, :language]

# This is required so that fields_for will draw a nested form.
# See ActionView::Helpers#nested_attributes_association?
# https://github.com/rails/rails/blob/a04c0619617118433db6e01b67d5d082eaaa0189/actionview/lib/action_view/helpers/form_helper.rb#L1890
def permissions_attributes= attributes
model.permissions_attributes= attributes
end

end
end
end
6 changes: 6 additions & 0 deletions app/forms/hydranorth/forms/ser_batch_edit_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Hydranorth
module Forms
class SerBatchEditForm < SerEditForm
end
end
end
16 changes: 16 additions & 0 deletions app/forms/hydranorth/forms/ser_edit_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Hydranorth
module Forms
class SerEditForm < SerPresenter
include HydraEditor::Form
self.required_fields = [:title, :creator, :subject, :license, :ser, :language]

# This is required so that fields_for will draw a nested form.
# See ActionView::Helpers#nested_attributes_association?
# https://github.com/rails/rails/blob/a04c0619617118433db6e01b67d5d082eaaa0189/actionview/lib/action_view/helpers/form_helper.rb#L1890
def permissions_attributes= attributes
model.permissions_attributes= attributes
end

end
end
end
80 changes: 80 additions & 0 deletions app/jobs/batch_update_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
class BatchUpdateJob
include Hydra::PermissionsQuery
include Sufia::Messages

def queue_name
:batch_update
end

attr_accessor :login, :title, :trid, :ser, :file_attributes, :batch_id, :visibility, :saved, :denied

def initialize(login, batch_id, title, trid, ser, file_attributes, visibility)
self.login = login
self.title = title || {}
if trid.present?
self.trid = trid
elsif ser.present?
self.ser = ser
end
self.file_attributes = file_attributes
self.visibility = visibility
self.batch_id = batch_id
self.saved = []
self.denied = []
end

def run
batch = Batch.find_or_create(self.batch_id)
user = User.find_by_user_key(self.login)
batch.generic_files.each do |gf|
update_file(gf, user)
end

batch.update(status: ["Complete"])

if denied.empty?
send_user_success_message(user, batch) unless saved.empty?
else
send_user_failure_message(user, batch)
end
end

def update_file(gf, user)
unless user.can? :edit, gf
ActiveFedora::Base.logger.error "User #{user.user_key} DENIED access to #{gf.id}!"
denied << gf
return
end
gf.title = title[gf.id] if title[gf.id]
gf.attributes = file_attributes
if (trid.present? && trid[gf.id])
gf.trid = trid[gf.id]
elsif (ser.present? && ser[gf.id])
gf.ser = ser[gf.id]
end
gf.visibility= visibility
save_tries = 0
begin
gf.save!
rescue RSolr::Error::Http => error
save_tries += 1
ActiveFedora::Base.logger.warn "BatchUpdateJob caught RSOLR error on #{gf.id}: #{error.inspect}"
# fail for good if the tries is greater than 3
raise error if save_tries >=3
sleep 0.01
retry
end #
Sufia.queue.push(ContentUpdateEventJob.new(gf.id, login))
saved << gf
end

def send_user_success_message user, batch
message = saved.count > 1 ? multiple_success(batch.id, saved) : single_success(batch.id, saved.first)
User.batchuser.send_message(user, message, success_subject, sanitize_text = false)
end

def send_user_failure_message user, batch
message = denied.count > 1 ? multiple_failure(batch.id, denied) : single_failure(batch.id, denied.first)
User.batchuser.send_message(user, message, failure_subject, sanitize_text = false)
end
end
18 changes: 18 additions & 0 deletions app/models/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ def Collection.indexer
def processing?
false
end

def self.find_or_create_with_type(resource_type)
cols = []
Collection.all.each do |c|
cols << c if c[:resource_type].include? resource_type
end
begin
case cols.length.to_s
when "1"
cols.first
when "0"
Collection.new(title: resource_type + " Collection", resource_type: [resource_type])
else
raise "More than one #{resource_type} collection exists."
end
end
end

# Compute the sum of each file in the collection
# Don't count anything that is not a file
Expand All @@ -31,4 +48,5 @@ def bytes
gf.respond_to?(:content) ? sum + gf.content.size.to_i : sum
end
end

end
9 changes: 9 additions & 0 deletions app/presenters/hydranorth/cstr_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Hydranorth
class CstrPresenter < GenericFilePresenter
include Hydra::Presenter
self.model_class = ::GenericFile
# Terms is the list of fields displayed by app/views/generic_files/_show_descriptions.html.erb
self.terms = [:resource_type, :title, :trid, :creator, :contributor, :description, :date_created, :license, :subject, :spatial, :temporal, :is_version_of, :source, :related_url, :language]

end
end
9 changes: 9 additions & 0 deletions app/presenters/hydranorth/ser_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Hydranorth
class SerPresenter < GenericFilePresenter
include Hydra::Presenter
self.model_class = ::GenericFile
# Terms is the list of fields displayed by app/views/generic_files/_show_descriptions.html.erb
self.terms = [:resource_type, :title, :ser, :creator, :contributor, :description, :date_created, :license, :subject, :spatial, :temporal, :is_version_of, :source, :related_url, :language]

end
end
13 changes: 12 additions & 1 deletion app/views/batch/_metadata.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
<div id="additional_title_clone">
<%= f.text_field :title, name: "title[#{gen_f.id}][]", value: gen_f.label, class: 'form-control', required: true %>
</div>
<% if @form[:resource_type].include? Sufia.config.special_reports['cstr'] %>
<%= f.input_label :trid, as: :single_field_with_help, label: "Computing Science Technical Report ID #{index + 1}" %>
<%= f.text_field :trid, name: "trid[#{gen_f.id}]", class: 'form-control', required: true %>
<% end %>
<% if @form[:resource_type].include? Sufia.config.special_reports['ser'] %>
<%= f.input_label :ser, as: :single_field_with_help, label: "Structural Engineering Report Number #{index + 1}" %>
<%= f.text_field :ser, name: "ser[#{gen_f.id}]", class: 'form-control', required: true %>
<% end %>
</div>
<% end %>
</div>
Expand All @@ -27,8 +35,11 @@
<div class="row">
<div class="col-sm-6">
<h3>Applies to all files just uploaded</h3>
<%= f.input :resource_type, as: :select_with_help, collection: Sufia.config.resource_types,
<% if (@form[:resource_type].include? Sufia.config.special_reports['cstr']) || (@form[:resource_type].include? Sufia.config.special_reports['ser']) %>
<% else %>
<%= f.input :resource_type, as: :select_with_help, collection: Sufia.config.resource_types,
input_html: { class: 'form-control', multiple: true, required: true } %>
<% end %>
<%= f.input :language, as: :select_with_help, collection: Sufia.config.languages, input_html: { class: 'form-control', required: true} %>
<%= f.input :creator, as: :multi_value_with_help, input_html: { required: true } %>
<%= f.input :subject, as: :multi_value_with_help, input_html: {required: true} %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/batch/_more_metadata.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div id="more_descriptions">
<button id="hide_addl_descriptions" class="btn btn-default" aria-label="hide additional metadata description fields">Hide Additional Fields</button>
<% (f.object.terms - [:title, :creator, :license, :resource_type, :language, :subject]).each do |term| %>
<% (f.object.terms - [:title, :trid, :ser, :creator, :license, :resource_type, :language, :subject]).each do |term| %>
<%# <%= f.input term, as: :multi_value_with_help, input_html: { required: false} %>
<%= render_edit_field_partial(term, f: f) %>
<% end %>
Expand Down
12 changes: 12 additions & 0 deletions app/views/generic_files/upload/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%= form_for(@generic_file, url: sufia.generic_files_path, html: { multipart: true, id: 'fileupload' }) do |f| %>
<% unless current_user.can_make_deposits_for.empty? %>
<div class="controls">
<%= label_tag :on_behalf_of, 'On Behalf of' %>
<%= select_tag :on_behalf_of, options_for_select(current_user.can_make_deposits_for), prompt: "Yourself" %>
</div>
<% end %>
<div class="well">
<%= render partial: 'generic_files/upload/special_reports' %>
<%= render partial: 'generic_files/upload/form_fields' %>
</div>
<% end %>
9 changes: 9 additions & 0 deletions app/views/generic_files/upload/_special_reports.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="controls">
<%= label_tag :special_reports, 'Uploading Special Reports?' %>
<%= radio_button_tag :resource_type, 'Computing Science Technical Report' %>
<%= label_tag :resource_type, 'Computing Science Technical Report' %>
<%= radio_button_tag :resource_type, 'Structural Engineering Report' %>
<%= label_tag :resource_type, 'Structural Engineering Report' %>
<%= button_tag "Clear", class: 'btn btn-warning cancel reset-special-reports', id: 'reset-special-reports', name: "reset-special-reports", onclick: "confirmation_needed = false;", type: :reset %>

</div>
6 changes: 6 additions & 0 deletions app/views/records/edit_fields/_resource_type.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<% if (@form[:resource_type].include? Sufia.config.special_reports['cstr']) || (@form[:resource_type].include? Sufia.config.special_reports['ser']) %>
<% else %>

<%= f.input :resource_type, as: :select_with_help, collection: Sufia.config.resource_types,
input_html: { class: 'form-control', multiple: true } %>
<% end %>
Loading