ruby-fedora 0.1.2 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/COPYING.LESSER.txt +165 -0
  2. data/COPYING.txt +674 -0
  3. data/Manifest.txt +19 -20
  4. data/README.txt +6 -1
  5. data/Rakefile +4 -0
  6. data/config/hoe.rb +2 -2
  7. data/config/requirements.rb +9 -2
  8. data/lib/active_fedora.rb +41 -0
  9. data/lib/active_fedora/base.rb +278 -8
  10. data/lib/active_fedora/content_model.rb +22 -0
  11. data/lib/active_fedora/datastream.rb +95 -0
  12. data/lib/active_fedora/fedora_object.rb +84 -0
  13. data/lib/active_fedora/metadata_datastream.rb +97 -0
  14. data/lib/active_fedora/model.rb +94 -0
  15. data/lib/active_fedora/property.rb +15 -0
  16. data/lib/active_fedora/qualified_dublin_core_datastream.rb +72 -0
  17. data/lib/active_fedora/relationship.rb +43 -0
  18. data/lib/active_fedora/rels_ext_datastream.rb +43 -0
  19. data/lib/active_fedora/semantic_node.rb +221 -0
  20. data/lib/active_fedora/solr_service.rb +20 -0
  21. data/lib/fedora/base.rb +2 -1
  22. data/lib/fedora/connection.rb +104 -134
  23. data/lib/fedora/datastream.rb +10 -1
  24. data/lib/fedora/fedora_object.rb +28 -24
  25. data/lib/fedora/generic_search.rb +71 -0
  26. data/lib/fedora/repository.rb +47 -3
  27. data/lib/ruby-fedora.rb +9 -8
  28. data/lib/util/class_level_inheritable_attributes.rb +23 -0
  29. data/solr/config/schema.xml +229 -0
  30. metadata +37 -24
  31. data/lib/active-fedora.rb +0 -1
  32. data/lib/ambition/adapters/active_fedora.rb +0 -10
  33. data/lib/ambition/adapters/active_fedora/base.rb +0 -14
  34. data/lib/ambition/adapters/active_fedora/query.rb +0 -48
  35. data/lib/ambition/adapters/active_fedora/select.rb +0 -104
  36. data/lib/ambition/adapters/active_fedora/slice.rb +0 -19
  37. data/lib/ambition/adapters/active_fedora/sort.rb +0 -43
  38. data/script/destroy +0 -14
  39. data/script/generate +0 -14
  40. data/script/txt2html +0 -74
  41. data/tasks/deployment.rake +0 -34
  42. data/tasks/environment.rake +0 -7
  43. data/tasks/website.rake +0 -17
  44. data/website/index.html +0 -93
  45. data/website/index.txt +0 -39
  46. data/website/javascripts/rounded_corners_lite.inc.js +0 -285
  47. data/website/stylesheets/screen.css +0 -138
  48. data/website/template.rhtml +0 -48
data/Manifest.txt CHANGED
@@ -1,3 +1,5 @@
1
+ COPYING.LESSER.txt
2
+ COPYING.txt
1
3
  History.txt
2
4
  License.txt
3
5
  Manifest.txt
@@ -5,30 +7,27 @@ README.txt
5
7
  Rakefile
6
8
  config/hoe.rb
7
9
  config/requirements.rb
8
- lib/ruby-fedora.rb
10
+ lib/active_fedora.rb
11
+ lib/active_fedora/base.rb
12
+ lib/active_fedora/content_model.rb
13
+ lib/active_fedora/datastream.rb
14
+ lib/active_fedora/fedora_object.rb
15
+ lib/active_fedora/metadata_datastream.rb
16
+ lib/active_fedora/model.rb
17
+ lib/active_fedora/property.rb
18
+ lib/active_fedora/qualified_dublin_core_datastream.rb
19
+ lib/active_fedora/relationship.rb
20
+ lib/active_fedora/rels_ext_datastream.rb
21
+ lib/active_fedora/semantic_node.rb
22
+ lib/active_fedora/solr_service.rb
9
23
  lib/fedora/base.rb
10
24
  lib/fedora/connection.rb
