Project

General

Profile

Date Beginend

Le Date Begin-end : qui permet de faire une requête sur deux colonnes différentes, "date de début" et "date de fin". On récupère ainsi les observations dont la date de début est intervenu après "date de début" et dont la fin est intervenu avant la "date de fin".

Le code javascript correspondant :

Partie ADMIN :


/*global Ext, sitools, ID, i18n, document, showResponse, alertFailure, LOCALE, ImageChooser, 
 showHelp*/
Ext.namespace('sitools.component.forms.multiParam');

sitools.component.forms.multiParam.DateBeginEnd = Ext.extend(sitools.component.forms.multiParam.abstractForm, {
    height : 250,
    id : 'sitools.component.forms.definitionId',
    initComponent : function () {
        this.winPropComponent.specificHeight = 350;
        this.winPropComponent.specificWidth = 400;
        sitools.component.forms.multiParam.DateBeginEnd.superclass.initComponent.call(this);

    /*
     * Build all the Columns mapping for the configuration, 
     * this object needs three columns
     */
    var labels = ['DATE FROM', 'DATE TO'];
    for (var i = 1; i <= 2; i++) {
        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 : false 

        }); 

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

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

    this.componentDefaultValue1 = new Ext.form.DateField({
        fieldLabel : i18n.get('label.defaultValue') + ' (DATE FROM)',
        name : 'componentDefaultValue1',
        anchor : '100%',
        format : DEFAULT_DATE_FORMAT
    });
    this.componentDefaultValue2 = new Ext.form.DateField({
        fieldLabel : i18n.get('label.defaultValue') + ' (DATE TO)',
        name : 'componentDefaultValue2',
        anchor : '100%',
        format : DEFAULT_DATE_FORMAT
    });

    this.add(this.componentDefaultValue1);
    this.add(this.componentDefaultValue2);

},
onRender : function () {
    sitools.component.forms.multiParam.DateBeginEnd.superclass.onRender.apply(this, arguments);
    if (this.action == 'modify') {
        if (!Ext.isEmpty(this.selectedRecord.data.defaultValues)) {
            if (Ext.isString(this.selectedRecord.data.defaultValues[0])) {
                this.componentDefaultValue1.setValue(this.selectedRecord.data.defaultValues[0].split('T')[0]);
            }
            else {
                this.componentDefaultValue1.setValue(this.selectedRecord.data.defaultValues[0].split('T')[0]);
            }
            if (Ext.isString(this.selectedRecord.data.defaultValues[1])) {
                this.componentDefaultValue2.setValue(this.selectedRecord.data.defaultValues[1].split('T')[0]);
            }
            else {
                this.componentDefaultValue2.setValue(this.selectedRecord.data.defaultValues[1].split('T')[0]);
            }
        }
    }
},
afterRender : function () {
    sitools.component.forms.multiParam.DateBeginEnd.superclass.afterRender.apply(this, arguments);
    if (this.action == 'modify') {
        if (!Ext.isEmpty(this.selectedRecord.data.defaultValues)) {
            this.componentDefaultValue1.setValue(this.selectedRecord.data.defaultValues[0]);
            this.componentDefaultValue2.setValue(this.selectedRecord.data.defaultValues[1]);

        }
    }

},

_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 defaultValue1 = Ext.isEmpty(f.findField('componentDefaultValue1')) ? "" : f.findField('componentDefaultValue1').getValue();
    var defaultValue2 = Ext.isEmpty(f.findField('componentDefaultValue2')) ? "" : f.findField('componentDefaultValue2').getValue();

    var columnObjects = this.find('specificType', 'mapParam');
    var code = [];
    Ext.each(columnObjects, function (columnObject) {
        code.push(columnObject.getValue());
    });

    if (action == 'modify') {
        var rec = gridFormComponents.getSelectionModel().getSelected();

        var labelParam1 = Ext.isEmpty(f.findField('LABEL_PARAM1')) ? "" : f.findField('LABEL_PARAM1').getValue();
        var css = Ext.isEmpty(f.findField('CSS')) ? "" : f.findField('CSS').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', [ defaultValue1, defaultValue2]);
    } else {
        var formComponentsStore = gridFormComponents.getStore();

        // 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;

        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 : [ defaultValue1, defaultValue2 ]
        }));
    }
    return true;
}

});

