Class: ArchivalObjectsController

Inherits:
ApplicationController show all
Defined in:
frontend/app/controllers/archival_objects_controller.rb

Constant Summary

Constants included from Searchable

Searchable::ABSTRACT

Instance Method Summary collapse

Methods inherited from ApplicationController

#archivesspace, can_access?, permission_mappings, set_access_control

Methods included from JsonHelper

#merge_notes, #process_json_notes

Methods included from Searchable

#default_search_opts, #get_filter_years, #handle_results, #html_notes, #process_results, #process_search_results, #repo_context, #search_terms, #set_up_advanced_search, #set_up_and_run_search, #set_up_search, #strip_facet_fields

Methods included from HandleFaceting

#fetch_only_facets, #get_pretty_facet_value, #strip_facets

Methods included from ManipulateNode

#inheritance, #process_mixed_content, #strip_mixed_content

Instance Method Details

#accept_childrenObject



122
123
124
# File 'frontend/app/controllers/archival_objects_controller.rb', line 122

def accept_children
  handle_accept_children(JSONModel(:archival_object))
end

#add_childrenObject



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'frontend/app/controllers/archival_objects_controller.rb', line 220

def add_children
  @parent = JSONModel(:archival_object).find(params[:id])
  @resource_uri = @parent['resource']['ref']

  if params[:archival_record_children].blank? or params[:archival_record_children]["children"].blank?

    @children = ArchivalObjectChildren.new
    flash.now[:error] = I18n.t("rde.messages.no_rows")

  else
    children_data = cleanup_params_for_schema(params[:archival_record_children], JSONModel(:archival_record_children).schema)

    begin
      @children = ArchivalObjectChildren.from_hash(children_data, false)

      if params["validate_only"] == "true"
        @exceptions = @children.children.collect {|c| JSONModel(:archival_object).from_hash(c, false)._exceptions}

        error_count = @exceptions.select {|e| !e.empty?}.length
        if error_count > 0
          flash.now[:error] = I18n.t("rde.messages.rows_with_errors", :count => error_count)
        else
          flash.now[:success] = I18n.t("rde.messages.rows_no_errors")
        end

        return render_aspace_partial :partial => "shared/rde"
      else
        @children.save(:archival_object_id => @parent.id)
      end

      return render :plain => I18n.t("rde.messages.success")
    rescue JSONModel::ValidationException => e
      @exceptions = @children.children.collect {|c| JSONModel(:archival_object).from_hash(c, false)._exceptions}

      if @exceptions.all?(&:blank?)
        e.errors.each { |key, vals| flash.now[:error] = "#{key} : #{vals.join('<br/>')}" }
      else
        flash.now[:error] = I18n.t("rde.messages.rows_with_errors", :count => @exceptions.select {|e| !e.empty?}.length)
      end
    end

  end

  render_aspace_partial :partial => "shared/rde"
end

#createObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'frontend/app/controllers/archival_objects_controller.rb', line 43

def create
  handle_crud(:instance => :archival_object,
              :find_opts => find_opts,
              :on_invalid => ->() { render_aspace_partial :partial => "new_inline" },
              :on_valid => ->(id) {

                success_message = @archival_object.parent ?
                                    I18n.t("archival_object._frontend.messages.created_with_parent", JSONModelI18nWrapper.new(:archival_object => @archival_object, :resource => @archival_object['resource']['_resolved'], :parent => @archival_object['parent']['_resolved']).enable_parse_mixed_content!(url_for(:root))) :
                                    I18n.t("archival_object._frontend.messages.created", JSONModelI18nWrapper.new(:archival_object => @archival_object, :resource => @archival_object['resource']['_resolved']).enable_parse_mixed_content!(url_for(:root)))

                if params.has_key?(:plus_one)
                  flash[:success] = success_message
                else
                  flash.now[:success] = success_message
                end

                if @archival_object["is_slug_auto"] == false &&
                   @archival_object["slug"] == nil &&
                   params["archival_object"] &&
                   params["archival_object"]["is_slug_auto"] == "1"

                  if params.has_key?(:plus_one)
                    flash[:warning] = I18n.t("slug.autogen_disabled")
                  else
                    flash.now[:warning] = I18n.t("slug.autogen_disabled")
                  end
                end

                render_aspace_partial :partial => "archival_objects/edit_inline"

              })