11
25
  lib/fedora/datastream.rb
12
26
  lib/fedora/fedora_object.rb
13
27
  lib/fedora/formats.rb
28
+ lib/fedora/generic_search.rb
14
29
  lib/fedora/repository.rb
15
- lib/active-fedora.rb
16
- lib/active_fedora/base.rb
17
- lib/ambition/adapters/active_fedora.rb
18
- lib/ambition/adapters/active_fedora/base.rb
19
- lib/ambition/adapters/active_fedora/query.rb
20
- lib/ambition/adapters/active_fedora/select.rb
21
- lib/ambition/adapters/active_fedora/slice.rb
22
- lib/ambition/adapters/active_fedora/sort.rb
23
- script/destroy
24
- script/generate
25
- script/txt2html
30
+ lib/ruby-fedora.rb
31
+ lib/util/class_level_inheritable_attributes.rb
26
32
  setup.rb
27
- tasks/deployment.rake
28
- tasks/environment.rake
29
- tasks/website.rake
30
- website/index.html
31
- website/index.txt
32
- website/javascripts/rounded_corners_lite.inc.js
33
- website/stylesheets/screen.css
34
- website/template.rhtml
33
+ solr/config/schema.xml
data/README.txt CHANGED
@@ -29,6 +29,11 @@ To run the active-fedora specs, install the following gems:
29
29
 
30
30
  Then run "rake spec"
31
31
 
32
+ == Package a new gem from ruby_fedora source
33
+
34
+ rake pkg
35
+
36
+
32
37
  == TODO
33
38
 
34
- - remove anything in test/* that are no longer relevant
39
+ - remove anything in test/* that are no longer relevant
data/Rakefile CHANGED
@@ -1,5 +1,9 @@
1
1
  require 'config/requirements'
2
2
  require 'config/hoe' # setup Hoe + all gem configuration
3
+ require 'rake/clean'
3
4
 
4
5
  Dir['tasks/**/*.rake'].each { |rake| load rake }
6
+ CLEAN.include('**/*.orig')
7
+
8
+ task :default=>:spec
5
9
 
data/config/hoe.rb CHANGED
@@ -31,7 +31,7 @@ end
31
31
  REV = nil
32
32
  # UNCOMMENT IF REQUIRED:
33
33
  # REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
34
- VERS = RubyFedora::VERSION::STRING + (REV ? ".#{REV}" : "")
34
+ VERS = Fedora::VERSION::STRING + (REV ? ".#{REV}" : "")
35
35
  RDOC_OPTS = ['--quiet', '--title', 'RubyFedora documentation',
36
36
  "--opname", "index.html",
37
37
  "--line-numbers",
@@ -65,4 +65,4 @@ end
65
65
  CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
66
66
  PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
67
67
  hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
68
- hoe.rsync_args = '-av --delete --ignore-errors'
68
+ hoe.rsync_args = '-av --delete --ignore-errors'
@@ -2,9 +2,16 @@ require 'fileutils'
2
2
  include FileUtils
3
3
 
4
4
  require 'rubygems'
5
- %w[rake hoe newgem rubigen].each do |req_gem|
5
+ %w[rake hoe rubigen facets extlib xml-simple solr-ruby mime-types activeresource].each do |req_gem|
6
+ case req_gem
7
+ when "solr-ruby": req_file_name = "solr"
8
+ when "mime-types": req_file_name = "mime/types"
9
+ when "xml-simple": req_file_name = "xmlsimple"
10
+ when "activeresource": req_file_name = "active_resource"
11
+ else req_file_name = req_gem
12
+ end
6
13
  begin
7
- require req_gem
14
+ require req_file_name
8
15
  rescue LoadError
9
16
  puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
17
  puts "Installation: gem install #{req_gem} -y"
@@ -0,0 +1,41 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__) unless
2
+ $LOAD_PATH.include?(File.dirname(__FILE__)) ||
3
+ $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
4
+
5
+ # Currently using ExtLib::Assertions. Could just pull that method into this code base...
6
+ gem 'extlib', '>=0.9.5'
7
+ require 'extlib'
8
+
9
+ module ActiveFedora #:nodoc:
10
+ module VERSION #:nodoc:
11
+ unless defined? MAJOR
12
+ MAJOR = 0
13
+ MINOR = 9
14
+ TINY = 0
15
+
16
+ STRING = [MAJOR, MINOR, TINY].join('.')
17
+ end
18
+ end
19
+
20
+ def self.repository
21
+ #RubyFedora::Repository.instance == nil? FedoraRepository.register(Repository.default_name) | Repository.instance
22
+ end
23
+
24
+ end
25
+
26
+
27
+ require 'active_fedora/base'
28
+ require 'active_fedora/model'
29
+ require 'active_fedora/property'
30
+ require 'active_fedora/metadata_datastream'
31
+ require 'active_fedora/qualified_dublin_core_datastream'
32
+ require 'active_fedora/fedora_object'
33
+ require 'active_fedora/semantic_node'
34
+ require 'active_fedora/rels_ext_datastream'
35
+ require 'active_fedora/solr_service'
36
+ require 'active_fedora/content_model'
37
+
38
+
39
+
40
+
41
+
@@ -1,22 +1,292 @@
1
+ require 'ruby-fedora'
2
+ require "rexml/document"
3
+
4
+ require 'util/class_level_inheritable_attributes'
5
+ require 'active_fedora/datastream'
6
+ require 'active_fedora/relationship'
7
+ require 'active_fedora/model'
8
+
9
+
1
10
  module ActiveFedora
