Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jflight/olc/OLCRecord.java


1   /**
2    * OLCRecord.java
3    *
4    * @author Ingo Koerber
5    */
6   
7   package jflight.olc;
8   
9   import java.util.*;
10  import java.io.*;
11  import java.text.SimpleDateFormat;
12  import java.net.URLEncoder;
13  import jflight.flightbook.*;
14  import jflight.model.*;
15  import jflight.tools.*;
16  
17  /**
18   *  This class represents all information which are necessary to enter
19   *  a valid flight into the online contest.
20   *  There are one constructor which creates a OLCRecord with all information.
21   *  Use the toString() method to create the right url.
22   *
23   *  @CVS-section:
24   *  @file_version
25   */
26  public class OLCRecord {
27      
28      // common parameter key constants
29      public static final String OLC_KEY_SURNAME = "OLCvnolc";
30      public static final String OLC_KEY_LASTNAME = "na";
31      public static final String OLC_KEY_BIRTHDAY = "geb";
32      public static final String OLC_KEY_GLIDER_TYPE = "gty";
33      public static final String OLC_KEY_IGC_FILENAME = "igcfn";
34      public static final String OLC_KEY_LOCATION = "sta";
35      public static final String OLC_KEY_REGION = "region";
36      
37      public static final String OLC_KEY_FLIGHTDATE = "ft";
38      public static final String OLC_KEY_CALLSIGN = "id";
39      public static final String OLC_KEY_DAEC_INDEX = "ind";
40      public static final String OLC_KEY_GLIDER_CLASS = "klasse";
41      public static final String OLC_KEY_START_TIME = "t0";
42      public static final String OLC_KEY_DEPARTURE_TIME = "s0";
43      public static final String OLC_KEY_FINISH_TIME = "s4";
44      public static final String OLC_KEY_LONG_IGC_FILENAME = "ligcfn";
45      
46      // keys for latitude parameters, X will be replaced with wp number
47      public static final String OLC_KEY_VALPOINTS_LATI_N_S = "wXbh";
48      public static final String OLC_KEY_VALPOINTS_LATI_DD = "wXbg";
49      public static final String OLC_KEY_VALPOINTS_LATI_MM = "wXbm";
50      public static final String OLC_KEY_VALPOINTS_LATI_MMM = "wXbmd";
51      
52      // keys for longitude parameters, X will be replaced with wp number
53      public static final String OLC_KEY_VALPOINTS_LONGI_E_W = "wXlh";
54      public static final String OLC_KEY_VALPOINTS_LONGI_DD = "wXlg";
55      public static final String OLC_KEY_VALPOINTS_LONGI_MM = "wXlm";
56      public static final String OLC_KEY_VALPOINTS_LONGI_MMM = "wXlmd";
57      
58      Vector parameters = new Vector();
59      public static SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yy");
60      public static SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
61      
62      // just a sequence number for 30 March 02 so 31 March will be 146551 a.s.o.
63      public static final int SEQ_TIL_300302 = 146551;
64      public static Calendar dateAt300202 = new GregorianCalendar(2002,2,30);
65      
66      
67      private String OLCGetGliderClass( int jflightGliderType ) {
68          String str;
69          switch( jflightGliderType ) {
70              case Glider.HG:
71              case Glider.HG_TL:
72                  str = new String("1");
73                  break;
74              case Glider.RW:
75              case Glider.RW_FAIRED:
76                  str = new String("2");
77                  break;
78              case Glider.PG_OPEN:
79              case Glider.PG:
80                  str = new String("3");
81                  break;
82              case Glider.PG_SPORT:
83                  str = new String("4");
84                  break;
85              case Glider.PG_TANDEM:
86                  str = new String("5");
87                  break;
88              case Glider.UD:
89              default:
90                  str = new String("0");
91                  break;
92          }
93          return str;
94      }
95          
96          /**
97           * OLCRecord constructor
98           *
99           * @param    currentFlight    a Flight object which holds all necessary
100          * information about the pilot, glider and location
101          * @param    valuationPoints  the track points which has been calculated by the
102          *                OLC-Optimizer
103          * @param    daecIndex        "100" ?
104          * @param    fileName         "" ?
105          *
106          */
107         public OLCRecord(Flight currentFlight, Trk[] valuationPoints,
108         String daecIndex, String fileName) {
109             parameters.addElement(new KeyValue(this.OLC_KEY_SURNAME,currentFlight.getPilot().getFirstName()));
110             parameters.addElement(new KeyValue(this.OLC_KEY_LASTNAME, currentFlight.getPilot().getLastName()));
111             String bDay = dateFormat.format(currentFlight.getPilot().getBirthDay());
112             parameters.addElement(new KeyValue(this.OLC_KEY_BIRTHDAY, bDay));
113             parameters.addElement(new KeyValue(this.OLC_KEY_GLIDER_TYPE, currentFlight.getGlider().getGliderName()));
114             //String igcFileName = currentFlight.getDataFileName().substring(0,8);
115             parameters.addElement(new KeyValue(this.OLC_KEY_IGC_FILENAME, fileName));
116             
117             parameters.addElement(new KeyValue(this.OLC_KEY_LOCATION, currentFlight.getLocation().getLocationName()));
118             parameters.addElement(new KeyValue(this.OLC_KEY_REGION, Integer.toString(currentFlight.getLocation().getRegion())));
119             
120             
121             long currentDate = currentFlight.getDate().getTime();
122             long at300302 = dateAt300202.getTime().getTime();
123             
124             int days = (int)((currentDate-at300302)/1000/60/60/24);
125             
126             parameters.addElement(new KeyValue(this.OLC_KEY_FLIGHTDATE,Integer.toString(days+this.SEQ_TIL_300302) ));
127             
128             parameters.addElement(new KeyValue(this.OLC_KEY_DAEC_INDEX,daecIndex));
129             String gliderClass = OLCGetGliderClass(currentFlight.getGlider().getGliderType());
130             parameters.addElement(new KeyValue(this.OLC_KEY_GLIDER_CLASS,gliderClass));
131             
132             String startTime = timeFormat.format(new Date(valuationPoints[0].timeStamp));
133             // start- and departure time are identical by default
134             parameters.addElement(new KeyValue(this.OLC_KEY_START_TIME,startTime));
135             parameters.addElement(new KeyValue(this.OLC_KEY_DEPARTURE_TIME,startTime));
136             
137             for(int i=0;i<valuationPoints.length;i++) {
138                 this.addValuationPoint(i,valuationPoints[i]);
139             }
140             Date finishDate = new Date(valuationPoints[valuationPoints.length-1].timeStamp);
141             String finishTime = timeFormat.format(finishDate);
142             parameters.addElement(new KeyValue(this.OLC_KEY_FINISH_TIME,finishTime));
143             parameters.addElement(new KeyValue(this.OLC_KEY_LONG_IGC_FILENAME,""));
144         }
145         
146         private String keyReplaceX(String key, int nr)  {
147             String nrStr = Integer.toString(nr);
148             return key.replace('X',nrStr.charAt(0));
149         }
150         
151         /**
152          * Method addValuationPoint
153          *
154          * @param    trackNr     value between 0 for starting point and
155          *            4 for landing
156          * @param    valPoint   a  Trk one of the five Waypoint which defines
157          *            the result of the track
158          *
159          */
160         private void addValuationPoint(int trackNr, Trk valPoint)  {
161             
162             // the latitude parameters
163             int[] latis = valPoint.getLatSeparatedToDDMMmmm();
164             if(latis[0] > 0)
165                 parameters.addElement(new KeyValue(keyReplaceX(OLC_KEY_VALPOINTS_LATI_N_S,trackNr),"N"));
166             else
167                 parameters.addElement(new KeyValue(keyReplaceX(OLC_KEY_VALPOINTS_LATI_N_S,trackNr),"S"));
168             
169             parameters.addElement(new KeyValue(keyReplaceX(OLC_KEY_VALPOINTS_LATI_DD,trackNr),
170             Integer.toString(latis[0])));
171             parameters.addElement(new KeyValue(keyReplaceX(OLC_KEY_VALPOINTS_LATI_MM,trackNr),
172             Integer.toString(latis[1])));
173             parameters.addElement(new KeyValue(keyReplaceX(OLC_KEY_VALPOINTS_LATI_MMM,trackNr),
174             Integer.toString(latis[2])));
175             
176             // the longitude parameters
177             int[] longis = valPoint.getLonSeparatedToDDMMmmm();
178             if(longis[0] > 0)
179                 parameters.addElement(new KeyValue(keyReplaceX(OLC_KEY_VALPOINTS_LONGI_E_W,trackNr),"E"));
180             else
181                 parameters.addElement(new KeyValue(keyReplaceX(OLC_KEY_VALPOINTS_LONGI_E_W,trackNr),"W"));
182             
183             parameters.addElement(new KeyValue(keyReplaceX(OLC_KEY_VALPOINTS_LONGI_DD,trackNr),
184             Integer.toString(longis[0])));
185             parameters.addElement(new KeyValue(keyReplaceX(OLC_KEY_VALPOINTS_LONGI_MM,trackNr),
186             Integer.toString(longis[1])));
187             parameters.addElement(new KeyValue(keyReplaceX(OLC_KEY_VALPOINTS_LONGI_MMM,trackNr),
188             Integer.toString(longis[2])));
189             
190         }
191 
192         // for jdk 1.1
193         String replaceAll( String str, String pattern, String replacement ) {
194             String result = str,
195                 newStr;
196             int offs, curOffs = 0;
197             
198             while( (offs=result.indexOf(pattern, curOffs)) != -1 ) {
199                 // System.out.println(str + " - " + pattern + " - " + replacement + " - " + curOffs + " - " + offs );
200                 newStr = new String(result.substring(0, offs));
201                 newStr = newStr.concat(replacement);
202                 newStr = newStr.concat(result.substring(offs + pattern.length(), result.length()));
203                 result = newStr;
204                 curOffs = offs + replacement.length();
205             }
206             
207             return result;
208         }
209         
210         
211         /**
212          * Method toString
213          *
214          * @return   a String which represents the complete OLC input, this can
215          *      be used to write a file which specifies the track
216          *      for the online contest
217          *
218          */
219         public String toString() {
220             JfStringBuffer buf = new JfStringBuffer();
221             //            Iterator iterator = parameters.iterator();
222             //            while(iterator.hasNext()) {
223             //                KeyValue element = (KeyValue)iterator.next();
224             for( int cnt=0;; cnt++ ) {
225                 try {
226                     KeyValue element = (KeyValue)parameters.elementAt(cnt);
227                     // problem: leading zeroes
228                     if( ((String)element.key).endsWith("md") ) {
229                         if( (new Integer(element.value)).intValue() < 10 ) {
230                             buf.append("&").append(element.key).append("=00").append(element.value);
231                         } else
232                         if( (new Integer(element.value)).intValue() < 100 ) {
233                             buf.append("&").append(element.key).append("=0").append(element.value);
234                         } else
235                         {
236                             buf.append("&").append(element.key).append("=").append(element.value);
237                         }
238                     }
239                     else {
240                         buf.append("&").append(element.key).append("=").append(element.value);
241                     }
242                 } catch (Exception e) {
243                     break;
244                 }
245             }
246             buf.delete(0,1);
247             String result = buf.toString();
248             if( new SysParams().getScreenType() != SysParams.PDA ) {
249                 /*
250                 result = result.replaceAll("\u00e4","ae");
251                 result = result.replaceAll("\u00fc","ue");
252                 result = result.replaceAll("\u00f6","oe");
253                 result = result.replaceAll("\u00c4","Ae");
254                 result = result.replaceAll("\u00dc","Ue");
255                 result = result.replaceAll("\u00d6","Oe");
256                  */
257                 result = result.replaceAll("\u00e4","%e4");
258                 result = result.replaceAll("\u00fc","%fc");
259                 result = result.replaceAll("\u00f6","%f6");
260                 result = result.replaceAll("\u00c4","%c4");
261                 result = result.replaceAll("\u00dc","%dc");
262                 result = result.replaceAll("\u00d6","%d6");
263                 
264                 result = result.replaceAll(" ","%20");
265                 result = result.replaceAll(":","%3A");
266                 result = result.replaceAll("\\.","%2E");
267                 result = result.replaceAll("-","%2D");
268             } else {
269                 result = replaceAll(result, "\u00e4","%e4");
270                 result = replaceAll(result, "\u00fc","%fc");
271                 result = replaceAll(result, "\u00f6","%f6");
272                 result = replaceAll(result, "\u00c4","%c4");
273                 result = replaceAll(result, "\u00dc","%dc");
274                 result = replaceAll(result, "\u00d6","%d6");
275                 
276                 result = replaceAll(result, " ","%20");
277                 result = replaceAll(result, ":","%3A");
278                 result = replaceAll(result, ".","%2E");
279                 result = replaceAll(result, "-","%2D");
280             }
281             return result;
282         }
283        
284         public static void main(String[] args) {
285             Flight test = new Flight();
286             Pilot pilot = new Pilot("ingo-dieter", "k\u00f6rber",new Date());
287             test.setPilot(pilot);
288             test.getLocation().setStartWptName("Wallberg a.\u00c4.");
289             test.getGlider().setGliderName("FreeX Moon");
290             test.setDataFileName("265xikf1.igc");
291             Trk[] trackps = new Trk[5];
292             trackps[0] = new Trk(48.10200,11.10200,(new Date()).getTime(),0);
293             trackps[1] = new Trk(48.30400,11.30400,(new Date()).getTime()+3600000,0);
294             trackps[2] = new Trk(48.50600,11.50600,(new Date()).getTime()+7200000,0);
295             trackps[3] = new Trk(48.70800,11.70800,(new Date()).getTime()+10800000,0);
296             trackps[4] = new Trk(48.90900,11.90900,(new Date()).getTime()+14400000,0);
297             OLCRecord r = new OLCRecord(test,trackps,"100","test.olc");
298             try {
299                 FileWriter writer = new FileWriter("test.olc");
300                 writer.write(r.toString());
301                 writer.close();
302             }
303             catch (IOException e) {}
304             
305         }
306         
307         class KeyValue {
308             public String key;
309             public String value;
310             
311             public KeyValue(String key, String value) {
312                 this.key = key;
313                 this.value = value;
314             }
315         }
316         
317     }
318