-
Notifications
You must be signed in to change notification settings - Fork 224
/
event.rb
169 lines (131 loc) · 5.44 KB
/
event.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
require 'time'
class Event < Sequel::Model(:event)
include ASModel
corresponds_to JSONModel(:event)
include Agents
include ExternalDocuments
include ExternalIDs
agent_role_enum("linked_agent_event_roles")
set_model_scope :repository
allow_in_global_repo
enable_suppression
one_to_many :date, :class => "ASDate"
def_nested_record(:the_property => :date,
:contains_records_of_type => :date,
:corresponding_to_association => :date,
:is_array => false)
define_relationship(:name => :event_link,
:json_property => 'linked_records',
:contains_references_to_types => proc {[Accession, Resource, ArchivalObject, DigitalObject, AgentPerson, AgentCorporateEntity, AgentFamily, AgentSoftware, DigitalObjectComponent, TopContainer]},
:class_callback => proc { |clz|
clz.instance_eval do
include DynamicEnums
uses_enums({
:property => 'role',
:uses_enum => ['linked_event_archival_record_roles']
})
end
})
def has_active_linked_records?
self.related_records(:event_link).each do |linked_record|
if linked_record.values.has_key?(:suppressed) && linked_record[:suppressed] == 0
return true
end
end
return false
end
def self.sequel_to_jsonmodel(objs, opts = {})
jsons = super
jsons.each do |json|
if json['timestamp']
json['timestamp'] = json['timestamp'].iso8601
end
end
jsons
end
#
# Some canned creators for system generated events
#
def self.for_component_transfer(archival_object_uri, source_resource_uri, target_resource_uri)
# first get the current user
user = User[:username => RequestContext.get(:current_username)]
# build event
event = JSONModel(:event).from_hash({
"event_type" => "component_transfer",
"timestamp" => Time.now.utc.iso8601,
"linked_records" => [
{"role" => "source", "ref" => source_resource_uri},
{"role" => "outcome", "ref" => target_resource_uri},
{"role" => "transfer", "ref" => archival_object_uri},
],
"linked_agents" => [
{"role" => "implementer", "ref" => JSONModel(:agent_person).uri_for(user.agent_record_id)}
]
})
# save the event to the DB in the global context
self.create_from_json(event, :system_generated => true)
end
def self.for_cataloging(agent_uri, record_uri)
#build event
event = JSONModel(:event).from_hash(
:linked_agents => [{:ref => agent_uri, :role => 'implementer'}],
:event_type => 'cataloged',
:timestamp => Time.now.utc.iso8601,
:linked_records => [{:ref => record_uri, :role => 'outcome'}]
)
RequestContext.in_global_repo do
Event.create_from_json(event, :system_generated => true)
end
end
def self.for_archival_record_merge(target, victims)
user = User[:username => RequestContext.get(:current_username)]
merge_note = ""
victims.each do |victim|
victim_json = victim.class.to_jsonmodel(victim)
if victim_json['identifier']
identifier = Identifiers.format(Identifiers.parse(victim_json['identifier']))
else
identifier = victim_json['digital_object_id']
end
merge_note += (identifier +
" -- " +
victim_json['title'] +
"\n")
end
event = JSONModel(:event).from_hash(
:linked_agents => [{
"role" => "implementer",
"ref" => JSONModel(:agent_person).uri_for(user.agent_record_id)
}],
:event_type => 'component_transfer',
:outcome => 'pass',
:outcome_note => merge_note,
:timestamp => Time.now.utc.iso8601,
:linked_records => [{:ref => target.uri, :role => 'outcome'}]
)
Event.create_from_json(event, :system_generated => true)
end
def self.for_repository_transfer(old_repo, new_repo, record)
event = {
:linked_agents => [
{
"role" => "transmitter",
"ref" => JSONModel(:agent_corporate_entity).uri_for(old_repo.agent_representation_id)
},
{
"role" => "recipient",
"ref" => JSONModel(:agent_corporate_entity).uri_for(new_repo.agent_representation_id)
}
].reject {|l| l['ref'].nil?},
:linked_records => [
{
"role" => "transfer",
"ref" => record.uri
}
],
:event_type => 'custody_transfer',
:timestamp => Time.now.utc.iso8601,
}
Event.create_from_json(JSONModel(:event).from_hash(event), :system_generated => true)
end
end