Class: DigitalObjectsController

Inherits:
ApplicationController show all
Defined in:
frontend/app/controllers/digital_objects_controller.rb,
public/app/controllers/digital_objects_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#archivesspace, can_access?, permission_mappings, set_access_control

Instance Method Details

#accept_childrenObject



224
225
226
# File 'frontend/app/controllers/digital_objects_controller.rb', line 224

def accept_children
  handle_accept_children(JSONModel(:digital_object))
end

#add_childrenObject



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
294
295
296
297
298
299
300
301
302
# File 'frontend/app/controllers/digital_objects_controller.rb', line 255

def add_children
  @parent = JSONModel(:digital_object).find(params[:id])
  @digital_object_uri = @parent.uri

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

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

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

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

      if params["validate_only"] == "true"
        @exceptions = @children.children.collect {|c| JSONModel(:digital_object_component).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(:digital_object_id => @parent.id)
      end

      return render :plain => t("rde.messages.success")
    rescue JSONModel::ValidationException => e
      @exceptions = @children
                    .children
                    .collect {|c| JSONModel(:digital_object_component).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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'frontend/app/controllers/digital_objects_controller.rb', line 145

def create
  handle_crud(:instance => :digital_object,
              :on_invalid => ->() {
                return render_aspace_partial :partial => "new" if inline?
                render :action => "new"
              },
              :on_valid => ->(id) {
                flash[:success] = t("digital_object._frontend.messages.created", digital_object_title: @digital_object.title)

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

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

                return render :json => @digital_object.to_hash if inline?
                redirect_to({
                              :controller => :digital_objects,
                              :action => :edit,
                              :id => id
                            })
              })
end

#current_recordObject



30
31
32
# File 'frontend/app/controllers/digital_objects_controller.rb', line 30

def current_record
  @digital_object
end

#defaultsObject



93
94
95
96
97
98
99
100
101
102
103
# File 'frontend/app/controllers/digital_objects_controller.rb', line 93

def defaults
  defaults = DefaultValues.get 'digital_object'

  values = defaults ? defaults.form_values : {:title => t("digital_object.title_default", :default => "")}

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

  @form_title = t("default_values.form_title.digital_object")

  render "defaults"
end

#deleteObject



194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'frontend/app/controllers/digital_objects_controller.rb', line 194

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

  begin
    digital_object.delete
  rescue ConflictException => e
    flash[:error] = t("digital_object._frontend.messages.delete_conflict", :error => t("errors.#{e.conflicts}", :default => e.message))
    return redirect_to(:controller => :digital_objects, :action => :show, :id => params[:id])
  end

  flash[:success] = t("digital_object._frontend.messages.deleted", digital_object_title: digital_object.title)
  redirect_to(:controller => :digital_objects, :action => :index, :deleted_uri => digital_object.uri)
end

#editObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'frontend/app/controllers/digital_objects_controller.rb', line 127

def edit
  flash.keep if not flash.empty? # keep the notices so they display on the subsequent ajax call

  if params[:inline]
    # only fetch the fully resolved record when rendering the full form
    @digital_object = JSONModel(:digital_object).find(params[:id], find_opts)

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

    return render_aspace_partial :partial => "digital_objects/edit_inline"
  end

  @digital_object = JSONModel(:digital_object).find(params[:id])
end

#indexObject



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

def index
  respond_to do |format|
    format.html {
      @search_data = Search.for_type(session[:repo_id], params[:include_components]==="true" ? ["digital_object", "digital_object_component"] : "digital_object", params_for_backend_search.merge({"facet[]" => SearchResultData.DIGITAL_OBJECT_FACETS}))
    }
    format.csv {
      search_params = params_for_backend_search.merge({"facet[]" => SearchResultData.DIGITAL_OBJECT_FACETS})
      search_params["type[]"] = params[:include_components] === "true" ? ["digital_object", "digital_object_component"] : [ "digital_object" ]
      uri = "/repositories/#{session[:repo_id]}/search"
      csv_response( uri, Search.build_filters(search_params), "#{t('digital_object._plural').downcase}." )
    }
  end
end

#mergeObject



229
230
231
232
233
# File 'frontend/app/controllers/digital_objects_controller.rb', line 229

def merge
  handle_merge( params[:refs] ,
                JSONModel(:digital_object).uri_for(params[:id]),
               'digital_object')
end

#newObject



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
# File 'frontend/app/controllers/digital_objects_controller.rb', line 62

def new
  @digital_object = JSONModel(:digital_object).new({:title => t("digital_object.title_default", :default => "")})._always_valid!

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

    if defaults
      @digital_object.update(defaults.values)
      @form_title = "#{t('actions.new_prefix')} #{t('digital_object._singular')}"
    end
  end

  if user_prefs['digital_object_spawn']
    if params[:spawn_from_resource_id]
      copy_from_record = Resource.find(params[:spawn_from_resource_id])
    elsif params[:spawn_from_accession_id]
      copy_from_record = Accession.find(params[:spawn_from_accession_id])
    elsif params[:spawn_from_archival_object_id]
      copy_from_record = ArchivalObject.find(params[:spawn_from_archival_object_id])
    end

    if copy_from_record
      updates = map_record_fields_to_digital_object(copy_from_record)
      @digital_object.update(updates)
    end
  end

  return render_aspace_partial :partial => "digital_objects/new" if params[:inline]
end

#node_from_rootObject



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

def node_from_root
  digital_object_uri = JSONModel(:digital_object).uri_for(params[:id])

  render :json => JSONModel::HTTP.get_json("#{digital_object_uri}/tree/node_from_root",
                                           'node_ids[]' => params[:node_ids])
end

#publishObject



209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'frontend/app/controllers/digital_objects_controller.rb', line 209

def publish
  digital_object = JSONModel(:digital_object).find(params[:id])

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

  if response.code == '200'
    flash[:success] = t("digital_object._frontend.messages.published", digital_object_title: digital_object.title)
  else
    flash[:error] = ASUtils.json_parse(response.body)['error'].to_s
  end

  redirect_to request.referer
end

#rdeObject



243
244
245
246
247
248
249
250
251
252
# File 'frontend/app/controllers/digital_objects_controller.rb', line 243

def rde
  flash.clear

  @parent = JSONModel(:digital_object).find(params[:id])
  @digital_object_uri = @parent.uri
  @children = DigitalObjectChildren.new
  @exceptions = []

  render_aspace_partial :partial => "shared/rde"
end

#showObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'frontend/app/controllers/digital_objects_controller.rb', line 35

def show
  flash.keep if not flash.empty? # keep the notices so they display on the subsequent ajax call

  if params[:inline]
    # only fetch the fully resolved record when rendering the full form
    @digital_object = JSONModel(:digital_object).find(params[:id], find_opts)

    flash.now[:info] = t("digital_object._frontend.messages.suppressed_info") if @digital_object.suppressed

    return render_aspace_partial :partial => "digital_objects/show_inline"
  end

  @digital_object = JSONModel(:digital_object).find(params[:id])
end

#suppressObject



305
306
307
308
309
310
311
# File 'frontend/app/controllers/digital_objects_controller.rb', line 305

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

  flash[:success] = t("digital_object._frontend.messages.suppressed", digital_object_title: digital_object.title)
  redirect_to(:controller => :digital_objects, :action => :show, :id => params[:id])
end

#transferObject



51
52
53
54
55
56
57
58
59
# File 'frontend/app/controllers/digital_objects_controller.rb', line 51

def transfer
  begin
    handle_transfer(JSONModel(:digital_object))
  rescue ArchivesSpace::TransferConflictException => e
    @transfer_errors = e.errors
    show
    render :action => :show
  end
end

#treeObject



236
237
238
239
240
# File 'frontend/app/controllers/digital_objects_controller.rb', line 236

def tree
  flash.keep # keep the flash... just in case this fires before the form is loaded

  render :json => fetch_tree
end

#tree_nodeObject



335
336
337
338
339
340
341
342
343
344
345
# File 'frontend/app/controllers/digital_objects_controller.rb', line 335

def tree_node
  digital_object_uri = JSONModel(:digital_object).uri_for(params[:id])
  node_uri = if !params[:node].blank?
               params[:node]
             else
               nil
             end

  render :json => JSONModel::HTTP.get_json("#{digital_object_uri}/tree/node",
                                           :node_uri => node_uri)
end

#tree_node_from_rootObject



25
26
27
28
29
30
31
# File 'public/app/controllers/digital_objects_controller.rb', line 25

def tree_node_from_root
  @root_uri = "/repositories/#{params[:rid]}/digital_objects/#{params[:id]}"
  url = @root_uri + '/tree/node_from_root_' + params[:node_ids].first
  render json: archivesspace.get_raw_record(url)
rescue RecordNotFound
  render json: {}, status: 404
end

#tree_rootObject



322
323
324
325
326
# File 'frontend/app/controllers/digital_objects_controller.rb', line 322

def tree_root
  digital_object_uri = JSONModel(:digital_object).uri_for(params[:id])

  render :json => JSONModel::HTTP.get_json("#{digital_object_uri}/tree/root")
end

#tree_waypointObject



347
348
349
350
351
352
353
354
355
356
357
358
# File 'frontend/app/controllers/digital_objects_controller.rb', line 347

def tree_waypoint
  digital_object_uri = JSONModel(:digital_object).uri_for(params[:id])
  node_uri = if !params[:node].blank?
               params[:node]
             else
               nil
             end

  render :json => JSONModel::HTTP.get_json("#{digital_object_uri}/tree/waypoint",
                                           :parent_node => node_uri,
                                           :offset => params[:offset])
end

#unsuppressObject



314
315
316
317
318
319
320
# File 'frontend/app/controllers/digital_objects_controller.rb', line 314

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

  flash[:success] = t("digital_object._frontend.messages.unsuppressed", digital_object_title: digital_object.title)
  redirect_to(:controller => :digital_objects, :action => :show, :id => params[:id])
end

#updateObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'frontend/app/controllers/digital_objects_controller.rb', line 172

def update
  handle_crud(:instance => :digital_object,
              :obj => JSONModel(:digital_object).find(params[:id], find_opts),
              :on_invalid => ->() {
                render_aspace_partial :partial => "edit_inline"
              },
              :on_valid => ->(id) {

                flash.now[:success] = t("digital_object._frontend.messages.updated", digital_object_title: @digital_object.title)
                if @digital_object["is_slug_auto"] == false &&
                   @digital_object["slug"] == nil &&
                   params["digital_object"] &&
                   params["digital_object"]["is_slug_auto"] == "1"

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

                render_aspace_partial :partial => "edit_inline"
              })
end

#update_defaultsObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'frontend/app/controllers/digital_objects_controller.rb', line 106

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

    flash[:success] = "Defaults updated"

    redirect_to :controller => :digital_objects, :action => :defaults
  rescue Exception => e
    flash[:error] = e.message
    redirect_to :controller => :digital_objects, :action => :defaults
  end
end