source: geisa/web/src/com/ether/Controller.java @ 806

Last change on this file since 806 was 806, checked in by npipsl, 9 years ago

changement annee

File size: 13.7 KB
Line 
1package com.ether;
2
3import com.ether.annotation.ControllerMethod;
4import com.ether.annotation.ParamName;
5import net.sf.json.JSONObject;
6import org.apache.commons.logging.Log;
7import org.apache.commons.logging.LogFactory;
8import org.jetbrains.annotations.NotNull;
9import org.springframework.beans.factory.annotation.Required;
10
11import java.io.File;
12import java.io.FilenameFilter;
13import java.io.IOException;
14import java.lang.reflect.Array;
15import java.util.Arrays;
16import java.util.Calendar;
17import java.util.Collections;
18import java.util.HashMap;
19import java.util.List;
20import java.util.Map;
21import java.util.Set;
22
23/**
24 * @author vmipsl
25 * @date 17 feb 2011
26 */
27public class Controller
28        extends ControllerEther
29{
30
31    /** *********************************************************** **/
32    /** *********************** VIEWS ***************************** **/
33    /**
34     * ********************************************************** *
35     */
36    @ControllerMethod(view = VIEW_INIT)
37    public Map<String, Object> home()
38            throws WebException
39    {
40        return new HashMap<String, Object>();
41    }
42
43    @ControllerMethod(view = VIEW_INIT2)
44    public Map<String, Object> geisaMethod()
45            throws WebException
46    {
47        return new HashMap<String, Object>();
48    }
49    /** *********************************************************** **/
50    /** *********************** CALLS ***************************** **/
51    /**
52     * ********************************************************** *
53     */
54
55    @ControllerMethod(jsonResult = true)
56    //public JSONObject createUserRequest( @ParamName("fortranRequest") final String fortranRequest,  @ParamName("nbGraph") @NotNull final String nbGraph)
57    //public JSONObject createUserRequest( @ParamName("fortranRequest") final String fortranRequest, @ParamName("transitionUpper") final String transitionUpper, @ParamName("transitionLower") final String transitionLower, @ParamName("nbGraph") @NotNull final String nbGraph)
58    public JSONObject createUserRequest( @ParamName("fortranRequest") final String fortranRequest,
59                                         @ParamName("transitionUpper") final String transitionUpper, @ParamName("transitionLower") final String transitionLower,
60                                         @ParamName("nbGraph") @NotNull final String nbGraph, @ParamName("nomFichier") final String nomFichier,
61                                         @ParamName("NomRepFileOut") final String NomRepFileOut)
62            throws WebException
63    {
64
65        final JSONObject result = new JSONObject();
66
67        //variable temps en milliseconde pour donner un nom unique au fichier crée
68        //final Calendar calendar = Calendar.getInstance();
69        //final long timeInMillis = calendar.getTimeInMillis();
70
71        try {
72            //Pour lancer le programme shell qui lance lr programme fortran
73
74            //création d'un répertoire par requete
75            final Process q = Runtime.getRuntime().exec("mkdir "+ LONG_PATH_IMAGES+NomRepFileOut);
76
77            // Process p = Runtime.getRuntime().exec(PATH_PROCESS+"graph.sh  " + fortranRequest + " " + transitionUpper  + " " + transitionLower +" " + timeInMillis + "");
78            final Process p = Runtime.getRuntime().exec(PATH_PROCESS+"graph.sh  " + fortranRequest + " " + transitionUpper  + " " + transitionLower + " " + nomFichier + " " + PATH_PROCESS+ " " + LONG_PATH_IMAGES+NomRepFileOut +"");
79
80            //permet de connaitre le nom du dernier fichier creer (en cas de plusieurs graphs à tracer) pour tester si le dernier fichier est bien present
81            //final String lastFileName = "graph" + timeInMillis +"-"+nbGraph+ ".png";
82            //final String lastFileName = nomFichier +"-"+nbGraph+ ".png";
83            final String lastFileName = "endFile";
84            final File lastFile = new java.io.File(LONG_PATH_IMAGES+NomRepFileOut+lastFileName);
85            final Integer maxTimeWait = 80000;
86
87
88            //si le fichier n'exite pas  et le temps d'exécution n'est pas trop long  (inferieur a   maxTimeWait)
89           Integer timeWait = 0;
90           while (!lastFile .exists()  && timeWait < maxTimeWait ){
91               try
92                {
93                    Thread.sleep(1000);
94                    timeWait+=1000;
95
96                }
97                catch( InterruptedException e )
98                {
99                    throw new WebException(WebException.WebCode.ERROR_EXECUTION_PROCESS, e);
100                }
101            }
102
103//            //filtre pour trouver le dernier fichier crée
104//            final FilenameFilter lastFileNameFilter = new FilenameFilter() {
105//
106//                public boolean accept( final File arg0, final String arg1) {
107//                    return arg1.endsWith(nbGraph+ ".png");
108//                }
109//            };
110//
111//            //(si le fichier n'exite pas ou si il existe mais la taille est nulle) et le temps d'exécution n'est pas trop long  (inferieur a   maxTimeWait)
112//            Integer timeWait = 0;
113//            while (( (new File(LONG_PATH_IMAGES+NomRepFileOut) != null && new File(LONG_PATH_IMAGES+NomRepFileOut).listFiles( lastFileNameFilter) != null && new File(LONG_PATH_IMAGES+NomRepFileOut).listFiles( lastFileNameFilter).length == 0) ) && timeWait < maxTimeWait ){
114//                try
115//                {
116//                    Thread.sleep(1000);
117//                    timeWait+=1000;
118//
119 //               }
120 //               catch( InterruptedException e )
121 //               {
122 //                   throw new WebException(WebException.WebCode.ERROR_EXECUTION_PROCESS, e);
123 //               }
124 //           }
125
126           //(si le fichier n'exite pas ou si il existe mais la taille est nulle) et le temps d'exécution n'est pas trop long  (inferieur a   maxTimeWait)
127 //           Integer timeWait = 0;
128 //           while ((!lastFile .exists() || (lastFile .exists()  && lastFile.length()==0)) && timeWait < maxTimeWait ){
129 //               try
130 //                {
131 //                    Thread.sleep(1000);
132 //                    timeWait+=1000;
133
134  //               }
135  //               catch( InterruptedException e )
136  //               {
137  //                   throw new WebException(WebException.WebCode.ERROR_EXECUTION_PROCESS, e);
138  //               }
139  //           }
140
141            //filtre pour trouver tous les fichier .png dans un répertoire
142            final FilenameFilter filenameFilter = new FilenameFilter() {
143
144                public boolean accept( final File arg0, final String arg1) {
145                    return arg1.endsWith(".png");
146                }
147            };
148
149
150            final String[] filteredFileList = new File(LONG_PATH_IMAGES+NomRepFileOut).list(filenameFilter);
151            final List<String> listeFichiers = Arrays.asList(filteredFileList);
152            //pour trier la liste des fichiers par ordre croissant
153            Collections.sort( listeFichiers );
154            result.put("fileList",  listeFichiers);
155
156            // TODO :test si les fichiers existent
157
158            //creation d'un tar des fichiers images crees
159            // final Process r = Runtime.getRuntime().exec("tar -cf "+LONG_PATH_IMAGES+"graph"+timeInMillis+"/image/GEISA.tar "+LONG_PATH_IMAGES+"graph"+timeInMillis+"/image/");
160
161
162            // TODO : vérifier que le temps n'est pas écoulé et si c'est le cas retourner une erreur
163            //  if(timeWait == maxTimeWait)
164
165
166        }
167        catch( IOException e )
168        {
169            throw new WebException( WebException.WebCode.PROBLEM_WITH_RUNTIME, e );
170        }
171
172        return result;
173    }
174
175//    @ControllerMethod(jsonResult = true)
176//    public JSONObject transition( @ParamName("transitionValue") final String transitionValue)
177//            throws WebException
178//    {
179//        final JSONObject result = new JSONObject();
180//        try
181//        {
182//            final List<IsotopeG03> allIsotopeG03 = _geisaService.getAllIsotopeG03();
183//            final String bob = "bib";
184//        }
185//        catch( ServiceException e )
186//        {
187//            throw new WebException( WebException.WebCode.ISOTOPE_NOT_FOUND, e );
188//        }
189//        return  result;
190//    }
191
192    @ControllerMethod(jsonResult = true)
193    public JSONObject getTransitionByIsotopeName( @ParamName("isotopesSelectedNameArray") final String isotopesSelectedName,
194                                                  @ParamName("geisaSelectedDatabase") final String geisaSelectedDatabase,
195                                                  @ParamName("spectralRangeLower") final Float spectralRangeLower,
196                                                  @ParamName("spectralRangeUpper") final Float spectralRangeUpper)
197            throws WebException
198    {
199        final JSONObject result = new JSONObject();
200        try
201        {
202            final List<String> isotopesSelectedNameList = Arrays.asList( isotopesSelectedName.split( "," ) );
203//            final List<IsotopeG03> isotopeG03 = _geisaService.getTransitionsByIsotopeG03Name( isotopesSelectedNameList, spectralRangeLower, spectralRangeUpper );
204            final Set<String> transitionsLower = _geisaService.getTransitionsLower( geisaSelectedDatabase, isotopesSelectedNameList, spectralRangeLower, spectralRangeUpper );
205            final Set<String> transitionsUpper = _geisaService.getTransitionsUpper( geisaSelectedDatabase, isotopesSelectedNameList, spectralRangeLower, spectralRangeUpper );
206            result.put( "transitionsLower", transitionsLower );
207            result.put( "transitionsUpper", transitionsUpper );
208        }
209        catch( ServiceException e )
210        {
211            throw new WebException( WebException.WebCode.ISOTOPE_NOT_FOUND, e );
212        }
213        return  result;
214    }
215
216    @ControllerMethod(jsonResult = true)
217    public JSONObject getTransitionByIsotopeNameByTransitionLower( @ParamName("isotopesSelectedNameArray") final String isotopesSelectedName,
218                                                                   @ParamName("geisaSelectedDatabase") final String geisaSelectedDatabase,
219                                                                   @ParamName("spectralRangeLower") final Float spectralRangeLower,
220                                                                   @ParamName("spectralRangeUpper") final Float spectralRangeUpper,
221                                                                   @ParamName("transitionLower") final String transitionLower)
222            throws WebException
223    {
224        final JSONObject result = new JSONObject();
225        try
226        {
227            //le passage de javascript à java par ajax transforme les + en espace donc (dans initGeisa-script.jsp) transformation des + en plus et dans le controller on fait l'inverse
228            final String replacedTransitionLower = transitionLower.replaceAll( "plus", "+" );
229            final List<String> isotopesSelectedNameList = Arrays.asList( isotopesSelectedName.split( "," ) );
230            final Set<String> transitionsUpperBis = _geisaService.getTransitionsUpperByTransitionLower( geisaSelectedDatabase, isotopesSelectedNameList, spectralRangeLower, spectralRangeUpper, replacedTransitionLower );
231            result.put( "transitionsUpperBis", transitionsUpperBis );
232        }
233        catch( ServiceException e )
234        {
235            throw new WebException( WebException.WebCode.ISOTOPE_NOT_FOUND, e );
236        }
237        return  result;
238    }
239
240
241    @ControllerMethod(jsonResult = true)
242    public JSONObject getTransitionByIsotopeNameByTransitionUpper( @ParamName("isotopesSelectedNameArray") final String isotopesSelectedName,
243                                                                   @ParamName("geisaSelectedDatabase") final String geisaSelectedDatabase,
244                                                                   @ParamName("spectralRangeLower") final Float spectralRangeLower,
245                                                                   @ParamName("spectralRangeUpper") final Float spectralRangeUpper,
246                                                                   @ParamName("transitionUpper") final String transitionUpper)
247            throws WebException
248    {
249        final JSONObject result = new JSONObject();
250        try
251        {
252            final String replacedTransitionUpper = transitionUpper.replaceAll( "plus", "+" );
253            final List<String> isotopesSelectedNameList = Arrays.asList( isotopesSelectedName.split( "," ) );
254            final Set<String> transitionsLowerBis = _geisaService.getTransitionsUpperByTransitionUpper( geisaSelectedDatabase, isotopesSelectedNameList, spectralRangeLower, spectralRangeUpper, replacedTransitionUpper );
255            result.put( "transitionsLowerBis", transitionsLowerBis );
256        }
257        catch( ServiceException e )
258        {
259            throw new WebException( WebException.WebCode.ISOTOPE_NOT_FOUND, e );
260        }
261        return  result;
262    }
263
264
265    @ControllerMethod(downloadFile = "")
266    public void downloadFile( @ParamName("fileName") final String fileName, @ParamName("fileNameToDisplay") final String fileNameToDisplay )
267    {
268    }
269
270    @Required
271    public void setGeisaService( final GeisaService geisaService )
272    {
273        _geisaService = geisaService;
274    }
275
276    private static final Log LOGGER = LogFactory.getLog( Controller.class );
277
278    public static final String PATH_IMAGES = "resources/images/graphGeisa/";
279    //local
280    public static final String LONG_PATH_IMAGES = "/home_local/npipsl/workspaces/GEISA/web/resources/images/graphGeisa/";
281    //  naboo
282    // public static final String LONG_PATH_IMAGES = "/usr/local/apache-tomcat/webapps/geisa/resources/images/graphGeisa/";
283
284
285    //local
286      public static final String PATH_PROCESS = "/home/npipsl/Desktop/geisa-Graphique/prog-ether/";
287    //naboo et machine opérationnelle
288    //public static final String PATH_PROCESS = "/home/www/GEISA/";
289    private static final String VIEW_INIT = "init";
290    private static final String VIEW_INIT2 = "initGeisa";
291
292    private GeisaService _geisaService;
293}
Note: See TracBrowser for help on using the repository browser.