Project

General

Profile

Boolean Custom

Le Boolean Custom permet de faire une requête en prenant en compte ou non certaines observations d'après leurs spécificité : long/short run, anti/center, windescriptor ou non ...

Le code javascript correspondant :

Côté ADMIN :


/***************************************
 * Copyright 2011 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
 * 
 * This file is part of SITools2.
 * 
 * SITools2 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.
 * 
 * SITools2 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 SITools2.  If not, see <http://www.gnu.org/licenses/>.
 ***************************************/
Ext.namespace('sitools.component.forms.multiParam');

sitools.component.forms.multiParam.BooleanCustom = Ext.extend(sitools.component.forms.multiParam.abstractForm, {
    height : 330,
    id : "sitools.component.forms.definitionId",
    initComponent : function () {
        this.winPropComponent.specificHeight = 430;
        this.winPropComponent.specificWidth = 380;
        sitools.component.forms.multiParam.BooleanCustom.superclass.initComponent.call(this);

        var labels = ['Column'];
        var i = 1;
        this['mapParam' + i] = new Ext.form.ComboBox({
            fieldLabel : labels[i - 1],
            triggerAction : 'all',
            name : "PARAM" + i,
            specificType : "mapParam", 
            columnIndex : i, 
            lazyRender : true,
            mode : 'local',
            store : this.storeColumn,
            valueField : 'columnAlias',
            displayField : 'header',
            anchor : '100%', 
            allowBlank : true 

        }); 

        if (this.action == "modify") {
            Ext.apply(this['mapParam' + i], {
                value : this.selectedRecord.data.code[i - 1]
            });
        }

        this.insert(i, this['mapParam' + i]);

        this.componentUncheckedValue = new Ext.form.TextField({
            fieldLabel : i18n.get('label.defaultValue') + 'Uncheked Value',
            name : 'componentUncheckedValue',
            anchor : '100%'
        });
        this.componentCheckedValue = new Ext.form.TextField({
            fieldLabel : i18n.get('label.defaultValue') + 'Cheked Value',
            name : 'componentCheckedValue',
            anchor : '100%'
        });
        this.add(this.componentUncheckedValue);
        this.add(this.componentCheckedValue);
        this.cb = new Ext.form.Checkbox ({
            fieldLabel : i18n.get('label.defaultValue') + 'Default State',
            name : 'defaultStatus',
            allowBlank : true,
            checked : false,
            value : false
        });
        this.add(this.cb);

    },
    onRender : function () {
        sitools.component.forms.multiParam.BooleanCustom.superclass.onRender.apply(this, arguments);
        if (this.action == 'modify') {
            if (!Ext.isEmpty(this.selectedRecord.data.defaultValues)) {
                this.componentUncheckedValue.setValue(this.selectedRecord.data.defaultValues[0]);
                this.componentCheckedValue.setValue(this.selectedRecord.data.defaultValues[1]);
                this.cb.setValue(this.selectedRecord.data.defaultValues[2]);
            }
        }
    },
    _onValidate : function (action, gridFormComponents) {
        var f = this.getForm();
        if (!f.isValid()) {
            Ext.Msg.alert(i18n.get('label.error'), i18n.get('warning.invalidForm'));
            return false;
        }
        var param1, UncheckedValue, CheckedValue, code, defaultState; // defaultValue;
        if (action == 'modify') {
            var rec = gridFormComponents.getSelectionModel().getSelected();
            param1 = Ext.isEmpty(f.findField('PARAM1')) ? "" : f.findField('PARAM1').getValue();
            code = [param1];
            var labelParam1 = Ext.isEmpty(f.findField('LABEL_PARAM1')) ? "" : f.findField('LABEL_PARAM1').getValue();
            var css = Ext.isEmpty(f.findField('CSS')) ? "" : f.findField('CSS').getValue();
            UncheckedValue = Ext.isEmpty(f.findField('componentUncheckedValue')) ? "" : f.findField('componentUncheckedValue').getValue();
            CheckedValue = Ext.isEmpty(f.findField('componentCheckedValue')) ? "" : f.findField('componentCheckedValue').getValue();
            defaultState = Ext.isEmpty(f.findField('defaultStatus')) ? "" : f.findField('defaultStatus').getValue();

            rec.set('label', labelParam1);
            rec.set('code', code);
            rec.set('css', css);
            rec.set('componentDefaultHeight', f.findField('componentDefaultHeight').getValue());
            rec.set('componentDefaultWidth', f.findField('componentDefaultWidth').getValue());

            rec.set('defaultValues', [ UncheckedValue,  CheckedValue, defaultState ]);
        } else {
            var formComponentsStore = gridFormComponents.getStore();
            defaultState = Ext.isEmpty(f.findField('defaultStatus')) ? "" : f.findField('defaultStatus').getValue();
            UncheckedValue = Ext.isEmpty(f.findField('componentUncheckedValue')) ? "" : f.findField('componentUncheckedValue').getValue();
            CheckedValue = Ext.isEmpty(f.findField('componentCheckedValue')) ? "" : f.findField('componentCheckedValue').getValue();

            // Génération de l'id
            var lastId = 0;
            var greatY = 0;
            formComponentsStore.each(function (component) {
                if (component.data.id > lastId) {
                    lastId = parseInt(component.data.id, 10);
                }
                if (component.data.ypos > greatY) {
                    greatY = parseInt(component.data.ypos, 10)  + parseInt(component.data.height, 10);
                }

            });
            var componentId = lastId + 1;
            componentId = componentId.toString();
            var componentYpos = greatY + 10;
            param1 = Ext.isEmpty(f.findField('PARAM1')) ? "" : f.findField('PARAM1').getValue();
            code = [param1];
            formComponentsStore.add(new Ext.data.Record({
                label : f.findField('LABEL_PARAM1').getValue(),
                type : this.ctype,
                code : code,
                width : f.findField('componentDefaultWidth').getValue(),
                height : f.findField('componentDefaultHeight').getValue(),
                id : componentId,
                ypos : componentYpos,
                css : f.findField('CSS').getValue(),
                jsAdminObject : this.jsAdminObject,
                jsUserObject : this.jsUserObject,
                defaultValues : [ UncheckedValue,  CheckedValue, defaultState ]
            }));
        }
        return true;
    }

});

