sufia-models 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.md +177 -0
  5. data/README.md +29 -0
  6. data/Rakefile +1 -0
  7. data/app/models/batch.rb +46 -0
  8. data/app/models/checksum_audit_log.rb +35 -0
  9. data/app/models/contact_form.rb +42 -0
  10. data/app/models/datastreams/batch_rdf_datastream.rb +23 -0
  11. data/app/models/datastreams/file_content_datastream.rb +18 -0
  12. data/app/models/datastreams/fits_datastream.rb +188 -0
  13. data/app/models/datastreams/generic_file_rdf_datastream.rb +75 -0
  14. data/app/models/datastreams/paranoid_rights_datastream.rb +37 -0
  15. data/app/models/datastreams/properties_datastream.rb +33 -0
  16. data/app/models/domain_term.rb +18 -0
  17. data/app/models/follow.rb +28 -0
  18. data/app/models/generic_file.rb +16 -0
  19. data/app/models/geo_names_resource.rb +34 -0
  20. data/app/models/group.rb +8 -0
  21. data/app/models/local_authority.rb +93 -0
  22. data/app/models/local_authority_entry.rb +18 -0
  23. data/app/models/single_use_link.rb +26 -0
  24. data/app/models/subject_local_authority_entry.rb +16 -0
  25. data/app/models/trophy.rb +12 -0
  26. data/app/models/version_committer.rb +17 -0
  27. data/lib/sufia/models.rb +11 -0
  28. data/lib/sufia/models/active_fedora/redis.rb +49 -0
  29. data/lib/sufia/models/active_record/redis.rb +56 -0
  30. data/lib/sufia/models/engine.rb +34 -0
  31. data/lib/sufia/models/file_content.rb +9 -0
  32. data/lib/sufia/models/file_content/extract_metadata.rb +60 -0
  33. data/lib/sufia/models/file_content/versions.rb +23 -0
  34. data/lib/sufia/models/generic_file.rb +183 -0
  35. data/lib/sufia/models/generic_file/actions.rb +39 -0
  36. data/lib/sufia/models/generic_file/audit.rb +119 -0
  37. data/lib/sufia/models/generic_file/characterization.rb +81 -0
  38. data/lib/sufia/models/generic_file/export.rb +339 -0
  39. data/lib/sufia/models/generic_file/permissions.rb +64 -0
  40. data/lib/sufia/models/generic_file/thumbnail.rb +91 -0
  41. data/lib/sufia/models/id_service.rb +57 -0
  42. data/lib/sufia/models/jobs/audit_job.rb +65 -0
  43. data/lib/sufia/models/jobs/batch_update_job.rb +86 -0
  44. data/lib/sufia/models/jobs/characterize_job.rb +43 -0
  45. data/lib/sufia/models/jobs/content_delete_event_job.rb +31 -0
  46. data/lib/sufia/models/jobs/content_deposit_event_job.rb +32 -0
  47. data/lib/sufia/models/jobs/content_new_version_event_job.rb +32 -0
  48. data/lib/sufia/models/jobs/content_restored_version_event_job.rb +40 -0
  49. data/lib/sufia/models/jobs/content_update_event_job.rb +32 -0
  50. data/lib/sufia/models/jobs/event_job.rb +33 -0
  51. data/lib/sufia/models/jobs/ffmpeg_transcode_job.rb +61 -0
  52. data/lib/sufia/models/jobs/resolrize_job.rb +23 -0
  53. data/lib/sufia/models/jobs/transcode_audio_job.rb +40 -0
  54. data/lib/sufia/models/jobs/transcode_video_job.rb +39 -0
  55. data/lib/sufia/models/jobs/unzip_job.rb +54 -0
  56. data/lib/sufia/models/jobs/user_edit_profile_event_job.rb +35 -0
  57. data/lib/sufia/models/jobs/user_follow_event_job.rb +37 -0
  58. data/lib/sufia/models/jobs/user_unfollow_event_job.rb +38 -0
  59. data/lib/sufia/models/model_methods.rb +39 -0
  60. data/lib/sufia/models/noid.rb +42 -0
  61. data/lib/sufia/models/solr_document_behavior.rb +125 -0
  62. data/lib/sufia/models/user.rb +126 -0
  63. data/lib/sufia/models/utils.rb +36 -0
  64. data/lib/sufia/models/version.rb +5 -0
  65. data/lib/tasks/sufia-models_tasks.rake +4 -0
  66. data/sufia-models.gemspec +28 -0
  67. metadata +151 -0
