~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/datasources/drivers/appserver/appserver/Connection.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# GNU Enterprise Datasource Library - Driver for GNUe-AppServer
 
2
#
 
3
# Copyright 2000-2005 Free Software Foundation
 
4
#
 
5
# This file is part of GNU Enterprise.
 
6
#
 
7
# GNU Enterprise is free software; you can redistribute it
 
8
# and/or modify it under the terms of the GNU General Public
 
9
# License as published by the Free Software Foundation; either
 
10
# version 2, or (at your option) any later version.
 
11
#
 
12
# GNU Enterprise is distributed in the hope that it will be
 
13
# useful, but WITHOUT ANY WARRANTY; without even the implied
 
14
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 
15
# PURPOSE. See the GNU General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public
 
18
# License along with program; see the file COPYING. If not,
 
19
# write to the Free Software Foundation, Inc., 59 Temple Place
 
20
# - Suite 330, Boston, MA 02111-1307, USA.
 
21
#
 
22
# $Id: Connection.py 7017 2005-02-12 22:45:04Z reinhard $
 
23
 
 
24
####                                   ####
 
25
#### IF YOU MODIFY ANY CONNECTION      ####
 
26
#### ATTRIBUTES, PLEASE UPDATE info.py ####
 
27
####                                   ####
 
28
 
 
29
import sys
 
30
 
 
31
from gnue.common.datasources import Exceptions
 
32
from gnue.common.datasources.drivers import Base
 
33
from gnue.common.rpc import client
 
34
from gnue.common.apps import errors, GConfig
 
35
 
 
36
import DataObject
 
37
 
 
38
from gnue.common.datasources.drivers.appserver.Schema.Discovery.Introspection import Introspection
 
39
 
 
40
# =============================================================================
 
41
# Connection class
 
42
# =============================================================================
 
43
 
 
44
class Connection (Base.Connection):
 
45
  """
 
46
  Handles a connection to the GNUe-AppServer backend.
 
47
  """
 
48
  defaultBehavior = Introspection
 
49
  supportedDataObjects = {
 
50
    'object': DataObject.DataObject
 
51
  }
 
52
 
 
53
  # ---------------------------------------------------------------------------
 
54
  # Constructor
 
55
  # ---------------------------------------------------------------------------
 
56
 
 
57
  def __init__ (self, connections, name, parameters):
 
58
    Base.Connection.__init__ (self, connections, name, parameters)
 
59
    self._filters = None
 
60
    self._server  = None
 
61
    self._sm      = None
 
62
    
 
63
 
 
64
  # ---------------------------------------------------------------------------
 
65
  # Define the needed information to log in
 
66
  # ---------------------------------------------------------------------------
 
67
 
 
68
  def getLoginFields (self):
 
69
    result = []
 
70
 
 
71
    cfg = gConfigDict (section = 'appserver')
 
72
    dbauth = cfg.get ('authentication', 'False')
 
73
    if dbauth.lower () in ['true', 'yes', 'y']:
 
74
      result.extend ([['_username', _('User Name'), 0],
 
75
                      ['_password', _('Password'), 1]])
 
76
 
 
77
    self.__getSessionManager ()
 
78
    self._filters = self._sm.getFilters (self.parameters.get ('_language', 'C'))
 
79
 
 
80
    for item in self._filters:
 
81
      (filterId, filterLabel) = item [0]
 
82
      for (label, search, field) in item [1]:
 
83
        result.append ([filterId, filterLabel + ':', False])
 
84
        break
 
85
 
 
86
    return result
 
87
 
 
88
 
 
89
  # ---------------------------------------------------------------------------
 
90
  # Open a connection
 
91
  # ---------------------------------------------------------------------------
 
92
 
 
93
  def connect (self, connectData):
 
94
 
 
95
    user   = connectData.get ('_username', None)
 
96
    passwd = connectData.get ('_password', None)
 
97
 
 
98
    self.__getSessionManager ()
 
99
 
 
100
    self.__updateFilters (connectData)
 
101
 
 
102
    try:
 
103
      if connectData.has_key ('_username'):
 
104
        del connectData ['_username']
 
