OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 156 Rev 174
Line 12... Line 12...
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.sql.ui.light;
14
 package org.openconcerto.sql.ui.light;
15
 
15
 
16
import org.openconcerto.sql.Configuration;
16
import org.openconcerto.sql.Configuration;
17
import org.openconcerto.sql.model.IFieldPath;
17
import org.openconcerto.sql.Log;
18
import org.openconcerto.sql.model.SQLFunctionField;
-
 
19
import org.openconcerto.sql.model.SQLFunctionField.SQLFunction;
-
 
20
import org.openconcerto.sql.model.SQLRowValues;
18
import org.openconcerto.sql.model.FieldPath;
21
import org.openconcerto.sql.model.SQLSelect;
19
import org.openconcerto.sql.model.SQLSelect;
22
import org.openconcerto.sql.model.Where;
20
import org.openconcerto.sql.model.Where;
23
import org.openconcerto.sql.request.ListSQLRequest;
21
import org.openconcerto.sql.request.ListSQLRequest;
24
import org.openconcerto.sql.view.list.ITableModel;
22
import org.openconcerto.sql.view.list.ITableModel;
25
import org.openconcerto.sql.view.list.SQLTableModelColumn;
-
 
26
import org.openconcerto.sql.view.list.SQLTableModelSource;
23
import org.openconcerto.sql.view.list.SQLTableModelSource;
27
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
24
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
-
 
25
import org.openconcerto.ui.light.TableSearchParameterType;
-
 
26
import org.openconcerto.ui.light.UserSearch;
28
import org.openconcerto.ui.light.SearchSpec;
27
import org.openconcerto.ui.light.UserSearchItem;
29
import org.openconcerto.utils.cc.ITransformer;
28
import org.openconcerto.utils.cc.ITransformer;
30
 
29
 
31
import java.util.ArrayList;
30
import java.util.ArrayList;
32
import java.util.HashSet;
31
import java.util.Arrays;
33
import java.util.List;
32
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
 
33
 
40
import net.minidev.json.JSONObject;
34
import net.minidev.json.JSONObject;
41
 
35
 
42
public class LightRowValuesTableOnline extends LightRowValuesTable {
36
public class LightRowValuesTableOnline extends LightRowValuesTable {
43
    private ITransformer<SQLSelect, SQLSelect> orginTransformer;
37
    private ITransformer<SQLSelect, SQLSelect> orginTransformer;
Line 46... Line 40...
46
        // Serialization
40
        // Serialization
47
    }
41
    }
48
 
42
 
49
    public LightRowValuesTableOnline(final Configuration configuration, final Number userId, final String id, final ITableModel model) {
43
    public LightRowValuesTableOnline(final Configuration configuration, final Number userId, final String id, final ITableModel model) {
50
        super(configuration, userId, id, model);
44
        super(configuration, userId, id, model);
51
 
-
 
52
        this.orginTransformer = ((SQLTableModelSourceOnline) model.getReq()).getReq().getSelectTransf();
45
        this.orginTransformer = ((SQLTableModelSourceOnline) model.getReq()).getReq().getSelectTransf();
53
    }
46
    }
54
 
47
 
55
    // Clone constructor
48
    // Clone constructor
56
    public LightRowValuesTableOnline(final LightRowValuesTableOnline tableElement) {
49
    public LightRowValuesTableOnline(final LightRowValuesTableOnline tableElement) {
57
        super(tableElement);
50
        super(tableElement);
58
 
-
 
59
        this.orginTransformer = tableElement.orginTransformer;
51
        this.orginTransformer = tableElement.orginTransformer;
60
    }
52
    }
61
 
53
 
62
    // Json constructor
54
    // Json constructor
63
    public LightRowValuesTableOnline(final JSONObject json) {
55
    public LightRowValuesTableOnline(final JSONObject json) {
64
        super(json);
56
        super(json);
65
        this.orginTransformer = null;
57
        this.orginTransformer = null;
66
    }
58
    }
67
 
59
 
68
    @Override
60
    @Override
