active-fedora 8.7.0 → 9.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (242) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +8 -15
  3. data/Gemfile +5 -5
  4. data/History.txt +0 -80
  5. data/README.md +1 -0
  6. data/Rakefile +0 -3
  7. data/active-fedora.gemspec +8 -7
  8. data/config/fedora.yml +5 -4
  9. data/config/predicate_mappings.yml +5 -5
  10. data/gemfiles/rails4.1.gemfile +10 -0
  11. data/gemfiles/rails4.2.beta.gemfile +10 -0
  12. data/lib/active_fedora.rb +151 -117
  13. data/lib/active_fedora/associations.rb +47 -15
  14. data/lib/active_fedora/associations/association.rb +29 -8
  15. data/lib/active_fedora/associations/association_scope.rb +5 -5
  16. data/lib/active_fedora/associations/belongs_to_association.rb +20 -63
  17. data/lib/active_fedora/associations/builder/association.rb +61 -25
  18. data/lib/active_fedora/associations/builder/belongs_to.rb +7 -94
  19. data/lib/active_fedora/associations/builder/collection_association.rb +11 -43
  20. data/lib/active_fedora/associations/builder/contains.rb +28 -0
  21. data/lib/active_fedora/associations/builder/has_and_belongs_to_many.rb +13 -3
  22. data/lib/active_fedora/associations/builder/has_many.rb +16 -10
  23. data/lib/active_fedora/associations/builder/property.rb +14 -0
  24. data/lib/active_fedora/associations/builder/singular_association.rb +14 -18
  25. data/lib/active_fedora/associations/builder/singular_property.rb +12 -0
  26. data/lib/active_fedora/associations/collection_association.rb +57 -80
  27. data/lib/active_fedora/associations/contains_association.rb +50 -0
  28. data/lib/active_fedora/associations/has_and_belongs_to_many_association.rb +44 -24
  29. data/lib/active_fedora/associations/has_many_association.rb +46 -14
  30. data/lib/active_fedora/associations/rdf.rb +86 -0
  31. data/lib/active_fedora/associations/singular_association.rb +4 -8
  32. data/lib/active_fedora/associations/singular_rdf.rb +15 -0
  33. data/lib/active_fedora/attached_files.rb +195 -0
  34. data/lib/active_fedora/attribute_methods.rb +122 -0
  35. data/lib/active_fedora/attribute_methods/dirty.rb +16 -0
  36. data/lib/active_fedora/attribute_methods/read.rb +61 -0
  37. data/lib/active_fedora/attribute_methods/write.rb +47 -0
  38. data/lib/active_fedora/attributes.rb +93 -44
  39. data/lib/active_fedora/attributes/primary_key.rb +12 -0
  40. data/lib/active_fedora/autosave_association.rb +2 -2
  41. data/lib/active_fedora/base.rb +16 -15
  42. data/lib/active_fedora/callbacks.rb +7 -7
  43. data/lib/active_fedora/change_set.rb +34 -0
  44. data/lib/active_fedora/cleaner.rb +62 -0
  45. data/lib/active_fedora/config.rb +4 -22
  46. data/lib/active_fedora/core.rb +173 -99
  47. data/lib/active_fedora/datastream.rb +4 -117
  48. data/lib/active_fedora/datastreams.rb +2 -263
  49. data/lib/active_fedora/datastreams/nokogiri_datastreams.rb +28 -51
  50. data/lib/active_fedora/{datastream_attribute.rb → delegated_attribute.rb} +57 -26
  51. data/lib/active_fedora/errors.rb +61 -0
  52. data/lib/active_fedora/fedora.rb +19 -0
  53. data/lib/active_fedora/fedora_attributes.rb +58 -26
  54. data/lib/active_fedora/file.rb +318 -0
  55. data/lib/active_fedora/file_configurator.rb +32 -32
  56. data/lib/active_fedora/file_path_builder.rb +24 -0
  57. data/lib/active_fedora/files_hash.rb +82 -0
  58. data/lib/active_fedora/fixity_service.rb +40 -0
  59. data/lib/active_fedora/indexing.rb +55 -82
  60. data/lib/active_fedora/indexing_service.rb +70 -0
  61. data/lib/active_fedora/ldp_resource.rb +26 -0
  62. data/lib/active_fedora/loadable_from_json.rb +112 -0
  63. data/lib/active_fedora/model.rb +5 -19
  64. data/lib/active_fedora/nested_attributes.rb +5 -6
  65. data/lib/active_fedora/nom_datastream.rb +15 -25
  66. data/lib/active_fedora/om_datastream.rb +26 -197
  67. data/lib/active_fedora/persistence.rb +95 -71
  68. data/lib/active_fedora/predicates.rb +4 -4
  69. data/lib/active_fedora/qualified_dublin_core_datastream.rb +17 -18
  70. data/lib/active_fedora/querying.rb +3 -4
  71. data/lib/active_fedora/railtie.rb +3 -6
  72. data/lib/active_fedora/rdf.rb +3 -1
  73. data/lib/active_fedora/rdf/datastream_indexing.rb +11 -0
  74. data/lib/active_fedora/rdf/fcrepo.rb +10 -324
  75. data/lib/active_fedora/rdf/indexing.rb +20 -16
  76. data/lib/active_fedora/rdf/ldp.rb +6 -0
  77. data/lib/active_fedora/rdf/ntriples_rdf_datastream.rb +1 -1
  78. data/lib/active_fedora/rdf/persistence.rb +5 -6
  79. data/lib/active_fedora/rdf/rdf_datastream.rb +44 -37
  80. data/lib/active_fedora/rdf/rdfxml_datastream.rb +13 -0
  81. data/lib/active_fedora/rdf/rels_ext.rb +26 -0
  82. data/lib/active_fedora/reflection.rb +256 -199
  83. data/lib/active_fedora/relation.rb +18 -6
  84. data/lib/active_fedora/relation/finder_methods.rb +69 -38
  85. data/lib/active_fedora/relation/query_methods.rb +7 -3
  86. data/lib/active_fedora/rspec_matchers/belong_to_associated_active_fedora_object_matcher.rb +7 -7
  87. data/lib/active_fedora/rspec_matchers/have_many_associated_active_fedora_objects_matcher.rb +8 -8
  88. data/lib/active_fedora/rspec_matchers/have_predicate_matcher.rb +9 -11
  89. data/lib/active_fedora/simple_datastream.rb +18 -13
  90. data/lib/active_fedora/solr_instance_loader.rb +18 -38
  91. data/lib/active_fedora/solr_service.rb +37 -20
  92. data/lib/active_fedora/sparql_insert.rb +45 -0
  93. data/lib/active_fedora/test_support.rb +1 -22
  94. data/lib/active_fedora/version.rb +1 -1
  95. data/lib/active_fedora/versionable.rb +90 -0
  96. data/lib/active_fedora/with_metadata.rb +37 -0
  97. data/lib/active_fedora/with_metadata/metadata_node.rb +70 -0
  98. data/lib/generators/active_fedora/config/config_generator.rb +0 -1
  99. data/lib/generators/active_fedora/config/solr/solr_generator.rb +1 -1
  100. data/lib/generators/active_fedora/model/model_generator.rb +5 -5
  101. data/lib/generators/active_fedora/model/templates/datastream_spec.rb.erb +1 -1
  102. data/lib/generators/active_fedora/model/templates/model_spec.rb.erb +2 -2
  103. data/lib/tasks/active_fedora_dev.rake +21 -27
  104. data/spec/config_helper.rb +1 -1
  105. data/spec/fixtures/mixed_rdf_descMetadata.nt +6 -6
  106. data/spec/fixtures/rails_root/config/predicate_mappings.yml +3 -19
  107. data/spec/fixtures/solr_rdf_descMetadata.nt +6 -6
  108. data/spec/integration/associations_spec.rb +133 -153
  109. data/spec/integration/attached_files_spec.rb +164 -0
  110. data/spec/integration/attributes_spec.rb +73 -12
  111. data/spec/integration/autosave_association_spec.rb +3 -3
  112. data/spec/integration/base_spec.rb +57 -351
  113. data/spec/integration/belongs_to_association_spec.rb +86 -76
  114. data/spec/integration/bug_spec.rb +3 -3
  115. data/spec/integration/collection_association_spec.rb +4 -4
  116. data/spec/integration/complex_rdf_datastream_spec.rb +54 -56
  117. data/spec/integration/delete_all_spec.rb +18 -15
  118. data/spec/integration/eradicate_spec.rb +54 -0
  119. data/spec/integration/fedora_solr_sync_spec.rb +7 -5
  120. data/spec/integration/field_to_solr_name_spec.rb +5 -5
  121. data/spec/integration/file_fixity_spec.rb +40 -0
  122. data/spec/integration/file_spec.rb +122 -0
  123. data/spec/integration/full_featured_model_spec.rb +53 -63
  124. data/spec/integration/has_and_belongs_to_many_associations_spec.rb +141 -114
  125. data/spec/integration/has_many_associations_spec.rb +142 -64
  126. data/spec/integration/json_serialization_spec.rb +50 -8
  127. data/spec/integration/model_spec.rb +12 -29
  128. data/spec/integration/nested_attribute_spec.rb +28 -20
  129. data/spec/integration/ntriples_datastream_spec.rb +60 -57
  130. data/spec/integration/om_datastream_spec.rb +51 -140
  131. data/spec/integration/rdf_nested_attributes_spec.rb +16 -14
  132. data/spec/integration/relation_delegation_spec.rb +7 -9
  133. data/spec/integration/relation_spec.rb +9 -7
  134. data/spec/integration/scoped_query_spec.rb +26 -26
  135. data/spec/integration/solr_instance_loader_spec.rb +69 -53
  136. data/spec/integration/solr_service_spec.rb +12 -73
  137. data/spec/integration/versionable_spec.rb +477 -0
  138. data/spec/integration/with_metadata_spec.rb +52 -0
  139. data/spec/samples/hydra-mods_article_datastream.rb +10 -6
  140. data/spec/samples/models/mods_article.rb +6 -2
  141. data/spec/samples/oral_history_sample.xml +1 -1
  142. data/spec/samples/oral_history_xml.xml +1 -1
  143. data/spec/samples/special_thing.rb +3 -3
  144. data/spec/spec_helper.rb +22 -12
  145. data/spec/support/an_active_model.rb +3 -7
  146. data/spec/unit/active_fedora_spec.rb +20 -17
  147. data/spec/unit/attached_files_spec.rb +203 -0
  148. data/spec/unit/attributes_spec.rb +286 -207
  149. data/spec/unit/base_active_model_spec.rb +8 -8
  150. data/spec/unit/base_datastream_management_spec.rb +11 -24
  151. data/spec/unit/base_extra_spec.rb +17 -67
  152. data/spec/unit/base_spec.rb +163 -428
  153. data/spec/unit/builder/has_and_belongs_to_many_spec.rb +2 -2
  154. data/spec/unit/callback_spec.rb +38 -23
  155. data/spec/unit/change_set_spec.rb +46 -0
  156. data/spec/unit/code_configurator_spec.rb +5 -5
  157. data/spec/unit/config_spec.rb +9 -14
  158. data/spec/unit/core_spec.rb +59 -8
  159. data/spec/unit/file_configurator_spec.rb +55 -53
  160. data/spec/unit/file_path_builder_spec.rb +18 -0
  161. data/spec/unit/file_spec.rb +221 -0
  162. data/spec/unit/files_hash_spec.rb +53 -0
  163. data/spec/unit/fixity_service_spec.rb +34 -0
  164. data/spec/unit/has_and_belongs_to_many_association_spec.rb +134 -0
  165. data/spec/unit/has_many_association_spec.rb +51 -0
  166. data/spec/unit/indexing_service_spec.rb +23 -0
  167. data/spec/unit/indexing_spec.rb +26 -0
  168. data/spec/unit/inheritance_spec.rb +9 -10
  169. data/spec/unit/model_spec.rb +15 -33
  170. data/spec/unit/nom_datastream_spec.rb +13 -10
  171. data/spec/unit/ntriples_datastream_spec.rb +81 -96
  172. data/spec/unit/om_datastream_spec.rb +137 -227
  173. data/spec/unit/persistence_spec.rb +28 -34
  174. data/spec/unit/predicates_spec.rb +29 -29
  175. data/spec/unit/property_spec.rb +1 -3
  176. data/spec/unit/qualified_dublin_core_datastream_spec.rb +27 -32
  177. data/spec/unit/query_spec.rb +116 -149
  178. data/spec/unit/rdf_datastream_spec.rb +25 -43
  179. data/spec/unit/rdf_resource_datastream_spec.rb +24 -123
  180. data/spec/unit/{rdfxml_rdf_datastream_spec.rb → rdfxml_datastream_spec.rb} +21 -25
  181. data/spec/unit/readonly_spec.rb +23 -0
  182. data/spec/unit/rspec_matchers/belong_to_associated_active_fedora_object_matcher_spec.rb +6 -6
  183. data/spec/unit/rspec_matchers/have_many_associated_active_fedora_objects_matcher_spec.rb +6 -6
  184. data/spec/unit/rspec_matchers/have_predicate_matcher_spec.rb +6 -6
  185. data/spec/unit/serializers_spec.rb +1 -1
  186. data/spec/unit/simple_datastream_spec.rb +12 -23
  187. data/spec/unit/solr_config_options_spec.rb +14 -17
  188. data/spec/unit/solr_service_spec.rb +38 -77
  189. data/spec/unit/sparql_insert_spec.rb +32 -0
  190. data/spec/unit/validations_spec.rb +8 -11
  191. metadata +96 -121
  192. data/lib/active_fedora/auditable.rb +0 -9
  193. data/lib/active_fedora/content_model.rb +0 -70
  194. data/lib/active_fedora/datastream_collections.rb +0 -302
  195. data/lib/active_fedora/datastream_hash.rb +0 -35
  196. data/lib/active_fedora/digital_object.rb +0 -55
  197. data/lib/active_fedora/fixture_exporter.rb +0 -33
  198. data/lib/active_fedora/fixture_loader.rb +0 -48
  199. data/lib/active_fedora/rdf/identifiable.rb +0 -66
  200. data/lib/active_fedora/rdf/project_hydra.rb +0 -12
  201. data/lib/active_fedora/rdf/rdfxml_rdf_datastream.rb +0 -13
  202. data/lib/active_fedora/rdf_xml_writer.rb +0 -49
  203. data/lib/active_fedora/relationship_graph.rb +0 -101
  204. data/lib/active_fedora/reload_on_save.rb +0 -16
  205. data/lib/active_fedora/rels_ext_datastream.rb +0 -100
  206. data/lib/active_fedora/rspec_matchers/match_fedora_datastream_matcher.rb +0 -41
  207. data/lib/active_fedora/rubydora_connection.rb +0 -35
  208. data/lib/active_fedora/semantic_node.rb +0 -164
  209. data/lib/active_fedora/service_definitions.rb +0 -88
  210. data/lib/active_fedora/sharding.rb +0 -58
  211. data/lib/active_fedora/solr_digital_object.rb +0 -68
  212. data/lib/active_fedora/unsaved_digital_object.rb +0 -58
  213. data/lib/generators/active_fedora/config/fedora/fedora_generator.rb +0 -12
  214. data/lib/generators/active_fedora/config/fedora/templates/fedora.yml +0 -38
  215. data/lib/generators/active_fedora/config/fedora/templates/fedora_conf/conf/development/fedora.fcfg +0 -953
  216. data/lib/generators/active_fedora/config/fedora/templates/fedora_conf/conf/test/fedora.fcfg +0 -953
  217. data/lib/tasks/active_fedora.rake +0 -83
  218. data/spec/fixtures/sharded_fedora.yml +0 -11
  219. data/spec/integration/auditable_spec.rb +0 -29
  220. data/spec/integration/datastream_collections_spec.rb +0 -127
  221. data/spec/integration/datastream_spec.rb +0 -90
  222. data/spec/integration/datastreams_spec.rb +0 -173
  223. data/spec/integration/load_from_solr_spec.rb +0 -66
  224. data/spec/integration/rels_ext_datastream_spec.rb +0 -82
  225. data/spec/support/mock_fedora.rb +0 -44
  226. data/spec/unit/content_model_spec.rb +0 -86
  227. data/spec/unit/datastream_collections_spec.rb +0 -420
  228. data/spec/unit/datastream_spec.rb +0 -83
  229. data/spec/unit/datastreams_spec.rb +0 -243
  230. data/spec/unit/has_and_belongs_to_many_collection_spec.rb +0 -96
  231. data/spec/unit/has_many_collection_spec.rb +0 -35
  232. data/spec/unit/rdf_vocab_spec.rb +0 -30
  233. data/spec/unit/rdf_xml_writer_spec.rb +0 -63
  234. data/spec/unit/relationship_graph_spec.rb +0 -115
  235. data/spec/unit/reload_on_save_spec.rb +0 -24
  236. data/spec/unit/rels_ext_datastream_spec.rb +0 -170
  237. data/spec/unit/rspec_matchers/match_fedora_datastream_matcher_spec.rb +0 -44
  238. data/spec/unit/rubydora_connection_spec.rb +0 -12
  239. data/spec/unit/semantic_node_spec.rb +0 -112
  240. data/spec/unit/service_definitions_spec.rb +0 -63
  241. data/spec/unit/solr_digital_object_spec.rb +0 -97
  242. data/spec/unit/unsaved_digital_object_spec.rb +0 -48
