1   /*
2    * Created on Mar 27, 2008
3    * 
4    * Copyright 2005 CafeSip.org 
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License"); 
7    * you may not use this file except in compliance with the License. 
8    * You may obtain a copy of the License at 
9    *
10   *	http://www.apache.org/licenses/LICENSE-2.0 
11   *
12   * Unless required by applicable law or agreed to in writing, software 
13   * distributed under the License is distributed on an "AS IS" BASIS, 
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
15   * See the License for the specific language governing permissions and 
16   * limitations under the License.
17   *
18   */
19  package org.cafesip.gwtcomp.examples.server;
20  
21  import java.security.Principal;
22  import java.util.Date;
23  import java.util.Random;
24  
25  import javax.servlet.http.HttpSession;
26  
27  import org.cafesip.gwtcomp.client.common.ChartData;
28  import org.cafesip.gwtcomp.client.common.ChartInfo;
29  import org.cafesip.gwtcomp.client.common.ChartInitData;
30  import org.cafesip.gwtcomp.client.common.SeriesInfo;
31  import org.cafesip.gwtcomp.client.common.TimeSeriesPlotData;
32  import org.cafesip.gwtcomp.client.common.TimeSeriesPlotInfo;
33  import org.cafesip.gwtcomp.server.LiveDataSource;
34  import org.cafesip.gwtcomp.server.LiveDataSessionInfo;
35  
36  /**
37   * @author Amit Chatterjee
38   * 
39   */
40  public class LiveDataSourceSample implements LiveDataSource
41  {
42      private Random rand = new Random(new Date().getTime());
43  
44      public LiveDataSourceSample()
45      {
46      }
47  
48      public ChartInitData getChartInfoAndInitialData(Principal user,
49              HttpSession session, LiveDataSessionInfo sessionData)
50      {
51          SeriesInfo[] serie = new SeriesInfo[2];
52  
53          serie[0] = new SeriesInfo("Google");
54          serie[1] = new SeriesInfo("Microsoft");
55          TimeSeriesPlotInfo plot = new TimeSeriesPlotInfo(serie, 0.0, 100.0,
56                  5000L);
57          ChartInfo cinfo = new ChartInfo("Stock Chart", true, plot, 5);
58  
59          TimeSeriesPlotData[] pdata = initInitialData();
60  
61          ChartInitData cdata = new ChartInitData(cinfo, new ChartData(
62                  new Date(), pdata));
63  
64          return cdata;
65  
66      }
67  
68      public ChartData getNextData(Principal user, HttpSession session,
69              LiveDataSessionInfo sessionData)
70      {
71          TimeSeriesPlotData[] pdata = new TimeSeriesPlotData[1];
72  
73          long dt = new Date().getTime();
74  
75          Double[] v1 = new Double[2];
76          v1[0] = new Double(genRandom());
77          v1[1] = new Double(genRandom());
78          pdata[0] = new TimeSeriesPlotData(new Date(dt), v1);
79  
80          return new ChartData(new Date(), pdata);
81      }
82  
83      private TimeSeriesPlotData[] initInitialData()
84      {
85          long period = 5 * 60 * 1000L;
86          long interval = 5 * 1000L;
87  
88          int times = (int) (period / interval);
89  
90          TimeSeriesPlotData[] pdata = new TimeSeriesPlotData[times];
91          long dt = new Date().getTime();
92          for (int i = 0; i < times; i++)
93          {
94              Double[] v = new Double[2];
95              v[0] = new Double(genRandom());
96              v[1] = new Double(genRandom());
97              pdata[i] = new TimeSeriesPlotData(new Date(dt
98                      - (interval * (times - (i + 1)))), v);
99          }
100 
101         return pdata;
102     }
103 
104     private Double genRandom()
105     {
106         return new Double(rand.nextInt(100));
107     }
108 
109     public void close(Principal user, HttpSession session,
110             LiveDataSessionInfo sessionData)
111     {
112     }
113 }