OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011-2019 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.Configuration;
import org.openconcerto.sql.Log;
import org.openconcerto.sql.model.FieldPath;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.request.ListSQLRequest;
import org.openconcerto.sql.view.list.ITableModel;
import org.openconcerto.sql.view.list.SQLTableModelSource;
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
import org.openconcerto.ui.light.TableSearchParameterType;
import org.openconcerto.ui.light.UserSearch;
import org.openconcerto.ui.light.UserSearchItem;
import org.openconcerto.utils.StringUtils;
import org.openconcerto.utils.cc.ITransformer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.minidev.json.JSONObject;

public class LightRowValuesTableOnline extends LightRowValuesTable {
    private ITransformer<SQLSelect, SQLSelect> orginTransformer;

    public LightRowValuesTableOnline() {
        // Serialization
    }

    public LightRowValuesTableOnline(final Configuration configuration, final Number userId, final String id, final ITableModel model) {
        super(configuration, userId, id, model);
        this.orginTransformer = ((SQLTableModelSourceOnline) model.getReq()).getReq().getSelectTransf();
    }

    // Clone constructor
    public LightRowValuesTableOnline(final LightRowValuesTableOnline tableElement) {
        super(tableElement);
        this.orginTransformer = tableElement.orginTransformer;
    }

    // Json constructor
    public LightRowValuesTableOnline(final JSONObject json) {
        super(json);
        this.orginTransformer = null;
    }

    @Override
    public void doSearch(final Configuration configuration, final UserSearch searchSpec, final int offset) {

        this.getTableSpec().setSearch(searchSpec);

        this.setOffset(offset);

        final SQLTableModelSource tableSource = this.getModel().getReq();
        final ListSQLRequest req = tableSource.getReq();
        final List<UserSearchItem> content = new ArrayList<>();
        if (searchSpec != null) {
            content.addAll(searchSpec.getContent());

            for (UserSearchItem item : content) {
                // ex "col0"
                String colId = item.getColumn();
                // ex "contains"
                String searchType = item.getType();
                TableSearchMatcher m = LightRowValuesTableOnline.this.getSearchMatcher(colId, searchType);
                if (m != null && m.getAdditionnalFieldsToFetch() != null) {
                    for (FieldPath p : m.getAdditionnalFieldsToFetch()) {
                        req.addToGraphToFetch(p.getPath(), Arrays.asList(p.getField().getFieldName()));
                    }
                }
            }
        }
        req.setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
            @Override
            public SQLSelect transformChecked(final SQLSelect sel) {
                if (LightRowValuesTableOnline.this.orginTransformer != null) {
                    LightRowValuesTableOnline.this.orginTransformer.transformChecked(sel);
                }
                if (searchSpec != null) {
                    //
                    final List<Where> wheres = new ArrayList<>();
                    for (UserSearchItem item : content) {
                        // ex "col0"
                        String colId = item.getColumn();
                        // ex "hello"
                        String userText = item.getText();
                        // ex "contains"
                        String searchType = item.getType();
                        TableSearchMatcher m = LightRowValuesTableOnline.this.getSearchMatcher(colId, searchType);
                        if (m != null) {
                            if (searchType.equals(UserSearchItem.TYPE_CONTAINS)) {
                                List<String> textParts = StringUtils.fastSplitTrimmed(userText, ' ');
                                for (String p : textParts) {
                                    final Where where = m.getWhere(sel, new TableSearchParameterType(searchType), p);
                                    if (where != null) {
                                        wheres.add(where);
                                    }
                                }
                            } else {
                                final Where where = m.getWhere(sel, new TableSearchParameterType(searchType), userText);
                                if (where != null) {
                                    wheres.add(where);
                                }
                            }

                        } else {
                            Log.get().warning("no TableSearchMatcher for " + item);
                        }

                    }

                    final Where w;
                    if (!wheres.isEmpty()) {
                        if (sel.getWhere() != null) {
                            w = Where.and(sel.getWhere(), Where.and(wheres));
                        } else {
                            w = Where.and(wheres);
                        }
                        sel.setWhere(w);
                    }
                }
                return sel;
            }
        });

    }

}