@@ -1,7 +1,7 @@
1
1
  module ActiveFedora
2
2
  # Represents the mapping between a model attribute and a field in a datastream
3
- class DatastreamAttribute
4
-
3
+ class DelegatedAttribute
4
+
5
5
  attr_accessor :dsid, :field, :datastream_class, :at, :multiple
6
6
 
7
7
  def initialize(field, dsid, datastream_class, args={})
@@ -12,11 +12,11 @@ module ActiveFedora
12
12
  self.at = args[:at]
13
13
  end
14
14
 
15
- # Gives the primary solr name for a column. If there is more than one indexer on the field definition, it gives the first
15
+ # Gives the primary solr name for a column. If there is more than one indexer on the field definition, it gives the first
16
16
  def primary_solr_name
17
- @datastream ||= datastream_class.new(nil, dsid)
17
+ @datastream ||= datastream_class.new
18
18
  if @datastream.respond_to?(:primary_solr_name)
19
- @datastream.primary_solr_name(field)
19
+ @datastream.primary_solr_name(field, dsid)
20
20
  else
21
21
  raise NoMethodError, "the datastream '#{datastream_class}' doesn't respond to 'primary_solr_name'"
22
22
  end
@@ -34,41 +34,72 @@ module ActiveFedora
34
34
  ds = datastream_for_attribute(obj, dsid)