end

#current_recordObject



104
105
106
# File 'frontend/app/controllers/archival_objects_controller.rb', line 104

def current_record
  @archival_object
end

#defaultsObject



127
128
129
130
131
132
133
134
135
136
137
# File 'frontend/app/controllers/archival_objects_controller.rb', line 127

def defaults
  defaults = DefaultValues.get 'archival_object'

  values = defaults ? defaults.form_values : {}

  @archival_object = JSONModel(:archival_object).new(values)._always_valid!

  @archival_object.display_string = I18n.t("default_values.form_title.archival_object")

  render "defaults"
end

#deleteObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'frontend/app/controllers/archival_objects_controller.rb', line 185

def delete
  archival_object = JSONModel(:archival_object).find(params[:id])

  previous_record = JSONModel::HTTP.get_json("#{archival_object['uri']}/previous")

  begin
    archival_object.delete
  rescue ConflictException => e
    flash[:error] = I18n.t("archival_object._frontend.messages.delete_conflict", :error => I18n.t("errors.#{e.conflicts}", :default => e.message))
    resolver = Resolver.new(archival_object['uri'])
    return redirect_to resolver.view_uri
  end

  flash[:success] = I18n.t("archival_object._frontend.messages.deleted", JSONModelI18nWrapper.new(:archival_object => archival_object).enable_parse_mixed_content!(url_for(:root)))

  if previous_record
    redirect_to :controller => :resources, :action => :show, :id => JSONModel(:resource).id_for(archival_object['resource']['ref']), :anchor => "tree::archival_object_#{JSONModel(:archival_object).id_for(previous_record['uri'])}"
  else
    # no previous node, so redirect to the resource
    resolver = Resolver.new(archival_object['resource']['ref'])
    redirect_to resolver.view_uri
  end
end

#editObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'frontend/app/controllers/archival_objects_controller.rb', line 29

def edit
  new_find_opts = find_opts
  new_find_opts["resolve[]"].push("top_container::container_locations")

  @archival_object = JSONModel(:archival_object).find(params[:id], new_find_opts)

  if @archival_object.suppressed
    return redirect_to(:action => :show, :id => params[:id], :inline => params[:inline])
  end

  render_aspace_partial :partial => "archival_objects/edit_inline" if inline?
end

#models_in_graphObject



326
327
328
329
330
331
332
333
# File 'frontend/app/controllers/archival_objects_controller.rb', line 326

def models_in_graph
  list_uri = JSONModel(:archival_object).uri_for(params[:id]) + "/models_in_graph"
  list = JSONModel::HTTP.get_json(list_uri)

  render :json => list.select { |type| type != "lang_material" }.map {|type|
    [type, I18n.t("#{type == 'archival_object' ? 'resource_component' : type}._singular")]
  }
end

#newObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'frontend/app/controllers/archival_objects_controller.rb', line 11

def new
  @archival_object = JSONModel(:archival_object).new._always_valid!
  @archival_object.parent = {'ref' => JSONModel(:archival_object).uri_for(params[:archival_object_id])} if params.has_key?(:archival_object_id)
  @archival_object.resource = {'ref' => JSONModel(:resource).uri_for(params[:resource_id])} if params.has_key?(:resource_id)
  @archival_object.position = params[:position]

  if user_prefs['default_values']
    defaults = DefaultValues.get 'archival_object'

    @archival_object.update(defaults.values) if defaults
  end


  return render_aspace_partial :partial => "archival_objects/new_inline" if inline?

  # render the full AO form
