OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 110 | 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.GridBagConstraints;
4
import java.awt.GridBagLayout;
5
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionListener;
7
import java.util.ArrayList;
8
import java.util.List;
9
 
10
import javax.swing.JButton;
11
import javax.swing.JCheckBox;
12
import javax.swing.JLabel;
13
import javax.swing.JPanel;
14
import javax.swing.SwingConstants;
15
 
16
import org.olap4j.OlapConnection;
17
import org.olap4j.metadata.Cube;
18
import org.olap4j.metadata.MetadataElement;
19
import org.openconcerto.ui.DefaultGridBagConstraints;
20
 
21
public class OLAPConfigurationPanel extends JPanel {
22
    private OLAPParametersPanel parameters;
23
    private ParameterHolder filters;
24
    private ParameterHolder rows;
25
    private ParameterHolder colums;
26
    private final JCheckBox checkHideEmpty;
27
    private OLAPMDXPanel mdx;
28
 
29
    OLAPConfigurationPanel(OLAPParametersPanel parameters, OlapConnection olapConnection, OLAPRenderer renderer, OLAPMDXPanel mdx) {
30
        this.setOpaque(false);
31
        this.parameters = parameters;
32
 
33
        this.mdx = mdx;
34
        this.setLayout(new GridBagLayout());
35
        GridBagConstraints c = new DefaultGridBagConstraints();
36
        c.fill = GridBagConstraints.BOTH;
37
        // Colonnes
38
        c.gridx = 0;
39
        c.gridy = 0;
40
        c.weightx = 0;
41
 
42
        JLabel columnsLabel = new JLabel("Colonnes", SwingConstants.RIGHT);
43
        this.add(columnsLabel, c);
44
        colums = new ParameterHolder(true);
45
        c.gridwidth = 3;
46
        c.gridx++;
47
        c.weightx = 1;
48
        this.add(colums, c);
49
 
50
        // Lignes
51
        c.gridx = 0;
52
        c.gridy++;
53
        c.weightx = 0;
54
        c.gridwidth = 1;
55
        JLabel rowsLabel = new JLabel("Lignes", SwingConstants.RIGHT);
56
        this.add(rowsLabel, c);
57
        rows = new ParameterHolder(true);
58
        c.gridx++;
59
        c.weightx = 1;
60
        c.gridwidth = 3;
61
        this.add(rows, c);
62
        // Filtre
63
        c.gridx = 0;
64
        c.gridy++;
65
        c.weightx = 0;
66
        c.gridwidth = 1;
159 ilm 67
        JLabel filtersLabel = new JLabel("Mesure", SwingConstants.RIGHT);
110 ilm 68
        this.add(filtersLabel, c);
69
 
70
        filters = new ParameterHolder(false);
71
        filters.setLimit(1);
72
        c.gridwidth = 3;
73
        c.gridx++;
74
        c.weightx = 0.5;
75
        this.add(filters, c);
76
        filters.addListener(this);
77
        colums.addListener(this);
78
        rows.addListener(this);
79
        //
80
        c.gridy++;
81
        c.gridwidth = 1;
82
        c.weightx = 0;
83
        c.fill = GridBagConstraints.NONE;
84
        final JButton inverseButton = new JButton("Inverser lignes et colonnes");
85
        inverseButton.setOpaque(false);
86
        this.add(inverseButton, c);
87
        c.gridx++;
88
 
89
        final JButton resetButton = new JButton("Réinitialiser");
90
        resetButton.setOpaque(false);
91
        this.add(resetButton, c);
92
        c.gridx++;
93
 
94
        checkHideEmpty = new JCheckBox("masquer les lignes et colonnes vides");
95
        checkHideEmpty.setOpaque(false);
96
        checkHideEmpty.setSelected(true);
97
        this.add(checkHideEmpty, c);
98
        checkHideEmpty.addActionListener(new ActionListener() {
99
 
100
            @Override
101
            public void actionPerformed(ActionEvent e) {
102
                configurationModified();
103
 
104
            }
105
        });
106
 
107
        inverseButton.addActionListener(new ActionListener() {
108
 
109
            @Override
110
            public void actionPerformed(ActionEvent e) {
111
                List<MetadataElement> rElements = new ArrayList<MetadataElement>();
112
                rElements.addAll(rows.getElements());
113
 
114
                List<MetadataElement> cElements = new ArrayList<MetadataElement>();
115
                cElements.addAll(colums.getElements());
116
 
117
                rows.clearElements();
118
                rows.addAll(cElements);
119
 
120
                colums.clearElements();
121
                colums.addAll(rElements);
122
            }
123
        });
124
        resetButton.addActionListener(new ActionListener() {
125
 
126
            @Override
127
            public void actionPerformed(ActionEvent e) {
128
                rows.clearElements();
129
                colums.clearElements();
130
                filters.clearElements();
131
                configurationModified();
132
            }
133
        });
134
    }
135
 
136
    public void add(MetadataElement element) {
137
        rows.add(element);
138
    }
139
 
140
    void configurationModified() {
141
 
142
        String query = "SELECT \n";
143
        try {
144
 
145
            Cube cube = parameters.getCube();
146
 
147
            List<MetadataElement> eColumns = this.colums.getElements();
148
 
149
            if (this.checkHideEmpty.isSelected()) {
150
                query += "NON EMPTY ";
151
            }
152
            query += "{Hierarchize({";
153
            for (int i = 0; i < eColumns.size(); i++) {
154
                MetadataElement metadataElement = eColumns.get(i);
155
                if (ParameterHolder.isDimension(metadataElement)) {
156
                    query += "{" + metadataElement.getUniqueName() + ".Members}";
157
                } else {
158
                    query += "{" + metadataElement.getUniqueName() + "}";
159
                }
160
                if (i < eColumns.size() - 1) {
161
                    query += ", ";
162
                }
163
            }
164
            query += "})} ON COLUMNS, \n";
165
            if (this.checkHideEmpty.isSelected()) {
166
                query += "NON EMPTY ";
167
            }
168
            query += "{Hierarchize({";
169
 
170
            List<MetadataElement> eRows = this.rows.getElements();
171
            for (int i = 0; i < eRows.size(); i++) {
172
                MetadataElement metadataElement = eRows.get(i);
173
                if (ParameterHolder.isDimension(metadataElement)) {
174
                    query += "{" + metadataElement.getUniqueName() + ".Members}";
175
                } else {
176
                    query += "{" + metadataElement.getUniqueName() + "}";
177
                }
178
                if (i < eRows.size() - 1) {
179
                    query += ", ";
180
                }
181
            }
182
            query += "})} ON ROWS ";
183
 
184
            query += "\nFROM " + cube.getUniqueName();
185
            if (filters.getElements().size() > 0) {
186
                query += " \nWHERE " + filters.getElements().get(0).getUniqueName();
187
            }
188
            System.out.println(query);
189
 
190
            // myQuery.getAxis(Axis.ROWS).addDimension(dimension)
191
 
192
            // myQuery.getAxis(Axis.FILTER).addDimension(timeDim);
193
            //
194
            // Member year1997 = salesCube.lookupMember(IdentifierNode.ofNames("Time",
195
            // "1997").getSegmentList());
196
            // timeDim.include(year1997);
197
            // productDim.include(Selection.Operator.CHILDREN, IdentifierNode.ofNames("Product",
198
            // "Drink", "Beverages").getSegmentList());
199
            //
200
            // productDim.exclude(IdentifierNode.ofNames("Product", "Drink", "Beverages",
201
            // "Carbonated Beverages").getSegmentList());
202
 
203
            // MdxParserFactory pFactory = olapConnection.getParserFactory();
204
            // MdxParser parser = pFactory.createMdxParser(olapConnection);
205
            // SelectNode parsedObject = parser.parseSelect(query);
206
            // MdxValidator validator = pFactory.createMdxValidator(olapConnection);
207
            // try {
208
            // validator.validateSelect(parsedObject);
209
            // } catch (OlapException e) {
210
            // System.out.println(e.getMessage());
211
            // }
212
 
213
            // myQuery.validate();
214
            // System.out.println(myQuery.getSelect().toString());
215
 
216
            // CellSet results = myQuery.execute();
217
 
218
            mdx.execute(query);
219
 
220
        } catch (Exception e) {
221
            e.printStackTrace();
222
        }
223
    }
224
}