35
35
  obj.mark_as_changed(field) if obj.value_has_changed?(field, v)
36
36
  if ds.kind_of?(ActiveFedora::RDFDatastream)
37
- ds.send("#{field}=", v)
37
+ write_rdf(ds, v)
38
38
  else
39
- terminology = at || [field]
40
- ds.send(:update_indexed_attributes, {terminology => v})
39
+ write_om(ds, v)
41
40
  end
42
41
  end
43
42
 
44
43
  def reader(obj, *opts)
45
- if obj.inner_object.is_a? SolrDigitalObject
46
- begin
47
- # Look in the cache
48
- return obj.inner_object.fetch(field)
49
- rescue NoMethodError => e
50
- # couldn't get it from solr, so try from fedora.
51
- ActiveFedora::Base.logger.info "Couldn't get #{field} out of solr, because #{e.message}. Trying another way." if ActiveFedora::Base.logger
52
- end
53
- end
54
- # Load from fedora
55
44
  ds = datastream_for_attribute(obj, dsid)
56
45
  if ds.kind_of?(ActiveFedora::RDFDatastream)
57
- ds.send(field)
46
+ read_rdf(ds)
58
47
  else
59
- terminology = at || [field]
60
- if terminology.length == 1 && opts.present?
61
- ds.send(terminology.first, *opts)
62
- else
63
- ds.send(:term_values, *terminology)
64
- end
48
+ read_om(ds, *opts)
65
49
  end
