Class: BatchUpdateJob

Inherits:
Object
  • Object
show all
Includes:
Hydra::PermissionsQuery
Defined in:
lib/sufia/models/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

Constructor Details

#initialize(login, params) ⇒ BatchUpdateJob

Returns a new instance of BatchUpdateJob.



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

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.



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

def batch_id
  @batch_id
end

#file_attributesObject

Returns the value of attribute file_attributes.



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

def file_attributes
  @file_attributes
end

#loginObject

Returns the value of attribute login.



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

def 
  @login
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#visibilityObject

Returns the value of attribute visibility.



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

def visibility
  @visibility
end

Instance Method Details

#file_list(files) ⇒ Object



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

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

#queue_nameObject



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

def queue_name
  :batch_update
end

#runObject



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

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



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

def update_file(gf, user)
  unless user.can? :edit, gf
    logger.error "User #{user.user_key} DENIED 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
    raise error if save_tries >=3
    sleep 0.01
    retry
  end #
  Sufia.queue.push(ContentUpdateEventJob.new(gf.pid, ))
  @saved << gf
end