11
+
2
12
  class Base
3
- def self.establish_connection(spec)
13
+ include MediaShelfClassLevelInheritableAttributes
14
+ ms_inheritable_attributes :ds_specs
15
+ include Model
16
+ include SemanticNode
17
+ def new_object?
18
+ @new_object
19
+ end
20
+
21
+ def initialize(attrs = {})
22
+ unless attrs[:pid]
23
+ attrs = attrs.merge!({:pid=>Fedora::Repository.instance.nextid})
24
+ @new_object=true
25
+ else
26
+ @new_object=false;
27
+ end
28
+ @inner_object = Fedora::FedoraObject.new(attrs)
29
+ @datastreams = {}
30
+ configure_defined_datastreams
31
+ end
32
+ def self.has_metadata(args, &block)
33
+ @ds_specs ||= Hash.new
34
+ @ds_specs[args[:name]]= [args[:type], block]
35
+ end
36
+
37
+ def save
38
+ # If it's a new object, set the conformsTo relationship for Fedora CMA
39
+ if new_object?
40
+ add_relationship(:conforms_to, ActiveFedora::ContentModel.pid_from_ruby_class(self.class))
41
+ end
42
+ @new_object =false
43
+ Fedora::Repository.instance.save(@inner_object)
44
+ datastreams_in_memory.each do |k,ds|
45
+ if ds.dirty? || ds.new_object?
46
+ if ds.kind_of?(ActiveFedora::MetadataDatastream)
47
+ metadata_is_dirty = true
48
+ end
49
+ ds.save
50
+ end
51
+ self.update_index if metadata_is_dirty == true
52
+ end
53
+ end
54
+
55
+ def delete
56
+ Fedora::Repository.instance.delete(@inner_object)
57
+ escaped_pid = self.pid.gsub(/(:)/, '\\:')
58
+ SolrService.instance.conn.delete(escaped_pid)
59
+ end
60
+
61
+ # Returns all known datastreams for the object. If the object has been saved to fedora, the persisted datastreams will be included.
62
+ # Datastreams that have been modified in memory are given preference over the copy in Fedora.
63
+ def datastreams
64
+ if @new_object
65
+ @datastreams = datastreams_in_memory
66
+ else
67
+ @datastreams = datastreams_in_fedora.merge(datastreams_in_memory)
68
+ end
69
+
70
+ end
71
+
72
+ def datastreams_in_fedora
73
+ mds = {}
74
+ self.datastreams_xml['datastream'].each do |ds|
75
+ ds.merge!({:pid => self.pid, :dsID => ds["dsid"]})
76
+ if ds["dsid"] == "RELS-EXT"
77
+ mds.merge!({ds["dsid"] => ActiveFedora::RelsExtDatastream.new(ds)})
78
+ else
79
+ mds.merge!({ds["dsid"] => ActiveFedora::Datastream.new(ds)})
80
+ end
81
+ mds[ds["dsid"]].new_object = false
82
+ end
83
+ mds
84
+ end
85
+
86
+ def datastreams_in_memory
87
+ @datastreams ||= Hash.new
88
+ end
89
+
90
+ def datastreams_xml
91
+ datastreams_xml = XmlSimple.xml_in(Fedora::Repository.instance.fetch_custom(self.pid, :datastreams))
92
+ end
93
+
94
+ def add_datastream(datastream)
95
+ datastream.pid = self.pid
96
+ datastreams[datastream.dsid] = datastream
97
+ return true
98
+ end
99
+ # Adds datastream to the object. Saves the datastream to fedora upon adding.
100
+ def add(datastream)
101
+ warn "Warning: ActiveFedora::Base.add has been deprected. Use add_datastream"
102
+ add_datastream(datastream)
4
103
  end
