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

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

Servlet _ Contour en cours

File size: 7.7 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                megapoliPlot.setValuesNumber( 10 );
109            }
110            else
111            {
112                // 2D plot : only one parameter to display
113                final Integer plateformId = (Integer) pIdPIdList.get( 0 ).getFirstValue();
114                final Integer parameterId = (Integer) pIdPIdList.get( 0 ).getSecondValue();
115//                final Data valuesLists = _etherService.getListsByPlateformByParameterByPeriodFor2D( plateformId, parameterId, formatedDateBegin, formatedDateEnd );
116                final Data valuesLists = BouchonHelper.createRealValuesFor2D();
117                megapoliPlot.setData( valuesLists );
118
119                megapoliPlot.setTimeSerie( false );
120                megapoliPlot.setLegendToHide( false );
121//                megapoliPlot.setValuesNumber( ( (ArrayList) valuesLists.getFirstArray() ).size() );
122                megapoliPlot.setValuesNumber( 5 );
123            }
124
125            final BufferedImage bufferedImage = _etherPlotService.createMainPane( megapoliPlot, Context.getLocale( request ) );
126            ImageIO.write( bufferedImage, "png", response.getOutputStream() );
127        }
128        catch( ServiceException se )
129        {
130            try
131            {
132                if( !flagException )
133                    createErrorPane( response, request, se.getLocalizedMessage(), se );
134            }
135            catch( Exception e2 )
136            {
137                throw new ServletException( "Error : no possibity to write image in response", e2 );
138            }
139        }
140        catch( Exception e1 )
141        {
142            try
143            {
144                if( !flagException )
145                    createErrorPane( response, request, "", e1 );
146            }
147            catch( Exception e2 )
148            {
149                throw new ServletException( "Error : no possibity to write image in response", e2 );
150            }
151        }
152    }
153
154    // TODO : supprimer cette méthode et utiliser JSON pour récupérer pIdPIdList
155    @Nullable
156    private List<Pair> extractpIdPIdListFromString( @NotNull final String pIdPIdArrayString )
157    {
158        if( pIdPIdArrayString.isEmpty() )
159            return null;
160
161        final String[] pIDPIdArray = pIdPIdArrayString.split( "," );
162        final List<Pair> pIdPIdList = new ArrayList<Pair>( pIDPIdArray.length );
163        for( final String pIdPId : pIDPIdArray )
164        {
165            final String[] pIdPIdSplit = pIdPId.split( "-" );
166            final Pair<Integer, Integer> pIdPIdPair = new Pair<Integer, Integer>();
167            pIdPIdPair.setFirstValue( Integer.valueOf( pIdPIdSplit[0] ) );
168            pIdPIdPair.setSecondValue( Integer.valueOf( pIdPIdSplit[1] ) );
169            pIdPIdList.add( pIdPIdPair );
170        }
171        return pIdPIdList;
172    }
173
174    private void createErrorPane( final HttpServletResponse response, final HttpServletRequest request, @NotNull final String mainError, @Nullable final Exception e )
175            throws IOException
176    {
177        final BufferedImage errorImage;
178        if( null != e )
179            errorImage = _etherPlotService.createErrorPane( mainError, e.toString(), Context.getLocale( request ) );
180        else
181            errorImage = _etherPlotService.createErrorPane( mainError, null, Context.getLocale( request ) );
182
183        ImageIO.write( errorImage, "png", response.getOutputStream() );
184    }
185
186    private static final Log LOGGER = LogFactory.getLog( ControllerPlot.class );
187
188    private EtherPlotService _etherPlotService;
189    private EtherService _etherService;
190}
Note: See TracBrowser for help on using the repository browser.