Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    FilterBuilder and DateTime

    Hi,

    I'm currently using SmartGWT 4.1p.
    (SmartClient Version: v9.1p_2014-03-16/PowerEdition Deployment (built 2014-03-16))
    I'm having some issues with the FilterBuilder and DateTime.

    When I try to filter on a DateTime, the criteria generated by the FilterBuilder does not contains the time.

    Here is the code,
    First my date parsers
    Code:
    DateUtil.setNormalDatetimeDisplayFormatter(new DateDisplayFormatter() {
    
    	public String format(Date date) {
    		return Localization.formatDateTime(date);
    	}
    });
    DateUtil.setNormalDateDisplayFormatter(new DateDisplayFormatter() {
    
    	public String format(Date date) {
    		return Localization.formatDate(date);
    	}
    });
    DateUtil.setNormalTimeDisplayFormatter(new DateDisplayFormatter() {
    
    	public String format(Date date) {
    		return Localization.formatTime(date);
    	}
    });
    
    DateUtil.setShortDatetimeDisplayFormatter(new DateDisplayFormatter() {
    
    	public String format(Date date) {
    		return Localization.formatDateTime(date);
    	}
    });
    
    DateUtil.setShortDateDisplayFormatter(new DateDisplayFormatter() {
    
    	public String format(Date date) {
    		return Localization.formatDate(date);
    	}
    });
    DateUtil.setShortTimeDisplayFormatter(new DateDisplayFormatter() {
    
    	public String format(Date date) {
    		return Localization.formatTime(date);
    	}
    });
    
    
    DateUtil.setDateParser(new DateParser() {
    
    	public Date parse(String dateString) {
    		if ((dateString == null) || (dateString.length() < 8)) return null;
    		if (dateString.length() > 11) return parseDateTime(dateString);
    		if (dateString.charAt(2) == ':') return parseTime(dateString);
    		return parseDate(dateString);
    	}
    });
    The code to create the FilterBuilder
    Code:
    //Initialises date parsers with DateUtil
    Localization.init();
    
    //Creates DataSource
    final DataSource dataSource = new DataSource();
    dataSource.setFields(new DataSourceField("date", FieldType.DATE));
    dataSource.setFields(new DataSourceField("time", FieldType.TIME));
    dataSource.setFields(new DataSourceField("datetime", FieldType.DATETIME));
    dataSource.setFields(new DataSourceField("creator", FieldType.CREATORTIMESTAMP));
    dataSource.setFields(new DataSourceField("modifier", FieldType.MODIFIERTIMESTAMP));
    
    //Creates the FilterBuilder
    final FilterBuilder filterBuilder = new FilterBuilder();
    filterBuilder.setDataSource(dataSource);
    
    
    VLayout test = new VLayout();
    test.setWidth(600);
    test.setHeight(400);
    test.addMember(filterBuilder);
    Button button = new Button("Do");
    button.addClickHandler(new ClickHandler() {
    	
    	@Override
    	public void onClick(ClickEvent event) {
    		Criteria criteria = filterBuilder.getCriteria();
    		String jsonCriteria = criteria == null? null : JSON.encode(criteria.getJsObj(), new JSONEncoder());
    		System.out.println(jsonCriteria);
    	}
    });
    test.addMember(button);
    Test case 1: Date
    Filter on the "date" column equals ''24 Mar 2014"
    The criteria is
    Code:
    {
        "_constructor":"AdvancedCriteria", 
        "operator":"and", 
        "criteria":[
            {
                "fieldName":"date", 
                "operator":"equals", 
                "value":"2014-03-24"
            }
        ]
    }

    Test case 2: Date Time
    Filter on the "datetime" column equals ''24 Mar 2014 - 09:10:11"
    The criteria is
    Code:
    {
        "_constructor":"AdvancedCriteria", 
        "operator":"and", 
        "criteria":[
            {
                "fieldName":"datetime", 
                "operator":"equals", 
                "value":"2014-03-23T23:00:11.000"
            }
        ]
    }
    I do not know why I don't have the hour and the minute.
    How can I retrieve the correct time in the criteria?

    Test case 3: Time
    Filter on the "time" column equals 10:20"
    The criteria is
    Code:
    {
        "_constructor":"AdvancedCriteria", 
        "operator":"and", 
        "criteria":[
            {
                "fieldName":"time", 
                "operator":"equals", 
                "value":"1970-01-01"
            }
        ]
    }
    I do not have the time, only the default date..

    What do I do wrong?

    Regards

    Julien

    #2
    Something appears to be wrong with your parser/formatter pairs, but you didn't post them, so we can't really comment yet.

    Comment


      #3
      Yes, I forget to post them

      Here is
      Code:
      private static DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("dd MMM yyyy - HH:mm:ss");
      private static DateTimeFormat dateFormat = DateTimeFormat.getFormat("dd MMM yyyy");
      private static DateTimeFormat timeFormat  = DateTimeFormat.getFormat("HH:mm:ss");

      Comment


        #4
        Those complete the parsing code, but not the formatting code, and the two are required to match and could easily be the cause of your problem, so we really need complete, runnable code here so we can see every detail.

        Comment


          #5
          Here is the code for the formatting and parsing.

          Code:
           public static String formatDateTime(Date datetime) {
          	return dateTimeFormat.format(datetime);
          }
          
          public static String formatDate(Date date) {
          	return dateFormat.format(date);
          }
          
          public static String formatTime(Date date) {
          	return timeFormat.format(date);
          }
          
          public static Date parseDate(String d) {
          	System.out.println("Parsing date...");
          	try {
          		return dateFormat.parse(d);
          	} catch (Exception e) {
          		e.printStackTrace();
          		return null;
          	}
          }
          
          public static Date parseDateTime(String d) {
          	System.out.println("Parsing datetime...");
          	try {
          		return dateTimeFormat.parse(d);
          	} catch (Exception e) {
          		e.printStackTrace();
          		return null;
          	}
          }
          
          public static Date parseTime(String d) {
          	System.out.println("Parsing time...");
          	try {
          		return timeFormat.parse(d);
          	} catch (Exception e) {
          		e.printStackTrace();
          		return null;
          	}
          }

          Comment


            #6
            Here is the runnable code

            Code:
            import com.google.gwt.core.client.EntryPoint;
            import com.google.gwt.i18n.client.DateTimeFormat;
            import com.smartgwt.client.data.Criteria;
            import com.smartgwt.client.data.DataSource;
            import com.smartgwt.client.data.DataSourceField;
            import com.smartgwt.client.types.FieldType;
            import com.smartgwt.client.util.DateDisplayFormatter;
            import com.smartgwt.client.util.DateParser;
            import com.smartgwt.client.util.DateUtil;
            import com.smartgwt.client.util.JSON;
            import com.smartgwt.client.util.JSONEncoder;
            import com.smartgwt.client.widgets.Button;
            import com.smartgwt.client.widgets.events.ClickEvent;
            import com.smartgwt.client.widgets.events.ClickHandler;
            import com.smartgwt.client.widgets.form.FilterBuilder;
            import com.smartgwt.client.widgets.layout.VLayout;
            
            public class TestFilterBuilder implements EntryPoint{
            
            	private static DateTimeFormat	dateTimeFormat	          = DateTimeFormat.getFormat("dd MMM yyyy - HH:mm:ss");
            	private static DateTimeFormat	dateFormat		          = DateTimeFormat.getFormat("dd MMM yyyy");
            	private static DateTimeFormat	timeFormat		          = DateTimeFormat.getFormat("HH:mm:ss");
            	
            	@Override
            	public void onModuleLoad() {
            		
            		init();
            
            		//Creates DataSource
            		final DataSource dataSource = new DataSource();
            		dataSource.setFields(new DataSourceField("date", FieldType.DATE));
            		dataSource.setFields(new DataSourceField("time", FieldType.TIME));
            		dataSource.setFields(new DataSourceField("datetime", FieldType.DATETIME));
            		dataSource.setFields(new DataSourceField("creator", FieldType.CREATORTIMESTAMP));
            		dataSource.setFields(new DataSourceField("modifier", FieldType.MODIFIERTIMESTAMP));
            
            		//Creates the FilterBuilder
            		final FilterBuilder filterBuilder = new FilterBuilder();
            		filterBuilder.setDataSource(dataSource);
            
            
            		VLayout test = new VLayout();
            		test.setWidth(600);
            		test.setHeight(400);
            		test.addMember(filterBuilder);
            		Button button = new Button("Do");
            		button.addClickHandler(new ClickHandler() {
            			@Override
            			public void onClick(ClickEvent event) {
            				Criteria criteria = filterBuilder.getCriteria();
            				String jsonCriteria = criteria == null? null : JSON.encode(criteria.getJsObj(), new JSONEncoder());
            				System.out.println(jsonCriteria);
            			}
            		});
            		test.addMember(button);
            		test.draw();
            			
            	}
            	
            	private void init(){
            		DateUtil.setNormalDatetimeDisplayFormatter(new DateDisplayFormatter() {
            
            			public String format(Date date) {
            				return dateTimeFormat.format(date);
            			}
            		});
            		DateUtil.setNormalDateDisplayFormatter(new DateDisplayFormatter() {
            
            			public String format(Date date) {
            				return dateFormat.format(date);
            			}
            		});
            		DateUtil.setNormalTimeDisplayFormatter(new DateDisplayFormatter() {
            
            			public String format(Date date) {
            				return timeFormat.format(date);
            			}
            		});
            
            		DateUtil.setShortDatetimeDisplayFormatter(new DateDisplayFormatter() {
            
            			public String format(Date date) {
            				return dateTimeFormat.format(date);
            			}
            		});
            		DateUtil.setShortDateDisplayFormatter(new DateDisplayFormatter() {
            
            			public String format(Date date) {
            				return dateFormat.format(date);
            			}
            		});
            		DateUtil.setShortTimeDisplayFormatter(new DateDisplayFormatter() {
            
            			public String format(Date date) {
            				return timeFormat.format(date);
            			}
            		});
            
            		DateUtil.setDateParser(new DateParser() {
            
            			public Date parse(String dateString) {
            				
            				if ((dateString == null) || (dateString.length() == 0)) 
            					return null;
            				try{ 
            					return dateTimeFormat.parse(dateString);
            				}catch(Exception e ){
            					e.printStackTrace();
            				}
            				try{
            					return timeFormat.parse(dateString);
            				}catch(Exception e){
            					e.printStackTrace();
            				}
            				try {
            					return dateFormat.parse(dateString);
            				} catch (Exception e) {
            					e.printStackTrace();
            				}
            				return null;
            			}
            		});
            	}
            
            }
            As you can see in my original post, (in the tree test cases), I try to retrieve the date, dateTime or time in the criteria generated by the FilterBuilder.
            Can you please have a look a this?

            Thanks in advance

            Comment


              #7
              We see the cause and it's been fixed - please retest with a build dated March 29 or later

              Comment


                #8
                Thanks, I will test next week

                Comment

                Working...
                X