5
104
 
6
- def initialize(attrs = nil)
105
+ def metadata_streams
106
+ results = []
107
+ datastreams.each_value do |ds|
108
+ if ds.kind_of?(ActiveFedora::MetadataDatastream)
109
+ results<<ds
110
+ end
111
+ end
112
+ return results
7
113
  end
8
114
 
9
- def self.find_objects(*args)
10
- puts args.inspect
115
+ def file_streams
116
+ results = []
117
+ datastreams.each_value do |ds|
118
+ if !ds.kind_of?(ActiveFedora::MetadataDatastream)
119
+ dsid = ds.dsid
120
+ if dsid != "DC" && dsid != "RELS-EXT"
121
+ results<<ds
122
+ end
123
+ end
124
+ end
125
+ return results
11
126
  end
12
127
 
13
- def create
128
+ # DC Datastream
129
+ def dc
130
+ #dc = REXML::Document.new(datastreams["DC"].content)
131
+ return datastreams["DC"]
132
+ end
133
+
134
+ # Returns the RELS-EXT Datastream
135
+ # Tries to grab from in-memory datastreams first
136
+ # Failing that, attempts to load from Fedora and addst to in-memory datastreams
137
+ # Failing that, creates a new RelsExtDatastream and adds it to the object
138
+ def rels_ext
139
+ if !datastreams.has_key?("RELS-EXT")
140
+ add_datastream(ActiveFedora::RelsExtDatastream.new)
141
+ end
142
+ return datastreams["RELS-EXT"]
143
+ end
144
+
145
+ # @returns Hash of relationships, as defined by SemanticNode
146
+ # Rely on rels_ext datastream to track relationships array
147
+ # Overrides accessor for relationships array used by SemanticNode.
148
+ def relationships
149
+ return rels_ext.relationships
150
+ end
151
+
152
+ # Add a Rels-Ext relationship to the Object.
153
+ # @param predicate
154
+ # @param object Either a string URI or an object that responds to .pid
155
+ def add_relationship(predicate, obj)
156
+ #predicate = ActiveFedora::RelsExtDatastream.predicate_lookup(predicate)
157
+ r = ActiveFedora::Relationship.new(:subject=>:self, :predicate=>predicate, :object=>obj)
158
+ rels_ext.add_relationship(r)
159
+ rels_ext.dirty = true
160
+ end
161
+
162
+ def inner_object
163
+ @inner_object
164
+ end
165
+
166
+ def pid
167
+ @inner_object.pid
14
168
  end
15
169
 
16
- def update
170
+ def to_param
171
+ self.pid
172
+ end
173
+ def internal_uri
174
+ "info:fedora/#{pid}"
17
175
  end
18
176
 
