1 package org.splat.ws_server.service.salome;
3 import java.io.BufferedWriter;
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;
15 import java.util.Scanner;
17 import javax.activation.DataHandler;
18 import javax.activation.FileDataSource;
19 import javax.servlet.ServletContext;
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;
35 public class SimanSalomeServiceImpl implements SimanSalomeService, ServletContextAware {
38 * The logger for the service.
40 protected final static Logger LOG = Logger
41 .getLogger(SimanSalomeServiceImpl.class);
44 * The Scenario Service.
46 private ScenarioService _scenarioService;
49 * The Repository Service.
51 private RepositoryService _repositoryService;
54 * The Project Settings Service.
56 private ProjectSettingsService _projectSettingsService;
59 * The Servlet Context.
61 private ServletContext _servletContext;
66 * @see org.splat.ws_server.service.salome.SimanSalomeService#createConfigFile(java.lang.Long, java.lang.Long, java.lang.Long)
69 public String createConfigFile(final Long studyId, final Long scenarioId,
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);
80 if (LOG.isDebugEnabled()) {
81 LOG.debug("Scenario Id = " + scenarioId);
84 List<StepDTO> listSteps = _scenarioService.getScenarioInfo(scenarioId);
86 File configFilePath = new File(_repositoryService.getDownloadDirectory(userId).getPath());
87 configFilePath.mkdirs();
88 File configFile = new File(configFilePath + "/siman-salome.conf");
90 //build the result file.
93 fstream = new FileWriter(configFile);
94 BufferedWriter out = new BufferedWriter(fstream);
96 out.write("Study ID : " + studyId + "\n");
97 out.write("Scenario ID : " + scenarioId + "\n");
98 out.write("User ID : " + userId + "\n");
101 Iterator<StepDTO> stepIter = listSteps.iterator();
102 Iterator<DocumentDTO> docIter;
103 Iterator<FileDTO> fileIter;
106 DocumentDTO docValue;
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");
116 docIter = stepValue.getDocs().iterator();
118 for ( ; docIter.hasNext(); ) {
119 docValue = docIter.next();
120 out.write(" Document : " + docValue.getTitle() + "\n");
121 out.write(" Document ID: " + docValue.getId() + "\n");
123 fileIter = docValue.getFiles().iterator();
125 for ( ; fileIter.hasNext(); ) {
126 fileValue = fileIter.next();
127 if (fileValue.isResult() == false) {
128 out.write(" Source file : " + fileValue.getPath() + "\n");
130 out.write(" Result file : " + fileValue.getPath() + "\n");
133 out.write(" File ID : " + fileValue.getId() + "\n");
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");
149 } catch (IOException e) {
150 // TODO Auto-generated catch block
154 return configFile.getAbsolutePath();
161 * @see org.splat.ws_server.service.salome.SimanSalomeService#getFile(java.lang.String)
164 public DataHandler getFile(final String fileURL) {
165 FileDataSource dataSource = new FileDataSource(fileURL);
166 DataHandler fileDataHandler = new DataHandler(dataSource);
167 return fileDataHandler;
173 * @see org.splat.ws_server.service.salome.SimanSalomeService#putFile(javax.activation.DataHandler, java.io.File)
176 public String /*DataHandler*/ putFile(final DataHandler dataHandler, final String fileName, final Long userId) {
178 //will be removed in future
180 _projectSettingsService.configure(_servletContext.getRealPath("/conf/som.xml"));
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);
188 File pathToDownloadDirectory = new File(_repositoryService.getDownloadDirectory(userId).getPath());
189 File vaultURLFile = new File(pathToDownloadDirectory + "/" + fileName);
191 pathToDownloadDirectory.mkdirs();
194 vaultURLFile.createNewFile();
195 } catch (IOException e) {
196 // TODO Auto-generated catch block
200 if (dataHandler != null) {
203 FileOutputStream outputStream = new FileOutputStream(vaultURLFile);
204 dataHandler.writeTo(outputStream);
206 outputStream.flush();
207 outputStream.close();
209 } catch (IOException e) {
210 // TODO Auto-generated catch block
215 return vaultURLFile.getPath();
216 /*FileDataSource fileDataSource = new FileDataSource(vaultURL);
217 DataHandler fileDataHandler = new DataHandler(fileDataSource);
218 return fileDataHandler;*/
226 * @see org.splat.ws_server.service.salome.SimanSalomeService#checkIn()
229 public String checkIn(final String fileURL, final Long scenarioId, final Long userId) {
231 File configFile = new File(fileURL);
233 //parse the config file and set properties to the DTO.
235 Map<Integer, StepDTO> numberStepMap = new HashMap<Integer, StepDTO>();
237 DocumentDTO document;
240 if (LOG.isDebugEnabled()) {
241 LOG.debug("The fileURL = " + fileURL);
245 Scanner input = new Scanner(configFile);
247 while(input.hasNext()) {
249 String nextLine = input.nextLine();
250 String[] tokens = nextLine.split(",");
251 int activityNumber = Integer.valueOf(tokens[2]);
253 if (numberStepMap.containsKey(activityNumber)) {
254 step = numberStepMap.get(activityNumber);
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);
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]));
272 numberStepMap.put(activityNumber, step);
278 List<StepDTO> listSteps = new ArrayList<StepDTO>(numberStepMap.values());
281 _scenarioService.checkin(scenarioId, userId, listSteps);
282 } catch (InvalidPropertyException e) {
284 } catch (MissedPropertyException e) {
286 } catch (MultiplyDefinedException e) {
288 } catch (MismatchException e) {
290 } catch (NotApplicableException e) {
292 } catch (IOException e) {
296 numberStepMap.clear();
298 //delete siman-salome.conf and salome-siman.conf files
300 fileURL.replace("salome-siman.conf", "siman-salome.conf");
301 File configFile2 = new File(fileURL);
302 configFile2.delete();
306 } catch (FileNotFoundException e) {
307 // TODO Auto-generated catch block
314 * Get the scenarioService.
315 * @return the scenarioService
317 public ScenarioService getScenarioService() {
318 return _scenarioService;
322 * Set the scenarioService.
323 * @param scenarioService the scenarioService to set
325 public void setScenarioService(final ScenarioService scenarioService) {
326 _scenarioService = scenarioService;
331 * Get the repositoryService.
332 * @return the repositoryService
334 public RepositoryService getRepositoryService() {
335 return _repositoryService;
340 * Set the repositoryService.
341 * @param repositoryService the repositoryService to set
343 public void setRepositoryService(final RepositoryService repositoryService) {
344 _repositoryService = repositoryService;
349 * Get the projectSettingsService.
350 * @return the projectSettingsService
352 public ProjectSettingsService getProjectSettingsService() {
353 return _projectSettingsService;
358 * Set the projectSettingsService.
359 * @param projectSettingsService the projectSettingsService to set
361 public void setProjectSettingsService(
362 final ProjectSettingsService projectSettingsService) {
363 _projectSettingsService = projectSettingsService;
368 public void setServletContext(final ServletContext ctx) {
369 _servletContext = ctx;