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