package org.geotools.jdbc;

import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import org.geotools.data.DefaultQuery;
import org.geotools.data.FeatureSource;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory;

/**
 * 
 *
 * @source $URL$
 */
public abstract class JDBCDateOnlineTest extends JDBCTestSupport {

    @Override
    protected abstract JDBCDateTestSetup createTestSetup();

    public void testMappings() throws Exception {
        SimpleFeatureType ft = dataStore.getSchema( tname("dates") );
        
        assertEquals( Date.class, ft.getDescriptor( aname("d") ).getType().getBinding() );
        assertEquals( Timestamp.class, ft.getDescriptor( aname("dt") ).getType().getBinding() );
        assertEquals( Time.class, ft.getDescriptor( aname("t") ).getType().getBinding() );
    }
    
    public void testFiltersByDate() throws Exception {
        
        
        
        
        FilterFactory ff = dataStore.getFilterFactory();
    
        DateFormat df = new SimpleDateFormat("yyyy-dd-MM");
    
        TimeZone[] zones = { TimeZone.getTimeZone("Etc/GMT+12"),
                TimeZone.getTimeZone("PST"), TimeZone.getTimeZone("EST"),
                TimeZone.getTimeZone("GMT"), TimeZone.getTimeZone("CET"),
                TimeZone.getTimeZone("Etc/GMT-12"),
                TimeZone.getTimeZone("Etc/GMT-14") };
        
        for (TimeZone zone : zones) {
           
            FeatureSource fs = dataStore.getFeatureSource( tname("dates") );
        
            // set JVM time zone
            TimeZone.setDefault(zone);
            //regenerate the database table using the new JVM Timezone
            
            setup.setUpData();
            df.setTimeZone(zone);
            // less than
            Filter f = ff.lessOrEqual(ff.property(aname("d")),
                    ff.literal(df.parse("2009-28-06")));
            System.out.println(f);
            assertEquals("wrong number of records for "+zone.getDisplayName(), 2, fs.getCount(new DefaultQuery(tname("dates"), f)));
    
            f = ff.lessOrEqual(ff.property(aname("d")),
                    ff.literal(df.parse("2009-28-06")));
            assertEquals("wrong number of records for "+zone.getDisplayName(),2, fs.getCount(new DefaultQuery(tname("dates"), f)));
            
        }
    }

    public void testFilterByTimeStamp() throws Exception {
        FeatureSource fs = dataStore.getFeatureSource(tname("dates"));
    
        FilterFactory ff = dataStore.getFilterFactory();
    
        // equal to
        Filter f = ff.equals(ff.property(aname("dt")),
                ff.literal("2009-06-28 15:12:41"));
        assertEquals(1, fs.getCount(new DefaultQuery(tname("dates"), f)));
    
        f = ff.equals(ff.property(aname("dt")), ff.literal(new SimpleDateFormat(
                "HH:mm:ss,dd-yyyy-MM").parse("15:12:41,28-2009-06")));
        assertEquals(1, fs.getCount(new DefaultQuery(tname("dates"), f)));
    }
    
    public void testFilterByTime() throws Exception {
        FeatureSource fs = dataStore.getFeatureSource(tname("dates"));
    
        FilterFactory ff = dataStore.getFilterFactory();
    
        // greather than or equal to
        Filter f = ff.greaterOrEqual(ff.property(aname("t")),
                ff.literal("13:10:12"));
        assertEquals(3, fs.getCount(new DefaultQuery(tname("dates"), f)));
    
        f = ff.greaterOrEqual(ff.property(aname("t")),
                ff.literal(new SimpleDateFormat("ss:HH:mm").parse("12:13:10")));
        assertEquals(3, fs.getCount(new DefaultQuery(tname("dates"), f)));
    }
}

@@ -1199,24 +1199,78 @@
 d-MM%22);%0A    
+    TimeZone originalTimeZone = TimeZone.getDefault();
 %0A        Tim
@@ -1577,33 +1577,42 @@
 14%22) %7D;%0A        
-%0A
+try %7B%0A    
         for (Tim
@@ -1633,32 +1633,30 @@
 ones) %7B%0A    
-  
+%0A
      
-%0A
             
@@ -1650,25 +1650,24 @@
             
- 
 FeatureSourc
@@ -1700,17 +1700,16 @@
 eSource(
- 
 tname(%22d
@@ -1710,37 +1710,36 @@
 ame(%22dates%22)
- 
 );%0A    
+%0A
     
-%0A
             
@@ -1763,32 +1763,36 @@
 one%0A            
+ 
+   
 TimeZone.setDefa
@@ -1814,18 +1814,23 @@
         
+   
+ 
 //
+ 
 regenera
@@ -1878,33 +1878,30 @@
 imezone%0A    
-   
+%0A
      
-%0A
             
@@ -1899,17 +1899,16 @@
         
- 
 setup.se
@@ -1914,24 +1914,28 @@
 etUpData();%0A
+    
             
@@ -1968,16 +1968,20 @@
         
+ 
+   
 // less 
@@ -1985,16 +1985,20 @@
 ss than%0A
+    
         
@@ -2064,32 +2064,36 @@
                 
+    
 ff.literal(df.pa
@@ -2125,16 +2125,23 @@
         
+ 
+   // 
 System.o
@@ -2159,32 +2159,36 @@
 f);%0A            
+    
 assertEquals(%22wr
@@ -2176,32 +2176,57 @@
    assertEquals(
+%0A                        
 %22wrong number of
@@ -2231,33 +2231,35 @@
 of records for %22
-+
+ + 
 zone.getDisplayN
@@ -2267,16 +2267,40 @@
 me(), 2,
+%0A                       
  fs.getC
@@ -2356,24 +2356,28 @@
             
+ 
+   
 f = ff.lessO
@@ -2400,32 +2400,36 @@
 ty(aname(%22d%22)),%0A
+    
                 
@@ -2473,32 +2473,36 @@
 ));%0A            
+    
 assertEquals(%22wr
@@ -2498,16 +2498,41 @@
 tEquals(
+%0A                        
 %22wrong n
@@ -2553,17 +2553,19 @@
 ds for %22
-+
+ + 
 zone.get
@@ -2578,18 +2578,43 @@
 yName(),
+ 
 2,
+%0A                       
  fs.getC
@@ -2661,24 +2661,25 @@
 ));%0A    
+%0A
         
 %0A       
@@ -2662,32 +2662,249 @@
 );%0A    %0A        
+    %7D%0A        %7D finally %7B%0A            // set JVM time zone%0A            TimeZone.setDefault(originalTimeZone);%0A            // regenerate the database table using the new JVM Timezone%0A    %0A            setup.setUpData();
 %0A        %7D%0A    %7D

reset timezone and database after tests