partie 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/>.
***************************************/
/*global Ext, sitools*/
Ext.ns('sitools.component.users.SubSelectionParameters.SingleSelection');

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

    initComponent : function () {
        this.fieldFrom = new Ext.form.DateField({
            allowBlank : true,
            format : DEFAULT_DATE_FORMAT, 
            width : 165,
            height : this.height,
            value : new Date(this.defaultValues[0]).format(DEFAULT_DATE_FORMAT)
        });
        this.fieldTo = new Ext.form.DateField({
            allowBlank : true,
            format : DEFAULT_DATE_FORMAT, 
            width : 165,
            height : this.height,
            value : new Date(this.defaultValues[1]).format(DEFAULT_DATE_FORMAT)
        });
        Ext.apply(this, {
            layout : 'table',
            columns : 2,     /* 3*/
            fieldLabel : this.label,
            stype : "sitoolsFormContainer",

            items : [ new Ext.Container({
                border : false,
                html : this.label + " : ", 
                width : "150" 
            }), this.fieldFrom, new Ext.Container({
                border : false,
                html : '&nbsp;'
            }), this.fieldTo ]
        });
        sitools.component.users.SubSelectionParameters.SingleSelection.DateBeginEnd.superclass.initComponent.apply(
                this, arguments);
    },

    /**
     * The code of the parameter to notify changed event.
     */
    code : null,

    /**
     * The left bound of the period.
     */
    fieldFrom : null,

    /**
     * The right bound of the period.
     */
    fieldTo : null,

    notifyValueSelected : function () {
        this.parent.notifyValueChanged(this.code);
    },

    isValueDefined : function () {
        if (this.fieldFrom.getValue() && this.fieldTo.getValue()) {
            return true;
        } else {
            return false;
        }
    },

    getSelectedValue : function () {
        return {
            from : this.fieldFrom.getValue(),
            to : this.fieldTo.getValue()
        };
    },

    getParameterValue : function () {
        var value = this.getSelectedValue();
        if (Ext.isEmpty(value) || Ext.isEmpty(value.from) || Ext.isEmpty(value.to)) {
            return null;
        }
        return this.type + "|" + this.code + "|" + value.from.format('Y-m-d') + "|" + value.to.format('Y-m-d') ;
    }
});

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.Arrays;
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 Date Between Component
 * 
 * 
 * @author d.arpin
 * <a href="https://sourceforge.net/tracker/?func=detail&atid=2158259&aid=3411383&group_id=531341">[3411383]</a><br/>
 * 2011/09/19 d.arpin {add quotes arround date Value}
 */
public final class DateBeginEndFilter 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 TEMPLATE_PARAM */
  private static final String TEMPLATE_PARAM = "p[#]";
  /**
   * the values from to
   */
  private String[] values;

  /**
   * The list of component that uses this filter
   */
  private enum TYPE_COMPONENT {
    /** DefaultType */
    DATE_BEGIN_END
  }

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

    super();
    this.setName("DateBeginEndFilter");
    this.setDescription("Required when using Date Between Components between 2 columns");

    this.setAuthor("HUSSON@IAS");
    this.setClassAuthor("HUSSON@IAS");
    this.setClassOwner("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,
    "DATE_BEGIN_END|columnFrom,columnTo|valueFrom|valueTo");
    rpd.put("0", paramInfo);
    this.setRequestParamsDescription(rpd);
    //

  }

  @Override
  public List<Predicat> createPredicats(Request request, List<Predicat> predicats) throws Exception {
    // Get the dataset
    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 columnfrom = null;
          String columnto = null;
          if (parameters.length >= VALUES) {
            columnfrom = parameters[COLUMN].split(",")[0];
            columnto = parameters[COLUMN].split(",")[1];

            Column colfrom = ds.findByColumnAlias(columnfrom);
            Column colto = ds.findByColumnAlias(columnto);

            if (colfrom != null && colfrom.getFilter() != null && colfrom.getFilter() && checkValues(parameters) && colto != null && colto.getFilter() != null && colto.getFilter()) {
              // escape the values to avoid SQL injection
              String valuefrom = "'" + SQLUtils.escapeString(values[0]) + "'";
              String valueto = "'" + SQLUtils.escapeString(values[1]) + "'";

              Predicat predicat = new Predicat();
              predicat.setLeftAttribute(colfrom);
              predicat.setNbOpenedParanthesis(1);
              predicat.setNbClosedParanthesis(0);
              predicat.setCompareOperator(Operator.GTE.value());
              predicat.setRightValue(valuefrom);
              predicats.add(predicat);
              predicat = new Predicat();
              predicat.setLeftAttribute(colto);
              predicat.setNbOpenedParanthesis(0);
              predicat.setNbClosedParanthesis(1);
              predicat.setCompareOperator(Operator.LTE.value());
              predicat.setRightValue(valueto);
              predicats.add(predicat);
            }

          }
        }
      }

      else {
        filterExists = false;
      }
    }

    return predicats;
  }

  /**
   * Check values of the form
   * 
   * @param parameters
   *          the parameters of the filter
   * @return true if values agree
   */
  private boolean checkValues(String[] parameters) {
    values = Arrays.copyOfRange(parameters, VALUES, parameters.length);
    if (values.length == 2) {
      return true;
    }
    return false;
  }

  /**
   * 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) {
        return null;
      }
    };
  }

}