]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-WS/src/org/splat/ws_server/service/salome/SimanSalomeServiceImpl.java
Salome HOME
8088c2d13741a4e7ac0272eab31b3c6e4dc46e60
[tools/siman.git] / Workspace / Siman-WS / src / org / splat / ws_server / service / salome / SimanSalomeServiceImpl.java
1 package org.splat.ws_server.service.salome;
2
3 import java.io.BufferedWriter;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.FileWriter;
8 import java.io.IOException;
9 import java.sql.SQLException;
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Scanner;
16
17 import javax.activation.DataHandler;
18 import javax.activation.FileDataSource;
19 import javax.servlet.ServletContext;
20
21 import org.apache.log4j.Logger;
22 import org.splat.kernel.InvalidPropertyException;
23 import org.splat.kernel.MismatchException;
24 import org.splat.kernel.MissedPropertyException;
25 import org.splat.kernel.MultiplyDefinedException;
26 import org.splat.kernel.NotApplicableException;
27 import org.splat.service.ScenarioService;
28 import org.splat.service.dto.DocumentDTO;
29 import org.splat.service.dto.FileDTO;
30 import org.splat.service.dto.StepDTO;
31 import org.splat.service.technical.ProjectSettingsService;
32 import org.splat.service.technical.RepositoryService;
33 import org.springframework.web.context.ServletContextAware;
34
35 public class SimanSalomeServiceImpl implements SimanSalomeService, ServletContextAware {
36         
37         /**
38          * The logger for the service.
39          */
40         protected final static Logger LOG = Logger
41                         .getLogger(SimanSalomeServiceImpl.class);
42
43         /**
44          * The Scenario Service.
45          */
46         private ScenarioService _scenarioService;
47         
48         /**
49          * The Repository Service.
50          */
51         private RepositoryService _repositoryService;
52         
53         /**
54          * The Project Settings Service.
55          */
56         private ProjectSettingsService _projectSettingsService;
57
58         /**
59          * The Servlet Context.
60          */
61         private ServletContext _servletContext;
62
63         /**
64          * 
65          * {@inheritDoc}
66          * @see org.splat.ws_server.service.salome.SimanSalomeService#createConfigFile(java.lang.Long, java.lang.Long, java.lang.Long)
67          */
68         @Override
69         public String createConfigFile(final Long studyId, final Long scenarioId,
70                         final Long userId) {
71                         
72                         try {
73                                 _projectSettingsService.configure(_servletContext.getRealPath("/conf/som.xml"));                        
74                         } catch (IOException ioException) {
75                                 LOG.debug("Sorry, IOException is throws", ioException);
76                         } catch (SQLException sqlException) {
77                                 LOG.debug("Sorry, impossible to populate the database", sqlException);
78                         }       
79                         
80                         if (LOG.isDebugEnabled()) {
81                                 LOG.debug("Scenario Id = " + scenarioId);
82                         }
83
84                         List<StepDTO> listSteps = _scenarioService.getScenarioInfo(scenarioId);
85                         
86                         File configFilePath = new File(_repositoryService.getDownloadDirectory(userId).getPath());
87                         configFilePath.mkdirs();
88                         File configFile = new File(configFilePath + "/siman-salome.conf");
89                         
90                         //build the result file.
91                 FileWriter fstream;
92                 try {
93                       fstream = new FileWriter(configFile);
94                       BufferedWriter out = new BufferedWriter(fstream);  
95                       
96                       out.write("Study ID : " + studyId + "\n");
97                       out.write("Scenario ID : " + scenarioId + "\n");
98                       out.write("User ID : " + userId + "\n");
99                       out.write("\n");
100                       
101                       Iterator<StepDTO> stepIter = listSteps.iterator();
102                       Iterator<DocumentDTO> docIter;
103                       Iterator<FileDTO> fileIter;
104                       
105                       StepDTO stepValue;
106                       DocumentDTO docValue;
107                       FileDTO fileValue;
108                       char fileState;
109                       
110                       for ( ; stepIter.hasNext(); ) {
111                           stepValue = stepIter.next();
112                           out.write("Activity : " + stepValue.getKey() + "\n");
113                           out.write("Activity ID : " + stepValue.getNumber() + "\n");
114                           out.write("SALOME module : " + stepValue.getModule() + "\n");
115                           
116                           docIter = stepValue.getDocs().iterator();
117                           
118                           for ( ; docIter.hasNext(); ) {
119                                   docValue = docIter.next();
120                                   out.write("    Document : " + docValue.getTitle() + "\n");
121                                   out.write("    Document ID: " + docValue.getId() + "\n");
122                                   
123                                   fileIter = docValue.getFiles().iterator();
124                                   
125                                   for ( ; fileIter.hasNext(); ) {
126                                           fileValue = fileIter.next();
127                                           if (fileValue.isResult() == false) {
128                                                   out.write("        Source file : " + fileValue.getPath() + "\n");
129                                           } else {
130                                                   out.write("        Result file : " + fileValue.getPath() + "\n");
131                                           }
132                                           
133                                           out.write("        File ID : " + fileValue.getId() + "\n");
134                                           
135                                           out.write("            Automatic processing : " + fileValue.getProcessing() + "\n");
136                                           fileState = fileValue.getState();
137                                           if ((fileState == 'Y') || (fileState == 'N')) {
138                                                   out.write("            State : file-actual\n");
139                                           } else if (fileState == 'O') {
140                                                   out.write("            State : file-outdated\n");
141                                           }
142                                   }
143                           }
144                           
145                           out.write("\n");
146                       }
147                       
148                       out.close();
149                     } catch (IOException e) {
150                           // TODO Auto-generated catch block
151                           e.printStackTrace();
152                     }
153                 
154                 return configFile.getAbsolutePath();
155         }
156         
157
158         /**
159          * 
160          * {@inheritDoc}
161          * @see org.splat.ws_server.service.salome.SimanSalomeService#getFile(java.lang.String)
162          */
163         @Override
164         public DataHandler getFile(final String fileURL) {
165                 FileDataSource dataSource = new FileDataSource(fileURL);
166                 DataHandler fileDataHandler = new DataHandler(dataSource);
167                 return fileDataHandler;
168         }
169         
170         /**
171          * 
172          * {@inheritDoc}
173          * @see org.splat.ws_server.service.salome.SimanSalomeService#putFile(javax.activation.DataHandler, java.io.File)
174          */
175         @Override
176         public String /*DataHandler*/ putFile(final DataHandler dataHandler, final String fileName, final Long userId) {
177                 
178                 //will be removed in future
179                 try {
180                         _projectSettingsService.configure(_servletContext.getRealPath("/conf/som.xml"));
181
182                 } catch (IOException ioException) {
183                         LOG.debug("Sorry, IOException is throws", ioException);
184                 } catch (SQLException sqlException) {
185                         LOG.debug("Sorry, impossible to populate the database", sqlException);
186                 }       
187                 
188                 File pathToDownloadDirectory = new File(_repositoryService.getDownloadDirectory(userId).getPath());
189                 File vaultURLFile = new File(pathToDownloadDirectory + "/" + fileName);
190                 
191                 pathToDownloadDirectory.mkdirs();
192                 
193                 try {
194                         vaultURLFile.createNewFile();
195                 } catch (IOException e) {
196                         // TODO Auto-generated catch block
197                         e.printStackTrace();
198                 }
199         
200                 if (dataHandler != null) {
201                         try {
202                                 
203                                 FileOutputStream outputStream = new FileOutputStream(vaultURLFile);
204                                 dataHandler.writeTo(outputStream);
205                         
206                                 outputStream.flush();
207                                 outputStream.close();
208                         
209                         } catch (IOException e) {
210                                 // TODO Auto-generated catch block
211                                 e.printStackTrace();
212                         }
213                 }
214                 
215                 return vaultURLFile.getPath();
216                 /*FileDataSource fileDataSource = new FileDataSource(vaultURL);
217                 DataHandler fileDataHandler = new DataHandler(fileDataSource);
218                 return fileDataHandler;*/               
219                 
220         }
221         
222         
223         /**
224          * 
225          * {@inheritDoc}
226          * @see org.splat.ws_server.service.salome.SimanSalomeService#checkIn()
227          */
228         @Override
229         public String checkIn(final String fileURL, final Long scenarioId, final Long userId) {
230                                 
231                 File configFile = new File(fileURL);
232                 
233                 //parse the config file and set properties to the DTO.  
234                 
235                 Map<Integer, StepDTO> numberStepMap = new HashMap<Integer, StepDTO>();
236                 FileDTO file;
237                 DocumentDTO document;
238                 StepDTO step;
239                 
240                 if (LOG.isDebugEnabled()) {
241                         LOG.debug("The fileURL = " + fileURL);
242                 }
243
244                 try {
245                         Scanner input = new Scanner(configFile);
246                         
247                         while(input.hasNext()) {
248                                 
249                             String nextLine = input.nextLine();
250                             String[] tokens = nextLine.split(",");
251                             int activityNumber = Integer.valueOf(tokens[2]);
252                             
253                             if (numberStepMap.containsKey(activityNumber)) {
254                                 step = numberStepMap.get(activityNumber);
255                                 
256                                 file = new FileDTO(tokens[4]);   
257                                     document = new DocumentDTO();
258                                     document.addFile(file);
259                                     document.setId(Long.valueOf(tokens[3]));
260                                     step.addDoc(document);
261                                 
262                             } else {
263                             
264                                     file = new FileDTO(tokens[4]);       
265                                     document = new DocumentDTO();
266                                     document.addFile(file);
267                                     document.setId(Long.valueOf(tokens[3]));
268                                     step = new StepDTO();
269                                     step.addDoc(document);
270                                     step.setNumber(Integer.valueOf(tokens[2]));
271                                     
272                                     numberStepMap.put(activityNumber, step);
273                             }
274                         }
275                 
276                         input.close();
277                         
278                         List<StepDTO> listSteps = new ArrayList<StepDTO>(numberStepMap.values());
279                 
280                         try {
281                                 _scenarioService.checkin(scenarioId, userId, listSteps);
282                         } catch (InvalidPropertyException e) {
283                                 e.printStackTrace();
284                         } catch (MissedPropertyException e) {
285                                 e.printStackTrace();
286                         } catch (MultiplyDefinedException e) {
287                                 e.printStackTrace();
288                         } catch (MismatchException e) {
289                                 e.printStackTrace();
290                         } catch (NotApplicableException e) {
291                                 e.printStackTrace();
292                         } catch (IOException e) {
293                                 e.printStackTrace();
294                         }
295                         
296                         numberStepMap.clear();
297                         
298                         //delete siman-salome.conf and salome-siman.conf files
299                         configFile.delete();
300                         fileURL.replace("salome-siman.conf", "siman-salome.conf");
301                         File configFile2 = new File(fileURL);
302                         configFile2.delete();
303                                         
304                         return "SUCCESS";
305                         
306                 } catch (FileNotFoundException e) {
307                         // TODO Auto-generated catch block
308                         e.printStackTrace();
309                         return "ERROR";
310                 }
311         }
312
313         /**
314          * Get the scenarioService.
315          * @return the scenarioService
316          */
317         public ScenarioService getScenarioService() {
318                 return _scenarioService;
319         }
320
321         /**
322          * Set the scenarioService.
323          * @param scenarioService the scenarioService to set
324          */
325         public void setScenarioService(final ScenarioService scenarioService) {
326                 _scenarioService = scenarioService;
327         }
328
329
330         /**
331          * Get the repositoryService.
332          * @return the repositoryService
333          */
334         public RepositoryService getRepositoryService() {
335                 return _repositoryService;
336         }
337
338
339         /**
340          * Set the repositoryService.
341          * @param repositoryService the repositoryService to set
342          */
343         public void setRepositoryService(final RepositoryService repositoryService) {
344                 _repositoryService = repositoryService;
345         }
346
347
348         /**
349          * Get the projectSettingsService.
350          * @return the projectSettingsService
351          */
352         public ProjectSettingsService getProjectSettingsService() {
353                 return _projectSettingsService;
354         }
355
356
357         /**
358          * Set the projectSettingsService.
359          * @param projectSettingsService the projectSettingsService to set
360          */
361         public void setProjectSettingsService(
362                         final ProjectSettingsService projectSettingsService) {
363                 _projectSettingsService = projectSettingsService;
364         }
365
366
367         @Override
368         public void setServletContext(final ServletContext ctx) {
369                 _servletContext = ctx;
370         }
371         
372         
373 }