end

#publishObject



267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'frontend/app/controllers/archival_objects_controller.rb', line 267

def publish
  @archival_object = JSONModel(:archival_object).find(params[:id], find_opts)

  response = JSONModel::HTTP.post_form("#{@archival_object.uri}/publish")

  if response.code == '200'
    flash[:success] = I18n.t("archival_object._frontend.messages.published", JSONModelI18nWrapper.new(:archival_object => @archival_object).enable_parse_mixed_content!(url_for(:root)))
  else
    flash[:error] = ASUtils.json_parse(response.body)['error'].to_s
  end

  redirect_to "#{request.referer}#tree::archival_object_#{params[:id]}"
end

#rdeObject



210
211
212
213
214
215
216
217
# File 'frontend/app/controllers/archival_objects_controller.rb', line 210

def rde
  @parent = JSONModel(:archival_object).find(params[:id])
  @resource_uri = @parent['resource']['ref']
  @children = ArchivalObjectChildren.new
  @exceptions = []

  render_aspace_partial :partial => "shared/rde"
end

#showObject



108
109
110
111
112
113
114
115
116
117
118
119
# File 'frontend/app/controllers/archival_objects_controller.rb', line 108

def show
  @resource_id = params['resource_id']

  new_find_opts = find_opts
  new_find_opts["resolve[]"].push("top_container::container_locations")

  @archival_object = JSONModel(:archival_object).find(params[:id], new_find_opts)

  flash.now[:info] = I18n.t("archival_object._frontend.messages.suppressed_info", JSONModelI18nWrapper.new(:archival_object => @archival_object).enable_parse_mixed_content!(url_for(:root))) if @archival_object.suppressed

  render_aspace_partial :partial => "archival_objects/show_inline" if inline?
end

#suppressObject



308
309
310
311
312
313
314
# File 'frontend/app/controllers/archival_objects_controller.rb', line 308

def suppress
  archival_object = JSONModel(:archival_object).find(params[:id])
  archival_object.set_suppressed(true)

  flash[:success] = I18n.t("archival_object._frontend.messages.suppressed", JSONModelI18nWrapper.new(:archival_object => archival_object).enable_parse_mixed_content!(url_for(:root)))
  redirect_to(:controller => :resources, :action => :show, :id => JSONModel(:resource).id_for(archival_object['resource']['ref']), :anchor => "tree::archival_object_#{params[:id]}")
end

#transferObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'frontend/app/controllers/archival_objects_controller.rb', line 160

def transfer
  begin
    post_data = {
      :target_resource => params["transfer"]["ref"],
      :component => JSONModel(:archival_object).uri_for(params[:id])
    }

    response = JSONModel::HTTP.post_form("/repositories/#{session[:repo_id]}/component_transfers", post_data)

    if response.code == '200'
      @archival_object = JSONModel(:archival_object).find(params[:id], find_opts)

      flash[:success] = I18n.t("archival_object._frontend.messages.transfer_success", JSONModelI18nWrapper.new(:archival_object => @archival_object, :resource => @archival_object['resource']['_resolved']).enable_parse_mixed_content!(url_for(:root)))
      redirect_to :controller => :resources, :action => :edit, :id => JSONModel(:resource).id_for(params["transfer"]["ref"]), :anchor => "tree::archival_object_#{params[:id]}"
    else
      raise ASUtils.json_parse(response.body)['error'].to_s
    end

  rescue Exception => e
    flash[:error] = I18n.t("archival_object._frontend.messages.transfer_error", :exception => e)
    redirect_to :controller => :resources, :action => :edit, :id => params["transfer"]["current_resource_id"], :anchor => "tree::archival_object_#{params[:id]}"
  end
end

#unpublishObject



282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'frontend/app/controllers/archival_objects_controller.rb', line 282

