source: ether_megapoli/trunk/web/src/com/ether/ControllerPlot.java @ 185

Last change on this file since 185 was 185, checked in by vmipsl, 13 years ago

Servlet _ TimeSerie? :

  • 1 paramètre ok
  • même paramètre sur différentes plateformes ok
File size: 7.9 KB
Line 
1package com.ether;
2
3import com.medias.Context;
4import org.apache.commons.logging.Log;
5import org.apache.commons.logging.LogFactory;
6import org.jetbrains.annotations.NotNull;
7import org.jetbrains.annotations.Nullable;
8import org.springframework.context.ApplicationContext;
9import org.springframework.web.context.support.WebApplicationContextUtils;
10
11import javax.imageio.ImageIO;
12import javax.servlet.ServletConfig;
13import javax.servlet.ServletException;
14import javax.servlet.http.HttpServlet;
15import javax.servlet.http.HttpServletRequest;
16import javax.servlet.http.HttpServletResponse;
17import java.awt.image.BufferedImage;
18import java.io.IOException;
19import java.util.ArrayList;
20import java.util.Calendar;
21import java.util.Date;
22import java.util.List;
23
24/**
25 * @author vmipsl
26 * @date 17 june 2011
27 */
28public class ControllerPlot
29        extends HttpServlet
30{
31    public void init( final ServletConfig servletConfig )
32            throws ServletException
33    {
34        try
35        {
36            final ApplicationContext appContext = WebApplicationContextUtils.getRequiredWebApplicationContext( servletConfig.getServletContext() );
37            _etherPlotService = (EtherPlotService) appContext.getBean( "etherPlotService", EtherPlotService.class );
38        }
39        catch( Throwable tx )
40        {
41            LOGGER.error( "Error initializing EtherService.", tx );
42            throw new ServletException( "Error initializing EtherService.", tx );
43        }
44    }
45
46    /**
47     * Creates an image with the plot and send it to the response
48     * Call by a jsp with <img src="visualization/plotEther?....">
49     *
50     * @param request
51     * @param response
52     * @throws ServletException
53     */
54    public void doGet( final HttpServletRequest request, final HttpServletResponse response )
55            throws ServletException
56    {
57        boolean flagException = false;
58
59        try
60        {
61            final String title = request.getParameter( "title" );
62            final String axeType = request.getParameter( "axeType" );
63
64            // TODO : utiliser JSON pour récupérer pIdPIdList
65            final String pIdPIdArrayString = request.getParameter( "pIdPIdList" );
66            final List<Pair> pIdPIdList = extractpIdPIdListFromString( pIdPIdArrayString );
67            if( null == pIdPIdList )
68            {
69                createErrorPane( response, request, WebException.WebCode.PLATEFORM_OR_PARAMETER_IS_NULL.toString(), null );
70                flagException = true;
71                throw new ServletException( WebException.WebCode.PLATEFORM_OR_PARAMETER_IS_NULL.toString(), null );
72            }
73
74            // Dates
75            final String dateBegin = request.getParameter( "dateBegin" );
76            final String dateEnd = request.getParameter( "dateEnd" );
77            final Calendar calendar = Calendar.getInstance();
78            Date formatedDateBegin = null;
79            Date formatedDateEnd = null;
80            if( null != dateBegin && null != dateEnd && !"false".equals( dateBegin ) && !"false".equals( dateEnd ) )
81                try
82                {
83                    calendar.setTimeInMillis( Long.valueOf( dateBegin ) );
84                    formatedDateBegin = calendar.getTime();
85                    calendar.setTimeInMillis( Long.valueOf( dateEnd ) );
86                    formatedDateEnd = calendar.getTime();
87                }
88                catch( Exception e )
89                {
90                    createErrorPane( response, request, WebException.WebCode.INVALID_DATE.toString(), e );
91                    flagException = true;
92                    throw new ServletException( WebException.WebCode.INVALID_DATE.toString(), e );
93                }
94
95            // Create plot
96            final MegapoliPlot megapoliPlot = new MegapoliPlot();
97            megapoliPlot.setTitle( title );
98            megapoliPlot.setAxeType( axeType );
99            megapoliPlot.setpIdPIdList( pIdPIdList );
100            megapoliPlot.setBeginDate( formatedDateBegin );
101            megapoliPlot.setEndDate( formatedDateEnd );
102
103            if( AxeTypeForFixedPlateform.TIME_LINE.name().equals( axeType ) || AxeTypeForFixedPlateform.TIME_POINTS.name().equals( axeType ) )
104            {
105                megapoliPlot.setTimeSerie( true );
106                megapoliPlot.setLegendToHide( pIdPIdList.size() == 1 );
107            }
108            else
109            {
110//                final Data valuesLists = _etherService.getListsByPlateformByParameterByPeriodFor2D( plateformId, parameterId, formatedDateBegin, formatedDateEnd );
111                final double[] latitudeValues = new double[50];
112                final double[] longitudeValues = new double[50];
113                final double[] parameterValues = new double[50];
114
115                for( int i = 0; i < 50; i++ )
116                {
117                    parameterValues[i] = Math.random() * i;
118                    latitudeValues[i] = Math.random() * i;
119                    longitudeValues[i] = Math.random() * i;
120                }
121                final Data valuesLists = new Data( parameterValues, latitudeValues, longitudeValues );
122
123                megapoliPlot.setData( valuesLists );
124//                final double[] latitudeArray = {2.1, 2.2, 2.3};
125//                final double[] longitudeValues = {112.1, 12.2, 22.3};
126
127                megapoliPlot.setTimeSerie( false );
128                megapoliPlot.setLegendToHide( false );
129//                megapoliPlot.setValuesNumber( ( (double[]) valuesLists.getFirstArray() ).length );
130            }
131
132            final BufferedImage bufferedImage = _etherPlotService.createMainPane( megapoliPlot, Context.getLocale( request ) );
133            ImageIO.write( bufferedImage, "png", response.getOutputStream() );
134        }
135        catch( ServiceException se )
136        {
137            try
138            {
139                if( !flagException )
140                    createErrorPane( response, request, se.getLocalizedMessage(), se );
141            }
142            catch( Exception e2 )
143            {
144                throw new ServletException( "Error : no possibity to write image in response", e2 );
145            }
146        }
147        catch( Exception e1 )
148        {
149            try
150            {
151                if( !flagException )
152                    createErrorPane( response, request, "", e1 );
153            }
154            catch( Exception e2 )
155            {
156                throw new ServletException( "Error : no possibity to write image in response", e2 );
157            }
158        }
159    }
160
161    // TODO : supprimer cette méthode et utiliser JSON pour récupérer pIdPIdList
162    @Nullable
163    private List<Pair> extractpIdPIdListFromString( @NotNull final String pIdPIdArrayString )
164    {
165        if( pIdPIdArrayString.isEmpty() )
166            return null;
167
168        final String[] pIDPIdArray = pIdPIdArrayString.split( "," );
169        final List<Pair> pIdPIdList = new ArrayList<Pair>( pIDPIdArray.length );
170        for( final String pIdPId : pIDPIdArray )
171        {
172            final String[] pIdPIdSplit = pIdPId.split( "-" );
173            final Pair<Integer, Integer> pIdPIdPair = new Pair<Integer, Integer>();
174            pIdPIdPair.setFirstValue( Integer.valueOf( pIdPIdSplit[0] ) );
175            pIdPIdPair.setSecondValue( Integer.valueOf( pIdPIdSplit[1] ) );
176            pIdPIdList.add( pIdPIdPair );
177        }
178        return pIdPIdList;
179    }
180
181    private void createErrorPane( final HttpServletResponse response, final HttpServletRequest request, @NotNull final String mainError, @Nullable final Exception e )
182            throws IOException
183    {
184        final BufferedImage errorImage;
185        if( null != e )
186            errorImage = _etherPlotService.createErrorPane( mainError, e.toString(), Context.getLocale( request ) );
187        else
188            errorImage = _etherPlotService.createErrorPane( mainError, null, Context.getLocale( request ) );
189
190        ImageIO.write( errorImage, "png", response.getOutputStream() );
191    }
192
193    private static final Log LOGGER = LogFactory.getLog( ControllerPlot.class );
194
195    private EtherPlotService _etherPlotService;
196
197}
Note: See TracBrowser for help on using the repository browser.