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

fr.ird.observe.client.form.referential.ReferentialFormUIModel Maven / Gradle / Ivy

package fr.ird.observe.client.form.referential;

/*-
 * #%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 fr.ird.observe.client.form.FormUIModel;
import fr.ird.observe.client.form.WithBeanTypeFormModel;
import fr.ird.observe.dto.IdHelper;
import fr.ird.observe.dto.form.Form;
import fr.ird.observe.dto.reference.ReferentialDtoReference;
import fr.ird.observe.dto.referential.I18nReferentialDto;
import fr.ird.observe.dto.referential.ReferentialDto;
import fr.ird.observe.services.service.referential.ReferentialDtoCacheManager;
import fr.ird.observe.spi.DtoModelHelper;
import fr.ird.observe.spi.context.ReferentialDtoContext;
import fr.ird.observe.spi.context.ReferentialFormContext;
import io.ultreia.java4all.lang.Objects2;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by tchemit on 02/10/17.
 *
 * @author Tony Chemit - [email protected]
 */
public class ReferentialFormUIModel> extends FormUIModel implements WithBeanTypeFormModel {

    private static final String PROPERTY_SELECTED_BEAN = "selectedBean";

    protected static final String SUFFIX_TEXT = ".text";
    protected static final String SUFFIX_MODEL = ".model";
    private static final String SUFFIX_SELECTED_INDEX = ".selectedIndex";
    protected static final String SUFFIX_SELECTED_ITEM = ".selectedItem";
    protected static final String SUFFIX_SELECTED = ".selected";
    private static final String SUFFIX_DATE = ".date";

    /** liste des bindings present sur tous les ecrans du referentiel */
    private final static String[] DEFAULT_DATABINDING = {
            ReferentialDto.PROPERTY_CODE + SUFFIX_TEXT,
            ReferentialDto.PROPERTY_URI + SUFFIX_TEXT,
            ReferentialDto.PROPERTY_STATUS + SUFFIX_SELECTED_INDEX,
            ReferentialDto.PROPERTY_NEED_COMMENT + SUFFIX_SELECTED
    };

//    /** liste des proprietes presents sur les entites parametrageLengthWeightAble */
//    private final static String[] DEFAULT_LENGTH_WEIGHT_PARAMETER_ABLE_DATABINDING = {
//            LengthWeightParameterDto.PROPERTY_START_DATE + SUFFIX_DATE,
//            LengthWeightParameterDto.PROPERTY_END_DATE + SUFFIX_DATE,
//            LengthWeightParameterDto.PROPERTY_SPECIES + SUFFIX_SELECTED_ITEM,
//            LengthWeightParameterDto.PROPERTY_OCEAN + SUFFIX_SELECTED_ITEM,
//            LengthWeightParameterDto.PROPERTY_SEX + SUFFIX_SELECTED_INDEX,
//            LengthWeightParameterDto.PROPERTY_LENGTH_WEIGHT_FORMULA + SUFFIX_TEXT,
//            LengthWeightParameterDto.PROPERTY_WEIGHT_LENGTH_FORMULA + SUFFIX_TEXT,
//            LengthWeightParameterDto.PROPERTY_COEFFICIENTS + SUFFIX_TEXT,
//            LengthWeightParameterDto.PROPERTY_MEAN_LENGTH + SUFFIX_MODEL,
//            LengthWeightParameterDto.PROPERTY_MEAN_WEIGHT + SUFFIX_MODEL
//    };

    /** liste des bindings presents sur les entites i18n */
    private final static String[] DEFAULT_I18N_DATABINDING = {
            I18nReferentialDto.PROPERTY_LABEL1 + SUFFIX_TEXT,
            I18nReferentialDto.PROPERTY_LABEL2 + SUFFIX_TEXT,
            I18nReferentialDto.PROPERTY_LABEL3 + SUFFIX_TEXT,
            I18nReferentialDto.PROPERTY_LABEL4 + SUFFIX_TEXT,
            I18nReferentialDto.PROPERTY_LABEL5 + SUFFIX_TEXT,
            I18nReferentialDto.PROPERTY_LABEL6 + SUFFIX_TEXT,
            I18nReferentialDto.PROPERTY_LABEL7 + SUFFIX_TEXT,
            I18nReferentialDto.PROPERTY_LABEL8 + SUFFIX_TEXT
    };

    /** liste des proprietes present sur tous les ecrans du referentiel */
    final static String[] DEFAULT_PROPERTIES = {
            ReferentialDto.PROPERTY_CODE,
            ReferentialDto.PROPERTY_URI,
            ReferentialDto.PROPERTY_STATUS,
            ReferentialDto.PROPERTY_NEED_COMMENT
    };

    protected D bean;
    protected Form form;

    private final String[] naturalIds;
    private final String[] extraBindings;

    /** la liste des bindings à activer lors de l'ouverture de l'ui */
    private final String[] dataBinding;

    private D selectedBean;

    private final ReferentialDtoContext dtoContext;
    private final ReferentialFormContext formContext;

    protected ReferentialFormUIModel(Class beanType) {
        this(beanType, (String[]) null, null);
    }

    protected ReferentialFormUIModel(Class beanType, String[] extraBindings) {
        this(beanType, (String[]) null, extraBindings);

    }

