Index: /ruby/trunk/lib/models/rm/common/change_control.rb
===================================================================
--- /ruby/trunk/lib/models/rm/common/change_control.rb	(revision 159)
+++ /ruby/trunk/lib/models/rm/common/change_control.rb	(revision 160)
@@ -41,7 +41,4 @@
             @audit = audit
           end
-        end
-
-        class Versioned_Object
         end
 
@@ -73,4 +70,5 @@
           end
 
+# remove hard coding of lifecycle
           def lifcycle_state=(lifecycle_state)
             if lifecycle_state.nil? ||
@@ -156,4 +154,139 @@
           end
         end
+
+        class Versioned_Object
+          attr_reader :uid, :owner_id, :time_created, :all_versions
+
+          def initialize(args = { })
+            self.uid = args[:uid]
+            self.owner_id = args[:owner_id]
+            self.time_created = args[:time_created]
+            self.all_versions = args[:all_versions]
+          end
+
+          def uid=(uid)
+            raise ArgumentError, 'uid is mandatory' if uid.nil?
+            @uid = uid
+          end
+
+          def owner_id=(owner_id)
+            raise ArgumentError, 'owner_id is mandatory' if owner_id.nil?
+            @owner_id = owner_id
+          end
+
+          def time_created=(time_created)
+            if time_created.nil?
+              raise ArgumentError, 'time_created is mandatory'
+            end
+            @time_created = time_created
+          end
+
+          def all_versions=(all_versions)
+            if all_versions.nil? || all_versions.size < 0
+              raise ArgumentError, 'version count invalid'
+            end
+            @all_versions = all_versions
+          end
+
+          def all_version_ids
+            ids = []
+            @all_versions.each{|id| ids << id.uid}
+            return ids
+          end
+
+          def version_count
+            return all_versions.size
+          end
+
+          def has_version_id?(a_ver_id)
+            raise ArgumentError, 'argument is mandatory' if a_ver_id.nil?
+            return self.all_version_ids.include?(a_ver_id)
+          end
+
+          def is_original_version?(a_ver_id)
+            if a_ver_id.nil? || !self.has_version_id?(a_ver_id)
+              raise ArgumentError, 'invalid a_ver_id'
+            end
+            return @all_versions[self.all_version_ids.index(a_ver_id)].instance_of? Original_Version
+          end
+
+          def has_version_at_time?(a_time)
+            raise ArgumentError, 'argument mandatory' if a_time.nil?
+            @all_versions.each do |ver|
+              if ver.commit_audit.time_committed == a_time
+                return true
+              end
+            end
+            return false
+          end
+
+          def version_with_id(a_ver_id)
+            if a_ver_id.nil? || !self.has_version_id?(a_ver_id)
+              raise ArgumentError, 'argument invalid'
+            end
+            return @all_versions[self.all_version_ids.index(a_ver_id)]
+          end
+
+          def version_at_time(a_time)
+            if a_time.nil? || !self.has_version_at_time?(a_time)
+              raise ArgumentError, 'argument invalid'
+            end
+            @all_versions.each do |ver|
+              if ver.commit_audit.time_committed == a_time
+                return ver
+              end
+            end
+          end
+
+          def latest_version
+            time_sorted_version = @all_versions.sort do |a,b|
+              a.commit_audit.time_committed <=> b.commit_audit.time_committed
+            end
+            return time_sorted_version.last
+          end
+
+          def latest_trunk_version
+            trunk_versions = [ ]
+            @all_versions.each do |ver|
+              if ver.uid.version_tree_id.trunk_version == '1'
+                trunk_versions << ver
+              end
+            end
+            sorted_trunk_version = trunk_versions.sort do |a,b|
+              a.commit_audit.time_committed <=> b.commit_audit.time_committed
+            end
+            return sorted_trunk_version.last
+          end
+
+          def trunk_lifecycle_state
+            return self.latest_trunk_version.lifecycle_state
+          end
+
+          def revision_history
+            revision_history_items = [ ]
+            @all_versions.each do |ver|
+              audits = [ ]
+              if ver.instance_of? Original_Version
+                audits << ver.attestations
+              end
+              audits << ver.commit_audit
+              revision_history_items << Revision_History_Item.new(
+                                          :audits => audits,
+                                          :version_id => ver.uid)
+            end
+            return Revision_History.new(revision_history_items)
+          end
+
+          def commit_original_version(args={ })
+            @all_versions << Original_Version.new(:uid => args[:uid],
+                                                  :preceding_version_uid => args[:preceding_version_uid],
+                                                  :contribution => args[:contribution],
+                                                  :commit_audit => args[:commit_audit],
+                                                  :lifecycle_state => args[:lifecycle_state],
+                                                  :data => args[:data],
+                                                  :attestations => args[:attestations],
+                                                  :signature => args[:signature])
+          end
+        end
       end # of Change_Control
     end # of Common
