Class: Annotations2triannon::AnnotationTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/annotations2triannon/annotation_tracker.rb

Overview

Annotation tracking

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name = 'anno_tracking.json') ⇒ AnnotationTracker

Returns a new instance of AnnotationTracker.



10
11
12
13
14
# File 'lib/annotations2triannon/annotation_tracker.rb', line 10

def initialize(file_name='anno_tracking.json')
  @config = Annotations2triannon.configuration
  @anno_file = File.join(@config.log_path, file_name)
  FileUtils.touch @anno_file
end

Instance Attribute Details

#anno_fileObject

Returns the value of attribute anno_file.



7
8
9
# File 'lib/annotations2triannon/annotation_tracker.rb', line 7

def anno_file
  @anno_file
end

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/annotations2triannon/annotation_tracker.rb', line 8

def config
  @config
end

Instance Method Details

#archiveObject

Save the current tracking file to an archive file tagged by timestamp



17
18
19
20
21
22
23
24
# File 'lib/annotations2triannon/annotation_tracker.rb', line 17

def archive
  # Date and time of day for calendar date (basic)
  # %Y%m%dT%H%M%S%z  => 20071119T083748-0600
  time_stamp = DateTime.now.strftime('%Y%m%dT%H%M%S%z')
  ext = File.extname(@anno_file)
  archive = @anno_file.sub(ext, "_#{time_stamp}#{ext}")
  FileUtils.copy(@anno_file, archive)
end

#delete_annotations(uris = []) ⇒ Object

DELETE previous annotations loaded to triannon Accepts an input array of annotation URIs or finds them in the annotation tracking data and removes them from triannon (if they exist). Logs warnings or errors for annotations that do not exist or fail to DELETE.

Raises:

  • (ArgumentError)


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
# File 'lib/annotations2triannon/annotation_tracker.rb', line 89

def delete_annotations(uris=[])
  raise ArgumentError, 'uris must be an Array<RDF::URI>' unless uris.instance_of? Array
  tc = TriannonClient::TriannonClient.new
  status = true
  uris = load_uris if uris.empty?
  anno_ids = uris.collect {|uri| tc.annotation_id(uri) }
  # TODO: Enable intersection code below when a better set of annotations
  # can be retrieved from triannon and/or Solr.
  anno_ids.each do |id|
    unless tc.delete_annotation(id)
      @config.logger.error("FAILURE to delete #{id}")
      status = false
    end
  end
  return status

  # Find the intersection of the annotation URIs and
  # the current set of annotations in triannon.
  # Note: the triannon /annotations response may be a limited subset of
  # annotations that does not include any of the previously submitted
  # annotation IDs.  If there are some annotations in the intersection,
  # we have to assume that they are all present and proceed to delete
  # them all.
  # TODO: Use Solr to get a better list of current annotation URIs
  # graph = tc.get_annotations
  # uris = tc.annotation_uris(graph)
  # ids = uris.collect {|uri| tc.annotation_id(uri)}
  # annos_to_remove = anno_ids & ids # intersection of arrays
  # if annos_to_remove.empty?
  #   @config.logger.warn("annotations were not found in triannon.")
  # end
  # if annos_to_remove.length < anno_ids.length
  #   @config.logger.warn("annotations are not current in triannon.")
  # end
  # annos_to_remove.each do |id|
  #   unless tc.delete_annotation(id)
  #     @config.logger.error("FAILURE to delete #{id}")
  #     status = false
  #   end
  # end
  # return status
end

#loadObject

retrieve the anno_tracking data from a file



28
29
30
31
32
33
34
35
36
# File 'lib/annotations2triannon/annotation_tracker.rb', line 28

def load
  begin
    json_load(@anno_file) || {}
  rescue
    msg = "FAILURE to load annotation tracking file #{@anno_file}"
    @config.logger.error(msg)
    {}
  end
end

#load_urisObject

Retrieve the annotation URIs from an anno_tracking data file Assumes the annotation tracking data is a hash with a structure:

manifest_uri: [annotation_list, annotation_list, ]

where each annotation_list is a hash with a structure:

anno_list_uri: [annotations, annotations, ]

where annotations is an array of hashes, with a structure:

uri: uri,
chars: body_content_chars

and the uri is a triannon annotation URI.



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/annotations2triannon/annotation_tracker.rb', line 55

def load_uris
  uris = []
  data = load
  data.each_pair do |manifest_uri, anno_lists|
    anno_lists.each_pair do |anno_list_uri, anno_list|
      anno_list.each do |anno_data|
        uris << RDF::URI.new(anno_data['uri'])
      end
    end
  end
  uris
end

#save(data) ⇒ Object

persist the anno_tracking data to a file

Parameters:

  • data (Hash)


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/annotations2triannon/annotation_tracker.rb', line 71

def save(data)
  begin
    json_save(@anno_file, data)
    puts "Annotation records updated in: #{@anno_file}"
    return true
  rescue
    msg = "FAILURE to save annotation tracking file #{@anno_file}"
    @config.logger.error(msg)
    return false
  end
end