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;
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;
28 public class SimanSalomeServiceImpl implements SimanSalomeService {
31 * The logger for the service.
33 protected final static Logger LOG = Logger
34 .getLogger(SimanSalomeServiceImpl.class);
37 * The Scenario Service.
39 private ScenarioService _scenarioService;
42 * The Repository Service.
44 private RepositoryService _repositoryService;
47 * The Project Settings Service.
49 private ProjectSettingsService _projectSettingsService;
54 * @see org.splat.ws_server.service.salome.SimanSalomeService#createConfigFile(java.lang.Long, java.lang.Long, java.lang.Long)
57 public String createConfigFile(final Long studyId, final Long scenarioId,
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");
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");
71 File textfile = new File("w:/testfile.txt");
75 fstream = new FileWriter(textfile);
76 BufferedWriter out = new BufferedWriter(fstream);
78 out.write("testURL " + testURL.getPath());
80 out.write("testURL == null\n");
83 out.write("test2URL " + test2URL.getPath());
85 out.write("test2URL == null\n");
87 out.write("path = " + path);
90 } catch (IOException e) {
91 // TODO Auto-generated catch block
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);
103 List<StepDTO> listSteps = _scenarioService.getScenarioInfo(scenarioId);
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");
109 //build the result file.
112 fstream = new FileWriter(configFile);
113 BufferedWriter out = new BufferedWriter(fstream);
115 out.write("Study ID : " + studyId + "\n");
116 out.write("Scenario ID : " + scenarioId + "\n");
117 out.write("User ID : " + userId + "\n");
120 Iterator<StepDTO> stepIter = listSteps.iterator();
121 Iterator<DocumentDTO> docIter;
122 Iterator<FileDTO> fileIter;
125 DocumentDTO docValue;
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");
135 docIter = stepValue.getDocs().iterator();
137 for ( ; docIter.hasNext(); ) {
138 docValue = docIter.next();
139 out.write(" Document : " + docValue.getTitle() + "\n");
140 out.write(" Document ID: " + docValue.getId() + "\n");
142 fileIter = docValue.getFiles().iterator();
144 for ( ; fileIter.hasNext(); ) {
145 fileValue = fileIter.next();
146 if (fileValue.isResult() == false) {
147 out.write(" Source file : " + fileValue.getPath() + "\n");
149 out.write(" Result file : " + fileValue.getPath() + "\n");
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");
166 } catch (IOException e) {
167 // TODO Auto-generated catch block
171 return configFile.getAbsolutePath();
178 * @see org.splat.ws_server.service.salome.SimanSalomeService#getFile(java.lang.String)
181 public DataHandler getFile(final String fileURL) {
182 FileDataSource dataSource = new FileDataSource(fileURL);
183 DataHandler fileDataHandler = new DataHandler(dataSource);
184 return fileDataHandler;
190 * @see org.splat.ws_server.service.salome.SimanSalomeService#putFile(javax.activation.DataHandler, java.io.File)
193 public String /*DataHandler*/ putFile(final DataHandler dataHandler, final String fileName, final Long userId) {
195 //will be removed in future
197 _projectSettingsService.configure("W:\\Eclipse workspace\\SIMAN\\SIMAN_SRC\\Workspace\\Siman-WS\\WebContent\\conf\\som.xml");
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);
205 File pathToDownloadDirectory = new File(_repositoryService.getDownloadDirectory(userId).getPath());
206 File vaultURLFile = new File(pathToDownloadDirectory + "\\" + fileName);
208 pathToDownloadDirectory.mkdirs();
211 vaultURLFile.createNewFile();
212 } catch (IOException e) {
213 // TODO Auto-generated catch block
219 FileOutputStream outputStream = new FileOutputStream(vaultURLFile);
220 dataHandler.writeTo(outputStream);
222 outputStream.flush();
223 outputStream.close();
225 } catch (IOException e) {
226 // TODO Auto-generated catch block
230 return vaultURLFile.getPath();
231 /*FileDataSource fileDataSource = new FileDataSource(vaultURL);
232 DataHandler fileDataHandler = new DataHandler(fileDataSource);
233 return fileDataHandler;*/
241 * @see org.splat.ws_server.service.salome.SimanSalomeService#checkIn()
244 public String checkIn(final String fileURL, final Long userId) {
246 File configFile = new File(fileURL);
248 //parse the config file and set properties to the DTO.
250 Map<Integer, StepDTO> numberStepMap = new HashMap<Integer, StepDTO>();
252 DocumentDTO document;
257 Scanner input = new Scanner(configFile);
259 while(input.hasNext()) {
261 String nextLine = input.nextLine();
262 String[] tokens = nextLine.split(",");
263 int activityNumber = Integer.valueOf(tokens[2]);
264 ScenarioId = Long.valueOf(tokens[0]);
266 if (numberStepMap.containsKey(activityNumber)) {
267 step = numberStepMap.get(activityNumber);
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);
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]));
285 numberStepMap.put(activityNumber, step);
291 List<StepDTO> listSteps = new ArrayList<StepDTO>(numberStepMap.values());
293 _scenarioService.checkin(ScenarioId, userId, listSteps);
295 numberStepMap.clear();
299 } catch (FileNotFoundException e) {
300 // TODO Auto-generated catch block
307 * Get the scenarioService.
308 * @return the scenarioService
310 public ScenarioService getScenarioService() {
311 return _scenarioService;
315 * Set the scenarioService.
316 * @param scenarioService the scenarioService to set
318 public void setScenarioService(final ScenarioService scenarioService) {
319 _scenarioService = scenarioService;
324 * Get the repositoryService.
325 * @return the repositoryService
327 public RepositoryService getRepositoryService() {
328 return _repositoryService;
333 * Set the repositoryService.
334 * @param repositoryService the repositoryService to set
336 public void setRepositoryService(final RepositoryService repositoryService) {
337 _repositoryService = repositoryService;
342 * Get the projectSettingsService.
343 * @return the projectSettingsService
345 public ProjectSettingsService getProjectSettingsService() {
346 return _projectSettingsService;
351 * Set the projectSettingsService.
352 * @param projectSettingsService the projectSettingsService to set
354 public void setProjectSettingsService(
355 final ProjectSettingsService projectSettingsService) {
356 _projectSettingsService = projectSettingsService;