Côté CLIENT :

/***************************************
 * Copyright 2011 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
 * 
 * This file is part of SITools2.
 * 
 * SITools2 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.
 * 
 * SITools2 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 SITools2.  If not, see <http://www.gnu.org/licenses/>.
 ***************************************/

Ext.ns('sitools.component.users.SubSelectionParameters.SingleSelection');

sitools.component.users.SubSelectionParameters.SingleSelection.BooleanCustom = Ext.extend(Ext.Container, {

    initComponent : function () {
        this.cbv = new Ext.form.Checkbox ({
            allowBlank : true,
            checked : eval(this.defaultValues[2]),
            value : eval(this.defaultValues[2])
        });
        Ext.apply(this, {
            layout : "hbox",
            stype : "sitoolsFormContainer",
            items : [new Ext.Container({
                border : false,
                html : this.label,
                width : 100
            }), this.cbv]
        });
        sitools.component.users.SubSelectionParameters.SingleSelection.BooleanCustom.superclass.initComponent.apply(
                this, arguments);
    },

    getSelectedValue : function () {
        if (this.cbv.getValue()) {
            return this.defaultValues[1];
        } else {
            return this.defaultValues[0];
        }

    },
    getParameterValue : function () {
        var value = this.getSelectedValue();
        return this.type + "|" + this.code + "|" + value;
    }

});

Code JAVA à inclure dans workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/filter et référencer la classe dans le fichier fr.cnes.sitools.filter.FilterHelper

/*******************************************************************************
 * Copyright 2011 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
 * 
 * This file is part of SITools2.
 * 
 * SITools2 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.
 * 
 * SITools2 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 SITools2.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package fr.cnes.sitools.filter.basic;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.restlet.Request;
import org.restlet.data.Form;
import org.restlet.ext.wadl.ParameterInfo;
import org.restlet.ext.wadl.ParameterStyle;

import fr.cnes.sitools.common.validator.ConstraintViolation;
import fr.cnes.sitools.common.validator.Validator;
import fr.cnes.sitools.dataset.DataSetApplication;
import fr.cnes.sitools.dataset.filter.business.AbstractFilter;
import fr.cnes.sitools.dataset.model.Column;
import fr.cnes.sitools.dataset.model.DataSet;
import fr.cnes.sitools.dataset.model.Operator;
import fr.cnes.sitools.dataset.model.Predicat;
import fr.cnes.sitools.util.SQLUtils;

/**
 * Filter defined for Single Value Component
 * 
 * 
 * @author d.arpin
 */
