commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

gnue-appserver/src geasList.py geasSession.py


From: Reinhard Mueller
Subject: gnue-appserver/src geasList.py geasSession.py
Date: Mon, 01 Sep 2003 16:11:37 -0400

CVSROOT:        /cvsroot/gnue
Module name:    gnue-appserver
Branch:         
Changes by:     Reinhard Mueller <address@hidden>       03/09/01 16:11:37

Modified files:
        src            : geasList.py geasSession.py 

Log message:
        More code cleanup. Quite satisfied with the code for now.

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/gnue-appserver/src/geasList.py.diff?tr1=1.28&tr2=1.29&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/gnue-appserver/src/geasSession.py.diff?tr1=1.35&tr2=1.36&r1=text&r2=text

Patches:
Index: gnue-appserver/src/geasList.py
diff -c gnue-appserver/src/geasList.py:1.28 gnue-appserver/src/geasList.py:1.29
*** gnue-appserver/src/geasList.py:1.28 Mon Sep  1 14:24:15 2003
--- gnue-appserver/src/geasList.py      Mon Sep  1 16:11:37 2003
***************
*** 19,25 ****
  # write to the Free Software Foundation, Inc., 59 Temple Place
  # - Suite 330, Boston, MA 02111-1307, USA.
  #
! # $Id: geasList.py,v 1.28 2003/09/01 18:24:15 reinhard Exp $
  
  from gnue.common.datasources import GDataSource, GConditions
  import geasInstance
--- 19,25 ----
  # write to the Free Software Foundation, Inc., 59 Temple Place
  # - Suite 330, Boston, MA 02111-1307, USA.
  #
! # $Id: geasList.py,v 1.29 2003/09/01 20:11:37 reinhard Exp $
  
  from gnue.common.datasources import GDataSource, GConditions
  import geasInstance
***************
*** 35,84 ****
    # Initalize
    # 
---------------------------------------------------------------------------
  
!   def __init__ (self, session, classdef):
      self._session = session
      self._classdef = classdef
!     self._prefetch = []         # property names to be prefetched
!     self._conditions = []       # conditions using property names
!     self._sort = []             # property names to be used as sort key
!     self._prefColumns = []      # (db) column names to be prefetched
!     self._sortColumns = []      # (db) column names to be used as sort key
  
!   # 
---------------------------------------------------------------------------
!   # Actually these 3 only set variables
!   # 
---------------------------------------------------------------------------
  
-   def setPrefetch (self, prefetch):
-     self._prefetch = prefetch
      # test if all properties in prefetch list are valid and find column names
-     self._prefColumns = []
      for propertyname in self._prefetch:
        propertydef = self._classdef.properties [propertyname]
        self._prefColumns.append (propertydef.column)
  
-   # conditions = a list consisting of condition elements written down in
-   # prefix notation i.e. (a=12) AND (c=121) -> AND = a 12 = c 121
-   # every element of the list is a tuple containing the following parts:
-   # type (eq,and,const...)  value  i.e. ('eq','') or ('const',12)
- 
-   def setConditions (self, conditions):
-     self._conditions = conditions
-     # TODO: translate property names to column names
-     # now building GCondition tree
-     self._conditionTree = GConditions.buildTreeFromPrefix (conditions)
- 
-   def setSort (self, sort):
-     self._sort = sort
      # test if all properties in sort list are valid and find column names
      for propertyname in self._sort:
        propertydef = self._classdef.properties [propertyname]
        self._sortColumns.append (propertydef.column)
  
!   # 
---------------------------------------------------------------------------
!   # Populate the list with data from the database backend
!   # 
---------------------------------------------------------------------------
! 
!   def populate (self):
      attributes = {}
      attributes ["name"]     = ""
      attributes ["database"] = self._session.database
--- 35,60 ----
    # Initalize
    # 
---------------------------------------------------------------------------
  
!   def __init__ (self, session, classdef, propertylist, sortorder):
      self._session = session
      self._classdef = classdef
!     self._prefetch = propertylist       # property names to be prefetched
!     self._sort = sortorder              # property names of the sort key
  
!     self._prefColumns = []              # (db) column names to be prefetched
!     self._sortColumns = []              # (db) column names of the sort key
  
      # test if all properties in prefetch list are valid and find column names
      for propertyname in self._prefetch:
        propertydef = self._classdef.properties [propertyname]
        self._prefColumns.append (propertydef.column)
  
      # test if all properties in sort list are valid and find column names
      for propertyname in self._sort:
        propertydef = self._classdef.properties [propertyname]
        self._sortColumns.append (propertydef.column)
  