105
      if connectData.has_key ('_password'):
 
106
        del connectData ['_password']
 
107
      connectData ['user']     = user
 
108
      connectData ['password'] = passwd
 
109
 
 
110
      self._sess_id = self._sm.open (connectData)
 
111
 
 
112
    except errors.RemoteError, e:
 
113
      if e.getName () == 'AuthError':
 
114
        raise Exceptions.LoginError, e.getMessage ()
 
115
      else:
 
116
        raise Exceptions.ConnectionError, e.getMessage ()
 
117
      
 
118
    except gException, e:
 
119
      raise Exceptions.ConnectError, e.getMessage ()
 
120
 
 
121
    except:
 
122
      raise Exceptions.ConnectError, errors.getException () [2]
 
123
 
 
124
    # Can be removed after the call to _dataConnection.cursor() is removed from
 
125
    # the Base driver
 
126
    self.native = self
 
127
 
 
128
  # ---------------------------------------------------------------------------
 
129
  # Commit active transaction
 
130
  # ---------------------------------------------------------------------------
 
131
 
 
132
  def commit (self):
 
133
    self._sm.commit (self._sess_id)
 
134
 
 
135
  # ---------------------------------------------------------------------------
 
136
  # Rollback active transaction
 
137
  # ---------------------------------------------------------------------------
 
138
 
 
139
  def rollback (self):
 
140
    self._sm.rollback (self._sess_id)
 
141
 
 
142
  # ---------------------------------------------------------------------------
 
143
  # Close connection
 
144
  # ---------------------------------------------------------------------------
 
145
 
 
146
  def close (self):
 
147
    if self._sm is not None:
 
148
      self._sm.close (self._sess_id, False)
 
149
 
 
150
  # Can be removed after the call to _dataConnection.cursor() is removed from
 
151
  # the Base driver
 
152
  def cursor(self):
 
153
    return None
 
154
 
 
155
 
 
156
  # ---------------------------------------------------------------------------
 
157
  # Create/return a connection to the appserver
 
158
  # ---------------------------------------------------------------------------
 
159
 
 
160
  def __getSessionManager (self):
 
161
 
 
162
    if self._server is None:
 
163
      params = {'host'     : self.parameters.get ('host'),
 
164
                'port'     : self.parameters.get ('port'),
 
165
                'transport': self.parameters.get ('transport')}
 
166
      rpcType = self.parameters.get ('rpctype')
 
167
      self._server = client.attach (rpcType, params)
 
168
 
 
169
      if self.parameters.has_key ('timeout'):
 
170
        self._server.setTimeout (float (self.parameters ['timeout']))
 
171
 
 
172
    if self._sm is None:
 
173
      self._sm = self._server.request ('Session')
 
174
 
 
175
    return self._sm
 
176
 
 
177
 
 
178
  # ---------------------------------------------------------------------------
 
179
  # Update the given filter values
 
180
  # ---------------------------------------------------------------------------
 
181
 
 
182
  def __updateFilters (self, connectData):
 
183
 
 
184
    for item in self._filters:
 
185
      (filterId, filterLabel) = item [0]
 
186
 
 
187
      if connectData.has_key (filterId):
 
188
        value = connectData [filterId]
 
189
        (label, search, field) = item [1][0]
 
190
 
 
191
        # if there are no filter values we've to skip replacement. Maybe the
 
192
        # user just wants to add new filter values
 
193
        if not len (item [3].keys ()):
 
194
          continue
 
195
 
 
196
        if item [2] is None:
 
197
          masterkey = None
 
198
        else:
 
199
          masterkey = connectData [item [2]]
 
200
 
 
201
        found = False
 
202
        vDict = item [3][masterkey]
 
203
        for record in vDict:
 
204
          if record [field] == value:
 
205
            connectData [filterId] = record ['gnue_id']
 
206
            found = True
 
207
            break
 
208
 
 
209
        if not found:
 
210
          raise Exceptions.LoginError, \
 
211
              u_("'%(value)s' is not a valid filter-value for '%(filter)s'") \
 
212
              % {'value': value,
 
213
                 'filter': label}