66
50
  end
67
51
 
68
52
  private
69
53
 
54
+ def write_om(ds, v)
55
+ terminology = at || [field]
56
+ ds.send(:update_indexed_attributes, {terminology => v})
57
+ end
58
+
59
+ def read_om(ds, *opts)
60
+ terminology = at || [field]
61
+ if terminology.length == 1 && opts.present?
62
+ ds.send(terminology.first, *opts)
63
+ else
64
+ ds.send(:term_values, *terminology)
65
+ end
66
+ end
67
+
68
+ def write_rdf(node, v)
69
+ term = if at
70
+ vals = at.dup
71
+ while vals.length > 1
72
+ node = node.send(vals.shift)
73
+ node = node.build if node.empty?
74
+ node = node.first
75
+ end
76
+ vals.first
77
+ else
78
+ field
79
+ end
80
+ node.send("#{term}=", v)
81
+ end
82
+
83
+ def read_rdf(node)
84
+ term = if at
85
+ vals = at.dup
86
+ while vals.length > 1
87
+ node = node.send(vals.shift)
88
+ node = if node.empty?
89
+ node.build
90
+ else
91
+ node.first
92
+ end
93
+ end
94
+ vals.first
95
+ else
96
+ field
97
+ end
98
+ node.send(term)
99
+ end
100
+
70
101
  def datastream_for_attribute(obj, dsid)
