enju_nii 0.0.4 → 0.1.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) hide show
  1. data/Rakefile +1 -1
  2. data/app/controllers/cinii_books_controller.rb +48 -0
  3. data/app/helpers/cinii_books_helper.rb +15 -0
  4. data/app/models/cinii_book.rb +66 -0
  5. data/app/views/cinii_books/index.html.erb +49 -0
  6. data/app/views/layouts/cinii_books.html.erb +23 -0
  7. data/app/views/nii_types/index.html.erb +2 -2
  8. data/config/locales/en.yml +10 -0
  9. data/config/locales/ja.yml +8 -0
  10. data/config/routes.rb +1 -1
  11. data/db/migrate/20121113163717_add_ncid_to_manifestation.rb +6 -0
  12. data/lib/enju_nii.rb +22 -0
  13. data/lib/enju_nii/cinii_book.rb +156 -0
  14. data/lib/enju_nii/engine.rb +7 -3
  15. data/lib/enju_nii/version.rb +1 -1
  16. data/lib/generators/enju_nii/views_generator.rb +17 -0
  17. data/spec/cassette_library/enju_nii/search.yml +876 -0
  18. data/spec/controllers/cinii_books_controller_spec.rb +24 -0
  19. data/spec/dummy/app/models/role.rb +47 -0
  20. data/spec/dummy/app/models/setting.rb +4 -0
  21. data/spec/dummy/app/models/user.rb +28 -0
  22. data/spec/dummy/app/models/user_has_role.rb +4 -0
  23. data/spec/dummy/config/application.yml +38 -0
  24. data/spec/dummy/config/initializers/devise.rb +205 -0
  25. data/spec/dummy/config/routes.rb +2 -0
  26. data/spec/dummy/db/development.sqlite3 +0 -0
  27. data/spec/dummy/db/migrate/001_create_patrons.rb +53 -0
  28. data/spec/dummy/db/migrate/005_create_manifestations.rb +56 -0
  29. data/spec/dummy/db/migrate/006_create_items.rb +28 -0
  30. data/spec/dummy/db/migrate/012_create_owns.rb +12 -0
  31. data/spec/dummy/db/migrate/015_create_creates.rb +12 -0
  32. data/spec/dummy/db/migrate/029_create_subjects.rb +27 -0
  33. data/spec/dummy/db/migrate/047_create_produces.rb +12 -0
  34. data/spec/dummy/db/migrate/073_create_carrier_types.rb +11 -0
  35. data/spec/dummy/db/migrate/112_create_frequencies.rb +12 -0
  36. data/spec/dummy/db/migrate/117_create_form_of_works.rb +12 -0
  37. data/spec/dummy/db/migrate/125_create_donates.rb +12 -0
  38. data/spec/dummy/db/migrate/142_create_classifications.rb +19 -0
  39. data/spec/dummy/db/migrate/143_create_subject_has_classifications.rb +16 -0
  40. data/spec/dummy/db/migrate/144_create_classification_types.rb +16 -0
  41. data/spec/dummy/db/migrate/145_create_subject_heading_types.rb +16 -0
  42. data/spec/dummy/db/migrate/146_create_subject_types.rb +16 -0
  43. data/spec/dummy/db/migrate/20080606052544_create_work_has_subjects.rb +18 -0
  44. data/spec/dummy/db/migrate/20080830154109_create_realizes.rb +13 -0
  45. data/spec/dummy/db/migrate/20080830172106_create_exemplifies.rb +13 -0
  46. data/spec/dummy/db/migrate/20080905191442_create_patron_types.rb +12 -0
  47. data/spec/dummy/db/migrate/20081025083323_create_countries.rb +28 -0
  48. data/spec/dummy/db/migrate/20081025083905_create_languages.rb +23 -0
  49. data/spec/dummy/db/migrate/20081027150907_create_picture_files.rb +15 -0
  50. data/spec/dummy/db/migrate/20081028083142_create_patron_import_files.rb +22 -0
  51. data/spec/dummy/db/migrate/20081028083208_create_resource_import_files.rb +22 -0
  52. data/spec/dummy/db/migrate/20090208044541_create_subject_heading_type_has_subjects.rb +16 -0
  53. data/spec/dummy/db/migrate/20090705133942_add_attachments_picture_to_picture_file.rb +15 -0
  54. data/spec/dummy/db/migrate/20090705212043_add_attachments_attachment_to_manifestation.rb +15 -0
  55. data/spec/dummy/db/migrate/20090719201843_create_extents.rb +12 -0
  56. data/spec/dummy/db/migrate/20090720091106_create_medium_of_performances.rb +12 -0
  57. data/spec/dummy/db/migrate/20090720091429_create_content_types.rb +12 -0
  58. data/spec/dummy/db/migrate/20090812151902_create_patron_relationship_types.rb +12 -0
  59. data/spec/dummy/db/migrate/20091012101112_add_dcndl_schema.rb +27 -0
  60. data/spec/dummy/db/migrate/20091025080447_create_licenses.rb +12 -0
  61. data/spec/dummy/db/migrate/20091202124834_create_versions.rb +18 -0
  62. data/spec/dummy/db/migrate/20091214131723_create_series_statements.rb +13 -0
  63. data/spec/dummy/db/migrate/20100129142347_create_import_requests.rb +15 -0
  64. data/spec/dummy/db/migrate/20100223121519_rename_series_statement_title_to_original_title.rb +13 -0
  65. data/spec/dummy/db/migrate/20100321235924_add_series_statement_identifier_to_series_statement.rb +10 -0
  66. data/spec/dummy/db/migrate/20100525124311_create_manifestation_relationships.rb +13 -0
  67. data/spec/dummy/db/migrate/20100606073747_create_patron_relationships.rb +13 -0
  68. data/spec/dummy/db/migrate/20100607044753_create_manifestation_relationship_types.rb +12 -0
  69. data/spec/dummy/db/migrate/20100814091104_add_position_to_patron_relationship.rb +11 -0
  70. data/spec/dummy/db/migrate/20100925043847_create_resource_import_results.rb +15 -0
  71. data/spec/dummy/db/migrate/20100925074559_create_patron_import_results.rb +12 -0
  72. data/spec/dummy/db/migrate/20101212070145_add_acquired_at_to_item.rb +9 -0
  73. data/spec/dummy/db/migrate/20110301035123_add_pub_date_to_manifestation.rb +9 -0
  74. data/spec/dummy/db/migrate/20110301121550_add_birth_date_and_death_date_to_patron.rb +11 -0
  75. data/spec/dummy/db/migrate/20110425133109_add_issn_to_series_statement.rb +9 -0
  76. data/spec/dummy/db/migrate/20110603184217_add_edit_mode_to_resource_import_file.rb +9 -0
  77. data/spec/dummy/db/migrate/20110618091240_add_periodical_to_series_statement.rb +9 -0
  78. data/spec/dummy/db/migrate/20110619064807_add_edition_string_to_manifestation.rb +9 -0
  79. data/spec/dummy/db/migrate/20110620173525_add_bookstore_id_to_item.rb +11 -0
  80. data/spec/dummy/db/migrate/20110627034940_create_series_statement_merge_lists.rb +9 -0
  81. data/spec/dummy/db/migrate/20110627035057_create_series_statement_merges.rb +12 -0
  82. data/spec/dummy/db/migrate/20110820131417_create_series_has_manifestations.rb +13 -0
  83. data/spec/dummy/db/migrate/20110913120629_add_lft_and_rgt_to_classification.rb +11 -0
  84. data/spec/dummy/db/migrate/20110916053430_rename_manifestation_number_list_to_number_string.rb +13 -0
  85. data/spec/dummy/db/migrate/20110916091020_add_volume_number_to_manifestation.rb +13 -0
  86. data/spec/dummy/db/migrate/20110916103953_add_manifestaiton_id_to_series_statement.rb +10 -0
  87. data/spec/dummy/db/migrate/20110918161853_rename_series_statement_manifestation_id_to_root_manifestation_id.rb +13 -0
  88. data/spec/dummy/db/migrate/20110918162329_add_note_to_series_statement.rb +9 -0
  89. data/spec/dummy/db/migrate/20111009183423_add_ndc_to_manifestation.rb +9 -0
  90. data/spec/dummy/db/migrate/20111124110059_create_create_types.rb +12 -0
  91. data/spec/dummy/db/migrate/20111124110319_create_realize_types.rb +12 -0
  92. data/spec/dummy/db/migrate/20111124110355_create_produce_types.rb +12 -0
  93. data/spec/dummy/db/migrate/20111124112131_add_create_type_to_create.rb +7 -0
  94. data/spec/dummy/db/migrate/20111201121844_create_roles.rb +12 -0
  95. data/spec/dummy/db/migrate/20111201155456_create_users.rb +13 -0
  96. data/spec/dummy/db/migrate/20111201155513_add_devise_to_users.rb +52 -0
  97. data/spec/dummy/db/migrate/20111201163718_create_user_has_roles.rb +10 -0
  98. data/spec/dummy/db/migrate/20120125152919_add_title_subseries_transcription_to_series_statement.rb +6 -0
  99. data/spec/dummy/db/migrate/20120129020544_add_budget_type_id_to_item.rb +6 -0
  100. data/spec/dummy/db/migrate/20120319120638_add_content_type_id_to_manifestation.rb +6 -0
  101. data/spec/dummy/db/migrate/20120406020752_add_url_to_subject.rb +5 -0
  102. data/spec/dummy/db/migrate/20120410104851_add_year_of_publication_to_manifestation.rb +5 -0
  103. data/spec/dummy/db/migrate/20120413072700_add_picture_meta_to_picture_file.rb +5 -0
  104. data/spec/dummy/db/migrate/20120413100352_add_fingerprint_to_picture_file.rb +5 -0
  105. data/spec/dummy/db/migrate/20120413161340_add_fingerprint_to_resource_import_file.rb +5 -0
  106. data/spec/dummy/db/migrate/20120413161403_add_fingerprint_to_patron_import_file.rb +5 -0
  107. data/spec/dummy/db/migrate/20120413170705_add_error_message_to_resource_import_file.rb +5 -0
  108. data/spec/dummy/db/migrate/20120413170720_add_error_message_to_patron_import_file.rb +5 -0
  109. data/spec/dummy/db/migrate/20120415060308_rename_resource_import_file_imported_at_to_executed_at.rb +9 -0
  110. data/spec/dummy/db/migrate/20120415060323_rename_patron_import_file_imported_at_to_executed_at.rb +9 -0
  111. data/spec/dummy/db/migrate/20120415164821_add_attachment_meta_to_manifestation.rb +5 -0
  112. data/spec/dummy/db/migrate/20120418081407_add_month_of_publication_to_manifestation.rb +5 -0
  113. data/spec/dummy/db/migrate/20120511072422_add_patron_identifier_to_patron.rb +6 -0
  114. data/spec/dummy/db/migrate/20120602141129_add_edit_mode_to_patron_import_file.rb +5 -0
  115. data/spec/dummy/db/schema.rb +738 -0
  116. data/spec/dummy/db/test.sqlite3 +0 -0
  117. data/spec/fixtures/carrier_types.yml +51 -0
  118. data/spec/fixtures/content_types.yml +19 -0
  119. data/spec/fixtures/countries.yml +2059 -0
  120. data/spec/fixtures/languages.yml +1901 -0
  121. data/spec/fixtures/patron_types.yml +35 -0
  122. data/spec/fixtures/roles.yml +21 -0
  123. data/spec/fixtures/subject_types.yml +28 -0
  124. data/spec/fixtures/user_has_roles.yml +41 -0
  125. data/spec/fixtures/users.yml +79 -0
  126. data/spec/models/cinii_book_spec.rb +18 -0
  127. data/spec/spec_helper.rb +12 -11
  128. data/spec/support/controller_macros.rb +1 -24
  129. data/spec/support/vcr.rb +4 -0
  130. metadata +309 -12
