Module: Bulkrax::ValidationHelper

Included in:
ImportersController
Defined in:
app/helpers/bulkrax/validation_helper.rb

Instance Method Summary collapse

Instance Method Details

#check_admin_setObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/helpers/bulkrax/validation_helper.rb', line 19

def check_admin_set
  return unless defined?(::Hyrax)

  if params[:importer][:admin_set_id].blank?
    params[:importer][:admin_set_id] = Bulkrax.object_factory.default_admin_set_id
  else
    Bulkrax.object_factory.find(params[:importer][:admin_set_id])
  end
  return true
rescue ActiveFedora::ObjectNotFoundError, Bulkrax::ObjectFactoryInterface::ObjectNotFoundError
  logger.warn("AdminSet #{params[:importer][:admin_set_id]} not found. Using default admin set.")
  params[:importer][:admin_set_id] = Bulkrax.object_factory.default_admin_set_id
  return true
end

#check_userObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/helpers/bulkrax/validation_helper.rb', line 34

def check_user
  if params[:importer][:user_id].blank?
    params[:importer][:user_id] = User.batch_user.id
  else
    User.find(params[:importer][:user_id])
  end
  return true
rescue ActiveRecord::RecordNotFound
  logger.warn("User #{params[:importer][:user_id]} not found. Using default batch_user.")
  params[:importer][:user_id] = User.batch_user.id
  return true
end

#return_json_responseObject



51
52
53
# File 'app/helpers/bulkrax/validation_helper.rb', line 51

def return_json_response
  json_response(@return_value[0], @return_value[1], @return_value[2])
end

#return_value(method, status, message) ⇒ Object



47
48
49
# File 'app/helpers/bulkrax/validation_helper.rb', line 47

def return_value(method, status, message)
  @return_value ||= [method, status, message]
end

#valid_bagit?Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
116
117
118
# File 'app/helpers/bulkrax/validation_helper.rb', line 110

def valid_bagit?
  return true if params[:importer][:parser_fields][:metadata_format].present? &&
                 params[:importer][:parser_fields][:metadata_file_name].present? &&
                 params[:importer][:parser_fields][:import_file_path].present?
  return_value('invalid',
               :unprocessable_entity,
               "[:importer][:parser_fields] [:metadata_format], [:metadata_file_name] and [:import_file_path] are required")
  return false
end

#valid_commit?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
# File 'app/helpers/bulkrax/validation_helper.rb', line 63

def valid_commit?
  return true if params[:commit].present? && valid_commit_message?(params[:commit])
  return_value('invalid',
               :unprocessable_entity,
               "[:commit] is required")
  return false
end

#valid_commit_message?(commit) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/helpers/bulkrax/validation_helper.rb', line 138

def valid_commit_message?(commit)
  # @todo - manual list because this line causes the importer script to fail - why?
  # Bulkrax.api_definition['bulkrax']['importer']['commit']['valid_values'].include?(commit)
  [
    "Create",
    "Create and Import",
    "Update Importer",
    "Update and Re-Import (update metadata and replace files)",
    "Update and Harvest Updated Items",
    "Update and Re-Harvest All Items",
    "Update and Re-Import (update metadata only)",
    "Update and Import (importer has not yet been run)"
  ].include?(commit)
end

#valid_create_params?Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
# File 'app/helpers/bulkrax/validation_helper.rb', line 5

def valid_create_params?
  check_admin_set
  check_user
  return true if valid_importer? && valid_commit? &&
                 valid_name? && valid_parser_klass? &&
                 valid_parser_fields?
end

#valid_csv?Boolean

Returns:

  • (Boolean)


120
121
122
123
124
125
126
# File 'app/helpers/bulkrax/validation_helper.rb', line 120

def valid_csv?
  return true if params[:importer][:parser_fields][:import_file_path].present?
  return_value('invalid',
               :unprocessable_entity,
               "[:importer][:parser_fields] [:import_file_path] is required")
  return false
end

#valid_importer?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
# File 'app/helpers/bulkrax/validation_helper.rb', line 55

def valid_importer?
  return true if params[:importer].present?
  return_value('invalid',
               :unprocessable_entity,
               "Missing required parameters")
  return false
end

#valid_name?Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
# File 'app/helpers/bulkrax/validation_helper.rb', line 71

def valid_name?
  return true if params[:importer][:name].present?
  return_value('invalid',
               :unprocessable_entity,
               "[:importer][:name] is required")
  return false
end

#valid_oai?Boolean

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
136
# File 'app/helpers/bulkrax/validation_helper.rb', line 128

def valid_oai?
  return true if params[:base_url].present? &&
                 params[:importer][:parser_fields][:set].present? &&
                 params[:importer][:parser_fields][:collection_name].present?
  return_value('invalid',
               :unprocessable_entity,
               "[:base_url], [:importer][:parser_fields][:set] and [:importer][:parser_fields][:collection_name] are required")
  return false
end

#valid_parser_fields?Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/helpers/bulkrax/validation_helper.rb', line 87

def valid_parser_fields?
  if params[:importer][:parser_fields].present?
    case params[:importer][:parser_klass]
    when 'Bulkrax::OaiParser'
      return valid_oai?
    when 'Bulkrax::CsvParser'
      return valid_csv?
    when 'Bulkrax::BagitParser'
      return valid_bagit?
    else
      return_value('invalid',
                   :unprocessable_entity,
                   "#{params[:importer][:parser_klass]} not recognised")
      return false
    end
  else
    return_value('invalid',
                 :unprocessable_entity,
                 "params[:importer][:parser_fields] is required")
    return false
  end
end

#valid_parser_klass?Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
# File 'app/helpers/bulkrax/validation_helper.rb', line 79

def valid_parser_klass?
  return true if params[:importer][:parser_klass].present?
  return_value('invalid',
               :unprocessable_entity,
               "[:importer][:parser_klass] is required")
  return false
end

#valid_update_params?Boolean

Returns:

  • (Boolean)


13
14
15
16
17
# File 'app/helpers/bulkrax/validation_helper.rb', line 13

def valid_update_params?
  check_admin_set
  check_user
  return valid_commit?
end