!     # prepare attributes of the datasource
      attributes = {}
      attributes ["name"]     = ""
      attributes ["database"] = self._session.database
***************
*** 86,97 ****
      if self._sortColumns != []:
        attributes ["order_by"] = string.joinfields (self._sortColumns, ",")
  
      self._datasource = GDataSource.DataSourceWrapper (
        connections = self._session.connections,
        attributes = attributes,
        fields = self._prefColumns)
  
!     self._resultset = self._datasource.createResultSet (self._conditionTree)
  
    # 
---------------------------------------------------------------------------
    # Get the first instance in the list
--- 62,106 ----
      if self._sortColumns != []:
        attributes ["order_by"] = string.joinfields (self._sortColumns, ",")
  
+     # create the datasource
      self._datasource = GDataSource.DataSourceWrapper (
        connections = self._session.connections,
        attributes = attributes,
        fields = self._prefColumns)
  
!   # 
---------------------------------------------------------------------------
!   # Populate the list with data from the database backend
!   # 
---------------------------------------------------------------------------
!   # conditions = a list consisting of condition elements written down in
!   # prefix notation i.e. (a=12) AND (c=121) -> AND = a 12 = c 121
!   # every element of the list is a tuple containing the following parts:
!   # type (eq,and,const...)  value  i.e. ('eq','') or ('const',12)
!   # 
---------------------------------------------------------------------------
! 
!   def populate (self, conditions):
!     # TODO: translate property names to column names in conditions
!     # now building GCondition tree
!     conditionTree = GConditions.buildTreeFromPrefix (conditions)
!     self._resultset = self._datasource.createResultSet (conditionTree)
! 
!   # 
---------------------------------------------------------------------------
!   # Populate the list with a single instance with given gnue_id, and return
!   # that instance
!   # 
---------------------------------------------------------------------------
! 
!   def find (self, object_id):
!     self.populate ([['eq', ''], ['field', 'gnue_id'], ['const', object_id]])
!     return self.firstInstance ()
! 
!   # 
---------------------------------------------------------------------------
!   # Populate the list with a single empty instance, and return that instance
!   # 
---------------------------------------------------------------------------
! 
!   def newInstance (self):
!     self._resultset = self._datasource.createEmptyResultSet ()
!     self._resultset.insertRecord ()
!     return geasInstance.geasInstance (self, self._resultset.current,
!                                       self._classdef)
  
    # 
---------------------------------------------------------------------------
    # Get the first instance in the list
***************
*** 116,122 ****
        return None
  
    # 
---------------------------------------------------------------------------
!   # get the length of the list (the number of entries)
    # 
---------------------------------------------------------------------------
  
    def count (self):
--- 125,131 ----
        return None
  
    # 
---------------------------------------------------------------------------
!   # Get the length of the list (the number of entries)
    # 
---------------------------------------------------------------------------
  
    def count (self):
***************
*** 139,150 ****
        c += 1
        instance = self.nextInstance ()
      return result
- 
-   # 
---------------------------------------------------------------------------
-   # insert a new empty instance in the list and return it
-   # 
---------------------------------------------------------------------------
- 
-   def insertNewInstance (self):
-     self._resultset.insertRecord ()
-     return geasInstance.geasInstance (self, self._resultset.current,
-                                       self._classdef)
--- 148,150 ----
Index: gnue-appserver/src/geasSession.py
diff -c gnue-appserver/src/geasSession.py:1.35 
gnue-appserver/src/geasSession.py:1.36
*** gnue-appserver/src/geasSession.py:1.35      Mon Sep  1 14:24:15 2003
--- gnue-appserver/src/geasSession.py   Mon Sep  1 16:11:37 2003
***************
*** 19,25 ****
  # write to the Free Software Foundation, Inc., 59 Temple Place
  # - Suite 330, Boston, MA 02111-1307, USA.
  #
! # $Id: geasSession.py,v 1.35 2003/09/01 18:24:15 reinhard Exp $
  
  import geasList
  import classrep
--- 19,25 ----
  # write to the Free Software Foundation, Inc., 59 Temple Place
  # - Suite 330, Boston, MA 02111-1307, USA.
  #
! # $Id: geasSession.py,v 1.36 2003/09/01 20:11:37 reinhard Exp $
  
  import geasList
  import classrep
***************
*** 62,67 ****
--- 62,71 ----
      self._listcount = 0
      self._authAdapter = authAdapter
  