data/Rakefile CHANGED
@@ -20,7 +20,7 @@ RDoc::Task.new(:rdoc) do |rdoc|
20
20
  rdoc.rdoc_files.include('lib/**/*.rb')
21
21
  end
22
22
 
23
- APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
23
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
24
24
  load 'rails/tasks/engine.rake'
25
25
 
26
26
 
@@ -0,0 +1,48 @@
1
+ class CiniiBooksController < ApplicationController
2
+ before_filter :authenticate_user!
3
+ before_filter :check_librarian
4
+
5
+ def index
6
+ if params[:page].to_i == 0
7
+ page = 1
8
+ else
9
+ page = params[:page]
10
+ end
11
+ @query = params[:query].to_s.strip
12
+ books = CiniiBook.search(params[:query], page)
13
+ @books = Kaminari.paginate_array(
14
+ books[:items], :total_count => books[:total_entries], :page => page
15
+ ).page(page).per(10)
16
+
17
+ respond_to do |format|
18
+ format.html
19
+ end
20
+ end
21
+
22
+ def create
23
+ if params[:book]
24
+ @manifestation = CiniiBook.import_ncid(params[:book][:ncid])
25
+ respond_to do |format|
26
+ if @manifestation.try(:save)
27
+ flash[:notice] = t('controller.successfully_created', :model => t('activerecord.models.manifestation'))
28
+ format.html { redirect_to manifestation_items_url(@manifestation) }
29
+ else
30
+ flash[:notice] = t('enju_ndl.record_not_found')
31
+ format.html { redirect_to ndl_books_url }
32
+ end
33
+ end
34
+ end
35
+ #rescue ActiveRecord::RecordInvalid => e
36
+ # respond_to do |format|
37
+ # flash[:notice] = e.message
38
+ # format.html { render :action => "index" }
39
+ # end
40
+ end
41
+
42
+ private
43
+ def check_librarian
44
+ unless current_user.try(:has_role?, 'Librarian')
45
+ access_denied
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,15 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module CiniiBooksHelper
3
+ def link_to_import_from_cinii(ncid)
4
+ if ncid.blank?
5
+ t('enju_nii.not_available')
6
+ else
7
+ manifestation = Manifestation.where(:ncid => ncid).first
8
+ unless manifestation
9
+ link_to t('enju_nii.add'), cinii_books_path(:book => {:ncid => ncid}), :method => :post
10
+ else
11
+ link_to t('enju_nii.already_exists'), manifestation
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,66 @@
1
+ # -*- encoding: utf-8 -*-
2
+ class CiniiBook
3
+ attr_reader :title, :creator, :publisher, :link, :ncid, :issued
4
+
5
+ def initialize(node)
6
+ @node = node
7
+ end
8
+
9
+ def title
10
+ @node.at('./xmlns:title').try(:content)
11
+ end
12
+
13
+ def creator
14
+ @node.at('./dc:creator').try(:content)
15
+ end
16
+
17
+ def publisher
18
+ @node.at('./dc:publisher').try(:content)
19
+ end
20
+
21
+ def link
22
+ @node.at('./xmlns:link').try(:content)
23
+ end
24
+
25
+ def ncid
26
+ url = @node.attributes['about'].try(:content)
27
+ if url
28
+ URI.parse(url).path.split('/').reverse.first
29
+ end
30
+ end
31
+
32
+ def issued
33
+ @node.at('./dc:date').try(:content)
34
+ end
35
+
36
+ def self.per_page
37
+ 10
38
+ end
39
+
40
+ def self.search(query, page = 1, per_page = self.per_page)
41
+ if query
42
+ cnt = self.per_page
43
+ page = 1 if page.to_i < 1
44
+ doc = Nokogiri::XML(Manifestation.search_cinii_book(query, {:p => page, :count => cnt, :raw => true}).to_s)
45
+ items = doc.xpath('//xmlns:item').collect{|node| self.new node }
46
+ total_entries = doc.at('//opensearch:totalResults').content.to_i
47
+
48
+ {:items => items, :total_entries => total_entries}
49
+ else
50
+ {:items => [], :total_entries => 0}
51
+ end
52
+ end
53
+
54
+ def self.import_ncid(ncid)
55
+ manifestation = Manifestation.where(:ncid => ncid).first
56
+ return if manifestation
57
+ url = "http://ci.nii.ac.jp/ncid/#{ncid}.rdf"
58
+ doc = Nokogiri::XML(open(url).read)
59
+ Manifestation.import_record_from_cinii_books(doc)
60
+ end
61
+
62
+ attr_accessor :url
63
+
64
+ class AlreadyImported < StandardError
65
+ end
66
+ end
@@ -0,0 +1,49 @@
1
+ <div id="content_detail" class="ui-corner-all">
2
+ <h1 class="title"><%= t('enju_nii.import_from_cinii_books') %></h1>
3
+ <div id="content_list">
4
+ <p id="notice"><%= notice %></p>
5
+
6
+ <%= form_for :books, :url => cinii_books_path, :html => {:method => 'get'} do -%>
7
+ <p>
8
+ <%= label_tag :search_form_top, t('page.search_term') -%>:
9
+ <%= search_field_tag 'query', h(@query), {:id => 'search_form_top', :class => 'search_form'} -%>
10
+ <%= submit_tag t('page.search') -%>
11
+ </p>
12
+ <%- end -%>
13
+
14
+ <% if @query.present? %>
15
+ <p><%= t('enju_nii.number_of_search_results', :count => @books.total_count %></p>
16
+
17
+ <table class="table table-striped index">
18
+ <tr>
19
+ <th></th>
20
+ <th><%= t('activerecord.attributes.manifestation.original_title') %></th>
21
+ </tr>
22
+ <% @books.each do |book| %>
23
+ <tr class="line<%= cycle("0", "1") -%>">
24
+ <td>
25
+ <%= link_to_import_from_cinii(book.ncid) %>
26
+ </td>
27
+ <td>
28
+ <%= link_to "#{book.title}", book.link %>
29
+ <br />
30
+ <%= book.creator %>
31
+ <%= book.publisher %>
32
+ (<%= book.issued %>)
33
+ </td>
34
+ </tr>
35
+ <% end %>
36
+ </table>
37
+
38
+ <br />
39
+
40
+ <%= paginate(@books) %>
41
+ <% else %>
42
+ <%= javascript_tag("$('#search_form_top').focus()") -%>
43
+ <% end %>
44
+
45
+ </div>
46
+ </div>
47
+
48
+ <div id="submenu" class="ui-corner-all">
49
+ </div>
@@ -0,0 +1,23 @@
1
+ <!DOCTYPE html>
2
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<%= @locale.to_s -%>" lang="<%= @locale.to_s -%>">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <%= render 'page/include' %>
6
+ <%= render 'page/portlets' %>
7
+ <title><%= t('enju_nii.import_from_cinii_books') %></title>
8
+ </head>
9
+ <body itemscope itemtype="http://schema.org/WebPage">
10
+
11
+ <%= render 'page/header' %>
12
+ <%= render 'page/menu' %>
13
+
14
+ <div id="content">
15
+
16
+ <%= yield %>
17
+
18
+ </div>
19
+
20
+ <%= render 'page/footer' %>
21
+
22
+ </body>
23
+ </html>
@@ -2,7 +2,7 @@
2
2
  <h1 class="title"><%= t('page.listing', :model => t('activerecord.models.nii_type')) -%></h1>
3
3
  <div id="content_list">
4
4
 
5
- <table class="index">
5
+ <table class="table table-striped index">
6
6
  <tr>
7
7
  <th id="position"></th>
8
8
  <th><%= t('activerecord.attributes.nii_type.name') -%></th>
@@ -28,7 +28,7 @@
28
28
  <%= link_to t('page.edit'), edit_nii_type_path(nii_type) -%>
29
29
  <% end %>
30
30
  <%- if can? :destroy, nii_type -%>
31
- <%= link_to t('page.destroy'), nii_type, :confirm => t('page.are_you_sure'), :method => :delete -%>
31
+ <%= link_to t('page.destroy'), nii_type, :data => {:confirm => t('page.are_you_sure')}, :method => :delete -%>
32
32
  <% end -%>
33
33
  </td>
34
34
  </tr>
@@ -0,0 +1,10 @@
1
+ en:
2
+ enju_nii:
3
+ import_from_cinii_books: Import from CiNii Books
4
+ record_not_found: Record not found.
5
+ not_available: Not available
6
+ add: Add
7
+ already_exists: Already exists
8
+ number_of_search_results:
9
+ one: "1 record found."
10
+ other: "%{count} records found."
@@ -0,0 +1,8 @@
1
+ ja:
2
+ enju_nii:
3
+ import_from_cinii_books: CiNii Books検索によるインポート
4
+ record_not_found: 資料が見つかりません。
5
+ not_available: 利用不可
6
+ add: 追加
7
+ already_exists: 登録済み
8
+ number_of_search_results: "%{count} 件の資料が見つかりました。"
@@ -1,4 +1,4 @@
1
1
  Rails.application.routes.draw do
2
2
  resources :nii_types
3
-
3
+ resources :cinii_books, :only => [:index, :create]
4
4
  end
@@ -0,0 +1,6 @@
1
+ class AddNcidToManifestation < ActiveRecord::Migration
2
+ def change
3
+ add_column :manifestations, :ncid, :string
4
+ add_index :manifestations, :ncid
5
+ end
6
+ end
@@ -1,4 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
1
2
  require "enju_nii/engine"
3
+ require 'open-uri'
4
+ require 'enju_nii/cinii_book'
2
5
 
3
6
  module EnjuNii
7
+ module ActsAsMethods
8
+ def self.included(base)
9
+ base.extend ClassMethods
10
+ end
11
+
12
+ module ClassMethods
13
+ def enju_nii_cinii_books
14
+ include EnjuNii::CiNiiBook
15
+ end
16
+ end
17
+ end
18
+
19
+ class RecordNotFound < StandardError
20
+ end
21
+
22
+ class InvalidIsbn < StandardError
23
+ end
4
24
  end
25
+
26
+ ActiveRecord::Base.send :include, EnjuNii::ActsAsMethods
@@ -0,0 +1,156 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module EnjuNii
3
+ module CiNiiBook
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def import_from_cinii(options = {:isbn => isbn})
10
+ return nil unless isbn
11
+ lisbn = Lisbn.new(isbn)
12
+ raise EnjuNii::InvalidIsbn unless lisbn.valid?
13
+
14
+ manifestation = Manifestation.find_by_isbn(lisbn.isbn)
15
+ return manifestation if manifestation
16
+
17
+ doc = return_rdf(lisbn.isbn)
18
+ raise EnjuNii::RecordNotFound unless doc
19
+ #raise EnjuNii::RecordNotFound if doc.at('//openSearch:totalResults').content.to_i == 0
20
+ import_record_from_cinii_books(doc)
21
+ end
22
+
23
+ def import_record_from_cinii_books(doc)
24
+ # http://ci.nii.ac.jp/info/ja/terms.html
25
+ return nil
26
+
27
+ ncid = doc.at('//cinii:ncid').try(:content)
28
+ manifestation = Manifestation.where(:ncid => ncid).first if ncid
29
+ return manifestation if manifestation
30
+
31
+ creators = get_creator(doc)
32
+ publishers = get_publisher(doc)
33
+
34
+ # title
35
+ title = get_title(doc)
36
+ manifestation = Manifestation.new(title)
37
+
38
+ # date of publication
39
+ pub_date = doc.at('//dc:date').try(:content)
40
+ if pub_date
41
+ date = pub_date.split('-')
42
+ if date[0] and date[1]
43
+ date = sprintf("%04d-%02d", date[0], date[1])
44
+ else
45
+ date = pub_date
46
+ end
47
+ end
48
+ manifestation.pub_date = pub_date
49
+
50
+ language = Language.where(:iso_639_3 => get_language(doc)).first
51
+ if language
52
+ manifestation.language_id = language.id
53
+ else
54
+ manifestation.language_id = 1
55
+ end
56
+
57
+ begin
58
+ urn = doc.at("//dcterms:hasPart[@rdf:resource]").attributes["resource"].value
59
+ if urn =~ /^urn:isbn/
60
+ manifestation.isbn = Lisbn.new(urn.gsub(/^urn:isbn:/, ""))
61
+ end
62
+ rescue NoMethodError
63
+ end
64
+
65
+ manifestation.carrier_type = CarrierType.where(:name => 'print').first
66
+ manifestation.manifestation_content_type = ContentType.where(:name => 'text').first
67
+ manifestation.ncid = ncid
68
+
69
+ if manifestation.valid?
70
+ #Patron.transaction do
71
+ manifestation.save!
72
+ publisher_patrons = Patron.import_patrons(publishers)
73
+ creator_patrons = Patron.import_patrons(creators)
74
+ manifestation.publishers = publisher_patrons
75
+ manifestation.creators = creator_patrons
76
+ #end
77
+ end
78
+
79
+ manifestation
80
+ end
81
+
82
+ def search_cinii_book(query, options = {})
83
+ options = {:p => 1, :count => 10, :raw => false}.merge(options)
84
+ doc = nil
85
+ results = {}
86
+ startrecord = options[:idx].to_i
87
+ if startrecord == 0
88
+ startrecord = 1
89
+ end
90
+ url = "http://ci.nii.ac.jp/books/opensearch/search?q=#{URI.escape(query)}&p=#{options[:p]}&count=#{options[:count]}&format=rss"
91
+ if options[:raw] == true
92
+ open(url).read
93
+ else
94
+ RSS::RDF::Channel.install_text_element("opensearch:totalResults", "http://a9.com/-/spec/opensearch/1.1/", "?", "totalResults", :text, "opensearch:totalResults")
95
+ RSS::BaseListener.install_get_text_element("http://a9.com/-/spec/opensearch/1.1/", "totalResults", "totalResults=")
96
+ feed = RSS::Parser.parse(url, false)
97
+ end
98
+ end
99
+
100
+ def return_rdf(isbn)
101
+ rss = self.search_cinii_by_isbn(isbn)
102
+ if rss.channel.totalResults.to_i == 0
103
+ rss = self.search_cinii_by_isbn(normalize_isbn(isbn))
104
+ end
105
+ if rss.items.first
106
+ Nokogiri::XML(open("#{rss.items.first.link}.rdf").read)
107
+ end
108
+ end
109
+
110
+ def search_cinii_by_isbn(isbn)
111
+ url = "http://ci.nii.ac.jp/books/opensearch/search?isbn=#{isbn}&format=rss"
112
+ RSS::RDF::Channel.install_text_element("opensearch:totalResults", "http://a9.com/-/spec/opensearch/1.1/", "?", "totalResults", :text, "opensearch:totalResults")
113
+ RSS::BaseListener.install_get_text_element("http://a9.com/-/spec/opensearch/1.1/", "totalResults", "totalResults=")
114
+ rss = RSS::Parser.parse(url, false)
115
+ end
116
+
117
+ private
118
+ def normalize_isbn(isbn)
119
+ if isbn.length == 10
120
+ Lisbn.new(isbn).isbn13
121
+ else
122
+ Lisbn.new(isbn).isbn10
123
+ end
124
+ end
125
+
126
+ def get_creator(doc)
127
+ doc.xpath("//foaf:maker/foaf:Person").map{|e|
128
+ {
129
+ :full_name => e.at("./foaf:name").content,
130
+ :full_name_transcription => e.xpath("./foaf:name[@xml:lang]").map{|n| n.content}.join("\n"),
131
+ :patron_identifier => e.attributes["about"].try(:content)
132
+ }
133
+ }
134
+ end
135
+
136
+ def get_publisher(doc)
137
+ doc.xpath("//dc:publisher").map{|e| {:full_name => e.content}}
138
+ end
139
+
140
+ def get_title(doc)
141
+ {
142
+ :original_title => doc.at("//dc:title[not(@xml:lang)]").content,
143
+ :title_transcription => doc.xpath("//dc:title[@xml:lang]").map{|e| e.try(:content)}.join("\n"),
144
+ :title_alternative => doc.xpath("//dcterms:alternative").map{|e| e.try(:content)}.join("\n")
145
+ }
146
+ end
147
+
148
+ def get_language(doc)
149
+ doc.at("//dc:language").try(:content)
150
+ end
151
+ end
152
+
153
+ class AlreadyImported < StandardError
154
+ end
155
+ end
156
+ end