71
- obj.datastreams[dsid] || raise(ArgumentError, "Undefined datastream id: `#{dsid}' in has_attributes")
102
+ obj.attached_files[dsid] || raise(ArgumentError, "Undefined datastream id: `#{dsid}' in has_attributes")
72
103
  end
73
104
 
74
105
  end
@@ -0,0 +1,61 @@
1
+ module ActiveFedora #:nodoc:
2
+
3
+ # Generic ActiveFedora exception class
4
+ class ActiveFedoraError < StandardError
5
+ end
6
+
7
+ # Raised when ActiveFedora cannot find the record by given id or set of ids
8
+ class ObjectNotFoundError < ActiveFedoraError
9
+ end
10
+
11
+ # Raised when attempting to access an attribute that has not been defined
12
+ class UnknownAttributeError < NoMethodError
13
+ end
14
+
15
+ # Raised when there is an error with the configuration files.
16
+ class ConfigurationError < ActiveFedoraError
17
+ end
18
+
19
+ # Raised when ActiveFedora cannot find the predicate mapping configuration file
20
+ class PredicateMappingsNotFoundError < ConfigurationError
21
+ end
22
+
23
+ # Raised when an object assigned to an association has an incorrect type.
24
+ #
25
+ # class Ticket < ActiveFedora::Base
26
+ # has_many :patches
27
+ # end
28
+ #
29
+ # class Patch < ActiveFedora::Base
30
+ # belongs_to :ticket, predicate: ActiveFedora::RDF::RelsExt.isComponentOf
31
+ # end
32
+ #
33
+ # # Comments are not patches, this assignment raises AssociationTypeMismatch.
34
+ # @ticket.patches << Comment.new(content: "Please attach tests to your patch.")
35
+ class AssociationTypeMismatch < ActiveFedoraError
36
+ end
37
+
38
+ # Raised when ActiveFedora cannot find the predicate corresponding to the given property
39
+ # in the predicate registy
40
+ class UnregisteredPredicateError < ActiveFedoraError
41
+ end
42
+
43
+ # Raised by ActiveFedora::Base.save! and ActiveFedora::Base.create! methods when record cannot be
44
+ # saved because record is invalid.
45
+ class RecordNotSaved < ActiveFedoraError
46
+ end
47
+
48
+ # Raised on attempt to update record that is instantiated as read only.
49
+ class ReadOnlyRecord < ActiveFedoraError
50
+ end
51
+
52
+ # Raised when trying to initialize a record that already exists. ActiveFedora::Base.find should
53
+ # be used instead
54
+ class IllegalOperation < ActiveFedoraError
55
+ end
56
+
57
+ # Used to rollback a transaction in a deliberate way without raising an exception.
58
+ # Transactions are currently incomplete
59
+ class Rollback < ActiveFedoraError
60
+ end
61
+ end
@@ -0,0 +1,19 @@
1
+ module ActiveFedora
2
+ class Fedora
3
+ def initialize(config)
4
+ @config = config
5
+ end
6
+
7
+ def host
8
+ @config[:url]
9
+ end
10
+
11
+ def base_path
12
+ @config[:base_path] || '/'
13
+ end
14
+
15
+ def connection
16
+ @connection ||= Ldp::Client.new(host)
17
+ end
18
+ end
19
+ end
@@ -1,45 +1,77 @@
1
1
  module ActiveFedora
