Class: RepositoriesController

Inherits:
ApplicationController show all
Includes:
ResultInfo
Defined in:
frontend/app/controllers/repositories_controller.rb,
public/app/controllers/repositories_controller.rb

Constant Summary collapse

DEFAULT_SEARCH_FACET_TYPES =
['primary_type', 'subjects', 'published_agents']
DEFAULT_REPO_SEARCH_OPTS =
{
   'sort' => 'title_sort asc',
  'resolve[]' => ['repository:id', 'resource:id@compact_resource', 'ancestors:id@compact_resource', 'top_container_uri_u_sstr:id'],
  'facet.mincount' => 1
}
DEFAULT_TYPES =
%w{archival_object digital_object agent resource accession}

Constants included from Searchable

Searchable::ABSTRACT

Instance Method Summary collapse

Methods included from ResultInfo

#breadcrumb_info, #fill_request_info, #get_rep_image, #handle_dates, #handle_external_docs, #process_agents, #process_digital, #process_digital_instance, #process_extents, #process_file_versions, #process_repo_info, #process_subjects

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

#createObject



39
40
41
42
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
# File 'frontend/app/controllers/repositories_controller.rb', line 39

def create
  handle_repository_oai_params(params)
  generate_names(params[:repository])
  handle_crud(:instance => :repository,
              :model => JSONModel(:repository_with_agent),
              :on_invalid => ->() {
                if @exceptions[:errors]["repo_code"]
                  @exceptions[:errors]["repository/repo_code"] = @exceptions[:errors].delete("repo_code")
                end

                @enum = JSONModel(:enumeration).find("/names/archival_record_level")

                return render_aspace_partial :partial => "repositories/new" if inline?
                return render :action => :new
              },
              :on_valid => ->(id) {
                MemoryLeak::Resources.refresh(:repository)

                return render :json => @repository.to_hash if inline?

                flash[:success] = I18n.t("repository._frontend.messages.created", JSONModelI18nWrapper.new(:repository => @repository))

                if AppConfig[:use_human_readable_urls] && params["repository"]["repository"] &&
                   (params["repository"]["repository"]["slug"].nil? ||
                    params["repository"]["repository"]["slug"].empty?)  &&
                   !params["repository"]["repository"]["is_slug_auto"]

                  flash[:success] = I18n.t("slug.autogen_repo_slug")
                end

                return redirect_to :controller => :repositories, :action => :new, :last_repo_id => id if params.has_key?(:plus_one)

                redirect_to :controller => :repositories, :action => :show, :id => id
              })
end

#current_recordObject



105
106
107
# File 'frontend/app/controllers/repositories_controller.rb', line 105

def current_record
  @repository
end

#deleteObject



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

def delete
  repository = JSONModel(:repository).find(params[:id])
  begin
    repository.delete
  rescue ConflictException => e
    flash[:error] = I18n.t("repository._frontend.messages.cannot_delete_nonempty")
    return redirect_to(:controller => :repositories, :action => :show, :id => params[:id])
  end

  MemoryLeak::Resources.refresh(:repository)

  flash[:success] = I18n.t("repository._frontend.messages.deleted", JSONModelI18nWrapper.new(:repository => repository))
  redirect_to(:controller => :repositories, :action => :index, :deleted_uri => repository.uri)
end

#editObject



75
76
77
78
# File 'frontend/app/controllers/repositories_controller.rb', line 75

def edit
  @repository = JSONModel(:repository_with_agent).find(params[:id])
  @enum = JSONModel(:enumeration).find("/names/archival_record_level")
end

#generate_names(repository_with_agent) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'frontend/app/controllers/repositories_controller.rb', line 25

def generate_names(repository_with_agent)
  name = JSONModel(:name_corporate_entity).new
  name['primary_name'] = repository_with_agent['repository']['name'].blank? ? '<generated>' : repository_with_agent['repository']['name']
  name['source'] = 'local'
  name['sort_name'] = name['primary_name']

  repository_with_agent['agent_representation']['names'] = [name]
  if repository_with_agent['agent_representation']['agent_contacts']['0']['name'].blank?
    repository_with_agent['agent_representation']['agent_contacts']['0']['name'] = name['primary_name']
    repository_with_agent['agent_representation']['agent_contacts']['0']['is_representative'] = 1
  end
end

#indexObject

get all repositories TODO: get this somehow in line with using the Searchable module



20
21
22
23
# File 'public/app/controllers/repositories_controller.rb', line 20

def index
  @search_data = Search.global(params_for_backend_search.merge({"facet[]" => []}),
                               "repositories")
end

#metadataObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
129
130
131
132
133
134
135
# File 'public/app/controllers/repositories_controller.rb', line 80