19
- def save
177
+ def state
178
+ @inner_object.state
179
+ end
180
+
181
+ def owner_id
182
+ @inner_object.owner_id
183
+ end
184
+
185
+ def create_date
186
+ @inner_object.create_date unless new_object?
187
+ end
188
+
189
+ def modified_date
190
+ @inner_object.modified_date unless new_object?
191
+ end
192
+
193
+ def errors
194
+ @inner_object.errors
195
+ end
196
+
197
+ def label
198
+ @inner_object.label
199
+ end
200
+ def self.deserialize(doc)
201
+ pid = doc.elements['/foxml:digitalObject'].attributes['PID']
202
+ proto = self.new(:pid=>pid)
203
+ proto.datastreams.each do |name,ds|
204
+ doc.elements.each("//foxml:datastream[@ID='#{name}']") do |el|
205
+ proto.datastreams[name]=ds.class.from_xml(ds, el)
206
+ end
207
+ end
208
+ proto
209
+ end
210
+
211
+ def fields
212
+ fields = {:id => {:values => [pid]}, :system_create_date => {:values => [self.create_date]}, :system_modified_date => {:values => [self.modified_date]}, :active_fedora_model_field => {:values => [self.class.inspect]}}
213
+ datastreams.values.each do |ds|
214
+ fields.merge!(ds.fields) if ds.kind_of?(ActiveFedora::MetadataDatastream)
215
+ end
216
+ return fields
217
+ end
218
+
219
+ def to_xml(xml=REXML::Document.new("<xml><fields/><content/></xml>"))
220
+ fields_xml = xml.root.elements['fields']
221
+ {:id => pid, :system_create_date => self.create_date, :system_modified_date => self.modified_date, :active_fedora_model_field => self.class.inspect}.each_pair do |attribute_name, value|
222
+ el = REXML::Element.new(attribute_name.to_s)
223
+ el.text = value
224
+ fields_xml << el
225
+ end
226
+ datastreams.each_value do |ds|
227
+ ds.to_xml(fields_xml) if ds.kind_of?(ActiveFedora::MetadataDatastream) || ds.kind_of?(ActiveFedora::RelsExtDatastream)
228
+ end
229
+ return xml.to_s
230
+ end
231
+
232
+ def to_solr(solr_doc = Solr::Document.new)
233
+ solr_doc << {:id => pid, :system_create_date => self.create_date, :system_modified_date => self.modified_date, :active_fedora_model_field => self.class.inspect}
234
+ datastreams.each_value do |ds|
235
+ solr_doc = ds.to_solr(solr_doc) if ds.kind_of?(ActiveFedora::MetadataDatastream) || ds.kind_of?(ActiveFedora::RelsExtDatastream)
236
+ end
237
+ return solr_doc
238
+ end
239
+
240
+ # Updates Solr index
241
+ def update_index
242
+ solr_doc = self.to_solr
243
+ SolrService.instance.conn.update(solr_doc)
244
+ end
245
+
246
+ def update_attributes(params={})
247
+ params.each do |k,v|
248
+ datastreams.values.each do |d|
249
+ if d.fields[k.to_sym]
250
+ d.send("#{k}_values=", v)
251
+ end
252
+ end
253
+ end
254
+ end
255
+
256
+ def update_indexed_attributes(params={})
257
+ params.each do |key,value|
258
+ datastreams.each do |dsn, dstream|
259
+ if dstream.fields[key.to_sym]
260
+ aname="#{key}_values"
261
+ curval = dstream.send("#{aname}")
262
+ cpv=value.dup#copy this, we'll need the original for the next ds
263
+ cpv.delete_if do |y,z|
264
+ if curval[y.to_i] and y.to_i > -1
265
+ curval[y.to_i]=z
266
+ true
267
+ else
268
+ false
269
+ end
270
+ end
271
+ cpv.each { |y,z| curval<<z}#just append everything left
272
+ dstream.send("#{aname}=", curval) #write it back to the ds
273
+ end
274
+ end
275
+ end
276
+
277
+ end
278
+
279
+ private
280
+ def configure_defined_datastreams
281
+ if self.class.ds_specs
282
+ self.class.ds_specs.each do |name,ar|
283
+ ds = ar.first.new(:dsid=>name)
284
+ ar.last.call(ds)
285
+ self.add_datastream(ds)
286
+ end
287
+ end
20
288
  end
289
+
290
+
21
291
  end
22
- end
292
+ end