Project

General

Profile

Download (4.94 KB) Statistics
| Branch: | Revision:

git_sitools_idoc / sitools-idoc / hesiod / javaExt / src / fr / ias / sitools / vo / ssa / CenterModeIntersection.java @ 779bac69

1
 /*******************************************************************************
2
 * Copyright 2010-2013 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
3
 *
4
 * This file is part of SITools2.
5
 *
6
 * SITools2 is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * SITools2 is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with SITools2.  If not, see <http://www.gnu.org/licenses/>.
18
 ******************************************************************************/
19
package fr.ias.sitools.vo.ssa;
20

    
21
import java.text.ParseException;
22
import java.text.SimpleDateFormat;
23
import java.util.Date;
24
import java.util.List;
25
import java.util.concurrent.TimeUnit;
26
import java.util.logging.Level;
27
import java.util.logging.Logger;
28

    
29
  /**
30
   * Constructs SQL predicat for Center mode intersection.
31
   */
32
  public class CenterModeIntersection extends AbstractSqlGeometryConstraint {
33
    /**
34
     * LOGGER
35
     */
36
     private static final Logger LOG = Logger.getLogger(CenterModeIntersection.class.getName());
37
    /**
38
     * Right ascension attribut.
39
     */
40
    private transient String raCol;
41
    /**
42
     * Declination attribut.
43
     */
44
    private transient String decCol;
45
    /**
46
     * Time attribut.
47
     */
48
    private transient String timeCol;
49
    /**
50
     * Band attribut.
51
     */
52
    private transient String bandCol;
53

    
54
    @Override
55
    public final void setGeometry(final Object geometry) {
56

    
57
      if (geometry instanceof String[]) {
58
        final String[] geometryArray = (String[]) geometry;
59
        
60
        if (geometryArray.length != 4) {
61
          throw new IllegalArgumentException("geometry must be an array of four elements that contains racolName, decColName, timeColName and bandColName");
62
        } else {
63
          this.raCol = geometryArray[0];
64
          this.decCol = geometryArray[1];
65
          this.timeCol = geometryArray[2];
66
          this.bandCol = geometryArray[3];
67
        }
68
      } else {
69
        throw new IllegalArgumentException("geometry must be an array of four elements that contains racolName, decColName, timeColName and bandColName");
70
      }
71
    }
72

    
73
    @Override
74
    public final String getSqlPredicat() {
75
      if (isPolesCollision()) {
76
        return null;
77
      }
78
      final List rangeSsa = (List) computeTimeAndBandRange();
79
      
80
      final String[] timeRange = (String[]) rangeSsa.get(0);
81
      final double[] bandRange = (double[]) rangeSsa.get(1);
82
      
83
      
84
      final List ranges = (List) computeRange();
85
      final List<Double[]> raRanges = (List<Double[]>) ranges.get(0);
86
      final double[] decRange = (double[]) ranges.get(1);
87
      String predicatDefinition;
88
      if (raRanges.size() == 1) {
89
        final Double[] raRange = raRanges.get(0);
90
        predicatDefinition = String.format(" AND ( %s BETWEEN %s AND %s ) AND ( %s BETWEEN %s AND %s )", decCol, decRange[0], decRange[1], raCol, raRange[0], raRange[1]);
91
      } else {
92
        final Double[] raRange1 = raRanges.get(0);
93
        final Double[] raRange2 = raRanges.get(1);
94
        predicatDefinition = String.format(" AND ( %s BETWEEN %s AND %s ) AND (( %s BETWEEN %s AND %s ) OR ( %s BETWEEN %s AND %s ))",
95
                                             decCol, decRange[0], decRange[1],
96
                                             raCol, raRange1[0], raRange1[1], raCol, raRange2[0], raRange2[1]);
97
      }
98
      
99
      if(timeRange[0].equals(timeRange[1])){
100
          timeRange[1] = addHourinTimeRange(timeRange[0]);
101
      }
102
      predicatDefinition += String.format(" AND ( %s BETWEEN %s AND %s ) AND ( %s BETWEEN '%s' AND '%s' )",
103
                                             bandCol, bandRange[0], bandRange[1],timeCol, timeRange[0], timeRange[1]);
104
      LOG.log(Level.SEVERE,"predicatDefinition : "+predicatDefinition);
105
      return predicatDefinition;
106
    }
107
    
108
    private String addHourinTimeRange(String timeTo){
109
        String a = null;
110
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
111
         try {
112
             Date date = df.parse(timeTo);
113
             Date newDate =  new Date(date.getTime()+TimeUnit.HOURS.toMillis(1));
114
             a = newDate.toString();
115
             LOG.log(Level.INFO," ************************  : newDate.toString : "+a);
116
             LOG.log(Level.INFO," ************************  : newDate.toString : "+newDate.toGMTString());
117
             LOG.log(Level.INFO," ************************  : newDate.toString : "+newDate.toLocaleString());
118
         } catch (ParseException ex) {
119
             Logger.getLogger(CenterModeIntersection.class.getName()).log(Level.SEVERE, null, ex);
120
             a=timeTo;
121
         }
122
        
123
        return a;
124
    }
125
  }