69
    public void doSearch(final Configuration configuration, final SearchSpec searchSpec, final int offset) {
61
    public void doSearch(final Configuration configuration, final UserSearch searchSpec, final int offset) {
-
 
62
 
70
        this.getTableSpec().setSearch(searchSpec);
63
        this.getTableSpec().setSearch(searchSpec);
71
 
64
 
72
        this.setOffset(offset);
65
        this.setOffset(offset);
73
 
66
 
74
        final SQLTableModelSource tableSource = this.getModel().getReq();
67
        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();
68
        final ListSQLRequest req = tableSource.getReq();
-
 
69
        final List<UserSearchItem> content = new ArrayList<>();
-
 
70
        if (searchSpec != null) {
-
 
71
            content.addAll(searchSpec.getContent());
-
 
72
 
-
 
73
            for (UserSearchItem item : content) {
-
 
74
                // ex "col0"
81
                final List<SQLTableModelColumn> columns = tableSource.getColumns();
75
                String colId = item.getColumn();
-
 
76
                // ex "contains"
-
 
77
                String searchType = item.getType();
-
 
78
                TableSearchMatcher m = LightRowValuesTableOnline.this.getSearchMatcher(colId, searchType);
-
 
79
                if (m != null && m.getAdditionnalFieldsToFetch() != null) {
-
 
80
                    for (FieldPath p : m.getAdditionnalFieldsToFetch()) {
-
 
81
                        req.addToGraphToFetch(p.getPath(), Arrays.asList(p.getField().getFieldName()));
-
 
82
                    }
-
 
83
                }
-
 
84
            }
-
 
85
        }
82
                req.setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
86
        req.setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
83
                    @Override
87
            @Override
84
                    public SQLSelect transformChecked(final SQLSelect sel) {
88
            public SQLSelect transformChecked(final SQLSelect sel) {
85
                        if (LightRowValuesTableOnline.this.orginTransformer != null) {
89
                if (LightRowValuesTableOnline.this.orginTransformer != null) {
86
                            LightRowValuesTableOnline.this.orginTransformer.transformChecked(sel);
90
                    LightRowValuesTableOnline.this.orginTransformer.transformChecked(sel);
87
                        }
91
                }
88
                        setWhere(sel, sInfo, columns);
-
 
89
                        return sel;
92
                if (searchSpec != null) {
90
                    }
93
                    //
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) {
-
 
122
            final Set<IFieldPath> fields = new HashSet<>(cols.size() * 2);
-
 
123
            for (final SQLTableModelColumn sqlTableModelColumn : cols) {
-
 
124
                fields.addAll(sqlTableModelColumn.getPaths());
-
 
125
            }
-
 
126
            final List<Where> wheres = new ArrayList<>();
94
                    final List<Where> wheres = new ArrayList<>();
127
            final List<Where> wFields = new ArrayList<>();
95
                    for (UserSearchItem item : content) {
128
 
-
 
-
 
96
                        // ex "col0"
129
            final List<String> texts = sInfo.getTexts();
97
                        String colId = item.getColumn();
-
 
98
                        // ex "hello"
130
            for (String string : texts) {
99
                        String userText = item.getText();
131
                wFields.clear();
100
                        // ex "contains"
132
                for (final IFieldPath fpath : fields) {
101
                        String searchType = item.getType();
133
                    if (fpath.getField().getType().getJavaType().equals(String.class)) {
102
                        TableSearchMatcher m = LightRowValuesTableOnline.this.getSearchMatcher(colId, searchType);
-
 
103
                        if (m != null) {
134
                        final Where w = new Where(new SQLFunctionField(SQLFunction.LOWER, sel.followFieldPath(fpath)), "LIKE", "%" + string.toLowerCase() + "%");
104
                            final Where where = m.getWhere(sel, new TableSearchParameterType(searchType), userText);
-
 
105
                            if (where != null) {
135
                        wFields.add(w);
106
                                wheres.add(where);
136
                    }
107
                            }
-
 
108
                        } else {
-
 
109
                            Log.get().warning("no TableSearchMatcher for " + item);
137
                }
110
                        }
138
                wheres.add(Where.or(wFields));
-
 
-
 
111
 
139
            }
112
                    }
140
 
113
 
141
            final Where w;
114
                    final Where w;
-
 
115
                    if (!wheres.isEmpty()) {
142
            if (sel.getWhere() != null) {
116
                        if (sel.getWhere() != null) {
143
                w = Where.and(sel.getWhere(), Where.and(wheres));
117
                            w = Where.and(sel.getWhere(), Where.and(wheres));
144
            } else {
118
                        } else {
145
                w = Where.and(wheres);
119
                            w = Where.and(wheres);
146
            }
120
                        }
147
            sel.setWhere(w);
121
                        sel.setWhere(w);
148
        }
122
                    }
149
    }
123
                }
-
 
124
                return sel;
-
 
125
            }
-
 
126
        });
-
 
127
 
-
 
128
    }
-
 
129
 
150
}
130
}