    public ReferentialFormUIModel(Class beanType, String[] naturalIds, String[] extraBindings) {
        this.dtoContext = DtoModelHelper.fromReferentialDto(beanType);
        this.formContext = DtoModelHelper.fromReferentialForm(beanType).orElse(null);

        boolean useDefault = naturalIds == null;

        // la clef naturelle par defaut est la premiere des properties
        this.naturalIds = useDefault ? new String[]{DEFAULT_PROPERTIES[0]} : naturalIds;

        List bindings = buildDataBindings(useDefault, extraBindings);
        this.dataBinding = bindings.toArray(new String[bindings.size()]);
        this.extraBindings = extraBindings;
    }

    public D getBean() {
        return bean;
    }

    public void setBean(D bean) {
        this.bean = bean;
    }

    @Override
    public Class getBeanType() {
        return dtoContext.toDtoType();
    }

//    void loadOrPreCreateForm(ReferentialDtoCacheManager dataSource, String parentId, String id) {
//
//        if (id == null) {
//            PreCreateFormRequestDto preCreateFormRequest = PreCreateFormRequestDto.of(parentId);
//            preCreateForm(dataSource, preCreateFormRequest);
//            return;
//        }
//
//        LoadFormRequestDto loadFormRequest;
//        if (parentId == null) {
//            loadFormRequest = LoadFormRequestDto.of(id);
//        } else {
//            loadFormRequest = LoadFormRequestDto.of(parentId, id);
//        }
//        loadForm(dataSource, loadFormRequest);
//    }
//
//    protected void loadForm(ReferentialDtoCacheManager dataSource, LoadFormRequestDto loadFormRequest) {
//        form = getLoadDtoFormServiceAction().loadForm(loadFormRequest);
//        formUIContext.loadFormDependencies(dataSource, formContext.toFormDefinition(), true);
//        formUIContext.updateUi(this);
//        form.copyTo(getBean());
//    }
//
//    protected Form loadForm(ReferentialDtoCacheManager dataSource, String id) {
//        form = getLoadDtoFormServiceAction().loadForm(LoadFormRequestDto.of(id));
//        formUIContext.loadFormDependencies(dataSource, formContext.toFormDefinition(), true);
//        formUIContext.updateUi(this);
//        form.copyTo(getBean());
//        return form;
//    }
//
//    void preCreateForm(ReferentialDtoCacheManager dataSource, PreCreateFormRequestDto preCreateFormRequest) {
//        form = getPreCreateFormServiceAction().preCreate(preCreateFormRequest);
//        formUIContext.loadFormDependencies(dataSource, formContext.toFormDefinition(), true);
//        formUIContext.updateUi(this);
//        form.copyTo(getBean());
//    }
//
//
//    public SaveResultDto save(D bean) {
//        return getSaveDtoServiceAction().save(SaveRequestDto.of(bean));
//    }
//
//    public SaveResultDto save(String parentId, D bean) {
//        return getSaveDtoServiceAction().save(SaveRequestDto.of(parentId, bean));
//    }
//
//    public DeleteResultDto delete(String id) {
//        return formUIContext.getDeleteServiceAction().delete(DeleteRequestDto.of(id));
//    }
//
//    public DeleteResultDto delete(String parentId, String id) {
//        return formUIContext.getDeleteServiceAction().delete(DeleteRequestDto.of(parentId, id));
//    }

    public void loadFormDependencies(ReferentialDtoCacheManager cacheManager, boolean clear) {
        formUIContext.loadFormDependencies(cacheManager, formContext.toFormDefinition(), clear);
    }

    public void updateUi() {
        formUIContext.updateUi(this);
    }

    public Class getReferenceType() {
        return null;
    }


    public String[] getNaturalIds() {
        return naturalIds;
    }

    public String[] getDataBinding() {
        return dataBinding;
    }

    public D getSelectedBean() {
        return selectedBean;
    }

    public void setSelectedBean(D selectedBean) {
        Object oldvalue = this.selectedBean;
        this.selectedBean = selectedBean;
        firePropertyChange(PROPERTY_SELECTED_BEAN, oldvalue, selectedBean);
    }

    /**
     * construction de la liste des databindings utiliser pour une entite.
     *
     * @param usedefault  un drapeau pour savoir si on doit conserver la premiere valeur par defaut
     * @param dataBinding les bindings supplémentaires a utiliser en plus de
     *                    ceux communs
     * @return la liste de tous les databindings utilises sur l'entite edite dans l'ui
     */
    protected List buildDataBindings(boolean usedefault, String[] dataBinding) {

        List result = new ArrayList<>();
        result.addAll(Arrays.asList(DEFAULT_DATABINDING));
        if (dataBinding != null) {
            result.addAll(Arrays.asList(dataBinding));
        }
        Class beanType = dtoContext.toDtoType();

        if (I18nReferentialDto.class.isAssignableFrom(beanType)) {
            result.addAll(Arrays.asList(DEFAULT_I18N_DATABINDING));
        }
//        if (LengthWeightParameterDto.class.isAssignableFrom(beanType)) {
//            result.addAll(Arrays.asList(DEFAULT_LENGTH_WEIGHT_PARAMETER_ABLE_DATABINDING));
//        }
        if (!usedefault) {

            // on supprime le premier binding
            result.remove(DEFAULT_DATABINDING[0]);
        }
        return result;

    }

    //FIXME!!!
    public > List getReferentialReferences(Class referenceType) {
        return null;
    }

    public void copyBean(D incomingBean) {
        if (incomingBean == null) {
            Objects2.clear(bean);
        } else {
            Objects2.copy(incomingBean, bean);
        }
    }

    @Override
    protected String computePrefix() {
        return "[" + IdHelper.getDtoSimplifiedName(getBeanType()) + "UI] ";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy