OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.sql.ui.light;

import org.openconcerto.sql.model.FieldPath;
import org.openconcerto.sql.model.FieldRef;
import org.openconcerto.sql.model.SQLFunctionField;
import org.openconcerto.sql.model.SQLFunctionField.SQLFunction;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.view.list.SQLTableModelColumn;
import org.openconcerto.ui.light.TableSearchParameterType;
import org.openconcerto.ui.light.UserSearchItem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public class TableSearchColumnMatcher implements TableSearchMatcher {
    private final SQLTableModelColumn col;

    public TableSearchColumnMatcher(SQLTableModelColumn col) {
        this.col = col;
    }

    @Override
    public boolean useMatch() {
        return false;
    }

    @Override
    public boolean match(SQLRowValues r, TableSearchParameterType type, String txt) {
        return true;
    }

    @Override
    public Where getWhere(SQLSelect sel, TableSearchParameterType type, String txt) {
        final Set<FieldPath> fields = this.col.getPaths();
        final List<Where> wFields = new ArrayList<>(fields.size());
        for (final FieldPath fpath : fields) {
            if (fpath.getField().getType().getJavaType().equals(String.class)) {
                final FieldRef fieldRef = sel.followFieldPath(fpath);
                if (type.getType().equals(UserSearchItem.TYPE_CONTAINS)) {
                    final Where w = new Where(new SQLFunctionField(SQLFunction.LOWER, fieldRef), "LIKE", "%" + txt.toLowerCase() + "%");
                    wFields.add(w);
                } else if (type.getType().equals(UserSearchItem.TYPE_STARTS_WITH)) {
                    final Where w = new Where(new SQLFunctionField(SQLFunction.LOWER, fieldRef), "LIKE", txt.toLowerCase() + "%");
                    wFields.add(w);
                } else if (type.getType().equals(UserSearchItem.TYPE_IS)) {
                    final Where w = new Where(new SQLFunctionField(SQLFunction.LOWER, fieldRef), "LIKE", txt.toLowerCase());
                    wFields.add(w);
                }
            }
        }
        return Where.or(wFields);
    }

    @Override
    public List<FieldPath> getAdditionnalFieldsToFetch() {
        return Collections.emptyList();
    }

}