[Bio] / CatalogServices / CatalogFactoryMixin.py Repository:
ViewVC logotype

View of /CatalogServices/CatalogFactoryMixin.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (download) (as text) (annotate)
Fri Jan 7 14:40:32 2005 UTC (15 years, 5 months ago) by efrank
Branch: MAIN
CVS Tags: post-st-migration2, post-st-migration, before-dynamics-persistence, lwc, treatman-lastDay-0, preTreatmanDyn, HEAD
Changes since 1.2: +4 -4 lines
migration to new schematools generated classes.
new chemdb.
factor chem out of seed data factory
imporved configuration of modeleditor


from FaST.CatalogEntryBss              import CatalogEntryBss
from FoundryInfo.FoundryItemKey          import FoundryItemKey


class CatalogFactoryMixin:
    """
    The Catalog aspects of the Kah architecture are likely to be shared
    by different kinds of data factories: looking up catalog entries by
    path, getting children, etc., is the same regardless of what kind of
    data they lead the user to.

    This class, CatalogFactoryMixin, handles the catalog specific services.
    It is not a factory itself, but is intended to be a mix-in class
    for other data factories that just want to get these standard
    capabilities.
    """
    


    #----------------------------------------------------------
    def __init__(self, txManager):
    #----------------------------------------------------------
        """
        @type txManager: FaST.TransactionManager

        txManager is that of the class mixing in this CatalogFactoryMixin.
        """

        self.__txMgr = txManager

        return
    
    #----------------------------------------------------------
    def getCatalogTop(self):
    #----------------------------------------------------------
        """
        Return a CatalogEnrtry correspoinding to the Top of the
        hierarchy.
        """

        #This is hardwired to be CatDirEntId 0

        return self.getCatalogEntryFromId( 0 )

    #----------------------------------------------------------
    def getCatalogEntryFromId(self, theCatDirEntId):
    #----------------------------------------------------------
        """
        Return a CatalogEnrtry correspoinding to the provided id.
        """

        query = "SELECT FullPathName, CatName, CatDirEntId, isTerminal" \
                "   FROM KahCatDirEntry"\
                 "  WHERE CatDirEntId=%d" % (theCatDirEntId) 
    
        c    = self.__txMgr.getCursor()
        c.execute(query)
        rows = c.fetchall() 
        
        if (c.rowcount == 1):
           row= rows[0]
           #                             path      name     dirEntId  isTerminal
           return CatalogEntryBss( self, row[0],  row[1], int(row[2]),    int(row[3]) )
        else:
           raise Exception, "Catalog Entry not found in Bss"
                

    #----------------------------------------------------------
    def getCatalogEntryChildren(self, aCatDirEntId):
    #----------------------------------------------------------
        """
        Given a CatDirEntId identifying a catalog entry,
        return a list of CatalogEntry's tha are direct children.
        """

        r=[]

        query = "SELECT k.ChildCatDirEntId,  p.CatName, p.isTerminal" \
                "   FROM KahCatSubDir k, KahCatDirEntry p"\
                 "  WHERE k.ParentCatDirentId = '%d'"\
                 "  AND   k.ChildCatDirEntId=p.CatDirEntId" % (aCatDirEntId)

    
        c    = self.__txMgr.getCursor()
        c.execute(query)
        rows = c.fetchall() 
        
        if (c.rowcount > 0):
           for row in rows:
               #         name     dirEntId  isTerminal
               r.append( (row[1],  row[0],    row[2] ))
                
        return r

    #----------------------------------------------------------
    def listVersions(self, aCatDirEntId):
    #----------------------------------------------------------
        """
        Given a CatDirEntId identifying a catalog entry,
        return a list of versions for that entry
        """

        query="select VerNo from KahCatDirEntVersions" \
               "   WHERE CatDirEntId=%d" % (aCatDirEntId)
        
        c    = self.__txMgr.getCursor()
        c.execute(query)
        rows = c.fetchall() 
        
        r=[]
        for row in rows:
            r.append( int( row[0] ))
                
        return r


    #-------------------------------------------------------------
    def getCatalogEntryByPath( self, path ):
    #-------------------------------------------------------------
        """
        Given a path, return the associated catalogEntry.
        """

        query = "SELECT CatDirEntId " \
                "FROM KahCatDirEntry " \
                "WHERE FullPathName='%s'" % (path)
        
        c    = self.__txMgr.getCursor()
        c.execute(query)
        rows = c.fetchone()
        
        if (c.rowcount ==1):
            catdirId = rows[0]
            return self.getCatalogEntryFromId(int(catdirId) )
        else:
            return None
       

    #-------------------------------------------------------------
    def getFoundryItemByPath( self, path , version=None):
    #-------------------------------------------------------------
        """
        Given a path, determine the associated catalogEntry
        and then return the FoundryItemKey associated with the
        version.
        """
        
        query = "SELECT CatDirEntId " \
                "FROM KahCatDirEntry " \
                "WHERE FullPathName='%s'" % ( path )
        
        
        c    = self.__txMgr.getCursor()
        c.execute(query)
        rows = c.fetchone()
        
        if (c.rowcount ==1):
            catdirId = rows[0]
            return self.getFoundryItem( int(catdirId), version)
        else:
            return None
       
        
    #-------------------------------------------------------------
    def getFoundryItem( self, catDirEntId, version=None):
    #-------------------------------------------------------------
        """
        Given a CatalogEntryId and version, return the FoundryItemKey
        associated with it (top of model).
          *****> handle version=None case!!!
        """
       
        #  If no version provided, then get the highest version number
        if (version==None):
            vQuery = "SELECT MAX(verno) "\
                     "FROM kahcatdirentversions "\
                     "WHERE CATDIRENTID = %d" % (catDirEntId)
            c    = self.__txMgr.getCursor()
            c.execute(vQuery)
            rows = c.fetchone()
            version = rows[0]
            if (None == version):
                return None

        query = "SELECT a.KeyIntVal, a.KeyStrVal, a.FoundryId "\
                "FROM KahNavCat2ObjMod a " \
                "WHERE a.CatDirEntId = %d AND a.verno = %d" %( int(catDirEntId), 
                                                               int(version))
        c    = self.__txMgr.getCursor()
        c.execute(query)
        rows = c.fetchone()
        if (c.rowcount ==1):
            return FoundryItemKey(self.getFoundryId(), [int(rows[0])], [rows[1]] )
        elif (c.rowcount ==0):
            return None
        else:
            raise Exception, "Database integrety error"
           
    #----------------------------------------------------------
    def setFoundryItem( self, aCatDirEntId, theItemKey):
    #----------------------------------------------------------
        """
        Associate a new FoundryItemKey (theItemKey) with a CatalogEntry
        (id is aCatDirEntId) by increasing the version number and binding
        the two together
        """

        ##
        # First, bump up the NextVer- the next version, thus claiming
        # the current one for our use
        ##
        
        # Note that there's an issue of atomicity that makes us do
        # this as a single sql statement
        
        #"Set NextVer=1 "\
        query="UPDATE KahCatDirEntry " \
              "SET NextVer=NextVer+1 " \
              "WHERE catdirentid=%d" % (aCatDirEntId)
        c    = self.__txMgr.getCursor()
        c.execute(query)

        query="select NextVer from KahCatDirEntry" \
               "   WHERE CatDirEntId=%d" % (aCatDirEntId)
        c.execute(query)
        rows = c.fetchall() 

        theVer = int(rows[0][0]) - 1

        ##
        # Now Register the new version
        ##
        query="INSERT INTO KahCatDirEntVersions " \
              "VALUES ( %d, %d, 0 )"  % (aCatDirEntId, theVer)
        c.execute(query)
        
        ##
        # And link it to the FoundryItem represented by
        # the key
        ##
        if (theItemKey.nStringValList() == 0 ):
            strVal=""
        else:
            strVal=theItemKey.getStringValList(0)
            

        if (theItemKey.nIntValList() == 0 ):
            intVal=0
        else:
            intVal=theItemKey.getIntValList(0)
            
        
        query = "INSERT INTO  KahNavCat2ObjMod " \
                "VALUES (%d, %d, '%s', %d, '%s')" % (aCatDirEntId, theVer,
                                               theItemKey.getFoundryId().getIdVal(),
                                               intVal, strVal)
        c    = self.__txMgr.getCursor()
        c.execute(query)

        self.__txMgr.commit()
        return

    #----------------------------------------------------------
    def newCatalogEntry(self, parentEntry, name, isTerminal):
    #----------------------------------------------------------
        """
        Create a new row in the database corresponding to a new 
        catalogEntry.  Returns an instance of CatalogEntryBss.
        parentEntry - instance of CatalogEntryBss.  New entry will be child.
        newName     - name for new CatalogEntry. string
        newIsTerminal- isTerminal value (0 or 1).  See doc in CatalogEntry.
        """

        fullPathName = parentEntry.path() + "/" + name

        #
        # make sure that "name" isn't already used: this needs to move up
        # into a template-pattern at the top level CatalogServices so that
        # all derivatives obey it.  or, it could be a trigger, but that's just
        # another form of burying.
        #

        currentKidNames=map(lambda x: x.name(), parentEntry.entries())
        if name in currentKidNames:
            raise Exception, "%s already a child of %s." % (name, parentEntry.name() )


        #
        # Get a CatDirEntryId for the new row by bumping up a sequence
        #
        try:
            catEntId = self.__txMgr.nextSeqVal( 'KahCatDirEntIdSequence' )
        except:
            raise Exception, "Failed to increment KahCatDirEntIdSequence"
                  

        #
        # Using that id, create the new CatDirEntry row
        #
        query = "INSERT INTO KahCatDirEntry "\
                " VALUES('%s', %d, %d, %d, '%s')" % (name, catEntId, isTerminal, 0, fullPathName )
    
        try:
            c    = self.__txMgr.getCursor()
            c.execute(query)
        except:
            raise Exception, "Failed to create KarCatDirEntry row with: " + query

        #
        # Associate the new entry with its parent.
        #

        query = "INSERT INTO KahCatSubDir "\
                " VALUES(%d, %d)" % (parentEntry.getCatDirEntId(), catEntId)

        try:
            c    = self.__txMgr.getCursor()
            c.execute(query)
        except:
            raise Exception, "Failed to create KahCatSubDir row."


        #
        # Make a CatDirEntry to represent this and return it.
        
        newEntry = CatalogEntryBss(self, fullPathName, name, catEntId, isTerminal)

        #
        # All is good, so commit
        #   DANGER!!! This should be out of our hands.  We may be
        #   part way through some larger task that is not complete.
        #   Need transaction manager here and equiv of mini transactions.
 
        self.__txMgr.commit()
        
        return newEntry

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3