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.annotation.UseJSON; |
---|
7 | |
---|
8 | import com.ether.tapas.FileRequest; |
---|
9 | import com.ether.tapas.Observatory; |
---|
10 | import com.ether.tapas.Tapas; |
---|
11 | import com.ether.user.User; |
---|
12 | import net.sf.json.JSONObject; |
---|
13 | import org.apache.commons.logging.Log; |
---|
14 | import org.apache.commons.logging.LogFactory; |
---|
15 | import org.jetbrains.annotations.NotNull; |
---|
16 | //import org.springframework.web.bind.annotation.RequestMapping; |
---|
17 | //import org.springframework.web.bind.annotation.RequestMethod; |
---|
18 | |
---|
19 | import java.io.File; |
---|
20 | import java.io.FileInputStream; |
---|
21 | import java.io.FileNotFoundException; |
---|
22 | import java.io.IOException; |
---|
23 | import java.io.InputStream; |
---|
24 | import java.util.ArrayList; |
---|
25 | import java.util.List; |
---|
26 | import java.util.Properties; |
---|
27 | |
---|
28 | import java.io.BufferedOutputStream; |
---|
29 | import java.io.File; |
---|
30 | import java.io.FileOutputStream; |
---|
31 | |
---|
32 | //import org.springframework.web.bind.annotation.RequestMapping; |
---|
33 | //import org.springframework.web.bind.annotation.RequestMethod; |
---|
34 | //import org.springframework.web.bind.*; |
---|
35 | |
---|
36 | |
---|
37 | //import org.springframework.web.bind.annotation.ResponseBody; |
---|
38 | //import org.springframework.web.bind.annotation.RequestMapping; |
---|
39 | //import org.springframework.web.bind.annotation.RequestMethod; |
---|
40 | //import org.springframework.web.bind.annotation.RequestParam; |
---|
41 | //import org.springframework.web.multipart.MultipartFile; |
---|
42 | |
---|
43 | /** |
---|
44 | * @author vmipsl |
---|
45 | * @date 02 march 2012 |
---|
46 | */ |
---|
47 | public class Controller |
---|
48 | extends ControllerEther |
---|
49 | { |
---|
50 | |
---|
51 | /** *********************************************************** **/ |
---|
52 | /** *********************** VIEWS ***************************** **/ |
---|
53 | /** *********************************************************** **/ |
---|
54 | /** |
---|
55 | * View for the form |
---|
56 | * |
---|
57 | * @return |
---|
58 | * @throws WebException |
---|
59 | */ |
---|
60 | @ControllerMethod(view = VIEW_FORM_TAPAS, loginMandatory = true, defaultView = VIEW_INDEX) |
---|
61 | public JSONObject viewForm() |
---|
62 | throws WebException |
---|
63 | { |
---|
64 | try |
---|
65 | { |
---|
66 | final List<Observatory> allObservatories = getTapasService().getAllObservatories(); |
---|
67 | |
---|
68 | final JSONObject jsonObject = new JSONObject(); |
---|
69 | jsonObject.put( "jSonFileFormats", getJSONFileFormat() ); |
---|
70 | jsonObject.put( "jSonYesNos", getJSONYesNo() ); |
---|
71 | jsonObject.put( "jSonObservatories", allObservatories ); |
---|
72 | jsonObject.put( "jSonSpectralUnits", getJSONSpectralUnit() ); |
---|
73 | jsonObject.put( "jSonInstrumentalFunctions", getJSONInstrumentalFunction() ); |
---|
74 | jsonObject.put( "jSonClimatoReferences", getJSONClimatoReference() ); |
---|
75 | return jsonObject; |
---|
76 | } |
---|
77 | catch( ServiceException e ) |
---|
78 | { |
---|
79 | throw new WebException( WebException.WebCode.ERROR_NO_OBSERVATORY_FOUND, e ); |
---|
80 | } |
---|
81 | |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | /** *********************************************************** **/ |
---|
86 | /** *********************** CALLS ***************************** **/ |
---|
87 | /** *********************************************************** **/ |
---|
88 | /** |
---|
89 | * This method create a XML file filled with the javaTapas fields |
---|
90 | * |
---|
91 | * @param javaTapas |
---|
92 | * @param user |
---|
93 | * @return |
---|
94 | * @throws WebException |
---|
95 | */ |
---|
96 | @ControllerMethod(jsonResult = true, userMandatory = true, defaultView = VIEW_INDEX) |
---|
97 | //@ControllerMethod(jsonResult = true, userMandatory = false, defaultView = VIEW_INDEX) |
---|
98 | public JSONObject createUserRequest( @ParamName("jsonTapas") @Mandatory @UseJSON final Tapas javaTapas, @NotNull final User user ) |
---|
99 | throws WebException |
---|
100 | { |
---|
101 | |
---|
102 | String test="12444"; |
---|
103 | |
---|
104 | if( null == javaTapas ) |
---|
105 | throw new WebException( WebException.WebCode.ERROR_JSON_TAPAS_IS_NULL, "Serialization error : jsonTapas is null", WebException.getExceptionThrowable() ); |
---|
106 | |
---|
107 | final Properties prop = new Properties(); |
---|
108 | final File propertiesFile = new File( WebHelper.PROPERTIES_FILE_PATH + WebHelper.PROPERTIES_FILE ); |
---|
109 | try |
---|
110 | { |
---|
111 | final InputStream is = new FileInputStream( propertiesFile ); |
---|
112 | prop.load( is ); |
---|
113 | } |
---|
114 | catch( FileNotFoundException e ) |
---|
115 | { |
---|
116 | throw new WebException( ServiceException.ServiceCode.PROPERTIES_FILE_NOT_FOUND, e ); |
---|
117 | } |
---|
118 | catch( IOException e ) |
---|
119 | { |
---|
120 | throw new WebException( ServiceException.ServiceCode.PROPERTIES_FILE_NOT_FOUND, e ); |
---|
121 | } |
---|
122 | |
---|
123 | // creation fichier requete xml |
---|
124 | try |
---|
125 | { |
---|
126 | javaTapas.setId( test ); |
---|
127 | getTapasService().createXMLRequest( javaTapas, prop ); |
---|
128 | } |
---|
129 | catch( ServiceException e ) |
---|
130 | { |
---|
131 | throw new WebException( WebException.WebCode.ERROR_XML_CREATION, e ); |
---|
132 | } |
---|
133 | |
---|
134 | // creation lien ftp |
---|
135 | try |
---|
136 | { |
---|
137 | getTapasService().createUserFtpDir( user, prop ); |
---|
138 | } |
---|
139 | catch( ServiceException e ) |
---|
140 | { |
---|
141 | throw new WebException( WebException.WebCode.ERROR_USERS_CREATION, e ); |
---|
142 | } |
---|
143 | |
---|
144 | //insertion requete dans bdd |
---|
145 | try |
---|
146 | { |
---|
147 | getTapasService().insertTapasRequest( user ); |
---|
148 | } |
---|
149 | catch( ServiceException e ) |
---|
150 | { |
---|
151 | throw new WebException( WebException.WebCode.ERROR_INSERT_TAPAS_REQUEST, e ); |
---|
152 | } |
---|
153 | |
---|
154 | //execution du qsub |
---|
155 | try |
---|
156 | { |
---|
157 | getTapasService().execAppelQSub( javaTapas.getId(), user, prop ); |
---|
158 | } |
---|
159 | catch( ServiceException e ) |
---|
160 | { |
---|
161 | throw new WebException( WebException.WebCode.ERROR_CALL_QSUB, e ); |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | final JSONObject result = new JSONObject(); |
---|
166 | result.put( "result", "Your request will be processed. You will receive the result link by email" ); |
---|
167 | return result; |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | |
---|
172 | private List<JSONObject> getJSONFileFormat() |
---|
173 | { |
---|
174 | final FileFormat[] fileFormats = FileFormat.values(); |
---|
175 | |
---|
176 | final List<JSONObject> jsonFileFormats = new ArrayList<JSONObject>( fileFormats.length ); |
---|
177 | |
---|
178 | for( final FileFormat fileFormat : fileFormats ) |
---|
179 | { |
---|
180 | final JSONObject jsonFileFormat = new JSONObject(); |
---|
181 | jsonFileFormat.put( "text", fileFormat.name() ); |
---|
182 | jsonFileFormat.put( "value", fileFormat.name() ); |
---|
183 | jsonFileFormats.add( jsonFileFormat ); |
---|
184 | } |
---|
185 | return jsonFileFormats; |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | |
---|
190 | private List<JSONObject> getJSONYesNo() |
---|
191 | { |
---|
192 | final YesNo[] yesNos = YesNo.values(); |
---|
193 | |
---|
194 | final List<JSONObject> jsonYesNos = new ArrayList<JSONObject>( yesNos.length ); |
---|
195 | |
---|
196 | for( final YesNo yesNo : yesNos ) |
---|
197 | { |
---|
198 | final JSONObject jsonYesNo = new JSONObject(); |
---|
199 | jsonYesNo.put( "text", yesNo.name() ); |
---|
200 | jsonYesNo.put( "value", yesNo.name() ); |
---|
201 | jsonYesNos.add( jsonYesNo ); |
---|
202 | } |
---|
203 | return jsonYesNos; |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | |
---|
208 | private List<JSONObject> getJSONSpectralUnit() |
---|
209 | { |
---|
210 | final SpectralUnit[] spectralUnits = SpectralUnit.values(); |
---|
211 | |
---|
212 | final List<JSONObject> jsonSpectralUnits = new ArrayList<JSONObject>( spectralUnits.length ); |
---|
213 | |
---|
214 | for( final SpectralUnit spectralUnit : spectralUnits ) |
---|
215 | { |
---|
216 | final JSONObject jsonSpectralUnit = new JSONObject(); |
---|
217 | jsonSpectralUnit.put( "text", spectralUnit.name() ); |
---|
218 | jsonSpectralUnit.put( "value", spectralUnit.name() ); |
---|
219 | jsonSpectralUnits.add( jsonSpectralUnit ); |
---|
220 | } |
---|
221 | return jsonSpectralUnits; |
---|
222 | } |
---|
223 | |
---|
224 | |
---|
225 | private List<JSONObject> getJSONInstrumentalFunction() |
---|
226 | { |
---|
227 | final InstrumentalFunction[] instrumentalFunctions = InstrumentalFunction.values(); |
---|
228 | |
---|
229 | final List<JSONObject> jsonInstrumentalFunctions = new ArrayList<JSONObject>( instrumentalFunctions.length ); |
---|
230 | final JSONObject jsonInstrumentalFunctionNone = new JSONObject(); |
---|
231 | jsonInstrumentalFunctionNone.put( "text", InstrumentalFunction.NONE ); |
---|
232 | jsonInstrumentalFunctionNone.put( "value", -1 ); |
---|
233 | jsonInstrumentalFunctions.add( jsonInstrumentalFunctionNone ); |
---|
234 | |
---|
235 | final JSONObject jsonInstrumentalFunctionGaussian = new JSONObject(); |
---|
236 | jsonInstrumentalFunctionGaussian.put( "text", InstrumentalFunction.GAUSSIAN ); |
---|
237 | jsonInstrumentalFunctionGaussian.put( "value", 1 ); |
---|
238 | jsonInstrumentalFunctions.add( jsonInstrumentalFunctionGaussian ); |
---|
239 | |
---|
240 | return jsonInstrumentalFunctions; |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | private List<JSONObject> getJSONClimatoReference() |
---|
245 | { |
---|
246 | final ClimatoReference[] climatoReferences = ClimatoReference.values(); |
---|
247 | |
---|
248 | final List<JSONObject> jsonClimatoReferences = new ArrayList<JSONObject>( climatoReferences.length ); |
---|
249 | |
---|
250 | for( final ClimatoReference climatoReference : climatoReferences ) |
---|
251 | { |
---|
252 | final JSONObject jsonClimatoReference = new JSONObject(); |
---|
253 | jsonClimatoReference.put( "text", climatoReference.name() ); |
---|
254 | jsonClimatoReference.put( "value", climatoReference.ordinal() ); |
---|
255 | jsonClimatoReferences.add( jsonClimatoReference ); |
---|
256 | } |
---|
257 | return jsonClimatoReferences; |
---|
258 | } |
---|
259 | |
---|
260 | |
---|
261 | private static final Log LOGGER = LogFactory.getLog( Controller.class ); |
---|
262 | |
---|
263 | private static final String VIEW_INDEX = "init_en"; |
---|
264 | private static final String VIEW_FORM_TAPAS = "project/formTapas"; |
---|
265 | private static final String VIEW_UPLOAD = "project/upload"; |
---|
266 | } |
---|