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.medias.annuaire.Personne; |
---|
7 | import com.medias.database.objects.Parametre; |
---|
8 | import com.medias.database.objects.Plateforme; |
---|
9 | import com.medias.megapoli.struts.forms.DatasForm; |
---|
10 | import com.medias.megapoli.trade.MesureAdapter; |
---|
11 | import com.medias.megapoli.trade.ValeurAdapter; |
---|
12 | import com.medias.megapoli.utils.Requete; |
---|
13 | import net.sf.json.JSONObject; |
---|
14 | import org.apache.commons.logging.Log; |
---|
15 | import org.apache.commons.logging.LogFactory; |
---|
16 | import org.jdom.Document; |
---|
17 | import org.jdom.output.XMLOutputter; |
---|
18 | import org.jetbrains.annotations.NotNull; |
---|
19 | import org.springframework.beans.factory.annotation.Required; |
---|
20 | import org.springframework.web.servlet.ModelAndView; |
---|
21 | |
---|
22 | import javax.servlet.http.HttpServletRequest; |
---|
23 | import javax.servlet.http.HttpServletResponse; |
---|
24 | import java.io.BufferedReader; |
---|
25 | import java.io.DataOutputStream; |
---|
26 | import java.io.InputStreamReader; |
---|
27 | import java.net.URL; |
---|
28 | import java.net.URLConnection; |
---|
29 | import java.net.URLEncoder; |
---|
30 | import java.util.ArrayList; |
---|
31 | import java.util.Calendar; |
---|
32 | import java.util.Date; |
---|
33 | import java.util.HashMap; |
---|
34 | import java.util.List; |
---|
35 | import java.util.Map; |
---|
36 | |
---|
37 | /** |
---|
38 | * @author vmipsl |
---|
39 | * @date 17 feb 2011 |
---|
40 | */ |
---|
41 | public class ControllerVisualization |
---|
42 | extends ControllerEther |
---|
43 | { |
---|
44 | /** *********************************************************** **/ |
---|
45 | /** *********************** VIEWS ***************************** **/ |
---|
46 | /** *********************************************************** **/ |
---|
47 | // Default view if methodName is unknown |
---|
48 | public ModelAndView home( final HttpServletRequest request, final HttpServletResponse response ) |
---|
49 | throws EtherException |
---|
50 | { |
---|
51 | return new ModelAndView( "index" ); |
---|
52 | } |
---|
53 | |
---|
54 | @ControllerMethod(view = VIEW_WORK) |
---|
55 | public Map<String, Object> viewInWork() |
---|
56 | throws ServiceException |
---|
57 | { |
---|
58 | return new HashMap<String, Object>(); |
---|
59 | } |
---|
60 | |
---|
61 | @ControllerMethod(view = VIEW_VISUALIZATION) |
---|
62 | public Map<String, Object> view() |
---|
63 | throws ServiceException |
---|
64 | { |
---|
65 | return new HashMap<String, Object>(); |
---|
66 | } |
---|
67 | |
---|
68 | @ControllerMethod(view = VIEW_VISUALIZATION_PARAMETER_BY_PLATEFORM) |
---|
69 | public Map<String, Object> viewParametersByPlateform() |
---|
70 | throws ServiceException |
---|
71 | { |
---|
72 | final List<Plateforme> plateforms = _etherService.getAllPlateforms(); |
---|
73 | final Date firstDate = _etherService.getFirstDate(); |
---|
74 | final Date endDate = _etherService.getLastDate(); |
---|
75 | |
---|
76 | final Map<String, Object> model = new HashMap<String, Object>(); |
---|
77 | model.put( "plateforms", getJsonHelper().toJSON( plateforms ) ); |
---|
78 | model.put( "axeTypesForFixedPlateforms", getJSONAxeTypesForFixedPlateforms() ); |
---|
79 | model.put( "axeTypesForMobilePlateforms", getJSONAxeTypesForMobilePlateforms() ); |
---|
80 | model.put( "firstDate", DateHelper.formatDate( firstDate, DateHelper.ENGLISH_DATE_PATTERN ) ); |
---|
81 | model.put( "lastDate", DateHelper.formatDate( endDate, DateHelper.ENGLISH_DATE_PATTERN ) ); |
---|
82 | return model; |
---|
83 | } |
---|
84 | |
---|
85 | /** *********************************************************** **/ |
---|
86 | /** *********************** CALLS ***************************** **/ |
---|
87 | /** *********************************************************** **/ |
---|
88 | @ControllerMethod(jsonResult = true) |
---|
89 | public JSONObject searchParametersByPlateform( @Mandatory @ParamName(ParameterConstants.PARAMETER_ID) final Integer plateformId ) |
---|
90 | throws ServiceException, EtherException |
---|
91 | { |
---|
92 | final List<Parametre> fullParametersByPlateform = _etherService.getParametersByPlateformId( plateformId ); |
---|
93 | final List<List<Parametre>> parametersByPlateform = manageMenusForParameterList( fullParametersByPlateform ); |
---|
94 | |
---|
95 | final JSONObject result = new JSONObject(); |
---|
96 | result.put( ParameterConstants.PARAMETER_PARAMETERS, getJsonHelper().toJSON( parametersByPlateform ) ); |
---|
97 | return result; |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * This method manage the sub-menus for the list of parameters (to avoid multiple "Particle Concentration" by example) |
---|
102 | * The fullParameters must be ordered with parameterName ascendant |
---|
103 | * |
---|
104 | * @param fullParameters |
---|
105 | * @return |
---|
106 | */ |
---|
107 | @NotNull |
---|
108 | private List<List<Parametre>> manageMenusForParameterList( @NotNull final List<Parametre> fullParameters ) |
---|
109 | { |
---|
110 | final List<List<Parametre>> parameterListWithMenu = new ArrayList<List<Parametre>>(); |
---|
111 | |
---|
112 | int i = 0; |
---|
113 | while( i < fullParameters.size() ) |
---|
114 | { |
---|
115 | final Parametre fullParameter = fullParameters.get( i ); |
---|
116 | final int firstIndex = fullParameters.indexOf( fullParameter ); |
---|
117 | final int lastIndex = fullParameters.lastIndexOf( fullParameter ); |
---|
118 | final List<Parametre> parameters = fullParameters.subList( firstIndex, lastIndex + 1 ); |
---|
119 | parameterListWithMenu.add( parameters ); |
---|
120 | i += parameters.size(); |
---|
121 | } |
---|
122 | |
---|
123 | return parameterListWithMenu; |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | @ControllerMethod(jsonResult = true, loginMandatory = true) |
---|
128 | public JSONObject downloadData( @ParamName(ParameterConstants.PARAMETER_DATE_BEGIN) final String dateBegin, |
---|
129 | @ParamName(ParameterConstants.PARAMETER_DATE_END) final String dateEnd, |
---|
130 | @ParamName(ParameterConstants.PARAMETER_PFID_PID_LIST) final String pIdPIdArrayString, |
---|
131 | @NotNull final Personne personne ) |
---|
132 | throws ServiceException, WebException |
---|
133 | { |
---|
134 | final JSONObject result = new JSONObject(); |
---|
135 | |
---|
136 | final List<Pair<String, String>> pfIdPIdList = EtherHelper.extractpfIdPIdListFromString( pIdPIdArrayString, String.class, String.class ); |
---|
137 | if( null == pfIdPIdList ) |
---|
138 | throw new WebException( WebException.WebCode.PLATEFORM_OR_PARAMETER_IS_NULL ); |
---|
139 | |
---|
140 | final DatasForm dataForm = createDataForm( dateBegin, dateEnd, pfIdPIdList ); |
---|
141 | |
---|
142 | final XMLOutputter outXml = new XMLOutputter(); |
---|
143 | final Document docXml = Requete.toXml( dataForm, personne ); |
---|
144 | final String requete = outXml.outputString( docXml ); |
---|
145 | try |
---|
146 | { |
---|
147 | final String encoded = "requete=" + URLEncoder.encode( requete, "UTF-8" ); |
---|
148 | // Configuration de l'URL |
---|
149 | final String urlCGIStr = (String) this.getServletContext().getAttribute( "APP_CGI" ); |
---|
150 | final URL urlCGI = new URL( urlCGIStr ); |
---|
151 | final URLConnection conn = urlCGI.openConnection(); |
---|
152 | conn.setDoOutput( true ); |
---|
153 | conn.setUseCaches( false ); |
---|
154 | conn.setRequestProperty( "content-type", "application/x-www-form-urlencoded" ); |
---|
155 | |
---|
156 | // Envoi de la requête |
---|
157 | final DataOutputStream out = new DataOutputStream( conn.getOutputStream() ); |
---|
158 | out.writeBytes( encoded ); |
---|
159 | out.flush(); |
---|
160 | out.close(); |
---|
161 | |
---|
162 | // Réponse du CGI |
---|
163 | final BufferedReader in = new BufferedReader( new InputStreamReader( conn.getInputStream() ) ); |
---|
164 | String aLine; |
---|
165 | while( ( aLine = in.readLine() ) != null ) |
---|
166 | { |
---|
167 | if( aLine.length() == 0 ) continue; |
---|
168 | if( aLine.startsWith( "1" ) ) |
---|
169 | { |
---|
170 | System.err.println( "ERROR :" + aLine ); |
---|
171 | } |
---|
172 | else System.out.println( "REPONSE :" + aLine ); |
---|
173 | } |
---|
174 | } |
---|
175 | catch( Exception e ) |
---|
176 | { |
---|
177 | result.put( ParameterConstants.PARAMETER_RESULT, ParameterConstants.PARAMETER_NOT_OK ); |
---|
178 | return result; |
---|
179 | } |
---|
180 | |
---|
181 | result.put( ParameterConstants.PARAMETER_RESULT, ParameterConstants.PARAMETER_OK ); |
---|
182 | return result; |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | * This method create a dataForms to simulate and use the struts actions "DataAccessAction" and "DataSubmitAction" |
---|
187 | * |
---|
188 | * @param dateBegin |
---|
189 | * @param dateEnd |
---|
190 | * @param pfIdPIdList |
---|
191 | * @return |
---|
192 | * @throws WebException |
---|
193 | */ |
---|
194 | @NotNull |
---|
195 | private DatasForm createDataForm( final String dateBegin, final String dateEnd, final List<Pair<String, String>> pfIdPIdList ) |
---|
196 | throws WebException |
---|
197 | { |
---|
198 | final DatasForm dataForm = new DatasForm(); |
---|
199 | |
---|
200 | // Plateforms and parameters |
---|
201 | final List<String> plateformIds = EtherHelper.getFirstValues( pfIdPIdList ); |
---|
202 | final List<String> parameterIds = EtherHelper.getSecondValues( pfIdPIdList ); |
---|
203 | dataForm.setSelectPlats( plateformIds.toArray( new String[plateformIds.size()] ) ); |
---|
204 | dataForm.setSelectParams( parameterIds.toArray( new String[parameterIds.size()] ) ); |
---|
205 | |
---|
206 | // Localisations |
---|
207 | final MesureAdapter mAdapter = new MesureAdapter(); |
---|
208 | mAdapter.loadLocsFromDatabase( dataForm ); |
---|
209 | |
---|
210 | // Dates |
---|
211 | final Calendar calendar = Calendar.getInstance(); |
---|
212 | String formatedDateBegin = null; |
---|
213 | String formatedDateEnd = null; |
---|
214 | if( null != dateBegin && null != dateEnd && !"false".equals( dateBegin ) && !"false".equals( dateEnd ) ) |
---|
215 | { |
---|
216 | try |
---|
217 | { |
---|
218 | calendar.setTimeInMillis( Long.valueOf( dateBegin ) ); |
---|
219 | formatedDateBegin = DateHelper.formatDate( calendar.getTime(), DateHelper.FRENCH_DATE_PATTERN ); |
---|
220 | calendar.setTimeInMillis( Long.valueOf( dateEnd ) ); |
---|
221 | formatedDateEnd = DateHelper.formatDate( calendar.getTime(), DateHelper.FRENCH_DATE_PATTERN ); |
---|
222 | dataForm.setDateDeb( formatedDateBegin ); |
---|
223 | dataForm.setDateFin( formatedDateEnd ); |
---|
224 | } |
---|
225 | catch( Exception e ) |
---|
226 | { |
---|
227 | throw new WebException( WebException.WebCode.INVALID_DATE, e ); |
---|
228 | } |
---|
229 | } |
---|
230 | else |
---|
231 | mAdapter.loadDatesFromDatabase( dataForm ); |
---|
232 | |
---|
233 | // Fill dataForm |
---|
234 | final ValeurAdapter vAdapter = new ValeurAdapter(); |
---|
235 | vAdapter.loadCountFromDatabase( dataForm ); |
---|
236 | dataForm.setListeCoords( dataForm.getLatMin() + "," + dataForm.getLatMax() + "," + dataForm.getLonMin() + "," + dataForm.getLonMax() ); |
---|
237 | dataForm.setListeDates( dataForm.getDateDeb() + "," + dataForm.getDateFin() ); |
---|
238 | dataForm.setOutput( FORMAT_DEFAULT ); |
---|
239 | dataForm.setCompression( COMPRESSION_DEFAULT ); |
---|
240 | |
---|
241 | return dataForm; |
---|
242 | } |
---|
243 | |
---|
244 | private List<JSONObject> getJSONAxeTypesForFixedPlateforms() |
---|
245 | { |
---|
246 | final AxeTypeForFixedPlateform[] axeTypes = AxeTypeForFixedPlateform.values(); |
---|
247 | |
---|
248 | final List<JSONObject> jsonAxeTypes = new ArrayList<JSONObject>( axeTypes.length ); |
---|
249 | |
---|
250 | for( final AxeTypeForFixedPlateform axeType : axeTypes ) |
---|
251 | { |
---|
252 | final JSONObject jsonAxeType = new JSONObject(); |
---|
253 | jsonAxeType.put( "text", axeType.name() ); |
---|
254 | jsonAxeType.put( "value", axeType.name() ); |
---|
255 | jsonAxeTypes.add( jsonAxeType ); |
---|
256 | } |
---|
257 | return jsonAxeTypes; |
---|
258 | } |
---|
259 | |
---|
260 | private List<JSONObject> getJSONAxeTypesForMobilePlateforms() |
---|
261 | { |
---|
262 | final AxeTypeForMobilePlateform[] axeTypes = AxeTypeForMobilePlateform.values(); |
---|
263 | |
---|
264 | final List<JSONObject> jsonAxeTypes = new ArrayList<JSONObject>( axeTypes.length ); |
---|
265 | |
---|
266 | for( final AxeTypeForMobilePlateform axeType : axeTypes ) |
---|
267 | { |
---|
268 | final JSONObject jsonAxeType = new JSONObject(); |
---|
269 | jsonAxeType.put( "text", axeType.name() ); |
---|
270 | jsonAxeType.put( "value", axeType.name() ); |
---|
271 | jsonAxeTypes.add( jsonAxeType ); |
---|
272 | } |
---|
273 | return jsonAxeTypes; |
---|
274 | } |
---|
275 | |
---|
276 | @Required |
---|
277 | public void setEtherService( @NotNull final EtherService etherService ) |
---|
278 | { |
---|
279 | _etherService = etherService; |
---|
280 | } |
---|
281 | |
---|
282 | private static final Log LOGGER = LogFactory.getLog( ControllerVisualization.class ); |
---|
283 | |
---|
284 | private static final String VIEW_WORK = "visualization/inWork"; |
---|
285 | private static final String VIEW_VISUALIZATION = "visualization/visu"; |
---|
286 | private static final String VIEW_VISUALIZATION_PARAMETER_BY_PLATEFORM = "visualization/visu_parameter_by_pf"; |
---|
287 | private static final String VIEW_DOWNLOAD_OK = "data/access/extract3"; |
---|
288 | |
---|
289 | private static final String FORMAT_DEFAULT = "NASA-AMES"; |
---|
290 | private static final String COMPRESSION_DEFAULT = "None"; |
---|
291 | private EtherService _etherService; |
---|
292 | } |
---|