1 | # This module is based on the UML,
|
---|
2 | # http://www.openehr.org/uml/release-1.0.1/Browsable/_9_0_76d0249_1109326589721_134411_997Report.html
|
---|
3 | # Ticket refs #64
|
---|
4 | include OpenEhr::RM::Common::Generic
|
---|
5 | module OpenEhr
|
---|
6 | module RM
|
---|
7 | module Common
|
---|
8 | module ChangeControl
|
---|
9 | class Contribution
|
---|
10 | attr_reader :uid, :versions, :audit
|
---|
11 |
|
---|
12 | def initialize(args = { })
|
---|
13 | self.uid = args[:uid]
|
---|
14 | self.versions = args[:versions]
|
---|
15 | self.audit = args[:audit]
|
---|
16 | end
|
---|
17 |
|
---|
18 | def uid=(uid)
|
---|
19 | if uid.nil?
|
---|
20 | raise ArgumentError, "uid should not be nil."
|
---|
21 | end
|
---|
22 | @uid = uid
|
---|
23 | end
|
---|
24 |
|
---|
25 | def versions=(versions)
|
---|
26 | unless versions.nil?
|
---|
27 | if versions.empty?
|
---|
28 | raise ArgumentError, 'versions shoud not be nil or empty.'
|
---|
29 | end
|
---|
30 | end
|
---|
31 | @versions = versions
|
---|
32 | end
|
---|
33 |
|
---|
34 | def audit=(audit)
|
---|
35 | if audit.nil?
|
---|
36 | raise ArgumentError, 'audit should not be nil.'
|
---|
37 | end
|
---|
38 | if audit.description.nil?
|
---|
39 | raise ArgumentError, 'audit.description should not be nil.'
|
---|
40 | end
|
---|
41 | @audit = audit
|
---|
42 | end
|
---|
43 | end
|
---|
44 |
|
---|
45 | class Version
|
---|
46 | attr_reader :uid, :preceding_version_uid, :lifecycle_state,
|
---|
47 | :commit_audit, :contribution
|
---|
48 | attr_accessor :data, :signature
|
---|
49 |
|
---|
50 | def initialize(args = { })
|
---|
51 | self.uid = args[:uid]
|
---|
52 | self.preceding_version_uid = args[:preceding_version_uid]
|
---|
53 | self.data = args[:data]
|
---|
54 | self.lifcycle_state = args[:lifecycle_state]
|
---|
55 | self.commit_audit = args[:commit_audit]
|
---|
56 | self.contribution = args[:contribution]
|
---|
57 | self.signature = args[:signature]
|
---|
58 | end
|
---|
59 |
|
---|
60 | def uid=(uid)
|
---|
61 | raise ArgumentError, "uid should not be nil" if uid.nil?
|
---|
62 | @uid = uid
|
---|
63 | end
|
---|
64 |
|
---|
65 | def preceding_version_uid=(preceding_version_uid)
|
---|
66 | if (!preceding_version_uid.nil?) == @uid.version_tree_id.is_first?
|
---|
67 | raise ArgumentError, 'preceding version is invalid'
|
---|
68 | end
|
---|
69 | @preceding_version_uid = preceding_version_uid
|
---|
70 | end
|
---|
71 |
|
---|
72 | # remove hard coding of lifecycle
|
---|
73 | def lifcycle_state=(lifecycle_state)
|
---|
74 | if lifecycle_state.nil? ||
|
---|
75 | !%w[532 553 523].include?(
|
---|
76 | lifecycle_state.defining_code.code_string)
|
---|
77 | raise ArgumentError, 'invalid lifecycle_state'
|
---|
78 | end
|
---|
79 | @lifecycle_state = lifecycle_state
|
---|
80 | end
|
---|
81 |
|
---|
82 | def commit_audit=(commit_audit)
|
---|
83 | if commit_audit.nil?
|
---|
84 | raise ArgumentError,'commit_audit is mandatory'
|
---|
85 | end
|
---|
86 | @commit_audit = commit_audit
|
---|
87 | end
|
---|
88 |
|
---|
89 | def contribution=(contribution)
|
---|
90 | raise ArgumentError, "contribution should not be nil" if contribution.nil? or contribution.type.empty?
|
---|
91 | @contribution = contribution
|
---|
92 | end
|
---|
93 |
|
---|
94 | def owner_id
|
---|
95 | return HierObjectId.new(@uid.object_id.value)
|
---|
96 | end
|
---|
97 |
|
---|
98 | def is_branch?
|
---|
99 | return @uid.is_branch?
|
---|
100 | end
|
---|
101 |
|
---|
102 | def canonical_form
|
---|
103 | raise NotImplementedError, 'canonical form not implemented'
|
---|
104 | end
|
---|
105 | end
|
---|
106 |
|
---|
107 | class ImportedVersion < Version
|
---|
108 | attr_reader :item
|
---|
109 |
|
---|
110 | def initialize(args = { })
|
---|
111 | self.item = args[:item]
|
---|
112 | super(:uid => @item.uid,
|
---|
113 | :preceding_version_uid => @item.preceding_version_uid,
|
---|
114 | :data => @item.data, :commit_audit=> args[:commit_audit],
|
---|
115 | :commit_audit => args[:commit_audit],
|
---|
116 | :contribution => args[:contribution],
|
---|
117 | :lifecycle_state => @item.lifecycle_state,
|
---|
118 | :signature => args[:signature])
|
---|
119 |
|
---|
120 | end
|
---|
121 |
|
---|
122 | def item=(item)
|
---|
123 | raise ArgumentError, 'item is mandatory' if item.nil?
|
---|
124 | @item = item
|
---|
125 | end
|
---|
126 | end
|
---|
127 |
|
---|
128 | class OriginalVersion < Version
|
---|
129 | attr_reader :attestations, :other_input_version_uids
|
---|
130 |
|
---|
131 | def initialize(args = { })
|
---|
132 | super(args)
|
---|
133 | self.attestations = args[:attestations]
|
---|
134 | self.other_input_version_uids = args[:other_input_version_uids]
|
---|
135 | end
|
---|
136 |
|
---|
137 | def attestations=(attestations)
|
---|
138 | if attestations.nil? || attestations.empty?
|
---|
139 | raise ArgumentError, 'attestations is mandatory'
|
---|
140 | end
|
---|
141 | @attestations = attestations
|
---|
142 | end
|
---|
143 |
|
---|
144 | def other_input_version_uids=(other_input_version_uids)
|
---|
145 | if !other_input_version_uids.nil? && other_input_version_uids.empty?
|
---|
146 | raise ArgumentError, 'invaild other_input_version_uids'
|
---|
147 | end
|
---|
148 | @other_input_version_uids = other_input_version_uids
|
---|
149 | end
|
---|
150 |
|
---|
151 | def is_merged?
|
---|
152 | # Java implementation has is_merged attribute, Eiffel implementation does
|
---|
153 | # nothing
|
---|
154 | end
|
---|
155 | end
|
---|
156 |
|
---|
157 | class VersionedObject
|
---|
158 | attr_reader :uid, :owner_id, :time_created, :all_versions
|
---|
159 |
|
---|
160 | def initialize(args = { })
|
---|
161 | self.uid = args[:uid]
|
---|
162 | self.owner_id = args[:owner_id]
|
---|
163 | self.time_created = args[:time_created]
|
---|
164 | self.all_versions = args[:all_versions]
|
---|
165 | end
|
---|
166 |
|
---|
167 | def uid=(uid)
|
---|
168 | raise ArgumentError, 'uid is mandatory' if uid.nil?
|
---|
169 | @uid = uid
|
---|
170 | end
|
---|
171 |
|
---|
172 | def owner_id=(owner_id)
|
---|
173 | raise ArgumentError, 'owner_id is mandatory' if owner_id.nil?
|
---|
174 | @owner_id = owner_id
|
---|
175 | end
|
---|
176 |
|
---|
177 | def time_created=(time_created)
|
---|
178 | if time_created.nil?
|
---|
179 | raise ArgumentError, 'time_created is mandatory'
|
---|
180 | end
|
---|
181 | @time_created = time_created
|
---|
182 | end
|
---|
183 |
|
---|
184 | def all_versions=(all_versions)
|
---|
185 | if all_versions.nil? || all_versions.size < 0
|
---|
186 | raise ArgumentError, 'version count invalid'
|
---|
187 | end
|
---|
188 | @all_versions = all_versions
|
---|
189 | end
|
---|
190 |
|
---|
191 | def all_version_ids
|
---|
192 | ids = []
|
---|
193 | @all_versions.each{|id| ids << id.uid}
|
---|
194 | return ids
|
---|
195 | end
|
---|
196 |
|
---|
197 | def version_count
|
---|
198 | return all_versions.size
|
---|
199 | end
|
---|
200 |
|
---|
201 | def has_version_id?(a_ver_id)
|
---|
202 | raise ArgumentError, 'argument is mandatory' if a_ver_id.nil?
|
---|
203 | return self.all_version_ids.include?(a_ver_id)
|
---|
204 | end
|
---|
205 |
|
---|
206 | def is_original_version?(a_ver_id)
|
---|
207 | if a_ver_id.nil? || !self.has_version_id?(a_ver_id)
|
---|
208 | raise ArgumentError, 'invalid a_ver_id'
|
---|
209 | end
|
---|
210 | return @all_versions[self.all_version_ids.index(a_ver_id)].instance_of? OriginalVersion
|
---|
211 | end
|
---|
212 |
|
---|
213 | def has_version_at_time?(a_time)
|
---|
214 | raise ArgumentError, 'argument mandatory' if a_time.nil?
|
---|
215 | @all_versions.each do |ver|
|
---|
216 | if ver.commit_audit.time_committed == a_time
|
---|
217 | return true
|
---|
218 | end
|
---|
219 | end
|
---|
220 | return false
|
---|
221 | end
|
---|
222 |
|
---|
223 | def version_with_id(a_ver_id)
|
---|
224 | if a_ver_id.nil? || !self.has_version_id?(a_ver_id)
|
---|
225 | raise ArgumentError, 'argument invalid'
|
---|
226 | end
|
---|
227 | return @all_versions[self.all_version_ids.index(a_ver_id)]
|
---|
228 | end
|
---|
229 |
|
---|
230 | def version_at_time(a_time)
|
---|
231 | if a_time.nil? || !self.has_version_at_time?(a_time)
|
---|
232 | raise ArgumentError, 'argument invalid'
|
---|
233 | end
|
---|
234 | @all_versions.each do |ver|
|
---|
235 | if ver.commit_audit.time_committed == a_time
|
---|
236 | return ver
|
---|
237 | end
|
---|
238 | end
|
---|
239 | end
|
---|
240 |
|
---|
241 | def latest_version
|
---|
242 | time_sorted_version = @all_versions.sort do |a,b|
|
---|
243 | a.commit_audit.time_committed <=> b.commit_audit.time_committed
|
---|
244 | end
|
---|
245 | return time_sorted_version.last
|
---|
246 | end
|
---|
247 |
|
---|
248 | def latest_trunk_version
|
---|
249 | trunk_versions = [ ]
|
---|
250 | @all_versions.each do |ver|
|
---|
251 | if ver.uid.version_tree_id.trunk_version == '1'
|
---|
252 | trunk_versions << ver
|
---|
253 | end
|
---|
254 | end
|
---|
255 | sorted_trunk_version = trunk_versions.sort do |a,b|
|
---|
256 | a.commit_audit.time_committed <=> b.commit_audit.time_committed
|
---|
257 | end
|
---|
258 | return sorted_trunk_version.last
|
---|
259 | end
|
---|
260 |
|
---|
261 | def trunk_lifecycle_state
|
---|
262 | return self.latest_trunk_version.lifecycle_state
|
---|
263 | end
|
---|
264 |
|
---|
265 | def revision_history
|
---|
266 | revision_history_items = [ ]
|
---|
267 | @all_versions.each do |ver|
|
---|
268 | audits = [ ]
|
---|
269 | if ver.instance_of? OriginalVersion
|
---|
270 | audits << ver.attestations
|
---|
271 | end
|
---|
272 | audits << ver.commit_audit
|
---|
273 | revision_history_items << RevisionHistoryItem.new(
|
---|
274 | :audits => audits,
|
---|
275 | :version_id => ver.uid)
|
---|
276 | end
|
---|
277 | return RevisionHistory.new(revision_history_items)
|
---|
278 | end
|
---|
279 |
|
---|
280 | def commit_original_version(args={ })
|
---|
281 | @all_versions << OriginalVersion.new(:uid => args[:uid],
|
---|
282 | :preceding_version_uid => args[:preceding_version_uid],
|
---|
283 | :contribution => args[:contribution],
|
---|
284 | :commit_audit => args[:commit_audit],
|
---|
285 | :lifecycle_state => args[:lifecycle_state],
|
---|
286 | :data => args[:data],
|
---|
287 | :attestations => args[:attestations],
|
---|
288 | :signature => args[:signature])
|
---|
289 | end
|
---|
290 |
|
---|
291 | def commit_original_merged_version(args = { })
|
---|
292 | @all_versions << OriginalVersion.new(:uid => args[:uid],
|
---|
293 | :contribution => args[:contribution],
|
---|
294 | :preceding_version_uid => args[:preceding_version_uid],
|
---|
295 | :commit_audit => args[:commit_audit],
|
---|
296 | :lifecycle_state => args[:lifecycle_state],
|
---|
297 | :data => args[:data],
|
---|
298 | :attestations => args[:attestations],
|
---|
299 | :other_input_version_uids => args[:other_input_version_uids],
|
---|
300 | :signature => args[:signature])
|
---|
301 | end
|
---|
302 |
|
---|
303 | def commit_imported_version(args = { })
|
---|
304 | @all_versions << ImportedVersion.new(:item => args[:item],
|
---|
305 | :contribution => args[:contribution],
|
---|
306 | :commit_audit => args[:commit_audit])
|
---|
307 | end
|
---|
308 |
|
---|
309 | def commit_attestation(args = { })
|
---|
310 | if args[:attestation].nil?
|
---|
311 | raise ArgumentError, 'attestation is mandatory'
|
---|
312 | end
|
---|
313 | if self.has_version_id?(args[:uid]) && self.is_original_version?(args[:uid])
|
---|
314 | self.version_with_id(args[:uid]).attestations << args[:attestation]
|
---|
315 | self.version_with_id(args[:uid]).signature = args[:signature]
|
---|
316 | else
|
---|
317 | raise ArgumentError, 'uid invalid'
|
---|
318 | end
|
---|
319 | end
|
---|
320 | end
|
---|
321 | end # of ChangeControl
|
---|
322 | end # of Common
|
---|
323 | end # of RM
|
---|
324 | end # of OpenEHR
|
---|