Class: ArchivalObjectsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#archivesspace, can_access?, permission_mappings, set_access_control

Instance Method Details

#accept_childrenObject



148
149
150
# File 'frontend/app/controllers/archival_objects_controller.rb', line 148

def accept_children
  handle_accept_children(JSONModel(:archival_object))
end

#add_childrenObject



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'frontend/app/controllers/archival_objects_controller.rb', line 249

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] = 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] = t("rde.messages.rows_with_errors", :count => error_count)
        else
          flash.now[:success] = 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 => 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] = t("rde.messages.rows_with_errors", :count => @exceptions.select {|e| !e.empty?}.length)
      end
    end

  end

  render_aspace_partial :partial => "shared/rde"
end

#createObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'frontend/app/controllers/archival_objects_controller.rb', line 57

def create
  handle_crud(:instance => :archival_object,
              :find_opts => find_opts,
              :on_invalid => ->() {
                if inline?
                  render_aspace_partial :partial => "new_inline"
                else
                  render action: :new
                end
              },
              :on_valid => ->(id) {

                resource = @archival_object['resource']['_resolved']
                parent = @archival_object['parent'] ? @archival_object['parent']['_resolved'] : false

                success_message = @archival_object.parent ?
                                    t("archival_object._frontend.messages.created_with_parent", archival_object_display_string: @archival_object.title, parent_display_string: parent['title'], resource_title: resource['title']) :
                                    t("archival_object._frontend.messages.created", archival_object_display_string: @archival_object.title, resource_title: resource['title'])
                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] = t("slug.autogen_disabled")
                  else
                    flash.now[:warning] = t("slug.autogen_disabled")
                  end
                end
                if inline?
                  render_aspace_partial :partial => "archival_objects/edit_inline"
                else
                  id = ArchivalObject.id_for(@archival_object.uri)
                  resource_id = Resource.id_for(@archival_object.resource['ref'])
                  redirect_to controller: :resources, action: :edit, id: resource_id, anchor: "tree::archival_object_#{id}"
                end
              })
end

#current_recordObject



130
131
132
# File 'frontend/app/controllers/archival_objects_controller.rb', line 130

def current_record
  @archival_object
end

#defaultsObject



153
154
155
156
157
158
159
160
161
162
163
# File 'frontend/app/controllers/archival_objects_controller.rb', line 153

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 = t("default_values.form_title.archival_object")

  render "defaults"
end

#deleteObject



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'frontend/app/controllers/archival_objects_controller.rb', line 211

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

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

  flash[:success] = t("archival_object._frontend.messages.deleted", archival_object_display_string: archival_object.title)

  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



43
44
45
46
47
48
49
50
51
52
53
54
# File 'frontend/app/controllers/archival_objects_controller.rb', line 43

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



355
356
357
358
359
360
361
362
# File 'frontend/app/controllers/archival_objects_controller.rb', line 355

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, t("#{type == 'archival_object' ? 'resource_component' : type}._singular")]
  }
end

#newObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'frontend/app/controllers/archival_objects_controller.rb', line 9

def new
  if params[:duplicate_from_archival_object]
    new_find_opts = find_opts
    new_find_opts["resolve[]"].push("top_container::container_locations")

    archival_object_id = params[:duplicate_from_archival_object][:uri].split('/').pop

    @archival_object = JSONModel(:archival_object).find(archival_object_id, new_find_opts)
    @archival_object.ref_id = nil
    @archival_object.instances = []
    @archival_object.position = params[:position]

    flash[:success] = t("archival_object._frontend.messages.duplicated", archival_object_display_string: @archival_object.display_string)
  else
    @archival_object = ArchivalObject.new._always_valid!
    @archival_object.parent = {'ref' => ArchivalObject.uri_for(params[:archival_object_id])} if params.has_key?(:archival_object_id)
    @archival_object.resource = {'ref' => Resource.uri_for(params[:resource_id])} if params.has_key?(:resource_id)
    @archival_object.position = params[:position]
    if defaults = user_defaults('archival_object')
      @archival_object.update(defaults.values)
    end

    if params[:accession_id]
      acc = Accession.find(params[:accession_id], find_opts)
      @archival_object.populate_from_accession(acc)

      flash.now[:info] = t("archival_object._frontend.messages.spawned", accession_display_string: acc.title)
      flash[:spawned_from_accession] = acc.id
    end
  end

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

#publishObject



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

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] = t("archival_object._frontend.messages.published", archival_object_title: @archival_object.title)
  else
    flash[:error] = ASUtils.json_parse(response.body)['error'].to_s
  end

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

#rdeObject



239
240
241
242
243
244
245
246
# File 'frontend/app/controllers/archival_objects_controller.rb', line 239

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



134
135
136
137
138
139
140
141
142
143
144
145
# File 'frontend/app/controllers/archival_objects_controller.rb', line 134

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] = t("archival_object._frontend.messages.suppressed_info") if @archival_object.suppressed

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

#suppressObject



337
338
339
340
341
342
343
# File 'frontend/app/controllers/archival_objects_controller.rb', line 337

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

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

#transferObject



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

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)
      resource = @archival_object['resource']['_resolved']
      flash[:success] = t("archival_object._frontend.messages.transfer_success", archival_object_display_string: @archival_object.title, resource_title: resource['title'])
      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] = 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



311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'frontend/app/controllers/archival_objects_controller.rb', line 311

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] = t("archival_object._frontend.messages.unpublished", archival_object_title: @archival_object.title)
  else
    flash[:error] = ASUtils.json_parse(response.body)['error'].to_s
  end

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

#unsuppressObject



346
347
348
349
350
351
352
# File 'frontend/app/controllers/archival_objects_controller.rb', line 346

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

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

#updateObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'frontend/app/controllers/archival_objects_controller.rb', line 103

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_success = parent ?
                  t("archival_object._frontend.messages.updated_with_parent", archival_object_display_string: @archival_object.title) :
                  t("archival_object._frontend.messages.updated", archival_object_display_string: @archival_object.title)
                flash.now[:success] = flash_success
                if @archival_object["is_slug_auto"] == false &&
                   @archival_object["slug"] == nil &&
                   params["archival_object"] &&
                   params["archival_object"]["is_slug_auto"] == "1"

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

                render_aspace_partial :partial => "edit_inline"
              })
end

#update_defaultsObject



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

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] = 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



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

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