All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.ird.observe.client.form.spi.update.BeanComboBoxUpdater Maven / Gradle / Ivy

package fr.ird.observe.client.form.spi.update;

/*-
 * #%L
 * ObServe Toolkit :: Common Client
 * %%
 * Copyright (C) 2008 - 2017 IRD, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import com.google.common.collect.ImmutableMap;
import fr.ird.observe.client.form.FormUIModel;
import fr.ird.observe.client.form.spi.FormUIComponentUpdateSupport;
import fr.ird.observe.dto.reference.DtoReference;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.apache.commons.lang3.BooleanUtils;
import org.nuiton.jaxx.widgets.select.BeanComboBox;


import static fr.ird.observe.client.form.FormUIHandler.FORCE_LOAD;
import static fr.ird.observe.client.form.FormUIHandler.LIST_NO_LOAD;

/**
 * Created by tchemit on 21/09/17.
 *
 * @author Tony Chemit - [email protected]
 */
public class BeanComboBoxUpdater extends FormUIComponentUpdateSupport, M> {

    public BeanComboBoxUpdater() {
        super();
    }

    @Override
    public boolean shouldUpdate(M model, BeanComboBox component) {
        Boolean noLoad = (Boolean) component.getClientProperty(LIST_NO_LOAD);
        Boolean forceLoadComboBox = (Boolean) component.getClientProperty(FORCE_LOAD);
        String propertyName = component.getProperty();

        //FIXME!!!
//        if (BooleanUtils.isNotTrue(forceLoadComboBox) && (BooleanUtils.isTrue(noLoad) || formManager.getForm() == null)) {
        if (BooleanUtils.isNotTrue(forceLoadComboBox) && BooleanUtils.isTrue(noLoad)) {

            log.debug(String.format("Skip loading of comboBox [%s-%s] (listNoLoad property found or form is null)", component.getBeanType().getSimpleName(), propertyName));
            return false;

        }
        return true;
    }

    @SuppressWarnings("unchecked")
    @Override
    public void update(M model) {
        Optional> beanComboBoxMap = model.getFormUIContext().getBeanComboBoxMap();
        if (beanComboBoxMap.isPresent()) {
            for (BeanComboBox beanComboBox : beanComboBoxMap.get().values()) {
                update(model, beanComboBox);
            }
        }
    }

    public  void update(M model, BeanComboBox component) {
        List data;
        if (shouldUpdate(model, component)) {
            data = Collections.emptyList();
        } else {
            data = getData(model, component.getName(), component);
        }
        log.debug(String.format("comboBox [%s-%s] : %d", component.getBeanType().getSimpleName(), component.getName(), data.size()));

        //FIXME Should we sort data ?
        component.setData(data);

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy