OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
142 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.sql.ui.light;
15
 
16
import org.openconcerto.sql.Configuration;
17
import org.openconcerto.sql.model.SQLField;
18
import org.openconcerto.sql.model.SQLFunctionField;
19
import org.openconcerto.sql.model.SQLFunctionField.SQLFunction;
20
import org.openconcerto.sql.model.SQLRowValues;
21
import org.openconcerto.sql.model.SQLSelect;
22
import org.openconcerto.sql.model.Where;
23
import org.openconcerto.sql.request.ListSQLRequest;
24
import org.openconcerto.sql.view.list.ITableModel;
25
import org.openconcerto.sql.view.list.SQLTableModelColumn;
26
import org.openconcerto.sql.view.list.SQLTableModelSource;
27
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
28
import org.openconcerto.ui.light.SearchSpec;
29
import org.openconcerto.utils.cc.ITransformer;
30
 
31
import java.util.ArrayList;
32
import java.util.HashSet;
33
import java.util.List;
34
import java.util.Set;
35
import java.util.concurrent.Callable;
36
import java.util.concurrent.FutureTask;
37
 
38
import javax.swing.SwingUtilities;
39
 
40
import net.minidev.json.JSONObject;
41
 
42
public class LightRowValuesTableOnline extends LightRowValuesTable {
43
    private final ITransformer<SQLSelect, SQLSelect> orginTransformer;
44
 
45
    public LightRowValuesTableOnline(final Configuration configuration, final Number userId, final String id, final ITableModel model) {
46
        super(configuration, userId, id, model);
47
 
48
        this.orginTransformer = ((SQLTableModelSourceOnline) model.getReq()).getReq().getSelectTransf();
49
    }
50
 
51
    // Clone constructor
52
    public LightRowValuesTableOnline(final LightRowValuesTableOnline tableElement) {
53
        super(tableElement);
54
 
55
        this.orginTransformer = tableElement.orginTransformer;
56
    }
57
 
58
    // Json constructor
59
    public LightRowValuesTableOnline(final JSONObject json) {
60
        super(json);
61
        this.orginTransformer = null;
62
    }
63
 
64
    @Override
65
    public void doSearch(final Configuration configuration, final SearchSpec searchSpec, final int offset) {
66
        this.getTableSpec().setSearch(searchSpec);
67
 
68
        this.setOffset(offset);
69
 
70
        final SQLTableModelSource tableSource = this.getModel().getReq();
71
        final SearchInfo sInfo = (searchSpec != null) ? new SearchInfo(searchSpec) : null;
72
 
73
        final FutureTask<ListSQLRequest> f = new FutureTask<ListSQLRequest>(new Callable<ListSQLRequest>() {
74
            @Override
75
            public ListSQLRequest call() throws Exception {
76
                final ListSQLRequest req = tableSource.getReq();
77
                final List<SQLTableModelColumn> columns = tableSource.getColumns();
78
                req.setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
79
                    @Override
80
                    public SQLSelect transformChecked(final SQLSelect sel) {
81
                        if (LightRowValuesTableOnline.this.orginTransformer != null) {
82
                            LightRowValuesTableOnline.this.orginTransformer.transformChecked(sel);
83
                        }
84
                        setWhere(sel, sInfo, columns);
85
                        return sel;
86
                    }
87
                });
88
                return req;
89
            }
90
        });
91
 
92
        // TODO: clean swing
93
        SwingUtilities.invokeLater(f);
94
 
95
        try {
96
            final ListSQLRequest req = f.get();
97
 
98
            // get values
99
            long t4 = System.currentTimeMillis();
100
            final List<SQLRowValues> rowValues = req.getValues();
101
            final int size = rowValues.size();
102
            long t5 = System.currentTimeMillis();
103
 
104
            System.err.println("DefaultTableContentHandler.handle() getValues() :" + size + " : " + (t5 - t4) + " ms");
105
        } catch (final Exception e) {
106
            throw new IllegalStateException(e);
107
        }
108
    }
109
 
110
    /**
111
     * Apply filter on ListSQLRequest
112
     *
113
     * @param sel - The ListSQLRequest select
114
     * @param sInfo - Search parameters
115
     */
116
    private final void setWhere(final SQLSelect sel, final SearchInfo sInfo, final List<SQLTableModelColumn> cols) {
117
        if (sInfo != null) {
118
            final Set<SQLField> fields = new HashSet<SQLField>();
119
            for (final SQLTableModelColumn sqlTableModelColumn : cols) {
120
                fields.addAll(sqlTableModelColumn.getFields());
121
            }
122
            final List<Where> wheres = new ArrayList<Where>();
123
            final List<Where> wFields = new ArrayList<Where>();
124
 
125
            final List<String> texts = sInfo.getTexts();
126
            for (String string : texts) {
127
                wFields.clear();
128
                for (final SQLField sqlField : fields) {
129
                    if (sqlField.getType().getJavaType().equals(String.class)) {
130
                        final Where w = new Where(new SQLFunctionField(SQLFunction.LOWER, sel.getAlias(sqlField)), "LIKE", "%" + string.toLowerCase() + "%");
131
                        wFields.add(w);
132
                    }
133
                }
134
                wheres.add(Where.or(wFields));
135
            }
136
 
137
            final Where w;
138
            if (sel.getWhere() != null) {
139
                w = Where.and(sel.getWhere(), Where.and(wheres));
140
            } else {
141
                w = Where.and(wheres);
142
            }
143
            sel.setWhere(w);
144
            System.err.println(sel.asString());
145
        }
146
    }
147
}