1 | package com.ether; |
---|
2 | |
---|
3 | import com.ether.annotation.ControllerMethod; |
---|
4 | import com.ether.annotation.Mandatory; |
---|
5 | import com.ether.annotation.ParamName; |
---|
6 | import com.ether.simulation.Variable; |
---|
7 | import com.medias.database.objects.Plateforme; |
---|
8 | import net.sf.json.JSONObject; |
---|
9 | import org.apache.commons.logging.Log; |
---|
10 | import org.apache.commons.logging.LogFactory; |
---|
11 | import org.jetbrains.annotations.NotNull; |
---|
12 | import org.springframework.web.servlet.ModelAndView; |
---|
13 | |
---|
14 | import javax.servlet.http.HttpServletRequest; |
---|
15 | import javax.servlet.http.HttpServletResponse; |
---|
16 | import java.util.ArrayList; |
---|
17 | import java.util.HashMap; |
---|
18 | import java.util.List; |
---|
19 | import java.util.Map; |
---|
20 | |
---|
21 | /** |
---|
22 | * @author vmipsl |
---|
23 | * @date 17 feb 2011 |
---|
24 | */ |
---|
25 | public class ControllerSimulation |
---|
26 | extends ControllerEther |
---|
27 | { |
---|
28 | /** *********************************************************** **/ |
---|
29 | /** *********************** VIEWS ***************************** **/ |
---|
30 | /** *********************************************************** **/ |
---|
31 | /** |
---|
32 | * Default view if methodName is unknown |
---|
33 | */ |
---|
34 | public ModelAndView home( final HttpServletRequest request, final HttpServletResponse response ) |
---|
35 | throws ServiceException |
---|
36 | { |
---|
37 | return new ModelAndView( VIEW_ERRORS ); |
---|
38 | } |
---|
39 | |
---|
40 | @ControllerMethod(view = VIEW_SIMULATION) |
---|
41 | public Map<String, Object> viewSimulation() |
---|
42 | throws ServiceException |
---|
43 | { |
---|
44 | final List<List<String>> models = getEtherService().getAllModels(); |
---|
45 | |
---|
46 | final Map<String, Object> model = new HashMap<String, Object>(); |
---|
47 | model.put( "simulations", getJSONModels( models ) ); |
---|
48 | return model; |
---|
49 | } |
---|
50 | |
---|
51 | /** *********************************************************** **/ |
---|
52 | /** *********************** CALLS ***************************** **/ |
---|
53 | /** *********************************************************** **/ |
---|
54 | /** |
---|
55 | * This method select the molecules available for the given model |
---|
56 | * |
---|
57 | * @param modelId |
---|
58 | * @return |
---|
59 | * @throws ServiceException |
---|
60 | */ |
---|
61 | @ControllerMethod(jsonResult = true) |
---|
62 | public JSONObject searchVariablesByModel( @Mandatory @ParamName(ParameterConstants.PARAMETER_ID) final Integer modelId ) |
---|
63 | throws ServiceException |
---|
64 | { |
---|
65 | final List<Variable> variablesByModel = getEtherService().getVariablesByModelId( modelId ); |
---|
66 | |
---|
67 | final JSONObject result = new JSONObject(); |
---|
68 | result.put( ParameterConstants.PARAMETER_VARIABLES, getJsonHelper().toJSON( variablesByModel ) ); |
---|
69 | return result; |
---|
70 | } |
---|
71 | |
---|
72 | @ControllerMethod(jsonResult = true) |
---|
73 | public JSONObject searchPlateformsByVariable( @Mandatory @ParamName(ParameterConstants.PARAMETER_CODE) final String variableCode ) |
---|
74 | throws ServiceException |
---|
75 | { |
---|
76 | final List<Plateforme> plateformsByParameter = getEtherService().getPlateformsByParameter( variableCode ); |
---|
77 | |
---|
78 | final JSONObject result = new JSONObject(); |
---|
79 | result.put( ParameterConstants.PARAMETER_PLATEFORMS, getJsonHelper().toJSON( plateformsByParameter ) ); |
---|
80 | return result; |
---|
81 | } |
---|
82 | |
---|
83 | private List<JSONObject> getJSONModels( @NotNull final List<List<String>> models ) |
---|
84 | { |
---|
85 | final List<JSONObject> jsonArray = new ArrayList<JSONObject>(); |
---|
86 | for( final List<String> model : models ) |
---|
87 | { |
---|
88 | final JSONObject jsonModel = new JSONObject(); |
---|
89 | jsonModel.put( "id", model.get( 0 ) ); |
---|
90 | jsonModel.put( "name", model.get( 1 ) ); |
---|
91 | jsonModel.put( "version", model.get( 2 ) ); |
---|
92 | jsonModel.put( "dates", model.get( 3 ).split( "," ) ); |
---|
93 | jsonModel.put( "levels", createLevels( Integer.parseInt( model.get( 4 ) ) ) ); |
---|
94 | jsonArray.add( jsonModel ); |
---|
95 | } |
---|
96 | return jsonArray; |
---|
97 | } |
---|
98 | |
---|
99 | private List<Integer> createLevels( @NotNull final Integer levelMax ) |
---|
100 | { |
---|
101 | final List<Integer> levels = new ArrayList<Integer>(); |
---|
102 | for( int i = 1; i <= levelMax; i++ ) |
---|
103 | levels.add( i ); |
---|
104 | return levels; |
---|
105 | } |
---|
106 | |
---|
107 | private static final Log LOGGER = LogFactory.getLog( ControllerSimulation.class ); |
---|
108 | |
---|
109 | private static final String VIEW_SIMULATION = "visualization/visu_simulation"; |
---|
110 | } |
---|