OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 140 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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