]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/controllers/SlcExecutionManager.java
Move to SLC legacy
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / controllers / SlcExecutionManager.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.web.mvc.controllers;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.InputStream;
21 import java.util.UUID;
22
23 import javax.xml.transform.stream.StreamSource;
24
25 import org.apache.commons.io.IOUtils;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.argeo.slc.core.attachment.Attachment;
29 import org.argeo.slc.core.attachment.AttachmentsStorage;
30 import org.argeo.slc.core.attachment.SimpleAttachment;
31 import org.argeo.slc.msg.ObjectList;
32 import org.argeo.slc.process.SlcExecution;
33 import org.springframework.oxm.Marshaller;
34 import org.springframework.oxm.Unmarshaller;
35 import org.springframework.xml.transform.StringResult;
36
37 public class SlcExecutionManager {
38 private final static Log log = LogFactory.getLog(SlcExecutionManager.class);
39
40 private Unmarshaller unmarshaller;
41 private Marshaller marshaller;
42 private AttachmentsStorage attachmentsStorage;
43
44 public SlcExecutionManager(Unmarshaller unmarshaller,
45 Marshaller marshaller, AttachmentsStorage attachmentsStorage) {
46
47 this.unmarshaller = unmarshaller;
48 this.marshaller = marshaller;
49 this.attachmentsStorage = attachmentsStorage;
50 }
51
52 void storeRealizedFlows(SlcExecution slcExecution) {
53
54 Attachment attachment = realizedFlowsAttachment(UUID.randomUUID()
55 .toString(), slcExecution);
56 InputStream in = null;
57 try {
58 ObjectList ol = new ObjectList(slcExecution.getRealizedFlows());
59 StringResult result = new StringResult();
60 marshaller.marshal(ol, result);
61 in = new ByteArrayInputStream(result.toString().getBytes());
62 attachmentsStorage.storeAttachment(attachment, in);
63
64 slcExecution.setRealizedFlowsXml(attachment.getUuid());
65 } catch (Exception e) {
66 log.error("Could not store realized flows as attachment #"
67 + attachment.getUuid(), e);
68 } finally {
69 IOUtils.closeQuietly(in);
70 }
71 }
72
73 void retrieveRealizedFlows(SlcExecution slcExecution) {
74 Attachment attachment = realizedFlowsAttachment(slcExecution
75 .getRealizedFlowsXml(), slcExecution);
76
77 ByteArrayOutputStream out = null;
78 ByteArrayInputStream in = null;
79 try {
80 // TODO: optimize with piped streams
81 out = new ByteArrayOutputStream();
82 attachmentsStorage.retrieveAttachment(attachment, out);
83
84 byte[] arr = out.toByteArray();
85 in = new ByteArrayInputStream(arr);
86 StreamSource source = new StreamSource(in);
87 ObjectList ol = (ObjectList) unmarshaller.unmarshal(source);
88 ol.fill(slcExecution.getRealizedFlows());
89 } catch (Exception e) {
90 log.error("Could not retrieve realized flows from attachment #"
91 + attachment.getUuid(), e);
92 } finally {
93 IOUtils.closeQuietly(in);
94 IOUtils.closeQuietly(out);
95 }
96 }
97
98 /** Unify labeling in the package */
99 static Attachment realizedFlowsAttachment(String attachmentUuid,
100 SlcExecution slcExecution) {
101 return new SimpleAttachment(attachmentUuid,
102 "RealizedFlows of SlcExecution #" + slcExecution.getUuid(),
103 "text/xml");
104 }
105 }