Index: /ruby/trunk/lib/models/tests/rm/test_common.rb
===================================================================
--- /ruby/trunk/lib/models/tests/rm/test_common.rb	(revision 159)
+++ /ruby/trunk/lib/models/tests/rm/test_common.rb	(revision 160)
@@ -342,4 +342,9 @@
                                   :change_type => dv_coded_text,
                                   :reason => DV_Text.new('signed'))
+    dv_date_time = DV_Date_Time.new('2009-07-07T21:10:12')
+    audit_details = Audit_Details.new(:system_id => 'MAGI',
+                                      :committer => party_proxy,
+                                      :time_committed => dv_date_time,
+                                      :change_type => dv_coded_text)
     assert_nothing_raised(Exception){
       @original_version = Original_Version.new(:uid => object_version_id3,
@@ -351,4 +356,5 @@
                                                :data => 'data',
                                                :lifecycle_state => dv_coded_text)}
+    dv_date_time = DV_Date_Time.new('2009-07-08T21:10:12')
     audit_details = Audit_Details.new(:system_id => 'CASPER',
                                       :committer => party_proxy,
@@ -366,4 +372,19 @@
                                        :versions => Set[object_ref],
                                        :audit => audit_details)}
+    dv_date_time = DV_Date_Time.new('2009-07-08T18:10:12')
+    audit_details = Audit_Details.new(:system_id => 'MAGI',
+                                      :committer => party_proxy,
+                                      :time_committed => dv_date_time,
+                                      :change_type => dv_coded_text)
+    @trunk_version = Version.new(:uid => object_version_id4,
+                                :lifecycle_state => dv_coded_text,
+                                :commit_audit => audit_details,
+                                :contribution => object_ref,
+                                :change_type => dv_coded_text)
+    assert_nothing_raised(Exception){
+      @versioned_object = Versioned_Object.new(:uid => hier_object_id,
+                                               :owner_id => object_ref,
+                                               :time_created => dv_date_time,
+                                               :all_versions => [@version,@original_version,@imported_version,@trunk_version])}
   end
 
@@ -373,4 +394,5 @@
     assert_instance_of Imported_Version, @imported_version
     assert_instance_of OpenEHR::RM::Common::Change_Control::Contribution, @contribution
+    assert_instance_of Versioned_Object, @versioned_object
   end
 
@@ -400,5 +422,4 @@
     assert_equal 'STU::VWX::1.2.3', @imported_version.preceding_version_uid.value
     assert_equal '532', @imported_version.lifecycle_state.defining_code.code_string
-    
   end
 
@@ -408,3 +429,34 @@
     assert_equal 'for contribution', @contribution.audit.description
   end
+
+  def test_versioned_object
+    assert_equal 'ABC', @versioned_object.uid.value
+    assert_equal 'unknown', @versioned_object.owner_id.namespace
+    assert_equal 18, @versioned_object.time_created.hour
+    assert_equal [@version, @original_version, @imported_version, @trunk_version],
+                    @versioned_object.all_versions
+    assert_equal 4, @versioned_object.version_count
+    assert_equal [@version.uid, @original_version.uid, @imported_version.uid, @trunk_version.uid],
+                   @versioned_object.all_version_ids
+    assert @versioned_object.has_version_id?(@version.uid)
+    assert !@versioned_object.is_original_version?(@version.uid)
+    assert @versioned_object.is_original_version?(@original_version.uid)
+    assert @versioned_object.has_version_at_time?(DV_Date_Time.new('2009-07-06T21:10:12'))
+    assert_equal @version, @versioned_object.version_with_id(@version.uid)
+    assert_equal @version, @versioned_object.version_at_time(@version.commit_audit.time_committed)
+    assert_equal @imported_version, @versioned_object.latest_version
+    assert_equal @trunk_version, @versioned_object.latest_trunk_version
+    assert_equal 'complete', @versioned_object.trunk_lifecycle_state.value
+    assert_equal '2009-07-08T18:10:12', @versioned_object.revision_history.most_recent_version_time_committed
+    assert_nothing_raised(Exception){
+      @versioned_object.commit_original_version(:contribution => @original_version.contribution,
+                                                :uid => @original_version.uid,
+                                                :preceding_version_uid => @original_version.preceding_version_uid,
+                                                :commit_audit => @original_version.commit_audit,
+                                                :attestations => @original_version.attestations,
+                                                :lifecycle_state => @original_version.lifecycle_state,
+                                                :data => 'commit original',
+                                                :signature => 'testtett')}
+    assert_equal 5, @versioned_object.version_count
+  end
 end