+     # TODO: gnue-common should have a concept of sessions. Until that is done,
+     # we have to keep a list of geasList objects that need to be committed.
+     self._dirtyLists = []
+ 
    # 
---------------------------------------------------------------------------
    # Get class definition from class name
    # 
---------------------------------------------------------------------------
***************
*** 93,136 ****
    # Create a new list of business objects of a given class
    # 
---------------------------------------------------------------------------
  
!   def createList (self, classname):
! 
!     classdef = self._getClassdef (classname)
  
!     # check if user has access rights for this list
      if not self._authAdapter.hasAccess (self, self._user, classname):
!       return None
  
      # create new List
!     newlist = geasList.geasList (self, classdef)
!     # every new list will be added to the _lists list
!     # which will be parsed for commit and rollback actions
!     self._listcount += 1
!     self._lists [self._listcount] = newlist
!     return newlist;
  
    # 
---------------------------------------------------------------------------
    # Commit the active transaction
    # 
---------------------------------------------------------------------------
  
    def commit (self):
!     # FIXME: gnue-common should support the concept of a session and commits
!     #        on session level
!     for l in self._lists.keys ():
!       if hasattr (self._lists[l], "_datasource"):
!         self._lists[l]._resultset.post ()
!         self._lists[l]._datasource.commit ()
  
    # 
---------------------------------------------------------------------------
    # Rollback the active transaction
    # 
---------------------------------------------------------------------------
  
    def rollback (self):
!     # FIXME: gnue-common should support the concept of a session and rollbacks
!     #        on session level
!     for l in self._lists.keys ():
!       if hasattr (self._lists[l], "_datasource"):
!         self._lists[l]._datasource.rollback ()
  
    # 
---------------------------------------------------------------------------
    # Create a new list of business objects of a given class
--- 97,135 ----
    # Create a new list of business objects of a given class
    # 
---------------------------------------------------------------------------
  
!   def _createList (self, classname, propertylist, sortorder):
  
!     # check if user has access rights for this class
      if not self._authAdapter.hasAccess (self, self._user, classname):
!       raise Exception, "Access to class %s denied" % classname
! 
!     classdef = self._getClassdef (classname)
  
      # create new List
!     return geasList.geasList (self, classdef, propertylist, sortorder)
  
    # 
---------------------------------------------------------------------------
    # Commit the active transaction
    # 
---------------------------------------------------------------------------
  
    def commit (self):
!     # TODO: gnue-common should support the concept of a session and commits
!     # on session level
!     for l in self._dirtyLists:
!       if hasattr (l, "_datasource"):
!         l._resultset.post ()
!         l._datasource.commit ()
  
    # 
---------------------------------------------------------------------------
    # Rollback the active transaction
    # 
---------------------------------------------------------------------------
  
    def rollback (self):
!     # TODO: gnue-common should support the concept of a session and rollbacks
!     # on session level
!     for l in self._dirtyLists:
!       if hasattr (l, "_datasource"):
!         l._datasource.rollback ()
  
    # 
---------------------------------------------------------------------------
    # Create a new list of business objects of a given class
***************
*** 138,153 ****
  
    def request (self, classname, conditions, sortorder, propertylist):
      # FIXME: this list needn't be considered by commit and rollback
!     list = self.createList (classname)
!     if list == None:
!       return 0
! 
      list_id = self._listcount
-     list.setPrefetch (["gnue_id"] + propertylist)
-     list.setConditions (conditions)
-     list.setSort (sortorder)
-     list.populate ()
- 
      return list_id;
  
    # 
---------------------------------------------------------------------------
--- 137,147 ----
  
    def request (self, classname, conditions, sortorder, propertylist):
      # FIXME: this list needn't be considered by commit and rollback
!     list = self._createList (classname, ["gnue_id"] + propertylist, sortorder)
!     list.populate (conditions)
!     self._listcount += 1
!     self._lists [self._listcount] = list
      list_id = self._listcount
      return list_id;
  
    # 
---------------------------------------------------------------------------
***************
*** 177,195 ****
    # 
---------------------------------------------------------------------------
  
    def load (self, classname, obj_id_list, propertylist):
!     # create a temporary geasList
!     classdef = self._getClassdef (classname)
!     list = geasList.geasList (self, classdef)
!     list.setPrefetch (["gnue_id"] + propertylist)
!     list.setSort (["gnue_id"])
!     # Accessing the database for every single object_id is not very elegant,
!     # but for now it works. -- Reinhard
      result = []
      for object_id in obj_id_list:
!       list.setConditions ([['eq', ''], ['field', 'gnue_id'],
!                            ['const', object_id]])
!       list.populate ()
!       instance = list.firstInstance ()
        result.append (instance.get (propertylist))
      return result
  
--- 171,180 ----
    # 
---------------------------------------------------------------------------
  
    def load (self, classname, obj_id_list, propertylist):
!     list = self._createList (self, classname, ["gnue_id"] + propertylist, [])
      result = []
      for object_id in obj_id_list:
!       instance = list.find (object_id)
        result.append (instance.get (propertylist))
      return result
  
***************
*** 201,226 ****
      result = []
      i = 0
      for object_id in obj_id_list:
!       # We have to create a geasList for each object, because commit only
!       # operates on lists. We should change that. -- Reinhard
!       list = self.createList (classname)
!       # We need to "reference" all properties so they become updated. This
!       # also should be changed. -- Reinhard
!       list.setPrefetch (["gnue_id"] + propertylist)
!       list.setSort (["gnue_id"])
!       # Even for an empty object_id, we need to popluate the list.
!       list.setConditions ([['eq', ''], ['field', 'gnue_id'],
!                            ['const', object_id]])
!       list.populate ()
        if object_id:
!         instance = list.firstInstance ()
          result.append (object_id)
        else:
!         instance = list.insertNewInstance ()
          newid = new_object_id ()
          instance.put (["gnue_id"], [newid])
          result.append (newid)
        instance.put (propertylist, data [i])
        i += 1
      return result
  
--- 186,206 ----
      result = []
      i = 0
      for object_id in obj_id_list:
!       # TODO: gnue-common should provide a method for updating a record 
without
!       # reading it first. Until that is done, we have to create a temporary
!       # list for every record we update and keep it in our list of pending
!       # commits.
!       list = self._createList (classname, ["gnue_id"] + propertylist, [])
        if object_id:
!         instance = list.find (object_id)
          result.append (object_id)
        else:
!         instance = list.newInstance ()
          newid = new_object_id ()
          instance.put (["gnue_id"], [newid])
          result.append (newid)
        instance.put (propertylist, data [i])
+       self._dirtyLists.append (list)
        i += 1
      return result
  
***************
*** 230,262 ****
  
    def delete (self, classname, obj_id_list):
      for object_id in obj_id_list:
!       # create a temporary geasList
!       list = self.createList (classname)
!       list.setPrefetch (["gnue_id"])
!       list.setSort (["gnue_id"])
!       list.setConditions ([['eq', ''], ['field', 'gnue_id'],
!                            ['const', object_id]])
!       list.populate ()
!       object = list.firstInstance ()
!       object.delete ()
  
    # 
---------------------------------------------------------------------------
    # Call a procedure of business objects
    # 
---------------------------------------------------------------------------
  
    def call (self, classname, obj_id_list, procedurename, parameters):
!     # create a temporary geasList
!     classdef = self._getClassdef (classname)
!     list = geasList.geasList (self, classdef)
!     list.setPrefetch (["gnue_id"]) # + propertylist) TODO: fetch propertylist
!     list.setSort (["gnue_id"])
!     # Accessing the database for every single object_id is not very elegant,
!     # but for now it works. -- Reinhard
      result = []
      for object_id in obj_id_list:
!       list.setConditions ([['eq', ''], ['field', 'gnue_id'],
!                            ['const', object_id]])
!       list.populate ()
!       object = list.firstInstance ()
!       result.append (object.call (procedurename, parameters))
      return result
--- 210,232 ----
  
    def delete (self, classname, obj_id_list):
      for object_id in obj_id_list:
!       # TODO: gnue-common should provide a method to delete a record without
!       # reading it first. Until that is done, we have to create a temporary
!       # list for every record we update and keep it in our list of pending
!       # commits.
!       list = self._createList (classname, ["gnue_id"], [])
!       instance = list.find (object_id)
!       instance.delete ()
!       self._dirtyLists.append (list)
  
    # 
---------------------------------------------------------------------------
    # Call a procedure of business objects
    # 
---------------------------------------------------------------------------
  
    def call (self, classname, obj_id_list, procedurename, parameters):
!     list = self._createList (classname, ["gnue_id"], [])
      result = []
      for object_id in obj_id_list:
!       instance = list.find (object_id)
!       result.append (instance.call (procedurename, parameters))
      return result




reply via email to

[Prev in Thread] Current Thread [Next in Thread]