Module: Dor::Identifiable

Extended by:
ActiveSupport::Concern
Includes:
Eventable, Upgradable, SolrDocHelper
Included in:
Abstract, AdminPolicyObject, BasicItem, Collection, Publishable, Set, WorkflowObject
Defined in:
lib/dor/models/identifiable.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

@@collection_hash =
{}
@@apo_hash =
{}
@@hydrus_apo_hash =
{}
@@hydrus_collection_hash =
{}

Instance Method Summary collapse

Methods included from Upgradable

add_upgrade_callback, included, run_upgrade_callbacks, #upgrade!

Methods included from Eventable

#add_event

Methods included from SolrDocHelper

#add_solr_value

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

Syntactic sugar for identifying applied DOR Concerns e.g., obj.is_identifiable? is the same as obj.is_a?(Dor::Identifiable)



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dor/models/identifiable.rb', line 43

def method_missing sym, *args
  if sym.to_s =~ /^is_(.+)\?$/
    begin
      klass = Dor.const_get $1.capitalize.to_sym
      return self.is_a?(klass)
    rescue NameError
      return false
    end
  else
    super
  end
end

Instance Method Details

#add_other_Id(type, val) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/dor/models/identifiable.rb', line 138

def add_other_Id(type,val)
  if self..otherId(type).length>0        
    raise 'There is an existing entry for '+node_name+', consider using update_other_identifier.'
  end
   = self.
  .add_otherId(type+':'+val)
end

#add_tag(tag) ⇒ Object



174
175
176
177
178
179
180
181
182
183
# File 'lib/dor/models/identifiable.rb', line 174

def add_tag(tag)
   = self.
  prefix=tag.split(/:/).first
  .tags.each do |existing_tag|
    if existing_tag.split(/:/).first ==prefix 
      raise 'An existing tag ('+existing_tag+') has the same prefix, consider using update_tag?'
    end
  end
  .add_value(:tag,tag)
end

#content_type_tagObject

helper method to get just the content type tag



36
37
38
39
# File 'lib/dor/models/identifiable.rb', line 36

def content_type_tag
  =tags.select {|tag| tag.include?('Process : Content Type')}
  .size == 1 ? [0].split(':').last.strip : ""
end

#initialize(attrs = {}) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/dor/models/identifiable.rb', line 21

def initialize attrs={}
  if Dor::Config.suri.mint_ids
    unless attrs[:pid]
      attrs = attrs.merge!({:pid=>Dor::SuriService.mint_id, :new_object => true})
    end
  end
  super
end

#remove_other_Id(type, val = nil) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/dor/models/identifiable.rb', line 160

def remove_other_Id(type,val=nil)
  ds_xml=self..ng_xml
  #split the thing they sent in to find the node name
  removed=false

  ds_xml.search('//otherId[@name=\''+type+'\']').each do |node|
    if node.content===val or val==nil
      node.remove
      removed=true
    end
  end
  return removed
end

#remove_tag(tag) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/dor/models/identifiable.rb', line 185

def remove_tag(tag)
   = self.
  ds_xml=.ng_xml
  removed=false
  ds_xml.search('//tag').each do |node|
    if node.content===tag
      node.remove
      removed=true
    end
  end
  return removed
end

#set_source_id(source_id) ⇒ Object



134
135
136
# File 'lib/dor/models/identifiable.rb', line 134

def set_source_id(source_id)
  self..sourceId = source_id
end

#tagsObject

helper method to get the tags as an array



31
32
33
# File 'lib/dor/models/identifiable.rb', line 31

def tags
  self..tag
end

#to_solr(solr_doc = Hash.new, *args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/dor/models/identifiable.rb', line 60

