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

Last change on this file since 186 was 186, 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.6 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            _etherService = (EtherService) appContext.getBean( "etherService", EtherService.class );
39        }
40        catch( Throwable tx )
41        {
42            LOGGER.error( "Error initializing EtherService.", tx );
43            throw new ServletException( "Error initializing EtherService.", tx );
44        }
45    }
46
47    /**
48     * Creates an image with the plot and send it to the response
49     * Call by a jsp with <img src="visualization/plotEther?....">
50     *
51     * @param request
52     * @param response
53     * @throws ServletException
54     */
55    public void doGet( final HttpServletRequest request, final HttpServletResponse response )
56            throws ServletException
57    {
58        boolean flagException = false;
59
60        try
61        {
62            final String title = request.getParameter( "title" );
63            final String axeType = request.getParameter( "axeType" );
64
65            // TODO : utiliser JSON pour récupérer pIdPIdList
66            final String pIdPIdArrayString = request.getParameter( "pIdPIdList" );
67            final List<Pair> pIdPIdList = extractpIdPIdListFromString( pIdPIdArrayString );
68            if( null == pIdPIdList )
69            {
70                createErrorPane( response, request, WebException.WebCode.PLATEFORM_OR_PARAMETER_IS_NULL.toString(), null );
71                flagException = true;
72                throw new ServletException( WebException.WebCode.PLATEFORM_OR_PARAMETER_IS_NULL.toString(), null );
73            }
74
75            // Dates
76            final String dateBegin = request.getParameter( "dateBegin" );
77            final String dateEnd = request.getParameter( "dateEnd" );
78            final Calendar calendar = Calendar.getInstance();
79            Date formatedDateBegin = null;
80            Date formatedDateEnd = null;
81            if( null != dateBegin && null != dateEnd && !"false".equals( dateBegin ) && !"false".equals( dateEnd ) )
82                try
83                {
84                    calendar.setTimeInMillis( Long.valueOf( dateBegin ) );
85                    formatedDateBegin = calendar.getTime();
86                    calendar.setTimeInMillis( Long.valueOf( dateEnd ) );
87                    formatedDateEnd = calendar.getTime();
88                }
89                catch( Exception e )
90                {
91                    createErrorPane( response, request, WebException.WebCode.INVALID_DATE.toString(), e );
92                    flagException = true;
93                    throw new ServletException( WebException.WebCode.INVALID_DATE.toString(), e );
94                }
95
96            // Create plot
97            final MegapoliPlot megapoliPlot = new MegapoliPlot();
98            megapoliPlot.setTitle( title );
99            megapoliPlot.setAxeType( axeType );
100            megapoliPlot.setpIdPIdList( pIdPIdList );
101            megapoliPlot.setBeginDate( formatedDateBegin );
102            megapoliPlot.setEndDate( formatedDateEnd );
103
104            if( AxeTypeForFixedPlateform.TIME_LINE.name().equals( axeType ) || AxeTypeForFixedPlateform.TIME_POINTS.name().equals( axeType ) )
105            {
106                megapoliPlot.setTimeSerie( true );
107                megapoliPlot.setLegendToHide( pIdPIdList.size() == 1 );
108            }
109            else
110            {
111                // 2D plot : only one parameter to display
112                final Integer plateformId = (Integer) pIdPIdList.get( 0 ).getFirstValue();
113                final Integer parameterId = (Integer) pIdPIdList.get( 0 ).getSecondValue();
114//                final Data valuesLists = _etherService.getListsByPlateformByParameterByPeriodFor2D( plateformId, parameterId, formatedDateBegin, formatedDateEnd );
115//                megapoliPlot.setData( valuesLists );
116
117                megapoliPlot.setTimeSerie( false );
118                megapoliPlot.setLegendToHide( false );
119//                megapoliPlot.setValuesNumber( ( (double[]) valuesLists.getFirstArray() ).length );
120                megapoliPlot.setValuesNumber( 5 );
121            }
122
123            final BufferedImage bufferedImage = _etherPlotService.createMainPane( megapoliPlot, Context.getLocale( request ) );
124            ImageIO.write( bufferedImage, "png", response.getOutputStream() );
125        }
126        catch( ServiceException se )
127        {
128            try
129            {
130                if( !flagException )
131                    createErrorPane( response, request, se.getLocalizedMessage(), se );
132            }
133            catch( Exception e2 )
134            {
135                throw new ServletException( "Error : no possibity to write image in response", e2 );
136            }
137        }
138        catch( Exception e1 )
139        {
140            try
141            {
142                if( !flagException )
143                    createErrorPane( response, request, "", e1 );
144            }
145            catch( Exception e2 )
146            {
147                throw new ServletException( "Error : no possibity to write image in response", e2 );
148            }
149        }
150    }
151
152    // TODO : supprimer cette méthode et utiliser JSON pour récupérer pIdPIdList
153    @Nullable
154    private List<Pair> extractpIdPIdListFromString( @NotNull final String pIdPIdArrayString )
155    {
156        if( pIdPIdArrayString.isEmpty() )
157            return null;
158
159        final String[] pIDPIdArray = pIdPIdArrayString.split( "," );
160        final List<Pair> pIdPIdList = new ArrayList<Pair>( pIDPIdArray.length );
161        for( final String pIdPId : pIDPIdArray )
162        {
163            final String[] pIdPIdSplit = pIdPId.split( "-" );
164            final Pair<Integer, Integer> pIdPIdPair = new Pair<Integer, Integer>();
165            pIdPIdPair.setFirstValue( Integer.valueOf( pIdPIdSplit[0] ) );
166            pIdPIdPair.setSecondValue( Integer.valueOf( pIdPIdSplit[1] ) );
167            pIdPIdList.add( pIdPIdPair );
168        }
169        return pIdPIdList;
170    }
171
172    private void createErrorPane( final HttpServletResponse response, final HttpServletRequest request, @NotNull final String mainError, @Nullable final Exception e )
173            throws IOException
174    {
175        final BufferedImage errorImage;
176        if( null != e )
177            errorImage = _etherPlotService.createErrorPane( mainError, e.toString(), Context.getLocale( request ) );
178        else
179            errorImage = _etherPlotService.createErrorPane( mainError, null, Context.getLocale( request ) );
180
181        ImageIO.write( errorImage, "png", response.getOutputStream() );
182    }
183
184    private static final Log LOGGER = LogFactory.getLog( ControllerPlot.class );
185
186    private EtherPlotService _etherPlotService;
187    private EtherService _etherService;
188}
Note: See TracBrowser for help on using the repository browser.