Changeset 297
- Timestamp:
- 01/30/08 16:49:59 (10 months ago)
- Files:
-
- trunk/src/dm/db.py (modified) (4 diffs)
- trunk/src/dm/dom/meta.py (modified) (5 diffs)
- trunk/src/dm/dom/registry.py (modified) (1 diff)
- trunk/src/dm/dom/temporaltest.py (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/dm/db.py
r296 r297 743 743 logger.debug(message) 744 744 self.domainObject.id = self.id 745 recordedTime = timepoint.recorded746 745 for metaAttr in self.meta.attributes: 747 746 dbName = metaAttr.dbName … … 752 751 ) 753 752 logger.debug(message) 754 if metaAttr.dom.isTemporal: 753 if domName == 'parent' and metaAttr.isDomainObjectRef: 754 # Continue if it's already loaded.... 755 if self.parent and self.parent.domainObject: 756 mapperValue = self.parent.domainObject 757 self.domainObject.parent = mapperValue 758 message = "Continuing with loaded parent: %s" % ( 759 self.domainObject.parent 760 ) 761 logger.debug(message) 762 continue 763 if metaAttr.dom.isTemporal or metaAttr.dom.isBitemporalActual: 755 764 r = metaAttr.dom.createTemporalCollection(self.domainObject) 765 if metaAttr.dom.isBitemporalActual: 766 r.sortOnName = 'dateCreated' 767 referenceTime = timepoint.actual 768 mostRecents = r.findDomainObjects( 769 __loadedList__=loadedList, 770 __dateCreatedOnOrBefore__=referenceTime 771 ) 772 mostRecent = r.findFirstDomainObject( 773 __loadedList__=loadedList, 774 __dateCreatedOnOrBefore__=referenceTime 775 ) 776 #print "LOAD: Bitemporal actual %s at %s" % ([i for i in mostRecents], referenceTime) 777 if mostRecent: 778 print "!!!!!! Bitemporal actuals for recorded time: %s" % timepoint.recorded 779 print "" 780 print [i for i in mostRecents] 781 print "" 782 print "!!!!!! Bitemporal latest for actual time: %s" % timepoint.actual 783 print "" 784 print mostRecent 785 print "" 786 print "!!!!!!" 787 788 elif metaAttr.dom.isBitemporal: 789 referenceTime = timepoint.recorded 790 #print "LOAD: Bitemporal recorded at %s " % referenceTime 791 else: 792 referenceTime = timepoint.recorded 793 #print "LOAD: Temporal recorded at %s" % referenceTime 756 794 mostRecent = r.findFirstDomainObject( 757 795 __loadedList__=loadedList, 758 __dateCreatedOnOrBefore__=re cordedTime796 __dateCreatedOnOrBefore__=referenceTime 759 797 ) 760 798 if mostRecent: 761 799 mappedValue = mostRecent.recordedValue 762 800 else: 801 #print "LOAD: No record, creating initial value..." 763 802 mappedValue = metaAttr.dom.createInitialValue( 764 803 self.domainObject 765 804 ) 805 #print "LOAD: Temporal '%s' value: %s" % (metaAttr.dom.name, mappedValue) 766 806 elif metaAttr.isDomainObjectRef: 767 807 mapper = getattr(self, dbName) … … 798 838 isChanged = False 799 839 for metaAttr in self.meta.attributes: 800 if metaAttr.dom.isTemporal :840 if metaAttr.dom.isTemporal or metaAttr.dom.isBitemporalActual: 801 841 domValue = getattr(self.domainObject, metaAttr.domName) 802 842 r = metaAttr.dom.createTemporalCollection(self.domainObject) … … 804 844 loadedList[self.domainObject] = self.domainObject 805 845 mostRecent = r.findFirstDomainObject(__loadedList__=loadedList) 806 #print "SAVE: Dom value (%s)" % (domValue) 846 print "SAVE: Attribute '%s' value: %s" % (metaAttr.dom.name, domValue) 847 if (r.ownerName != 'parent') or (r.owner != self.domainObject): 848 msg = "r: %s %s %s" % (r, r.ownerName, r.owner) 849 raise Exception, msg 807 850 if mostRecent != None: 808 #print "SAVE: Most recent (%s)" % (mostRecent.recordedValue) 809 if domValue != mostRecent.recordedValue: 810 #print "SAVE: Stale version record. Creating new..." 811 r.create(recordedValue=domValue, 812 __loadedList__=loadedList) 851 print "SAVE: Recorded value: %s" % mostRecent.recordedValue 852 if metaAttr.dom.isBitemporal or (domValue != mostRecent.recordedValue): 853 if metaAttr.dom.isBitemporal: 854 print "SAVE: Creating new bitemporal history." 855 newRecorded = r.create(__loadedList__=loadedList) 856 #newRecorded.parent = self.domainObject 857 #newRecorded.save() 858 print "SAVE: %s" % newRecorded 859 actualsMeta = mostRecent.meta.attributeNames['recordedValue'] 860 newActuals = actualsMeta.createTemporalCollection(newRecorded) 861 print "SAVE: Copying old bitemporal actual records...." 862 oldActuals = actualsMeta.createTemporalCollection(mostRecent) 863 #msg = "newActuals: %s %s %s" % (newActuals, newActuals.ownerName, newActuals.owner) 864 #raise Exception, msg 865 866 for oldActual in oldActuals: 867 print "SAVE: Copying old bitemporal actual: %s" % oldActual.recordedValue 868 newActual = newActuals.create( 869 recordedValue=oldActual.recordedValue, 870 dateCreated=oldActual.dateCreated, 871 __loadedList__=loadedList 872 ) 873 print "SAVE: %s" % newActual 874 print "SAVE: Setting new bitemporal actual.." 875 newRecorded.recordedValue = domValue 876 newRecorded.save() 877 elif metaAttr.dom.isBitemporalActual: 878 print "SAVE: Creating new bitemporal actual: %s" % domValue 879 newActual = r.create( 880 recordedValue=domValue, 881 dateCreated=timepoint.actual, 882 __loadedList__=loadedList 883 ) 884 print "SAVE: %s" % newActual 885 else: 886 print "SAVE: Creating new temporal: %s" % domValue 887 r.create( 888 recordedValue=domValue, 889 __loadedList__=loadedList 890 ) 813 891 else: 814 p ass # print "SAVE: Fresh version record."892 print "SAVE: Fresh version record. No change needed." 815 893 else: 816 #print "SAVE: No version record. Creating new..." 817 r.create(recordedValue=domValue,__loadedList__=loadedList) 894 print "SAVE: No version record. Creating first." 895 #r.ownerName = 'parent' 896 #r.owner = self.domainObject 897 if metaAttr.dom.isBitemporal: 898 first = r.create( 899 __loadedList__=loadedList 900 ) 901 first.recordedValue = domValue 902 #first.parent = self.domainObject 903 #first.save() 904 print "SAVE: Created first bitemporal recorded record: %s" % first 905 906 elif metaAttr.dom.isBitemporalActual: 907 first = r.create( 908 recordedValue=domValue, 909 dateCreated=timepoint.actual, 910 __loadedList__=loadedList 911 ) 912 print "SAVE: Created first bitemporal actual record: %s" % first 913 else: 914 first = r.create( 915 recordedValue=domValue, 916 __loadedList__=loadedList 917 ) 918 print "SAVE: Created first temporal record: %s" % first 919 818 920 elif metaAttr.isDomainObjectRef: 819 921 domainObject = getattr(self.domainObject, metaAttr.domName) trunk/src/dm/dom/meta.py
r296 r297 94 94 def createDomainClass(self, baseClass): 95 95 className = self.name 96 classAttrs = {'meta': self} 96 classAttrs = { 97 'meta': self, 98 'isUnique': self.isUnique, 99 'isCached': self.isCached, 100 } 97 101 return self.createClass(className, baseClass, classAttrs) 98 102 … … 110 114 raise Exception(msg) 111 115 return self.attributeNames[attrName] 112 113 114 class TemporalCollection(object):115 116 def __init__(self, register):117 self.register = register118 119 def __get__(self, obj, type=None):120 print "Got __get__()"121 122 def __set__(self, obj, value):123 print "Got __set__(%s)" % value124 116 125 117 … … 156 148 if self.isBitemporal: 157 149 self.isTemporal = True 158 self.isBitemporal = False # Cut this out for the moment.150 self.isBitemporalActual = False 159 151 self.temporalDomainClass = None 160 152 … … 184 176 def setTemporalDomainClass(self, domainClass): 185 177 self.temporalDomainClass = domainClass 186 self.temporalDomainClass.registerKeyName = 'dateCreated' 178 self.temporalDomainClass.registerKeyName = 'id' 179 self.temporalDomainClass.isUnique = False 187 180 self.temporalDomainClass.isCached = True 188 self.temporalDomainClass.sortOnName = 'id' 181 if self.isBitemporalActual: 182 self.temporalDomainClass.sortOnName = 'dateCreated' 183 else: 184 self.temporalDomainClass.sortOnName = 'id' 189 185 self.temporalDomainClass.sortAscending = False 190 186 191 187 def createTemporalCollection(self, domainObject): 188 if not domainObject: 189 raise Exception, "Need a domain object! %s" % domainObject 192 190 register = self.temporalDomainClass.createRegister() 191 register.owner = domainObject 193 192 register.ownerName = 'parent' 194 register.owner = domainObject195 193 return register 196 194 … … 265 263 attrMeta.isTemporal = True 266 264 attrMeta.isBitemporal = False 265 attrMeta.isBitemporalActual = True 267 266 else: 268 267 attrMeta.isTemporal = False 268 attrMeta.isRequired = self.isRequired 269 269 return attrMeta 270 270 trunk/src/dm/dom/registry.py
r296 r297 84 84 for name in classRegister: 85 85 self.checkHasAsForHasManys(classRegister[name]) 86 self.createTemporalAttributePersistence(domainClass.meta)87 88 def createTemporalAttributePersistence(self, domainClassMeta):89 86 temporalAttrs = [] 90 for a in domainClass Meta.attributes:87 for a in domainClass.meta.attributes: 91 88 if a.isTemporal: 92 new = self.createTemporalDomainClass(domainClassMeta, a) 89 temporalMeta = domainClass.meta 90 if a.isBitemporal: 91 new = self.createBitemporalDomainClass(temporalMeta, a) 92 else: 93 new = self.createTemporalDomainClass(temporalMeta, a) 93 94 a.setTemporalDomainClass(new) 94 95 96 def createBitemporalDomainClass(self, classMeta, attrMeta): 97 temporalClassName = self.createTemporalDomainClassName(classMeta.name, attrMeta.name) 98 temporalMetaObject = DomainObject.metaClass(temporalClassName) 99 temporalMetaObject.recordedValue = attrMeta.duplicateTemporal() 100 temporalMetaObject.parent = HasA(classMeta.name, isRequired=True) 101 temporalMetaObject.dateCreated = DateTime(isIndexed=True, 102 isRequired=True, default=mx.DateTime.now) 103 temporalMetaObject.isUnique = False 104 temporalMetaObject.isCached = True 105 return self.createDomainClass(temporalMetaObject) 106 95 107 def createTemporalDomainClass(self, classMeta, attrMeta): 96 temporalClassName = classMeta.name + '_t_' + attrMeta.name97 metaTemporalObject = DomainObject.metaClass(temporalClassName)98 metaTemporalObject.recordedValue = attrMeta.duplicateTemporal()99 metaTemporalObject.parent = HasA(classMeta.name, isRequired=True)100 metaTemporalObject.dateCreated = DateTime(isIndexed=True,108 temporalClassName = self.createTemporalDomainClassName(classMeta.name, attrMeta.name) 109 temporalMetaObject = DomainObject.metaClass(temporalClassName) 110 temporalMetaObject.recordedValue = attrMeta.duplicateTemporal() 111 temporalMetaObject.parent = HasA(classMeta.name, isRequired=True) 112 temporalMetaObject.dateCreated = DateTime(isIndexed=True, 101 113 isRequired=True, default=mx.DateTime.now) 102 return self.createDomainClass(metaTemporalObject) 114 temporalMetaObject.isUnique = False 115 temporalMetaObject.isCached = True 116 return self.createDomainClass(temporalMetaObject) 117 118 def createTemporalDomainClassName(self, className, attrName): 119 return className + '_t_' + attrName 103 120 104 121 def setMetaAttributesFromClass(self, domainClass, domainClassMeta): trunk/src/dm/dom/temporaltest.py
r296 r297 7 7 import mx.DateTime 8 8 from time import sleep 9 from dm.dom.temporal import TemporalProperty, BitemporalProperty 10 from dm.dom.temporal import TemporalCollection, BitemporalCollection 9 11 10 12 def suite(): 11 13 suites = [ 12 unittest.makeSuite(TestTemporal), 14 unittest.makeSuite(TestTemporal), # Example 15 unittest.makeSuite(TestTemporalProperty), 16 unittest.makeSuite(TestBitemporalProperty), 13 17 ] 14 18 return unittest.TestSuite(suites) … … 17 21 # Todo: Separate out the testing of indexes from testing of temporal attribute. 18 22 19 class Temporal(SimpleNamedObject): 23 class Temporal(SimpleNamedObject): # Example 20 24 "Temporally attributed domain object." 21 25 … … 24 28 description = String(default='', isTemporal=True) 25 29 firstkiss = DateTime(isTemporal=True, default=mx.DateTime.now()) 26 state = HasA('State', isTemporal=True, isRequired=None) 27 haircolor = String(default='', isBitemporal=True) 28 29 30 class TestTemporal(TestCase): 30 state = HasA('State', isTemporal=True, isRequired=False) 31 haircolor = String(default='My Natural Color', isBitemporal=True) 32 33 34 class TestTemporalProperty(TestCase): 35 36 def setUp(self): 37 super(TestTemporalProperty, self).setUp() 38 self.collection = TemporalProperty.createRegister() 39 40 def tearDown(self): 41 self.collection = None 42 43 def test_collection(self): 44 self.failUnlessEqual(type(self.collection), TemporalCollection) 45 46 47 class TestBitemporalProperty(TestCase): 48 49 def setUp(self): 50 super(TestBitemporalProperty, self).setUp() 51 self.collection = BitemporalProperty.createRegister() 52 53 def tearDown(self): 54 self.collection = None 55 56 def test_collection(self): 57 self.failUnlessEqual(type(self.collection), BitemporalCollection) 58 59 60 61 62 class TestTemporal(TestCase): # Example 31 63 "TestCase for the Temporal class." 32 64 … … 127 159 temporal.haircolor = haircolor2 128 160 temporal.save() 161 print "Sleeping now!" 162 import time 163 time.sleep(5) 129 164 # - check present is most recent 130 165 self.timepoint.reset() … … 160 195 self.failUnlessEqual(temporal.haircolor, haircolor3) 161 196 197 #raise Exception, str(temporal) 198 162 199 # Make changes to the actual time. 163 164 165 return166 167 168 200 self.timepoint.reset() 169 201 temporal = self.temporals[self.fixtureName] … … 177 209 self.timepoint.reset() 178 210 temporal = self.temporals[self.fixtureName] 211 print "Sleeping... %s" % temporal.haircolor 212 sleep(5) 179 213 self.failUnlessEqual(temporal.haircolor, haircolor3) 180 214 self.timepoint.actual = yearsAgo
