Changeset 4057

Show
Ignore:
Timestamp:
05/02/08 07:21:25 (3 months ago)
Author:
ed
Message:

This commit contains many changes related to the way that child bizobjs sync up with their parent. There were many places in the code where it was assumed that the link was via the parent's PK; while that is true in the majority of cases, child bizobjs can be linked via any field, not just the PK. I added a new method named 'getParentLinkValue()' that will return the value of the field in the parent record that defines the link to the child.

Please test this with all your code. It is working for all of my apps and the unit tests, but I don't have anything that links a child by anything other than the parent PK.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/dabo/biz/dBizobj.py

    r4055 r4057  
    848848        """ 
    849849        if self.DataSource and self.LinkField and self.Parent: 
    850             if self.Parent.IsAdding or self.Parent.RowCount == 0: 
     850            if self.Parent.RowCount == 0: 
    851851                # Parent is new and not yet saved, so we cannot have child records yet. 
    852852                self.setWhereClause("") 
    853853                filtExpr = " 1 = 0 " 
    854854            else: 
    855                 if self.ParentLinkField: 
    856                     # The link to the parent is something other than the PK 
    857                     val = self.escQuote(self.Parent.getFieldVal(self.ParentLinkField)) 
    858                 else: 
    859                     val = self.escQuote(self.getParentPK()) 
     855                val = self.escQuote(self.getParentLinkValue()) 
    860856                linkFieldParts = self.LinkField.split(".") 
    861857                if len(linkFieldParts) < 2: 
     
    868864                filtExpr = " %s.%s = %s " % (dataSource, linkField, val) 
    869865            self._CurrentCursor.setChildFilterClause(filtExpr) 
     866 
     867 
     868    def getParentLinkValue(self): 
     869        """Return the value of the parent record on which this bizobj is dependent. Usually this 
     870        is the PK of the parent, but can be a non-PK field, if this bizobj's ParentLinkField is 
     871        not empty. 
     872        """ 
     873        ret = None 
     874        if self.Parent: 
     875            fld = self.ParentLinkField 
     876            if not fld: 
     877                fld = self.Parent.KeyField 
     878            try: 
     879                ret = self.Parent.getFieldVal(fld) 
     880            except dException.NoRecordsException: 
     881                ret = NO_RECORDS_PK 
     882        return ret 
    870883 
    871884 
     
    971984        self._CurrentCursor.moveToRowNum(rownum) 
    972985        if updateChildren: 
    973             pk = self.getPK() 
    974986            for child in self.__children: 
    975                 # Let the child know the current dependent PK 
    976                 child.setCurrentParent(pk
     987                # Let the child update to the current record. 
     988                child.setCurrentParent(
    977989 
    978990 
     
    986998            if updateChildren: 
    987999                for child in self.__children: 
    988                     # Let the child know the current dependent PK 
    989                     child.setCurrentParent(pk
     1000                    # Let the child update to the current record. 
     1001                    child.setCurrentParent(
    9901002 
    9911003 
     
    10221034 
    10231035 
    1024     def isAnyChanged(self, parentPK=None): 
     1036    def isAnyChanged(self, useCurrentParent=None): 
    10251037        """Returns True if any record in the current record set has been changed.""" 
    1026         if parentPK is None: 
     1038        if useCurrentParent is None: 
    10271039            try: 
    10281040                cc = self._CurrentCursor 
     
    10301042                cc = None 
    10311043        else: 
    1032             cc = self.__cursors.get(parentPK, None) 
     1044            key = self.getParentLinkValue() 
     1045            cc = self.__cursors.get(key, None) 
    10331046        if cc is None: 
    10341047            # No cursor, no changes. 
     
    10391052 
    10401053        # Nothing's changed in the top level, so we need to recurse the children: 
    1041         try: 
    1042             pk = self.getPK() 
    1043         except dException.NoRecordsException: 
    1044             # If there are no records, there can be no changes 
    1045             return False 
    1046  
    10471054        for child in self.__children: 
    1048             if child.isAnyChanged(parentPK=pk): 
     1055            if child.isAnyChanged(useCurrentParent=useCurrentParent): 
    10491056                return True 
    10501057        # If we made it to here, there are no changes. 
     
    10691076        if not ret: 
    10701077            # see if any child bizobjs have changed 
    1071             try: 
    1072                 pk = self.getPK() 
    1073             except dException.NoRecordsException: 
     1078            if not self.RowCount: 
    10741079                # If there are no records, there can be no changes 
    10751080                return False 
    10761081            for child in self.__children: 
    1077                 ret = child.isAnyChanged(parentPK=pk
     1082                ret = child.isAnyChanged(useCurrentParent=True
    10781083                if ret: 
    10791084                    break 
     
    11491154        if self.LinkField: 
    11501155            if val is None: 
    1151                 val = self.getParentPK() 
     1156                val = self.getParentLinkValue() 
    11521157            self.scan(self._setParentFK, val) 
    11531158 
     
    11561161 
    11571162 
    1158     def setCurrentParent(self, val=None, fromChildRequery=None): 
    1159         """ Lets dependent child bizobjs know the current value of their parent 
     1163    def setCurrentParent(self, val=None): 
     1164        """ Lets dependent child bizobjs update to the current parent 
    11601165        record. 
    11611166        """ 
    11621167        if self.LinkField: 
    1163             if val is None and not fromChildRequery
    1164                 val = self.getParentPK() 
     1168            if val is None
     1169                val = self.getParentLinkValue() 
    11651170            # Update the key value for the cursor 
    11661171            self.__currentCursorKey = val 
     
    12641269            raise dException.BusinessRuleViolation, errMsg 
    12651270 
    1266         newAutopop = (self.IsAdding and self.AutoPopulatePK) 
    1267         try: 
    1268             pk = self.getPK() 
    1269         except dException.NoRecordsException: 
    1270             # There aren't any records, all children should requery to 0 records. 
    1271             # We can't set the pk to None, because None has special meaning  
    1272             # elsewhere (self.__currentCursorKey). 
    1273             pk = NO_RECORDS_PK 
    1274  
    12751271        for child in self.__children: 
    12761272            # Let the child know the current dependent PK 
    12771273            if child.RequeryWithParent: 
    1278                 child.setCurrentParent(pk, fromChildRequery=True) 
    1279                 if newAutopop and (child.RowCount == 0): 
    1280                     parentPK = None 
    1281                 else: 
    1282                     parentPK = pk 
    1283                 if not child.isAnyChanged(parentPK=parentPK): 
     1274                child.setCurrentParent() 
     1275                if not child.isAnyChanged(useCurrentParent=True): 
    12841276                    child.requery() 
    12851277        self.afterChildRequery()