OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | Rev 174 | Go to most recent revision | Details | Compare with Previous | 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;
151 ilm 17
import org.openconcerto.sql.model.IFieldPath;
142 ilm 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 {
156 ilm 43
    private ITransformer<SQLSelect, SQLSelect> orginTransformer;
142 ilm 44
 
156 ilm 45
    public LightRowValuesTableOnline() {
46
        // Serialization
47
    }
48
 
142 ilm 49
    public LightRowValuesTableOnline(final Configuration configuration, final Number userId, final String id, final ITableModel model) {
50
        super(configuration, userId, id, model);
51
 
52
        this.orginTransformer = ((SQLTableModelSourceOnline) model.getReq()).getReq().getSelectTransf();
53
    }
54
 
55
    // Clone constructor
56
    public LightRowValuesTableOnline(final LightRowValuesTableOnline tableElement) {
57
        super(tableElement);
58
 
59
        this.orginTransformer = tableElement.orginTransformer;
60
    }
61
 
62
    // Json constructor
63
    public LightRowValuesTableOnline(final JSONObject json) {
64
        super(json);
65
        this.orginTransformer = null;
66
    }
67
 
68
    @Override
69
    public void doSearch(final Configuration configuration, final SearchSpec searchSpec, final int offset) {
70
        this.getTableSpec().setSearch(searchSpec);
71
 
72
        this.setOffset(offset);
73
 
74
        final SQLTableModelSource tableSource = this.getModel().getReq();
75
        final SearchInfo sInfo = (searchSpec != null) ? new SearchInfo(searchSpec) : null;
76
 
77
        final FutureTask<ListSQLRequest> f = new FutureTask<ListSQLRequest>(new Callable<ListSQLRequest>() {
78
            @Override
79
            public ListSQLRequest call() throws Exception {
80
                final ListSQLRequest req = tableSource.getReq();
81
                final List<SQLTableModelColumn> columns = tableSource.getColumns();
82
                req.setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
83
                    @Override
84
                    public SQLSelect transformChecked(final SQLSelect sel) {
85
                        if (LightRowValuesTableOnline.this.orginTransformer != null) {
86
                            LightRowValuesTableOnline.this.orginTransformer.transformChecked(sel);
87
                        }
88
                        setWhere(sel, sInfo, columns);
89
                        return sel;
90
                    }
91
                });
92
                return req;
93
            }
94
        });
95
 
96
        // TODO: clean swing
97
        SwingUtilities.invokeLater(f);
98
 
99
        try {
100
            final ListSQLRequest req = f.get();
101
 
102
            // get values
103
            long t4 = System.currentTimeMillis();
104
            final List<SQLRowValues> rowValues = req.getValues();
105
            final int size = rowValues.size();
106
            long t5 = System.currentTimeMillis();
107
 
108
            System.err.println("DefaultTableContentHandler.handle() getValues() :" + size + " : " + (t5 - t4) + " ms");
109
        } catch (final Exception e) {
110
            throw new IllegalStateException(e);
111
        }
112
    }
113
 
114
    /**
115
     * Apply filter on ListSQLRequest
116
     *
117
     * @param sel - The ListSQLRequest select
118
     * @param sInfo - Search parameters
119
     */
120
    private final void setWhere(final SQLSelect sel, final SearchInfo sInfo, final List<SQLTableModelColumn> cols) {
121
        if (sInfo != null) {
151 ilm 122
            final Set<IFieldPath> fields = new HashSet<>(cols.size() * 2);
142 ilm 123
            for (final SQLTableModelColumn sqlTableModelColumn : cols) {
151 ilm 124
                fields.addAll(sqlTableModelColumn.getPaths());
142 ilm 125
            }
151 ilm 126
            final List<Where> wheres = new ArrayList<>();
127
            final List<Where> wFields = new ArrayList<>();
142 ilm 128
 
129
            final List<String> texts = sInfo.getTexts();
130
            for (String string : texts) {
131
                wFields.clear();
151 ilm 132
                for (final IFieldPath fpath : fields) {
133
                    if (fpath.getField().getType().getJavaType().equals(String.class)) {
134
                        final Where w = new Where(new SQLFunctionField(SQLFunction.LOWER, sel.followFieldPath(fpath)), "LIKE", "%" + string.toLowerCase() + "%");
142 ilm 135
                        wFields.add(w);
136
                    }
137
                }
138
                wheres.add(Where.or(wFields));
139
            }
140
 
141
            final Where w;
142
            if (sel.getWhere() != null) {
143
                w = Where.and(sel.getWhere(), Where.and(wheres));
144
            } else {
145
                w = Where.and(wheres);
146
            }
147
            sel.setWhere(w);
148
        }
149
    }
150
}