1 |
|
# |
2 |
|
# This file is part of GNU Enterprise. |
3 |
|
# |
4 |
|
# GNU Enterprise is free software; you can redistribute it |
5 |
|
# and/or modify it under the terms of the GNU General Public |
6 |
|
# License as published by the Free Software Foundation; either |
7 |
|
# version 2, or(at your option) any later version. |
8 |
|
# |
9 |
|
# GNU Enterprise is distributed in the hope that it will be |
10 |
|
# useful, but WITHOUT ANY WARRANTY; without even the implied |
11 |
|
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
12 |
|
# PURPOSE. See the GNU General Public License for more details. |
13 |
|
# |
14 |
|
# You should have received a copy of the GNU General Public |
15 |
|
# License along with program; see the file COPYING. If not, |
16 |
|
# write to the Free Software Foundation, Inc., 59 Temple Place |
17 |
|
# - Suite 330, Boston, MA 02111-1307, USA. |
18 |
|
# |
19 |
|
# Copyright 2000-2003 Free Software Foundation |
20 |
|
# |
21 |
|
# FILE: |
22 |
|
# Introspection.py |
23 |
|
# |
24 |
|
# DESCRIPTION: |
25 |
|
# |
26 |
|
# NOTES: |
27 |
|
# |
28 |
|
|
29 |
|
__all__ = ['Introspection'] |
30 |
|
|
31 |
|
import string |
32 |
|
from string import lower, join, split |
33 |
|
import sys |
34 |
|
|
35 |
|
from gnue.common.apps import GDebug, GConfig |
36 |
|
from gnue.common.datasources import GIntrospection |
37 |
|
|
38 |
|
class Introspection(GIntrospection.Introspection): |
39 |
|
|
40 |
|
# list of the types of Schema objects this driver provides |
41 |
|
types = [('object',_('Business Object Class'),1)] |
42 |
|
|
43 |
|
# |
44 |
|
# TODO: This is a quick hack to get this class |
45 |
|
# TODO: into the new-style schema format. |
46 |
|
# TODO: getSchema* should be merged into find() |
47 |
|
# |
48 |
|
def find(self, name=None, type=None): |
49 |
|
if name is None: |
50 |
|
return self.getSchemaList(type) |
51 |
|
else: |
52 |
|
rs = self.getSchemaByName(name, type) |
53 |
|
if rs: |
54 |
|
return [rs] |
55 |
|
else: |
56 |
|
return None |
57 |
|
|
58 |
|
|
59 |
|
############# |
60 |
|
# |
61 |
|
# get list of all business classes |
62 |
|
# |
63 |
|
# TODO: Merge into find() |
64 |
|
# |
65 |
|
def getSchemaList(self, type=None): |
66 |
|
if not(type in ('object',None)): |
67 |
|
return [] |
68 |
|
|
69 |
|
try: |
70 |
|
listcursor = self._connection.request('gnue_class',[],['gnue_module'],['gnue_name','gnue_comment','gnue_module']) |
71 |
|
except Exception, msg: |
72 |
|
print "error %s" %msg |
73 |
|
GDebug.printMesg(1,_("Error creating introspection module list \n\n --- %s ---)") % msg) |
74 |
|
return [] |
75 |
|
|
76 |
|
list = [] |
77 |
|
data = ['1'] |
78 |
|
while len(data): |
79 |
|
data = listcursor.fetch() |
80 |
|
for classdef in data: |
81 |
|
print classdef |
82 |
|
schema = GIntrospection.Schema(attrs={'id':string.lower(classdef['gnue_name']), |
83 |
|
'name':classdef['gnue_name'], |
84 |
|
'type':'object', |
85 |
|
'gnue_id':classdef['gnue_id']}, |
86 |
|
getChildSchema=self.__getChildSchema) |
87 |
|
list.append(schema) |
88 |
|
|
89 |
|
|
90 |
|
listcursor.close() |
91 |
|
|
92 |
|
return list |
93 |
|
|
94 |
|
############# |
95 |
|
# |
96 |
|
# get schema for one single business class |
97 |
|
# |
98 |
|
|
99 |
|
def getSchemaByName(self, name, type=None): |
100 |
|
try: |
101 |
|
listcursor = self._connection.request('gnue_class', [["eq", ""], ["field", "gnue_name"], ["const", name]], |
102 |
|
['gnue_module'],['gnue_name','gnue_comment','gnue_module']) |
103 |
|
except Exception, msg: |
104 |
|
print "error %s" %msg |
105 |
|
GDebug.printMesg(1,_("Error fetching class %s \n\n --- %s ---)") % (name,msg)) |
106 |
|
return [] |
107 |
|
|
108 |
|
data = listcursor.fetch() |
109 |
|
if len(data): |
110 |
|
classdef = data[0] |
111 |
|
print classdef |
112 |
|
schema = GIntrospection.Schema(attrs={'id':string.lower(classdef['gnue_name']), |
113 |
|
'name':classdef['gnue_name'], |
114 |
|
'type':'object', |
115 |
|
'gnue_id':classdef['gnue_id']}, |
116 |
|
getChildSchema=self.__getChildSchema) |
117 |
|
listcursor.close() |
118 |
|
|
119 |
|
return schema |
120 |
|
|
121 |
|
############# |
122 |
|
# |
123 |
|
# get schema for one single business class |
124 |
|
# |
125 |
|
|
126 |
|
def __getChildSchema(self, parent): |
127 |
|
try: |
128 |
|
# fetch all properties used by class "parent" |
129 |
|
listcursor = self._connection.request('gnue_property', # class |
130 |
|
[["eq", ""], ["field", "gnue_class"], |
131 |
|
["const", parent.gnue_id]], # condition |
132 |
|
['gnue_module'], # sort |
133 |
|
['gnue_name','gnue_comment','gnue_module','gnue_class','gnue_type', |
134 |
|
'gnue_length','gnue_scale']) |
135 |
|
|
136 |
|
except Exception, msg: |
137 |
|
print "error %s" %msg |
138 |
|
GDebug.printMesg(1,_("Error while loading class properties for class %s \n\n --- %s ---)") % (parent.name,msg)) |
139 |
|
return [] |
140 |
|
|
141 |
|
list = [] |
142 |
|
data = ['1'] |
143 |
|
while len(data): |
144 |
|
data = listcursor.fetch() |
145 |
|
for propdef in data: |
146 |
|
print propdef |
147 |
|
attrs={'id': "%s.%s" % (parent.id, string.lower(propdef['gnue_name'])), |
148 |
|
'name': propdef['gnue_name'], |
149 |
|
'precision': propdef['gnue_scale'], # TODO: check if scale and precision is the same |
150 |
|
'datatype':propdef['gnue_type'], |
151 |
|
'nativetype': 'unknown', |
152 |
|
'required': 0 } # TODO: classrep has no 'required' field |
153 |
|
|
154 |
|
# TODO: make classrep types a bit more straigtforward |
155 |
|
if propdef['gnue_type'] =='id' or propdef['gnue_type'] =='gnue_module' \ |
156 |
|
or propdef['gnue_type'] =='gnue_class': |
157 |
|
attrs['length'] = 32 |
158 |
|
else: |
159 |
|
attrs['length'] = propdef['gnue_length'] |
160 |
|
|
161 |
|
list.append(GIntrospection.Schema(attrs=attrs)) |
162 |
|
|
163 |
|
listcursor.close() |
164 |
|
|
165 |
|
return list |
166 |
|
|