1 # -----------------------------------------------------------------------
2 # Copyright (C) 2007 Georgia Public Library Service
3 # Bill Erickson <billserickson@gmail.com>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 # -----------------------------------------------------------------------
16 from osrf.log import *
17 from osrf.json import *
18 from oils.utils.idl import oilsGetIDLParser
19 from osrf.ses import osrfClientSession
20 from oils.const import *
23 ACTIONS = ['create', 'retrieve', 'batch_retrieve', 'update', 'delete', 'search']
25 class CSEditor(object):
26 def __init__(self, **args):
28 self.app = args.get('app', OILS_APP_CSTORE)
29 self.authtoken = args.get('authtoken', args.get('auth'))
30 self.requestor = args.get('requestor')
31 self.connect = args.get('connect')
32 self.xact = args.get('xact')
41 # -------------------------------------------------------------------------
42 # Creates a session if one does not already exist. If necessary, connects
43 # to the remote service and starts a transaction
44 # -------------------------------------------------------------------------
45 def session(self, ses=None):
46 if not self.__session:
47 self.__session = osrfClientSession(self.app)
49 if self.connect or self.xact:
50 self.log(osrfLogDebug,'connecting to ' + self.app)
51 self.__session.connect()
54 self.log(osrfLogInfo, "starting new db transaction")
55 self.request(self.app + '.transaction.begin')
60 # -------------------------------------------------------------------------
61 # Logs string with some meta info
62 # -------------------------------------------------------------------------
63 def log(self, func, string):
65 if self.xact: s += "1|"
67 if self.requestor: s += str(self.requestor.id())
70 func("%s %s" % (s, string))
73 # -------------------------------------------------------------------------
74 # Rolls back the existing db transaction
75 # -------------------------------------------------------------------------
77 if self.__session and self.xact:
78 self.log(osrfLogInfo, "rolling back db transaction")
79 self.request(self.app + '.transaction.rollback')
82 # -------------------------------------------------------------------------
83 # Commits the existing db transaction
84 # -------------------------------------------------------------------------
86 if self.__session and self.xact:
87 self.log(osrfLogInfo, "comitting db transaction")
88 self.request(self.app + '.transaction.commit')
92 # -------------------------------------------------------------------------
93 # Disconnects from the remote service
94 # -------------------------------------------------------------------------
97 self.__session.disconnect()
101 # -------------------------------------------------------------------------
103 # -------------------------------------------------------------------------
104 def request(self, method, params=[]):
106 # XXX improve param logging here
108 self.log(osrfLogInfo, "request %s %s" % (method, str(params)))
110 if self.xact and self.session().state != OSRF_APP_SESSION_CONNECTED:
111 self.log(osrfLogErr, "csedit lost its connection!")
116 req = self.session().request2(method, params)
121 self.log(osrfLogErr, "request error: %s" % str(e))
127 # -------------------------------------------------------------------------
128 # Returns true if our requestor is allowed to perform the request action
129 # 'org' defaults to the requestors ws_ou
130 # -------------------------------------------------------------------------
131 def allowed(self, perm, org=None):
135 def runMethod(self, action, type, arg, options={}):
137 method = "%s.direct.%s.%s" % (self.app, type, action)
139 if options.get('idlist'):
140 method = method.replace('search', 'id_list')
141 del options['idlist']
143 if action == 'search':
144 method = method.replace('$', '.atomic')
146 if action == 'batch_retrieve':
147 method = method.replace('batch_retrieve', 'search')
152 if len(options.keys()):
153 params.append(options)
155 val = self.request( method, params )
159 def rawSearch(self, args):
160 method = "%s.json_query.atomic" % self.app
161 self.log(osrfLogDebug, "rawSearch args: %s" % str(args))
162 return self.request(method, [args])
164 def rawSearch2(self, hint, fields, where, from_=None):
166 from_ = {'%s' % hint : {}}
169 'select' : { '%s' % hint : fields },
171 'where' : { "+%s" % hint : where }
173 return self.rawSearch(args)
176 def fieldSearch(self, hint, fields, where):
177 return self.rawSearch2(hint, fields, where)
181 # -------------------------------------------------------------------------
182 # Creates a class method for each action on each type of fieldmapper object
183 # -------------------------------------------------------------------------
184 def oilsLoadCSEditor():
185 obj = oilsGetIDLParser().IDLObject
187 for k, fm in obj.iteritems():
188 for action in ACTIONS:
190 fmname = fm['fieldmapper'].replace('::', '_')
191 type = fm['fieldmapper'].replace('::', '.')
192 name = "%s_%s" % (action, fmname)
194 str = 'def %s(self, arg, **options):\n' % name
195 str += '\treturn self.runMethod("%s", "%s", arg, dict(options))\n' % (action, type)
196 str += 'setattr(CSEditor, "%s", %s)' % (name, name)