def unpublish
  @archival_object = JSONModel(:archival_object).find(params[:id], find_opts)

  response = JSONModel::HTTP.post_form("#{@archival_object.uri}/unpublish")

  if response.code == '200'
    flash[:success] = I18n.t("archival_object._frontend.messages.unpublished", JSONModelI18nWrapper.new(:archival_object => @archival_object).enable_parse_mixed_content!(url_for(:root)))
  else
    flash[:error] = ASUtils.json_parse(response.body)['error'].to_s
  end

  redirect_to "#{request.referer}#tree::archival_object_#{params[:id]}"
end

#unsuppressObject



317
318
319
320
321
322
323
# File 'frontend/app/controllers/archival_objects_controller.rb', line 317

def unsuppress
  archival_object = JSONModel(:archival_object).find(params[:id])
  archival_object.set_suppressed(false)

  flash[:success] = I18n.t("archival_object._frontend.messages.unsuppressed", JSONModelI18nWrapper.new(:archival_object => archival_object).enable_parse_mixed_content!(url_for(:root)))
  redirect_to(:controller => :resources, :action => :show, :id => JSONModel(:resource).id_for(archival_object['resource']['ref']), :anchor => "tree::archival_object_#{params[:id]}")
end

#updateObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'frontend/app/controllers/archival_objects_controller.rb', line 77

def update
  params['archival_object']['position'] = params['archival_object']['position'].to_i if params['archival_object']['position']

  @archival_object = JSONModel(:archival_object).find(params[:id], find_opts)
  resource = @archival_object['resource']['_resolved']
  parent = @archival_object['parent'] ? @archival_object['parent']['_resolved'] : false

  handle_crud(:instance => :archival_object,
              :obj => @archival_object,
              :on_invalid => ->() { return render_aspace_partial :partial => "edit_inline" },
              :on_valid => ->(id) {
                flash.now[:success] = parent ?
                  I18n.t("archival_object._frontend.messages.updated_with_parent", JSONModelI18nWrapper.new(:archival_object => @archival_object, :resource => @archival_object['resource']['_resolved'], :parent => parent).enable_parse_mixed_content!(url_for(:root))) :
                  I18n.t("archival_object._frontend.messages.updated", JSONModelI18nWrapper.new(:archival_object => @archival_object, :resource => @archival_object['resource']['_resolved']).enable_parse_mixed_content!(url_for(:root)))

                if @archival_object["is_slug_auto"] == false &&
                   @archival_object["slug"] == nil &&
                   params["archival_object"] &&
                   params["archival_object"]["is_slug_auto"] == "1"

                  flash.now[:warning] = I18n.t("slug.autogen_disabled")
                end

                render_aspace_partial :partial => "edit_inline"
              })
end

#update_defaultsObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'frontend/app/controllers/archival_objects_controller.rb', line 140

def update_defaults
  begin
    DefaultValues.from_hash({
                              "record_type" => "archival_object",
                              "lock_version" => params[:archival_object].delete('lock_version'),
                              "defaults" => cleanup_params_for_schema(
                                                                      params[:archival_object],
                                                                      JSONModel(:archival_object).schema
                                                                      )
                            }).save

    flash[:success] = I18n.t("default_values.messages.defaults_updated")
    redirect_to :controller => :archival_objects, :action => :defaults
  rescue Exception => e
    flash[:error] = e.message
    redirect_to :controller => :archival_objects, :action => :defaults
  end
end

#validate_rowsObject



297
298
299
300
301
302
303
304
305
# File 'frontend/app/controllers/archival_objects_controller.rb', line 297

def validate_rows
  row_data = cleanup_params_for_schema(params[:archival_record_children], JSONModel(:archival_record_children).schema)

  # build the AOC record but don't bother validating it yet...
  aoc = ArchivalObjectChildren.from_hash(row_data, false, true)

  # validate each row individually (to avoid weird indexes in the error paths)
  render :json => aoc.children.collect {|c| JSONModel(:archival_object).from_hash(c, false)._exceptions}
end