2
2
  module FedoraAttributes
3
+ extend ActiveSupport::Concern
3
4
 
4
- delegate :state=, :label=, to: :inner_object
5
+ included do
6
+ include RDF::Indexing
7
+ include ActiveTriples::Properties
8
+ include ActiveTriples::Reflection
9
+ delegate :rdf_subject, :get_values, to: :resource
5
10
 
6
- #return the pid of the Fedora Object
7
- # if there is no fedora object (loaded from solr) get the instance var
8
- # TODO make inner_object a proxy that can hold the pid
9
- def pid
10
- @pid ||= @inner_object.pid
11
- end
11
+ property :has_model, predicate: ::RDF::URI.new("http://fedora.info/definitions/v4/rels-ext#hasModel")
12
+ property :create_date, predicate: ActiveFedora::RDF::Fcrepo.created
13
+ property :modified_date, predicate: ActiveFedora::RDF::Fcrepo.lastModified
12
14
 
13
- def id ### Needed for the nested form helper
14
- self.pid
15
- end
15
+ def create_date
16
+ super.first
17
+ end
16
18
 
17
- #return the owner id
18
- def owner_id
19
- Array(@inner_object.ownerId).first
19
+ def modified_date
20
+ super.first
21
+ end
20
22
  end
21
-
22
- def owner_id=(owner_id)
23
- @inner_object.ownerId=(owner_id)
23
+
24
+ # Override ActiveTriples method for setting properties
25
+ def set_value(*args)
26
+ raise ReadOnlyRecord if readonly?
27
+ resource.set_value(*args)
24
28
  end
25
29
 
26
- def label
27
- Array(@inner_object.label).first
30
+ def id
31
+ if uri.kind_of?(::RDF::URI) && uri.value.blank?
32
+ nil
33
+ elsif uri.present?
34
+ self.class.uri_to_id(URI.parse(uri))
35
+ end
28
36
  end
29
37
 
30
- def state
31
- Array(@inner_object.state).first
38
+ # TODO: Remove after we no longer support #pid.
39
+ def pid
40
+ Deprecation.warn FedoraAttributes, "#{self.class}#pid is deprecated and will be removed in active-fedora 10.0. Use #{self.class}#id instead."
41
+ id
32
42
  end
33
43
 
34
- #return the create_date of the inner object (unless it's a new object)
35
- def create_date
36
- @inner_object.new_record? ? Time.now : Array(@inner_object.createdDate).first
44
+ def uri
45
+ # TODO could we return a RDF::URI instead?
46
+ uri = @ldp_source.try(:subject_uri)
47
+ uri.value == '' ? uri : uri.to_s
37
48
  end
38
49
 
39
- #return the modification date of the inner object (unless it's a new object)
40
- def modified_date
41
- @inner_object.new_record? ? Time.now : Array(@inner_object.lastModifiedDate).first
50
+ ##
51
+ # The resource is the RdfResource object that stores the graph for
52
+ # the datastream and is the central point for its relationship to
53
+ # other nodes.
54
+ #
55
+ # set_value, get_value, and property accessors are delegated to this object.
56
+ def resource
57
+ @resource ||= self.class.resource_class.new(@ldp_source.graph.rdf_subject, @ldp_source.graph)
42
58
  end
43
59
 
60
+ module ClassMethods
61
+ # We make a unique class, because properties belong to a class.
62
+ # This keeps properties from different objects separate.
63
+ def resource_class
64
+ @generated_resource_class ||= begin
65
+ klass = self.const_set(:GeneratedResourceSchema, Class.new(ActiveTriples::Resource))
66
+ klass.properties.merge(self.properties).each do |property, config|
67
+ klass.property(config.term,
68
+ predicate: config.predicate,
69
+ class_name: config.class_name,
70
+ multivalue: config.multivalue)
71
+ end
72
+ klass
73
+ end
74
+ end
75
+ end
44
76
  end
45
77
  end