public final class BooleanCustom extends AbstractFilter {
  /**
   * The index of TYPE
   */
  private static final int TYPE = 0;
  /**
   * The index of COLUMN
   */
  private static final int COLUMN = 1;
  /**
   * The index of Values
   */
  private static final int VALUES = 2;

  /**
   * The list of component that uses this filter
   */
  private enum TYPE_COMPONENT {
    /** Boolean Custom type */
    BOOLEAN_CUSTOM
  }

  /** The TEMPLATE_PARAM */
  private static final String TEMPLATE_PARAM = "p[#]";

  /**
   * Default constructor
   */
  public BooleanCustom() {

    super();
    this.setName("BooleanCustom");
    this.setDescription("Required when using Boolean Custom Component");

    this.setAuthor("Matthieu Husson");
    this.setClassAuthor("IAS");
    this.setClassOwner("MH@IAS");
    this.setVersion("0.1");
    this.setDefaultFilter(true);

    HashMap<String, ParameterInfo> rpd = new HashMap<String, ParameterInfo>();

    ParameterInfo paramInfo;
    paramInfo = new ParameterInfo("p[#]", false, "xs:string", ParameterStyle.QUERY, "BOOLEAN_CUSTOM|columnAlias|value");
    rpd.put("0", paramInfo);
    this.setRequestParamsDescription(rpd);
    //

  }

  @Override
  public List<Predicat> createPredicats(Request request, List<Predicat> predicats) throws Exception {
    DataSetApplication dsApplication = null;
    DataSet ds = null;

    Form params = request.getResourceRef().getQueryAsForm();
    boolean filterExists = true;
    int i = 0;
    // Build predicat for filters param
    while (filterExists) {
      String index = TEMPLATE_PARAM.replace("#", Integer.toString(i++));
      String formParam = params.getFirstValue(index);
      if (formParam != null) {
        String[] parameters = formParam.split("\\|");
        TYPE_COMPONENT[] types = TYPE_COMPONENT.values();
        Boolean trouve = false;
        for (TYPE_COMPONENT typeCmp : types) {
          if (typeCmp.name().equals(parameters[TYPE])) {
            trouve = true;
          }
        }
        if (trouve) {
          if (dsApplication == null) {
            dsApplication = (DataSetApplication) getContext().getAttributes().get("DataSetApplication");
            ds = dsApplication.getDataSet();
          }
          String columnAlias = null;
          if (parameters.length >= VALUES) {

            /*
             * columnsAlias = parameters[COLUMN].split(","); ArrayList<Column> columns = new ArrayList<Column>(); for
             * (String columnAlias : columnsAlias) { Column col = ds.findByColumnAlias(columnAlias); if (col != null) {
             * columns.add(col); }
             * 
             * }
             */
            columnAlias = parameters[COLUMN];
            Column col = ds.findByColumnAlias(columnAlias);
            if (col != null && col.getFilter() != null && col.getFilter()) {
              // get the value and escape it to avoid SQL injection
              String value = SQLUtils.escapeString(parameters[VALUES]);
              Predicat predicat = new Predicat();
              if (value != null) {
                predicat.setLeftAttribute(col);
                predicat.setNbOpenedParanthesis(0);
                predicat.setNbClosedParanthesis(0);
                predicat.setCompareOperator(Operator.EQ.value());
                predicat.setRightValue("'" + value + "'");

                if(value.equalsIgnoreCase("false")){
                  predicats.add(predicat);
                }
              }
            }

          }
        }
      }

      else {
        filterExists = false;
      }
    }

    return predicats;
  }

  /**
   * Gets the validator for this Filter
   * 
   * @return the validator for the filter
   */
  @Override
  public Validator<AbstractFilter> getValidator() {
    return new Validator<AbstractFilter>() {
      @Override
      public Set<ConstraintViolation> validate(AbstractFilter item) {
        // TODO Auto-generated method stub
        return null;
      }
    };
  }

}