1 | package com.ether; |
---|
2 | |
---|
3 | import com.medias.Context; |
---|
4 | import gov.noaa.pmel.sgt.dm.SGTMetaData; |
---|
5 | import gov.noaa.pmel.sgt.dm.SimpleLine; |
---|
6 | import gov.noaa.pmel.util.GeoDateArray; |
---|
7 | import org.apache.commons.logging.Log; |
---|
8 | import org.apache.commons.logging.LogFactory; |
---|
9 | import org.jetbrains.annotations.NotNull; |
---|
10 | import org.jetbrains.annotations.Nullable; |
---|
11 | import org.springframework.context.ApplicationContext; |
---|
12 | import org.springframework.web.context.support.WebApplicationContextUtils; |
---|
13 | |
---|
14 | import javax.imageio.ImageIO; |
---|
15 | import javax.servlet.ServletConfig; |
---|
16 | import javax.servlet.ServletException; |
---|
17 | import javax.servlet.http.HttpServlet; |
---|
18 | import javax.servlet.http.HttpServletRequest; |
---|
19 | import javax.servlet.http.HttpServletResponse; |
---|
20 | import java.awt.image.BufferedImage; |
---|
21 | import java.io.IOException; |
---|
22 | import java.util.ArrayList; |
---|
23 | import java.util.Calendar; |
---|
24 | import java.util.Date; |
---|
25 | import java.util.List; |
---|
26 | |
---|
27 | /** |
---|
28 | * @author vmipsl |
---|
29 | * @date 17 june 2011 |
---|
30 | */ |
---|
31 | public class ControllerPlot |
---|
32 | extends HttpServlet |
---|
33 | { |
---|
34 | public void init( final ServletConfig servletConfig ) |
---|
35 | throws ServletException |
---|
36 | { |
---|
37 | try |
---|
38 | { |
---|
39 | final ApplicationContext appContext = WebApplicationContextUtils.getRequiredWebApplicationContext( servletConfig.getServletContext() ); |
---|
40 | _etherService = (EtherService) appContext.getBean( "etherService", EtherService.class ); |
---|
41 | _etherPlotService = (EtherPlotService) appContext.getBean( "etherPlotService", EtherPlotService.class ); |
---|
42 | } |
---|
43 | catch( Throwable tx ) |
---|
44 | { |
---|
45 | LOGGER.error( "Error initializing EtherService.", tx ); |
---|
46 | throw new ServletException( "Error initializing EtherService.", tx ); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * Creates an image with the plot and send it to the response |
---|
52 | * Call by a jsp with <img src="visualization/plotEther?...."> |
---|
53 | * |
---|
54 | * @param request |
---|
55 | * @param response |
---|
56 | * @throws ServletException |
---|
57 | */ |
---|
58 | public void doGet( final HttpServletRequest request, final HttpServletResponse response ) |
---|
59 | throws ServletException |
---|
60 | { |
---|
61 | boolean flagException = false; |
---|
62 | |
---|
63 | try |
---|
64 | { |
---|
65 | //Parameters |
---|
66 | final Integer plateformId = Integer.valueOf( request.getParameter( "plateformId" ) ); |
---|
67 | final Integer parameterId = Integer.valueOf( request.getParameter( "parameterId" ) ); |
---|
68 | final String title = request.getParameter( "title" ); |
---|
69 | final String axeType = request.getParameter( "axeType" ); |
---|
70 | |
---|
71 | final String dateBegin = request.getParameter( "dateBegin" ); |
---|
72 | final String dateEnd = request.getParameter( "dateEnd" ); |
---|
73 | final Calendar calendar = Calendar.getInstance(); |
---|
74 | final Date formatedDateBegin; |
---|
75 | final Date formatedDateEnd; |
---|
76 | try |
---|
77 | { |
---|
78 | calendar.setTimeInMillis( Long.valueOf( dateBegin ) ); |
---|
79 | formatedDateBegin = calendar.getTime(); |
---|
80 | calendar.setTimeInMillis( Long.valueOf( dateEnd ) ); |
---|
81 | formatedDateEnd = calendar.getTime(); |
---|
82 | } |
---|
83 | catch( Exception e ) |
---|
84 | { |
---|
85 | createErrorPane( response, request, WebException.WebCode.INVALID_DATE.toString(), e ); |
---|
86 | flagException = true; |
---|
87 | throw new ServletException( WebException.WebCode.INVALID_DATE.toString(), e ); |
---|
88 | } |
---|
89 | |
---|
90 | // Create plot |
---|
91 | //** ******************************************************************** **// |
---|
92 | // TODO : replace List<Data> by List<value> and List<double> |
---|
93 | //** ******************************************************************** **// |
---|
94 | // final List<Pair<Double, Date>> values = _etherService.getValuesByPlateformByParameterByPeriod( plateformId, parameterId, formatedDateBegin, formatedDateEnd ); |
---|
95 | final List<Pair<Double, Date>> values = new ArrayList<Pair<Double, Date>>(); |
---|
96 | final Pair<Double, Date> p1 = new Pair<Double, Date>( Double.valueOf( 23 ), new Date() ); |
---|
97 | final Pair<Double, Date> p2 = new Pair<Double, Date>( Double.valueOf( 24 ), new Date() ); |
---|
98 | final Pair<Double, Date> p3 = new Pair<Double, Date>( Double.valueOf( 25 ), new Date() ); |
---|
99 | final Pair<Double, Date> p4 = new Pair<Double, Date>( Double.valueOf( 26 ), new Date() ); |
---|
100 | values.add( p1 ); |
---|
101 | values.add( p2 ); |
---|
102 | values.add( p3 ); |
---|
103 | values.add( p4 ); |
---|
104 | final double[] dataArray = extractDoubles( values ); |
---|
105 | final Date[] dateValues = extractDates( values ); |
---|
106 | |
---|
107 | final GeoDateArray dateArray = new GeoDateArray( dateValues ); |
---|
108 | //** ******************************************************************** **// |
---|
109 | //** ******************************************************************** **// |
---|
110 | |
---|
111 | |
---|
112 | final SimpleLine data = new SimpleLine( dateArray, dataArray, "legend" ); |
---|
113 | SGTMetaData meta = new SGTMetaData( "Longitude", "degrees East", false, false ); |
---|
114 | data.setXMetaData( meta ); |
---|
115 | |
---|
116 | meta = new SGTMetaData( "parameterName", "parameterUnit", false, false ); |
---|
117 | data.setYMetaData( meta ); |
---|
118 | |
---|
119 | final MegapoliPlot megapoliPlot = new MegapoliPlot(); |
---|
120 | megapoliPlot.setTitle( title ); |
---|
121 | megapoliPlot.setData( data ); |
---|
122 | |
---|
123 | final BufferedImage bufferedImage = _etherPlotService.createMainPane( megapoliPlot, Context.getLocale( request ) ); |
---|
124 | ImageIO.write( bufferedImage, "png", response.getOutputStream() ); |
---|
125 | } |
---|
126 | catch( Exception e1 ) |
---|
127 | { |
---|
128 | try |
---|
129 | { |
---|
130 | if( !flagException ) |
---|
131 | createErrorPane( response, request, "", e1 ); |
---|
132 | } |
---|
133 | catch( Exception e2 ) |
---|
134 | { |
---|
135 | throw new ServletException( "Error : no possibity to write image in response", e2 ); |
---|
136 | } |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | private void createErrorPane( final HttpServletResponse response, final HttpServletRequest request, @NotNull final String mainError, @NotNull final Exception e ) |
---|
141 | throws IOException |
---|
142 | { |
---|
143 | final BufferedImage errorImage = _etherPlotService.createErrorPane( mainError, e.toString(), Context.getLocale( request ) ); |
---|
144 | ImageIO.write( errorImage, "png", response.getOutputStream() ); |
---|
145 | } |
---|
146 | |
---|
147 | // TODO : remove this code !! ultracrados ! |
---|
148 | private double[] extractDoubles( @NotNull final List<Pair<Double, Date>> datas ) |
---|
149 | { |
---|
150 | final double[] doubles = new double[datas.size()]; |
---|
151 | int i = 0; |
---|
152 | for( final Pair<Double, Date> data : datas ) |
---|
153 | { |
---|
154 | doubles[i] = data.getFirstValue().doubleValue(); |
---|
155 | i++; |
---|
156 | } |
---|
157 | return doubles; |
---|
158 | } |
---|
159 | |
---|
160 | // TODO : remove this code !! ultracrados ! |
---|
161 | private Date[] extractDates( @NotNull final List<Pair<Double, Date>> datas ) |
---|
162 | { |
---|
163 | final Date[] dates = new Date[datas.size()]; |
---|
164 | int i = 0; |
---|
165 | for( final Pair<Double, Date> data : datas ) |
---|
166 | { |
---|
167 | dates[i] = data.getSecondValue(); |
---|
168 | i++; |
---|
169 | } |
---|
170 | return dates; |
---|
171 | } |
---|
172 | |
---|
173 | private static final Log LOGGER = LogFactory.getLog( ControllerPlot.class ); |
---|
174 | |
---|
175 | private EtherService _etherService; |
---|
176 | private EtherPlotService _etherPlotService; |
---|
177 | |
---|
178 | } |
---|