def to_solr(solr_doc=Hash.new, *args)
  self.assert_content_model
  super(solr_doc)
  solr_doc[Dor::INDEX_VERSION_FIELD] = Dor::VERSION
  solr_doc[solr_name('indexed_at',:date)] = Time.now.utc.xmlschema
  add_solr_value(solr_doc, 'indexed_day', Time.now.beginning_of_day.utc.xmlschema, :string, [:searchable, :facetable])
  datastreams.values.each do |ds|
    unless ds.new?
      add_solr_value(solr_doc,'ds_specs',ds.datastream_spec_string,:string,[:displayable])
    end
  end
  add_solr_value(solr_doc, 'title_sort', self.label, :string, [:sortable])
  rels_doc = Nokogiri::XML(self.datastreams['RELS-EXT'].content)
  apos=rels_doc.search('//rdf:RDF/rdf:Description/hydra:isGovernedBy','hydra' => 'http://projecthydra.org/ns/relations#', 'fedora' => 'info:fedora/fedora-system:def/relations-external#', 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'   )
  apos.each do |apo_node|
    druid=apo_node['rdf:resource']
    if druid
      druid=druid.gsub('info:fedora/','')
      if @@apo_hash.has_key? druid or @@hydrus_apo_hash.has_key? druid
        add_solr_value(solr_doc, "hydrus_apo_title", @@hydrus_apo_hash[druid], :string, [:searchable, :facetable]) if @@hydrus_apo_hash.has_key? druid
        add_solr_value(solr_doc, "apo_title", @@apo_hash[druid] , :string, [:searchable, :facetable]) if @@apo_hash.has_key? druid 
      else
        begin
          apo_object=Dor.find(druid)
          if apo_object.tags.include? 'Project : Hydrus'
            add_solr_value(solr_doc, "hydrus_apo_title", apo_object.label, :string, [:searchable, :facetable])
            @@hydrus_apo_hash[druid]=apo_object.label
          else
            add_solr_value(solr_doc, "apo_title", apo_object.label, :string, [:searchable, :facetable])
            @@apo_hash[druid]=apo_object.label
          end
        rescue
          add_solr_value(solr_doc, "apo_title", druid, :string, [:searchable, :facetable])
        end
      end
    end
  end
  collections=rels_doc.search('//rdf:RDF/rdf:Description/fedora:isMemberOfCollection','fedora' => 'info:fedora/fedora-system:def/relations-external#', 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'   )
  collections.each do |collection_node| 
    druid=collection_node['rdf:resource']
    if(druid)
      druid=druid.gsub('info:fedora/','')
      if @@collection_hash.has_key? druid or @@hydrus_collection_hash.has_key? druid
        add_solr_value(solr_doc, "hydrus_collection_title", @@hydrus_collection_hash[druid], :string, [:searchable, :facetable]) if @@hydrus_collection_hash.has_key? druid
        add_solr_value(solr_doc, "collection_title", @@collection_hash[druid], :string, [:searchable, :facetable]) if @@collection_hash.has_key? druid
      else
        begin
          collection_object=Dor.find(druid)
          if collection_object.tags.include? 'Project : Hydrus'
            add_solr_value(solr_doc, "hydrus_collection_title", collection_object.label, :string, [:searchable, :facetable])
            @@hydrus_collection_hash[druid]=collection_object.label
          else
            add_solr_value(solr_doc, "collection_title", collection_object.label, :string, [:searchable, :facetable])
            @@collection_hash[druid]=collection_object.label
          end
        rescue
          add_solr_value(solr_doc, "collection_title", druid, :string, [:searchable, :facetable])
        end
      end
    end
  end 
  # Fix for ActiveFedora 3.3 to ensure all date fields are properly formatted as UTC XML Schema datetime strings
  solr_doc.each_pair { |k,v| 
    if k =~ /_dt|_date$/
      if v.is_a?(Array)
        solr_doc[k] = v.collect { |t| Time.parse(t.to_s).utc.xmlschema }
      else
        solr_doc[k] = Time.parse(v.to_s).utc.xmlschema
      end
    end
  }

  solr_doc
end

#update_other_Id(type, new_val, val = nil) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/dor/models/identifiable.rb', line 146

def update_other_Id(type,new_val, val=nil)
   = self.
  ds_xml=.ng_xml
  #split the thing they sent in to find the node name
  updated=false
  ds_xml.search('//otherId[@name=\''+type+'\']').each do |node|
    if node.content==val or val==nil
      node.content=new_val
      updated=true
    end
  end
  return updated
end

#update_tag(old_tag, new_tag) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/dor/models/identifiable.rb', line 198

def update_tag(old_tag,new_tag)
   = self.
  ds_xml=.ng_xml
  updated=false
  ds_xml.search('//tag').each do |node|
    if node.content==old_tag
      node.content=new_tag
      updated = true
    end
  end
  return updated 
end