Class: BatchUpdateJob

Inherits:
Object
  • Object
show all
Includes:
GenericFileHelper, Hydra::AccessControlsEnforcement
Defined in:
lib/sufia/jobs/batch_update_job.rb

Overview

Copyright © 2012 The Pennsylvania State University

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GenericFileHelper

#display_title

Constructor Details

#initialize(login, params) ⇒ BatchUpdateJob

Returns a new instance of BatchUpdateJob.



26
27
28
29
30
31
32
33
# File 'lib/sufia/jobs/batch_update_job.rb', line 26

def initialize(, params)
  self. = 
  self.title = params[:title]
  self.file_attributes = params[:generic_file]
  self.visibility = params[:visibility]
  self.batch_id = params[:id]

end

Instance Attribute Details

#batch_idObject

Returns the value of attribute batch_id.



24
25
26
# File 'lib/sufia/jobs/batch_update_job.rb', line 24

def batch_id
  @batch_id
end

#file_attributesObject

Returns the value of attribute file_attributes.



24
25
26
# File 'lib/sufia/jobs/batch_update_job.rb', line 24

def file_attributes
  @file_attributes
end

#loginObject

Returns the value of attribute login.



24
25
26
# File 'lib/sufia/jobs/batch_update_job.rb', line 24

def 
  @login
end

#titleObject

Returns the value of attribute title.



24
25
26
# File 'lib/sufia/jobs/batch_update_job.rb', line 24

def title
  @title
end

#visibilityObject

Returns the value of attribute visibility.



24
25
26
# File 'lib/sufia/jobs/batch_update_job.rb', line 24

def visibility
  @visibility
end

Instance Method Details

#file_list(files) ⇒ Object



81
82
83
84
# File 'lib/sufia/jobs/batch_update_job.rb', line 81

def file_list ( files)
  return files.map {|gf| '<a href="'+Sufia::Engine.routes.url_helpers.generic_files_path+'/'+gf.noid+'">'+display_title(gf)+'</a>'}.join(', ')
  
end

#queue_nameObject



20
21
22
# File 'lib/sufia/jobs/batch_update_job.rb', line 20

def queue_name
  :batch_update
end

#runObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sufia/jobs/batch_update_job.rb', line 35

def run
  batch = Batch.find_or_create(self.batch_id)
  user = User.find_by_user_key(self.)
  @saved = []
  @denied = []

  batch.generic_files.each do |gf|
    update_file(gf, user)
  end
  batch.update_attributes({status:["Complete"]})
  
  job_user = User.batchuser()
  
  message = '<a class="batchid ui-helper-hidden">ss-'+batch.noid+'</a>The file(s) '+ file_list(@saved)+ " have been saved." unless @saved.empty?
  job_user.send_message(user, message, 'Batch upload complete') unless @saved.empty?
 
  message = '<a class="batchid ui-helper-hidden">'+batch.noid+'</a>The file(s) '+ file_list(@denied)+" could not be updated.  You do not have sufficient privileges to edit it." unless @denied.empty?
  job_user.send_message(user, message, 'Batch upload permission denied') unless @denied.empty?
   
end

#update_file(gf, user) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sufia/jobs/batch_update_job.rb', line 56

def update_file(gf, user)
  unless user.can? :edit, get_permissions_solr_response_for_doc_id(gf.pid)[1]
    logger.error "User #{user.user_key} DEEEENIED access to #{gf.pid}!"
    @denied << gf
    return
  end
  gf.title = title[gf.pid] if title[gf.pid] rescue gf.label
  gf.attributes=file_attributes
  gf.set_visibility(visibility)

  save_tries = 0
  begin
    gf.save!
  rescue RSolr::Error::Http => error
    save_tries += 1
    logger.warn "BatchUpdateJob caught RSOLR error on #{gf.pid}: #{error.inspect}"
    # fail for good if the tries is greater than 3
    rescue_action_without_handler(error) if save_tries >=3
    sleep 0.01
    retry
  end #
  Sufia.queue.push(ContentUpdateEventJob.new(gf.pid, ))
  @saved << gf
end