OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 140 | 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 {
159 ilm 23
    private String loadInfo = "";
24
 
110 ilm 25
    public OLAPPanel() {
26
        this.setOpaque(false);
27
        this.setLayout(new GridLayout(1, 1));
28
 
29
        reload();
30
 
31
    }
32
 
33
    public void reload() {
34
        this.removeAll();
35
        this.invalidate();
36
        try {
140 ilm 37
            final File dir = new File(ComptaPropsConfiguration.getConfFile(ComptaPropsConfiguration.productInfo).getParent());
110 ilm 38
            final File f = new File(dir, "openconcerto_catalog.xml");
39
            String catalog = "empty";
40
            if (f.exists()) {
41
                System.out.println("OLAP: using catalog " + f.getAbsolutePath());
42
                catalog = FileUtils.read(f, "UTF8");
159 ilm 43
                loadInfo = f.getAbsolutePath() + " ok";
110 ilm 44
            } else {
45
                System.out.println("OLAP: using embedded catalog (" + f.getAbsolutePath() + " missing)");
46
                final InputStream in = this.getClass().getResourceAsStream("openconcerto_catalog.xml");
47
                catalog = FileUtils.readUTF8(in);
159 ilm 48
                loadInfo = f.getAbsolutePath() + " missing";
110 ilm 49
            }
50
            final String url = getOlapURL(ComptaPropsConfiguration.getInstanceCompta(), catalog);
51
            final Connection rConnection = DriverManager.getConnection(url);
52
 
53
            final OlapConnection oConnection = rConnection.unwrap(OlapConnection.class);
54
 
55
            final Schema schema = oConnection.getOlapSchema();
56
            final OLAPMainPanel p = new OLAPMainPanel(oConnection, schema, this);
57
            this.add(p);
58
        } catch (Exception e) {
59
            final JPanel p = new JPanel();
60
            p.setOpaque(false);
61
            p.setLayout(new FlowLayout(FlowLayout.LEFT));
62
            final JButton comp = new JButton("Recharger la configuration");
63
            p.add(comp);
64
            this.add(p);
65
            ExceptionHandler.handle("OLAP init error", e);
66
            comp.addActionListener(new ActionListener() {
67
 
68
                @Override
69
                public void actionPerformed(ActionEvent e) {
70
                    reload();
71
                }
72
            });
73
        }
74
 
75
        this.revalidate();
76
 
77
    }
78
 
79
    private String getOlapURL(ComptaPropsConfiguration instance, String catalog) {
80
 
81
        String url = null;
82
        try {
83
            final SQLDataSource dataSource = instance.getSystemRoot().getDataSource();
84
            File file = File.createTempFile("openconcerto", ".xml");
85
            catalog = catalog.replace("_SCHEMA_", instance.getSocieteBaseName());
86
            FileUtils.write(catalog, file);
87
            url = "jdbc:mondrian:JdbcDrivers=" + dataSource.getDriverClassName() + ";Jdbc=" + dataSource.getUrl() + "?user=" + dataSource.getUsername() + "&password=" + dataSource.getPassword()
88
                    + ";Catalog=file:";
89
 
90
            url += file.getCanonicalPath().replace('\\', '/') + ";";
91
            System.out.println("OLAPPanel.getOlapURL():" + url);
92
        } catch (Exception e) {
93
            ExceptionHandler.handle("OLAP configuration issue", e);
94
        }
95
        return url;
96
    }
159 ilm 97
 
98
    public String getLoadInfo() {
99
        return this.loadInfo;
100
    }
110 ilm 101
}