]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/core/process/FileSlcExecutionNotifier.java
Replace test result id by UUID
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / core / process / FileSlcExecutionNotifier.java
1 package org.argeo.slc.core.process;
2
3 import java.io.File;
4 import java.io.FileWriter;
5 import java.text.SimpleDateFormat;
6 import java.util.Date;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
10
11 import javax.xml.transform.stream.StreamResult;
12
13 import org.springframework.oxm.Marshaller;
14
15 import org.apache.commons.io.IOUtils;
16
17 import org.argeo.slc.core.SlcException;
18
19 public class FileSlcExecutionNotifier implements SlcExecutionNotifier {
20 private final static SimpleDateFormat sdf = new SimpleDateFormat(
21 "yyyyMMdd-HHmmss");
22
23 private String basePath;
24 private Marshaller marshaller;
25
26 private Map<String, String> uuidToDir = new HashMap<String, String>();
27
28 public void addSteps(SlcExecution slcExecution,
29 List<SlcExecutionStep> additionalSteps) {
30 writeSlcExecution(slcExecution);
31 }
32
33 public void newExecution(SlcExecution slcExecution) {
34 String dirPath = basePath + File.separator + sdf.format(new Date())
35 + '-' + slcExecution.getUuid();
36 File dir = new File(dirPath);
37 dir.mkdirs();
38
39 uuidToDir.put(slcExecution.getUuid(), dirPath);
40
41 writeSlcExecution(slcExecution);
42 }
43
44 public void updateExecution(SlcExecution slcExecution) {
45 writeSlcExecution(slcExecution);
46 }
47
48 protected void writeSlcExecution(SlcExecution slcExecution) {
49 FileWriter out = null;
50 try {
51 out = new FileWriter(getFilePath(slcExecution));
52 marshaller.marshal(slcExecution, new StreamResult(out));
53 } catch (Exception e) {
54 throw new SlcException("Cannot marshall SlcExecution to "
55 + getFilePath(slcExecution), e);
56 } finally {
57 IOUtils.closeQuietly(out);
58 }
59 }
60
61 protected String getFileName(SlcExecution slcExecution) {
62 return "SlcExecution-" + slcExecution.getUuid() + ".xml";
63 }
64
65 protected String getFilePath(SlcExecution slcExecution) {
66 String dirPath = uuidToDir.get(slcExecution.getUuid());
67 return dirPath + File.separator + "SlcExecution-"
68 + slcExecution.getUuid() + ".xml";
69 }
70
71 public void setBasePath(String basePath) {
72 this.basePath = basePath;
73 }
74
75 public void setMarshaller(Marshaller marshaller) {
76 this.marshaller = marshaller;
77 }
78
79
80 }