def 
  md = {
        '@context' => "http://schema.org/",
        '@type' => 'ArchiveOrganization',
        '@id' => AppConfig[:public_proxy_url] + @result['uri'],
        #this next bit will always be the repo name, not the name associated with the agent record (which is overwritten if the repo record is updated)
        'name' => @result['agent_representation']['_resolved']['display_name']['sort_name'],
        'url' => @result['url'],
        'logo' => @result['image_url'],
        #this next bit will never work since ASpace has a bug with how it enacts its repo-to-agent concept (at least in versions 2.4 and 2.5).... and you can't add an authority ID directly to a repo record.
        'sameAs' => @result['agent_representation']['_resolved']['display_name']['authority_id'],
        'parentOrganization' => {
          '@type' => 'Organization',
          'name' => @result['parent_institution_name']
        }
        # removing contactPoint, since this would need to be on the repo record (along with different contact types... e.g. reference assistance, digitization requests, whatever)
        # 'contactPoint' => @result['agent_representation']['_resolved']['agent_contacts'][0]['name']
      }

  if @result['org_code']
    if @result['country']
      md['identifier'] = @result['country']+'-'+ @result['org_code']
    else
      md['identifier'] = @result['org_code']
    end
  end

  if @result['repo_info']

    md['description'] = @result['repo_info']['top']['description'] if @result['repo_info']['top'] && @result['repo_info']['top']['description']
    md['email'] = @result['repo_info']['email'] if @result['repo_info']['email']

    if @result['repo_info']['telephones']
      md['faxNumber'] = @result['repo_info']['telephones']
        .select {|t| t['number_type'] == 'fax'}
        .map {|f| f['number']}

      md['telephone'] = @result['repo_info']['telephones']
        .select {|t| t['number_type'] == 'business'}
        .map {|b| b['number']}
    end

    if @result['repo_info']['address']
      md['address'] = {
        '@type' => 'PostalAddress',
        'streetAddress' => @result['repo_info']['address'].join(", "),
        'addressLocality' => @result['repo_info']['city'],
        'addressRegion' => @result['repo_info']['region'],
        'postalCode' => @result['repo_info']['post_code'],
        'addressCountry' => @result['repo_info']['country']
      }
    end
  end

  md.compact
end

#newObject



16
17
18
19
20
21
22
# File 'frontend/app/controllers/repositories_controller.rb', line 16

def new
  @enum = JSONModel(:enumeration).find("/names/archival_record_level")
  @repository = JSONModel(:repository_with_agent).new('repository' => {},
                                                      'agent_representation' => {
                                                        'agent_contacts' => [{}]
                                                      })._always_valid!
end

#run_transferObject



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'frontend/app/controllers/repositories_controller.rb', line 150

def run_transfer
  old_uri = JSONModel(:repository).uri_for(params[:id])
  response = JSONModel::HTTP.post_form(JSONModel(:repository).uri_for(params[:id]) + "/transfer",
                                       "target_repo" => params[:ref])

  if response.code == '200'
    flash[:success] = I18n.t("actions.transfer_successful")
    redirect_to(:action => :index)
  else
    @transfer_errors = ASUtils.json_parse(response.body)['error']
    return transfer
  end
end

#searchObject



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 'public/app/controllers/repositories_controller.rb', line 55

def search
  @repo_id = params.require(:rid)
  @base_search = "/repositories/#{repo_id}/search?"
  begin
    new_search_opts = DEFAULT_REPO_SEARCH_OPTS
    new_search_opts['repo_id'] = @repo_id
    set_up_advanced_search(DEFAULT_TYPES, DEFAULT_SEARCH_FACET_TYPES, new_search_opts, params)
  #   NOTE the redirect back here on error!
  rescue Exception => error
    Rails.logger.debug( error.backtrace )
    flash[:error] = I18n.t('errors.unexpected_error')
    redirect_back(fallback_location: "/repositories/#{@repo_id}/" ) and return
  end
  page = Integer(params.fetch(:page, "1"))
  @results = archivesspace.advanced_search('/search', page, @criteria)
  if @results['total_hits'].blank? || @results['total_hits'] == 0
    flash[:notice] = I18n.t('search_results.no_results')
    redirect_back(fallback_location: @base_search)
  else
    process_search_results(@base_search)
    Rails.logger.debug("@repo_id: #{@repo_id}")
    render
  end
end

#selectObject



115
116
117
118
119
120
121
122
123
124
125
126
# File 'frontend/app/controllers/repositories_controller.rb', line 115

def select
  # Clear top container previous search when switching repositories
  session[:top_container_previous_search] = {}

  selected = @repositories.find {|r| r.id.to_s == params[:id]}
  self.class.session_repo(session, selected.uri, selected.slug)
  set_user_repository_cookie selected.uri

  flash[:success] = I18n.t("repository._frontend.messages.changed", JSONModelI18nWrapper.new(:repository => selected))

  redirect_to :root
end

#showObject



109
110
111
112
113
# File 'frontend/app/controllers/repositories_controller.rb', line 109

def show
  @repository = JSONModel(:repository_with_agent).find(params[:id])
  flash.now[:info] = I18n.t("repository._frontend.messages.selected") if @repository.id === session[:repo_id]
  @enum = JSONModel(:enumeration).find("/names/archival_record_level")
end

#transferObject



144
145
146
147
# File 'frontend/app/controllers/repositories_controller.rb', line 144

def transfer
  @repository = JSONModel(:repository_with_agent).find(params[:id])
  render :transfer
end

#typeaheadObject



165
166
167
# File 'frontend/app/controllers/repositories_controller.rb', line 165

def typeahead
  render :json => Search.global(params_for_backend_search, "repositories")
end

#updateObject



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

def update
  handle_repository_oai_params(params)
  generate_names(params[:repository])
  handle_crud(:instance => :repository,
              :model => JSONModel(:repository_with_agent),
              :replace => false,
              :obj => JSONModel(:repository_with_agent).find(params[:id]),
              :on_invalid => ->() { return render :action => :edit },
              :on_valid => ->(id) {
                MemoryLeak::Resources.refresh(:repository)

                flash[:success] = I18n.t("repository._frontend.messages.updated", JSONModelI18nWrapper.new(:repository => @repository))

                if AppConfig[:use_human_readable_urls] && params["repository"]["repository"] &&
                   (params["repository"]["repository"]["slug"].nil? ||
                    params["repository"]["repository"]["slug"].empty?)  &&
                   !params["repository"]["repository"]["is_slug_auto"]

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

                redirect_to :controller => :repositories, :action => :show, :id => id
              })
end