@@ -0,0 +1,75 @@
1
+ # Copyright © 2012 The Pennsylvania State University
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ class GenericFileRdfDatastream < ActiveFedora::NtriplesRDFDatastream
16
+ map_predicates do |map|
17
+ map.part_of(:to => "isPartOf", :in => RDF::DC)
18
+ map.contributor(:in => RDF::DC) do |index|
19
+ index.as :stored_searchable, :facetable
20
+ end
21
+ map.creator(:in => RDF::DC) do |index|
22
+ index.as :stored_searchable, :facetable
23
+ end
24
+ map.title(:in => RDF::DC) do |index|
25
+ index.as :stored_searchable
26
+ end
27
+ map.description(:in => RDF::DC) do |index|
28
+ index.type :text
29
+ index.as :stored_searchable
30
+ end
31
+ map.publisher(:in => RDF::DC) do |index|
32
+ index.as :stored_searchable, :facetable
33
+ end
34
+ map.date_created(:to => "created", :in => RDF::DC) do |index|
35
+ index.as :stored_searchable
36
+ end
37
+ map.date_uploaded(:to => "dateSubmitted", :in => RDF::DC) do |index|
38
+ index.type :date
39
+ index.as :stored_sortable
40
+ end
41
+ map.date_modified(:to => "modified", :in => RDF::DC) do |index|
42
+ index.type :date
43
+ index.as :stored_sortable
44
+ end
45
+ map.subject(:in => RDF::DC) do |index|
46
+ index.as :stored_searchable, :facetable
47
+ end
48
+ map.language(:in => RDF::DC) do |index|
49
+ index.as :stored_searchable, :facetable
50
+ end
51
+ map.rights(:in => RDF::DC) do |index|
52
+ index.as :stored_searchable
53
+ end
54
+ map.resource_type(:to => "type", :in => RDF::DC) do |index|
55
+ index.as :stored_searchable, :facetable
56
+ end
57
+ map.identifier(:in => RDF::DC) do |index|
58
+ index.as :stored_searchable
59
+ end
60
+ map.based_near(:in => RDF::FOAF) do |index|
61
+ index.as :stored_searchable, :facetable
62
+ end
63
+ map.tag(:to => "relation", :in => RDF::DC) do |index|
64
+ index.as :stored_searchable, :facetable
65
+ end
66
+ map.related_url(:to => "seeAlso", :in => RDF::RDFS)
67
+ end
68
+ begin
69
+ LocalAuthority.register_vocabulary(self, "subject", "lc_subjects")
70
+ LocalAuthority.register_vocabulary(self, "language", "lexvo_languages")
71
+ LocalAuthority.register_vocabulary(self, "tag", "lc_genres")
72
+ rescue
73
+ puts "tables for vocabularies missing"
74
+ end
75
+ end
@@ -0,0 +1,37 @@
1
+ # Copyright © 2012 The Pennsylvania State University
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'hydra/datastream/rights_metadata'
16
+ # subclass built-in Hydra RightsDatastream and build in extra model-level validation
17
+ class ParanoidRightsDatastream < Hydra::Datastream::RightsMetadata
18
+ use_terminology Hydra::Datastream::RightsMetadata
19
+
20
+ VALIDATIONS = [
21
+ {:key => :edit_users, :message => 'Depositor must have edit access', :condition => lambda { |obj| !obj.edit_users.include?(obj.depositor) }},
22
+ {:key => :edit_groups, :message => 'Public cannot have edit access', :condition => lambda { |obj| obj.edit_groups.include?('public') }},
23
+ {:key => :edit_groups, :message => 'Registered cannot have edit access', :condition => lambda { |obj| obj.edit_groups.include?('registered') }}
24
+ ]
25
+
26
+ def validate(object)
27
+ valid = true
28
+ VALIDATIONS.each do |validation|
29
+ if validation[:condition].call(object)
30
+ object.errors[validation[:key]] ||= []
31
+ object.errors[validation[:key]] << validation[:message]
32
+ valid = false
33
+ end
34
+ end
35
+ return valid
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+ # Copyright © 2012 The Pennsylvania State University
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ # properties datastream: catch-all for info that didn't have another home. Particularly depositor.
16
+ class PropertiesDatastream < ActiveFedora::OmDatastream
17
+ set_terminology do |t|
18
+ t.root(:path=>"fields" )
19
+ # This is where we put the user id of the object depositor -- impacts permissions/access controls
20
+ t.depositor :index_as=>[:stored_searchable]
21
+ # This is where we put the relative path of the file if submitted as a folder
22
+ t.relative_path
23
+
24
+ t.proxy_depositor path: 'proxyDepositor', :index_as=>:symbol
25
+ end
26
+
27
+ def self.xml_template
28
+ builder = Nokogiri::XML::Builder.new do |xml|
29
+ xml.fields
30
+ end
31
+ builder.doc
32
+ end
33
+ end
@@ -0,0 +1,18 @@
1
+ # Copyright © 2012 The Pennsylvania State University
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ class DomainTerm < ActiveRecord::Base
16
+ attr_accessible :model, :term
17
+ has_and_belongs_to_many :local_authorities, :uniq=> true
18
+ end
@@ -0,0 +1,28 @@
1
+ # Copyright © 2012 The Pennsylvania State University
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ class Follow < ActiveRecord::Base
16
+
17
+ extend ActsAsFollower::FollowerLib
18
+ extend ActsAsFollower::FollowScopes
19
+
20
+ # NOTE: Follows belong to the "followable" interface, and also to followers
21
+ belongs_to :followable, :polymorphic => true
22
+ belongs_to :follower, :polymorphic => true
23
+
24
+ def block!
25
+ self.update_attribute(:blocked, true)
26
+ end
27
+
28
+ end
@@ -0,0 +1,16 @@
1
+ # Copyright © 2012 The Pennsylvania State University
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ class GenericFile < ActiveFedora::Base
15
+ include Sufia::GenericFile
16
+ end
@@ -0,0 +1,34 @@
1
+ # Copyright © 2012 The Pennsylvania State University
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ class GeoNamesResource < ActiveResource::Base
16
+ self.site = "http://api.geonames.org/"
17
+ self.element_name = "searchJSON"
18
+ self.collection_name = "searchJSON"
19
+
20
+ def self.collection_path(prefix_options = {}, query_options = nil)
21
+ super(prefix_options, query_options).gsub(/\.json|\.xml/, "")
22
+ end
23
+
24
+ def self.instantiate_collection(collection, prefix_options = {})
25
+ col = super(collection["geonames"],prefix_options)
26
+ col.map! {|item| { label: item.name+ (item.adminName1 ? ", " + item.adminName1 : "")+", " + item.countryName, value: item.name+ (item.adminName1 ? ", " + item.adminName1 : "")+", " + item.countryName} }
27
+ return col
28
+ end
29
+
30
+ def self.find_location(location)
31
+ return GeoNamesResource.find(:all, :params => { :q =>location, :username=>"cam156", :maxRows=>10})
32
+ end
33
+
34
+ end
@@ -0,0 +1,8 @@
1
+ ## This could extend from a module that would make it an ldap group
2
+ class Group
3
+
4
+ def self.exists?(cn)
5
+ false
6
+ end
7
+
8
+ end
@@ -0,0 +1,93 @@
1
+ # Copyright © 2012 The Pennsylvania State University
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'rdf'
16
+ require 'rdf/rdfxml'
17
+
18
+ class LocalAuthority < ActiveRecord::Base
19
+ attr_accessible :name
20
+ has_and_belongs_to_many :domain_terms, :uniq=> true
21
+ has_many :local_authority_entries
22
+
23
+ def self.harvest_rdf(name, sources, opts = {})
24
+ return unless self.where(:name => name).empty?
25
+ authority = self.create(:name => name)
26
+ format = opts.fetch(:format, :ntriples)
27
+ predicate = opts.fetch(:predicate, RDF::SKOS.prefLabel)
28
+ entries = []
29
+ sources.each do |uri|
30
+ RDF::Reader.open(uri, :format => format) do |reader|
31
+ reader.each_statement do |statement|
32
+ if statement.predicate == predicate
33
+ entries << LocalAuthorityEntry.new(:local_authority => authority,
34
+ :label => statement.object.to_s,
35
+ :uri => statement.subject.to_s)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ LocalAuthorityEntry.import entries
41
+ end
42
+
43
+ def self.harvest_tsv(name, sources, opts = {})
44
+ return unless self.where(:name => name).empty?
45
+ authority = self.create(:name => name)
46
+ prefix = opts.fetch(:prefix, "")
47
+ entries = []
48
+ sources.each do |uri|
49
+ open(uri) do |f|
50
+ f.each_line do |tsv|
51
+ fields = tsv.split(/\t/)
52
+ entries << LocalAuthorityEntry.new(:local_authority => authority,
53
+ :uri => "#{prefix}#{fields[0]}/",
54
+ :label => fields[2])
55
+ end
56
+ end
57
+ end
58
+ LocalAuthorityEntry.import entries
59
+ end
60
+
61
+ def self.register_vocabulary(model, term, name)
62
+ authority = self.find_by_name(name)
63
+ return if authority.blank?
64
+ model = model.to_s.sub(/RdfDatastream$/, '').underscore.pluralize
65
+ domain_term = DomainTerm.find_or_create_by_model_and_term(:model => model, :term => term)
66
+ return if domain_term.local_authorities.include? authority
67
+ domain_term.local_authorities << authority
68
+ end
69
+
70
+ def self.entries_by_term(model, term, query)
71
+ return if query.empty?
72
+ lowQuery = query.downcase
73
+ hits = []
74
+ # move lc_subject into it's own table since being part of the usual structure caused it to be too slow.
75
+ # When/if we move to having multiple dictionaries for subject we will need to also do a check for the appropriate dictionary.
76
+ if (term == 'subject' && model == 'generic_files') # and local_authoritiy = lc_subject
77
+ sql = SubjectLocalAuthorityEntry.where("lowerLabel like ?", "#{lowQuery}%").select("label, uri").limit(25).to_sql
78
+ SubjectLocalAuthorityEntry.find_by_sql(sql).each do |hit|
79
+ hits << {:uri => hit.uri, :label => hit.label}
80
+ end
81
+ else
82
+ dterm = DomainTerm.where(:model => model, :term => term).first
83
+ if dterm
84
+ authorities = dterm.local_authorities.collect(&:id).uniq
85
+ sql = LocalAuthorityEntry.where("local_authority_id in (?)", authorities).where("lower(label) like ?", "#{lowQuery}%").select("label, uri").limit(25).to_sql
86
+ LocalAuthorityEntry.find_by_sql(sql).each do |hit|
87
+ hits << {:uri => hit.uri, :label => hit.label}
88
+ end
89
+ end
90
+ end
91
+ return hits
92
+ end
93
+ end
@@ -0,0 +1,18 @@
1
+ # Copyright © 2012 The Pennsylvania State University
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ class LocalAuthorityEntry < ActiveRecord::Base
16
+ belongs_to :local_authority
17
+ attr_accessible :local_authority, :label, :uri
18
+ end
@@ -0,0 +1,26 @@
1
+ class SingleUseLink < ActiveRecord::Base
2
+
3
+ attr_accessible :downloadKey, :expires, :itemId, :path
4
+
5
+
6
+
7
+ def self.create_show(item_id)
8
+ create_path(item_id, Sufia::Engine.routes.url_helpers.generic_file_path(item_id) )
9
+ end
10
+
11
+ def self.create_download(item_id)
12
+ create_path(item_id, Sufia::Engine.routes.url_helpers.download_path(item_id) )
13
+ end
14
+
15
+ def expired?
16
+ now = DateTime.now
17
+ return (now > expires)
18
+ end
19
+
20
+ protected
21
+ def self.create_path(itemId, path)
22
+ expires = DateTime.now.advance(hours:24)
23
+ key = Digest::SHA2.new << DateTime.now.to_f.to_s
24
+ return create({downloadKey:key.to_s, expires:expires, path:path, itemId:itemId} )
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ # Copyright © 2012 The Pennsylvania State University
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ class SubjectLocalAuthorityEntry < ActiveRecord::Base
16
+ end
@@ -0,0 +1,12 @@
1
+ class Trophy < ActiveRecord::Base
2
+ attr_accessible :generic_file_id, :user_id
3
+
4
+ validate :count_within_limit, :on => :create
5
+
6
+ def count_within_limit
7
+ if Trophy.where(user_id:self.user_id).count >= 5
8
+ errors.add(:base, "Exceeded trophy limit")
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,17 @@
1
+ # Copyright © 2012 The Pennsylvania State University
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ class VersionCommitter < ActiveRecord::Base
16
+ attr_accessible :obj_id, :datastream_id, :version_id, :committer_login
17
+ end