OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 110 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
110 ilm 1
package org.openconcerto.modules.reports.olap;
2
 
3
import java.awt.FlowLayout;
4
import java.awt.GridLayout;
5
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionListener;
7
import java.io.File;
8
import java.io.InputStream;
9
import java.sql.Connection;
10
import java.sql.DriverManager;
11
 
12
import javax.swing.JButton;
13
import javax.swing.JPanel;
14
 
15
import org.olap4j.OlapConnection;
16
import org.olap4j.metadata.Schema;
17
import org.openconcerto.erp.config.ComptaPropsConfiguration;
18
import org.openconcerto.sql.model.SQLDataSource;
19
import org.openconcerto.utils.ExceptionHandler;
20
import org.openconcerto.utils.FileUtils;
21
 
22
public class OLAPPanel extends JPanel {
23
    public OLAPPanel() {
24
        this.setOpaque(false);
25
        this.setLayout(new GridLayout(1, 1));
26
 
27
        reload();
28
 
29
    }
30
 
31
    public void reload() {
32
        this.removeAll();
33
        this.invalidate();
34
        try {
140 ilm 35
            final File dir = new File(ComptaPropsConfiguration.getConfFile(ComptaPropsConfiguration.productInfo).getParent());
110 ilm 36
            final File f = new File(dir, "openconcerto_catalog.xml");
37
            String catalog = "empty";
38
            if (f.exists()) {
39
                System.out.println("OLAP: using catalog " + f.getAbsolutePath());
40
                catalog = FileUtils.read(f, "UTF8");
41
            } else {
42
                System.out.println("OLAP: using embedded catalog (" + f.getAbsolutePath() + " missing)");
43
                final InputStream in = this.getClass().getResourceAsStream("openconcerto_catalog.xml");
44
                catalog = FileUtils.readUTF8(in);
45
            }
46
            final String url = getOlapURL(ComptaPropsConfiguration.getInstanceCompta(), catalog);
47
            final Connection rConnection = DriverManager.getConnection(url);
48
 
49
            final OlapConnection oConnection = rConnection.unwrap(OlapConnection.class);
50
 
51
            final Schema schema = oConnection.getOlapSchema();
52
            final OLAPMainPanel p = new OLAPMainPanel(oConnection, schema, this);
53
            this.add(p);
54
        } catch (Exception e) {
55
            final JPanel p = new JPanel();
56
            p.setOpaque(false);
57
            p.setLayout(new FlowLayout(FlowLayout.LEFT));
58
            final JButton comp = new JButton("Recharger la configuration");
59
            p.add(comp);
60
            this.add(p);
61
            ExceptionHandler.handle("OLAP init error", e);
62
            comp.addActionListener(new ActionListener() {
63
 
64
                @Override
65
                public void actionPerformed(ActionEvent e) {
66
                    reload();
67
                }
68
            });
69
        }
70
 
71
        this.revalidate();
72
 
73
    }
74
 
75
    private String getOlapURL(ComptaPropsConfiguration instance, String catalog) {
76
 
77
        String url = null;
78
        try {
79
            final SQLDataSource dataSource = instance.getSystemRoot().getDataSource();
80
            File file = File.createTempFile("openconcerto", ".xml");
81
            catalog = catalog.replace("_SCHEMA_", instance.getSocieteBaseName());
82
            FileUtils.write(catalog, file);
83
            url = "jdbc:mondrian:JdbcDrivers=" + dataSource.getDriverClassName() + ";Jdbc=" + dataSource.getUrl() + "?user=" + dataSource.getUsername() + "&password=" + dataSource.getPassword()
84
                    + ";Catalog=file:";
85
 
86
            url += file.getCanonicalPath().replace('\\', '/') + ";";
87
            System.out.println("OLAPPanel.getOlapURL():" + url);
88
        } catch (Exception e) {
89
            ExceptionHandler.handle("OLAP configuration issue", e);
90
        }
91
        return url;
92
    }
93
}