@@ -0,0 +1,318 @@
1
+ module ActiveFedora
2
+
3
+ #This class represents a Fedora datastream
4
+ class File
5
+ include AttributeMethods # allows 'content' to be tracked
6
+ include ActiveModel::Dirty
7
+ extend Deprecation
8
+ extend ActiveTriples::Properties
9
+ generate_method 'content'
10
+
11
+ extend ActiveModel::Callbacks
12
+ define_model_callbacks :save, :create, :destroy
13
+ define_model_callbacks :initialize, only: :after
14
+
15
+ attr_accessor :last_modified
16
+
17
+ # @param parent_or_url [ActiveFedora::Base, String, Hash, NilClass] the parent resource or the URI of this resource
18
+ # @param path_name [String] the path partial relative to the resource
19
+ # @param options [Hash]
20
+ def initialize(parent_or_url_or_hash = nil, dsid=nil, options={})
21
+ case parent_or_url_or_hash
22
+ when Hash
23
+ content = ''
24
+ @ldp_source = Ldp::Resource::BinarySource.new(ldp_connection, nil, content, ActiveFedora.fedora.host + ActiveFedora.fedora.base_path)
25
+ when nil, String
26
+ #TODO this is similar to Core#build_ldp_resource
27
+ content = ''
28
+ @ldp_source = Ldp::Resource::BinarySource.new(ldp_connection, parent_or_url_or_hash, content, ActiveFedora.fedora.host + ActiveFedora.fedora.base_path)
29
+ when ActiveFedora::Base
30
+ Deprecation.warn File, "Initializing a file by passing a container is deprecated. Initialize with a uri instead. This capability will be removed in active-fedora 10.0"
31
+ uri = if parent_or_url_or_hash.uri.kind_of?(::RDF::URI) && parent_or_url_or_hash.uri.value.empty?
32
+ nil
33
+ else
34
+ "#{parent_or_url_or_hash.uri}/#{dsid}"
35
+ end
36
+ @ldp_source = Ldp::Resource::BinarySource.new(ldp_connection, uri, nil, ActiveFedora.fedora.host + ActiveFedora.fedora.base_path)
37
+
38
+ else
39
+ raise "The first argument to #{self} must be a String or an ActiveFedora::Base. You provided a #{parent_or_url.class}"
40
+ end
41
+
42
+ @attributes = {}.with_indifferent_access
43
+ end
44
+
45
+ def ldp_source
46
+ @ldp_source || raise("NO source")
47
+ end
48
+
49
+ def described_by
50
+ raise "#{self} isn't persisted yet" if new_record?
51
+ links['describedby'].first
52
+ end
53
+
54
+ def ldp_connection
55
+ ActiveFedora.fedora.connection
56
+ end
57
+
58
+ # TODO this is like FedoraAttributes#uri
59
+ def uri
60
+ ldp_source.subject
61
+ end
62
+
63
+ def new_record?
64
+ ldp_source.new?
65
+ end
66
+
67
+ def uri= uri
68
+ @ldp_source = Ldp::Resource::BinarySource.new(ldp_connection, uri, '', ActiveFedora.fedora.host + ActiveFedora.fedora.base_path)
69
+ end
70
+
71
+ # When restoring from previous versions, we need to reload certain attributes from Fedora
72
+ def reload
73
+ return if new_record?
74
+ reset
75
+ end
76
+
77
+ def reset
78
+ @ldp_source = Ldp::Resource::BinarySource.new(ldp_connection, uri)
79
+ @original_name = nil
80
+ @mime_type = nil
81
+ @content = nil
82
+ @metadata = nil
83
+ end
84
+
85
+ def check_fixity
86
+ FixityService.new(@ldp_source.subject).check
87
+ end
88
+
89
+ def datastream_will_change!
90
+ attribute_will_change! :profile
91
+ end
92
+
93
+ def attribute_will_change!(attr)
94
+ if attr == 'content'
95
+ changed_attributes['content'] = true
96
+ else
97
+ super
98
+ end
99
+ end
100
+
101
+ def remote_content
102
+ return if new_record?
103
+ @ds_content ||= retrieve_content
104
+ end
105
+
106
+ attr_writer :mime_type
107
+ def mime_type
108
+ @mime_type ||= fetch_mime_type unless new_record?
109
+ @mime_type || default_mime_type
110
+ end
111
+
112
+ def metadata
113
+ @metadata ||= ActiveFedora::WithMetadata::MetadataNode.new(self)
114
+ end
115
+
116
+ def original_name
117
+ @original_name ||= fetch_original_name_from_headers
118
+ end
119
+
120
+ def digest
121
+ response = metadata.ldp_source.graph.query(:predicate => ::RDF::URI.new("http://fedora.info/definitions/v4/repository#digest"))
122
+ response.map(&:object)
123
+ end
124
+
125
+ def persisted_size
126
+ ldp_source.head.headers['Content-Length'].to_i unless new_record?
127
+ end
128
+
129
+ def dirty_size
130
+ content.size if changed? && content.respond_to?(:size)
131
+ end
132
+
133
+ def size
134
+ dirty_size || persisted_size
135
+ end
136
+
137
+ def has_content?
138
+ size && size > 0
139
+ end
140
+
141
+ def empty?
142
+ !has_content?
143
+ end
144
+
145
+ def content_changed?
146
+ return true if new_record? and !local_or_remote_content(false).blank?
147
+ local_or_remote_content(false) != @ds_content
148
+ end
149
+
150
+ def changed?
151
+ super || content_changed?
152
+ end
153
+
154
+
155
+ class << self
156
+ def default_attributes
157
+ {}
158
+ end
159
+ end
160
+
161
+ def default_attributes
162
+ @default_attributes ||= self.class.default_attributes
163
+ end
164
+
165
+ def default_attributes= attributes
166
+ @default_attributes = default_attributes.merge attributes
167
+ end
168
+
169
+ def original_name= name
170
+ @original_name = name
171
+ end
172
+
173
+ def inspect
174
+ "#<#{self.class} uri=\"#{uri}\" >"
175
+ end
176
+
177
+ #compatibility method for rails' url generators. This method will
178
+ #urlescape escape dots, which are apparently
179
+ #invalid characters in a dsid.
180
+ def to_param
181
+ dsid.gsub(/\./, '%2e')
182
+ end
183
+
184
+ # @abstract Override this in your concrete datastream class.
185
+ # @return [boolean] does this datastream contain metadata (not file data)
186
+ def metadata?
187
+ false
188
+ end
189
+
190
+ # Freeze datastreams such that they can be loaded from Fedora, but can't be changed
191
+ def freeze
192
+ @frozen = true
193
+ end
194
+
195
+ def frozen?
196
+ !!@frozen
197
+ end
198
+
199
+ # serializes any changed data into the content field
200
+ def serialize!
201
+ end
202
+
203
+ def to_solr(solr_doc={}, opts={})
204
+ solr_doc
205
+ end
206
+
207
+ protected
208
+
209
+ def default_mime_type
210
+ 'text/plain'
211
+ end
212
+
213
+ # The string to prefix all solr fields with. Override this method if you want
214
+ # a prefix other than the default
215
+ def prefix(dsid)
216
+ dsid ? "#{dsid.underscore}__" : ''
217
+ end
218
+
219
+ def fetch_original_name_from_headers
220
+ return if new_record?
221
+ m = ldp_source.head.headers['Content-Disposition'].match(/filename="(?<filename>[^"]*)";/)
222
+ m[:filename]
223
+ end
224
+
225
+ def fetch_mime_type
226
+ ldp_source.head.headers['Content-Type']
227
+ end
228
+
229
+ private
230
+
231
+ def links
232
+ @links ||= Ldp::Response.links(ldp_source.head)
233
+ end
234
+
235
+
236
+ # Rack::Test::UploadedFile is often set via content=, however it's not an IO, though it wraps an io object.
237
+ def behaves_like_io?(obj)
238
+ [IO, Tempfile, StringIO].any? { |klass| obj.kind_of? klass } || (defined?(Rack) && obj.is_a?(Rack::Test::UploadedFile))
239
+ end
240
+
241
+ # Persistence is an included module, so that we can include other modules which override these methods
242
+ module Persistence
243
+ def content= string_or_io
244
+ content_will_change! unless @content == string_or_io
245
+ @content = string_or_io
246
+ end
247
+
248
+ def content
249
+ local_or_remote_content(true)
250
+ end
251
+
252
+ def save(*)
253
+ return unless content_changed?
254
+ payload = behaves_like_io?(content) ? content.read : content
255
+ headers = { 'Content-Type' => mime_type }
256
+ headers['Content-Disposition'] = "attachment; filename=\"#{@original_name}\"" if @original_name
257
+ ldp_source.content = payload
258
+ if new_record?
259
+ ldp_source.create do |req|
260
+ req.headers = headers
261
+ end
262
+ else
263
+ ldp_source.update do |req|
264
+ req.headers = headers
265
+ end
266
+ end
267
+ reset
268
+ changed_attributes.clear
269
+ end
270
+
271
+ def retrieve_content
272
+ ldp_source.get.body
273
+ end
274
+
275
+ # @param range [String] the Range HTTP header
276
+ # @yield [chunk] a block that receives chunked content
277
+ def stream(range = nil, &block)
278
+ uri = URI.parse(self.uri)
279
+
280
+ headers = {}
281
+ headers['Range'] = range if range
282
+ Net::HTTP.start(uri.host, uri.port) do |http|
283
+ request = Net::HTTP::Get.new uri, headers
284
+ http.request request do |response|
285
+ raise "Couldn't get data from Fedora (#{uri}). Response: #{response.code}" unless response.is_a?(Net::HTTPSuccess)
286
+ response.read_body do |chunk|
287
+ block.call(chunk)
288
+ end
289
+ end
290
+ end
291
+ end
292
+
293
+ private
294
+
295
+ def local_or_remote_content(ensure_fetch = true)
296
+ return @content if new_record?
297
+
298
+ @content ||= ensure_fetch ? remote_content : @ds_content
299
+
300
+ if behaves_like_io?(@content)
301
+ begin
302
+ @content.rewind
303
+ @content.read
304
+ ensure
305
+ @content.rewind
306
+ end
307
+ else
308
+ @content
309
+ end
310
+ @content
311
+ end
312
+ end
313
+
314
+ include ActiveFedora::File::Persistence
315
+ include ActiveFedora::Versionable
316
+ end
317
+
318
+ end