001    package org.hackystat.projectbrowser.page.telemetry.datapanel;
002    
003    import java.io.Serializable;
004    import java.math.BigInteger;
005    import java.util.ArrayList;
006    import java.util.List;
007    import org.hackystat.telemetry.service.resource.chart.jaxb.TelemetryPoint;
008    import org.hackystat.telemetry.service.resource.chart.jaxb.TelemetryStream;
009    
010    /**
011     * Group a selected flag with a TelemetryStream, 
012     * so that this instance can be flagged as selected or not.
013     * @author Shaoxuan
014     *
015     */
016    public class SelectableTelemetryStream implements Serializable {
017      /** Support serialization. */
018      public static final long serialVersionUID = 1L;
019      /** Determine this stream is selected or not. */
020      private boolean selected = false;
021      /** The TelemetryStream of this stream. */
022      private final TelemetryStream telemetryStream;
023      /** The color of this stream. */
024      private String streamColor = "";
025      /** The color of the marker of this stream. */
026      private String markerColor = "";
027      /** the marker of this stream. */
028      private String marker = "";
029      /** thickness of the line.*/
030      private double thickness = 2;
031      /** length of the line segment.*/
032      private double lineLength = 1;
033      /** length of the blank segment.*/
034      private double blankLength = 0;
035      /** the maximum of this stream. */
036      private double maximum;
037      /** the minimum of this stream. */
038      private double minimum;
039      /**
040       * @param telemetryStream the TelemetryStream of this instance.
041       */
042      public SelectableTelemetryStream(TelemetryStream telemetryStream) {
043        this.telemetryStream = telemetryStream;
044        //initial the maximum and minimum value.
045        List<Double> streamData = this.getStreamData();
046        maximum = -1;
047        minimum = 99999999;
048        for (Double value : streamData) {
049          if (value > maximum) {
050            maximum = value;
051          }
052          if (value >= 0 && value < minimum) {
053            minimum = value;
054          }
055        }
056        if (!isEmpty()) {
057          BigInteger upperBound = this.telemetryStream.getYAxis().getUpperBound();
058          BigInteger lowerBound = this.telemetryStream.getYAxis().getLowerBound();
059          
060          //TODO : developing test out, delete when done.
061          /*
062          String name = this.telemetryStream.getName();
063          System.out.println("Stream: " + name + "'s upperBound is " + upperBound);
064          System.out.println("Stream: " + name + "'s lowerBound is " + lowerBound);
065          */
066          
067          if (upperBound != null && upperBound.doubleValue() > maximum) {
068            maximum = upperBound.doubleValue();
069          }
070          if (lowerBound != null && lowerBound.doubleValue() < minimum) {
071            minimum = lowerBound.doubleValue();
072          }
073        }
074      }
075      /**
076       * @param selected the selected to set
077       */
078      public void setSelected(boolean selected) {
079        this.selected = selected;
080      }
081      /**
082       * @return the selected
083       */
084      public boolean isSelected() {
085        return selected;
086      }
087      /**
088       * @return the telemetryStream
089       */
090      public TelemetryStream getTelemetryStream() {
091        return telemetryStream;
092      }
093      /**
094       * Set the color to both stream and marker color.
095       * @param color the color to set.
096       */
097      public void setColor(String color) {
098        this.setStreamColor(color);
099        this.setMarkerColor(color);
100      }
101      /**
102       * @return color of stream.
103       */
104      public String getStreamColor() {
105        return streamColor;
106      }
107      /**
108       * @param streamColor the stream color to set.
109       */
110      public void setStreamColor(String streamColor) {
111        this.streamColor = streamColor;
112      }
113      /**
114       * @return color of marker.
115       */
116      public String getMarkerColor() {
117        return markerColor;
118      }
119      /**
120       * @param markerColor the marker color to set.
121       */
122      public void setMarkerColor(String markerColor) {
123        this.markerColor = markerColor;
124      }
125      /**
126       * Returns a background-color attribute with the value of color.
127       * @return The background-color key-value pair.
128       */
129      public String getBackgroundColorValue() {
130        return "background-color:#" + getStreamColor();
131      }
132      /**
133       * @param marker the marker to set
134       */
135      public void setMarker(String marker) {
136        this.marker = marker;
137      }
138      /**
139       * @return the marker
140       */
141      public String getMarker() {
142        return marker;
143      }
144      /**
145       * @return the isEmpty
146       */
147      public final boolean isEmpty() {
148        return this.maximum < 0;
149      }
150      /**
151       * @return the maximum
152       */
153      public double getMaximum() {
154        return maximum;
155      }
156      /**
157       * @return the minimum
158       */
159      public double getMinimum() {
160        return minimum;
161      }
162      
163      /**
164       * @param thickness the thickness to set
165       */
166      public void setThickness(double thickness) {
167        this.thickness = thickness;
168      }
169      /**
170       * @return the thickness
171       */
172      public double getThickness() {
173        return thickness;
174      }
175      /**
176       * @param lineLength the lineLength to set
177       */
178      public void setLineLength(double lineLength) {
179        this.lineLength = lineLength;
180      }
181      /**
182       * @return the lineLength
183       */
184      public double getLineLength() {
185        return lineLength;
186      }
187      /**
188       * @param blankLength the blankLength to set
189       */
190      public void setBlankLength(double blankLength) {
191        this.blankLength = blankLength;
192      }
193      /**
194       * @return the blankLength
195       */
196      public double getBlankLength() {
197        return blankLength;
198      }
199      /**
200       * @return the list of data of this stream
201       */
202      public final List<Double> getStreamData() {
203        List<Double> streamData = new ArrayList<Double>();
204        for (TelemetryPoint point : this.getTelemetryStream().getTelemetryPoint()) {
205          if (point.getValue() == null) {
206            streamData.add(-1.0);
207          }
208          else {
209            Double value = Double.valueOf(point.getValue());
210            if (value.isNaN()) {
211              value = -2.0;
212            }
213            streamData.add(value);
214          }
215        }
216        return streamData;
217      }
218      
219      /**
220       * Return a image url that shows only one marker.
221       * Using google chart to generate this image.
222       * there is an example output:
223       * http://chart.apis.google.com/chart?
224       *    chs=20x20&cht=ls&chd=t:-1,1.0,-1&chds=0.9,1.1&chm=c,FF0000,0,-1,20.0
225       * @return the image url
226       */
227      public String getMarkerImageUrl() {
228        if (!this.isSelected() || this.isEmpty() || this.marker.length() <= 0) {
229          return "";
230        }
231        String imageUrl = "http://chart.apis.google.com/chart?" +
232                "chs=45x15&cht=ls&chd=t:1.0,1.0,1.0&chds=0.9,1.1&" +
233                "chm=" + marker + "," + markerColor + ",0,1,10.0&" +
234                "chls=" + thickness + "," + lineLength + "," + blankLength + "&" +
235                "chco=" + streamColor;
236        return imageUrl;
237      }
238      
239      /**
240       * Return the Unit of this stream.
241       * @return String of the unit.
242       */
243      public String getUnitName() {
244        return this.telemetryStream.getYAxis().getUnits();
245      }
246      /**
247       * Return the name of this stream.
248       * @return String of the name.
249       */
250      public String getStreamName() {
251        return this.telemetryStream.getName();
252      }
253    }