OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
156 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.model.Where;
17
import org.openconcerto.sql.request.ComboSQLRequest;
18
import org.openconcerto.sql.sqlobject.IComboSelectionItem;
19
import org.openconcerto.ui.light.LightUIComboBoxElement;
20
import org.openconcerto.ui.light.LightUIComboRequest;
21
 
22
import java.util.ArrayList;
23
import java.util.Arrays;
24
import java.util.List;
25
import java.util.Locale;
26
import java.util.Optional;
27
import java.util.regex.Pattern;
28
 
29
public class LightUISQLComboRequest implements LightUIComboRequest {
30
 
31
    private final static Pattern QUERY_SPLIT_PATTERN = Pattern.compile("\\s+");
32
 
33
    private final ComboSQLRequest request;
34
 
35
    public LightUISQLComboRequest(ComboSQLRequest request) {
36
        super();
37
        this.request = request;
38
    }
39
 
40
    @Override
41
    public List<LightUIComboBoxElement> getItems(String filter, Optional<LightUIComboBoxElement> selection) {
42
        final Where where = selection.isPresent() ? new Where(this.request.getPrimaryTable().getKey(), "=", selection.get().getId()) : null;
43
        final List<IComboSelectionItem> items = this.request.getComboItems(true, Arrays.asList(QUERY_SPLIT_PATTERN.split(filter)), Locale.getDefault(), where);
44
        final List<LightUIComboBoxElement> res = new ArrayList<>(items.size());
45
        for (final IComboSelectionItem item : items) {
46
            res.add(new LightUIComboBoxElement(item.getId(), item.getLabel()));
47
        }
48
        return res;
49
    }
50
}