~zaber/openobject-addons/stable_5.0-extra-addons

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################
import wizard
import pooler
import csv
import os
import time
import netsvc
import zipfile


form1 = '''<?xml version="1.0"?>
<form string="New Module Name">
    <field name='module_name'/>
</form>'''

field1 = {
    'module_name': {'string':'Module Name', 'type':'char','size':'64', 'required':True},
}


class wizard_olap_extract(wizard.interface):
    
    def zipper(self,zipname,filename,mode="a"):
        z = zipfile.ZipFile(zipname+".zip", mode)
        z.write(filename)
        print "\n",z.printdir()
 
    
    def write_csv(self,filename,content=[]):
        fid = open(filename, 'w')        
        if fid == -1:
            print 'File: temp.csv not found or could not be opened'
            return False
        else:
            writer=csv.writer(fid, 'TINY',delimiter=',')
            for row in content:
                writer.writerow(row)
            fid.close    
        return True
    
    def table_depth(self,cr,uid,list=[],context={}):

        res={}
        templeft={}
        tempright={}
        for table_id in list:
            lines=pooler.get_pool(cr.dbname).get('olap.cube.table').browse(cr, uid,table_id,context)            
            res[str(lines.id)] = ["cubetable_"+str(lines.id),lines.name and lines.name or '',lines.table_alias and lines.table_alias or '',lines.key_left and lines.key_left or '',lines.key_right and lines.key_right or '',lines.table_left_id.id and "cubetable_"+str(lines.table_left_id.id) or '',lines.table_right_id.id and "cubetable_"+str(lines.table_right_id.id) or '']
            if(lines.table_left_id.id):
                templeft = self.table_depth(cr,uid,[lines.table_left_id.id],context)
        
                if templeft:
                    for temp in templeft:
                        if not res.has_key(temp):
                            res[temp]= templeft[temp]


            if(lines.table_right_id.id):
                tempright = self.table_depth(cr,uid,[lines.table_right_id.id],context) 
        
                if tempright:
                    for temp in tempright:
                        if not res.has_key(temp):
                            res[temp]= tempright[temp]
        return res
        
    
    def _extract_schema(self, cr, uid, data, context={}):

        _name = data['form']['module_name']
        _modulename = "olap_"+_name
        
        dirname = _modulename
        if not os.path.isdir("./addons/" + dirname + "/"):
            os.mkdir("./addons/" + dirname + "/")        
        
        os.chdir("./addons/"+dirname)

        _init = "import %s" % _modulename
        f = open("__init__.py","w")
        f.write(_init)
        f.close()
        self.zipper(_modulename, "__init__.py","w")
        
        _init_files = {
                       "1":"olap.fact.database.csv",
                       "2":"olap.schema.csv",
                       "5":"olap.cube.table.csv",
                       "6":"olap.cube.csv",
                       "7":"olap.dimension.csv",
                       "8":"olap.hierarchy.csv",
                       "9":"olap.level.csv",
                       "10":"olap.measure.csv"                    
                       }        
        _init_xml = """"%(1)s",
                    "%(2)s",
                    "%(5)s",
                    "%(6)s",
                    "%(7)s",
                    "%(8)s",
                    "%(9)s",
                    "%(10)s",
        """ % _init_files
        
        data['modulename'] = _modulename
        data['init_xml'] = _init_xml

        _terp= """{
                "name" : "%(modulename)s",
                "version" : "0.1",
                "author" : "Tiny",
                "website" : "http://tinyerp.com/",
                "depends" : ["olap"],
                "category" : "Generic Modules/Others",
                "description": "Module will load the data in olap tables",
                "init_xml" :[
                    %(init_xml)s
                    ],
                "update_xml" : [],
                "demo_xml" : [],
                "active": False,
                "installable": True
            }"""%data

        f = open("__terp__.py","w")
        f.write(_terp)
        f.close()        
        self.zipper(_modulename, "__terp__.py")  
                  

        schema_id = data['id']

        lines=pooler.get_pool(cr.dbname).get('olap.schema').browse(cr, uid, schema_id,context)
        
        _extract=[]
        _extract.append(['id','name','db_name','db_login','db_password'])
        _extract.append(["db_"+str(lines.database_id.id),lines.database_id.name,lines.database_id.db_name,lines.database_id.db_login,lines.database_id.db_password])
        self.write_csv('olap.fact.database.csv',_extract)
        self.zipper(_modulename, "olap.fact.database.csv")
        
        _extract=[]
        _extract.append(['id','name','database_id:id','loaded','configure','ready','state','note'])
        _extract.append(["schema_"+str(lines.id),lines.name,"db_"+str(lines.database_id.id),lines.loaded and lines.loaded or '',lines.configure and lines.configure or '', lines.ready and lines.ready or '',lines.state,lines.note and lines.note or ''])
        self.write_csv('olap.schema.csv',_extract)
        self.zipper(_modulename, "olap.schema.csv")                
        
        cube_ids = lines.cube_ids
        _extractcubes=[]
        _extractcubes.append(['id','name','table_id:id','schema_id:id'])
        
        _extractmeasures=[]
        _extractmeasures.append(['id','name','cube_id:id','value_column','value_sql','agregator','datatype','formatstring'])        
        
        _extractdimension=[]
        _extractdimension.append(['id','name','foreign_key','foreign_key_table','cube_id:id'])
        
        _extracthiers=[]
        _extracthiers.append(['id','name','primary_key','primary_key_table','field_name','member_all','member_default','sequence','type','dimension_id:id','table_id:id'])

        _extractlevels=[]
        _extractlevels.append(['id','name','column_name','column_id_name','type','table_name','sequence','hierarchy_id:id'])

        _cube_table_ids = []
        for cube in cube_ids:
            _extractcubes.append(["cube_"+str(cube.id),cube.name,"cubetable_"+str(cube.table_id.id),"schema_"+str(cube.schema_id.id)])    
            _cube_table_ids.append(cube.table_id.id)   
            
            measure_ids = cube.measure_ids
            for measure in measure_ids:
                _extractmeasures.append(["msr_"+str(measure.id),measure.name,"cube_"+str(measure.cube_id.id),measure.value_column and measure.value_column or '',measure.value_sql and measure.value_sql or '',measure.agregator,measure.datatype and measure.datatype or '',measure.formatstring])        
            
            dimension_ids = cube.dimension_ids
            for dimension in dimension_ids:
                _extractdimension.append(["dim_"+str(dimension.id),dimension.name,dimension.foreign_key and dimension.foreign_key or '',dimension.foreign_key_table and dimension.foreign_key_table or '',"cube_"+str(dimension.cube_id.id)])
                
                hiers_ids = dimension.hierarchy_ids
                for hier in hiers_ids:
                    _extracthiers.append(["hier_"+str(hier.id),hier.name,hier.primary_key,hier.primary_key_table and hier.primary_key_table or '',hier.field_name,hier.member_all,hier.member_default,hier.sequence,hier.type,"dim_"+str(hier.dimension_id.id),"cubetable_"+str(hier.table_id.id)])
                    _cube_table_ids.append(hier.table_id.id)
                    
                    level_ids = hier.level_ids
                    for level in level_ids:
                        _extractlevels.append(["lvl_"+str(level.id),level.name,level.column_name,level.column_id_name,level.type,level.table_name,level.sequence,"hier_"+str(level.hierarchy_id.id)])
                        
        res={}
        _extract=[]
        _extract.append(['id','name','table_alias','key_left','key_right','table_left_id:id','table_right_id:id'])
        res = self.table_depth(cr,uid,_cube_table_ids,context)
        key = res.keys()
        key = map(lambda x: int(x),key)
        key.sort()
        
        for k in key:
            _extract.append(res[str(k)])
        
        self.write_csv('olap.cube.csv',_extractcubes)
        self.zipper(_modulename, "olap.cube.csv")
        
        self.write_csv('olap.measure.csv',_extractmeasures)
        self.zipper(_modulename, "olap.measure.csv")   
        
        self.write_csv('olap.dimension.csv',_extractdimension)
        self.zipper(_modulename, "olap.dimension.csv")
        
        self.write_csv('olap.hierarchy.csv',_extracthiers)
        self.zipper(_modulename, "olap.hierarchy.csv")      
        
        self.write_csv('olap.level.csv',_extractlevels)
        self.zipper(_modulename, "olap.level.csv")                                    

        self.write_csv('olap.cube.table.csv',_extract)
        self.zipper(_modulename, "olap.cube.table.csv")      

        return {}
        
    states = {
              
       'init': {
            'actions': [],
            'result': {'type':'form','arch':form1, 'fields':field1, 'state':[('end','Cancel'),('ok','OK')]}
        },              
              
        'ok' : {
            'actions' : [],
            'result' : {'type' : 'action' ,'action':_extract_schema,'state':'end'}
        }
      }

wizard_olap_extract('olap.extract.schema')