Project

General

Profile

1
package eu.dnetlib.msro.worker;
2

    
3
import java.awt.image.BufferedImage;
4
import java.io.ByteArrayOutputStream;
5
import java.io.IOException;
6

    
7
import javax.imageio.ImageIO;
8

    
9
import org.apache.commons.codec.binary.Base64;
10
import org.apache.commons.io.IOUtils;
11
import org.apache.commons.logging.Log;
12
import org.apache.commons.logging.LogFactory;
13
import org.springframework.beans.factory.annotation.Autowired;
14

    
15
import com.googlecode.sarasvati.GraphProcess;
16
import com.googlecode.sarasvati.visual.ProcessImageMapCreator;
17

    
18
import eu.dnetlib.common.services.AbstractBaseService;
19
import eu.dnetlib.msro.worker.ui.ProcessToImageMapHelper;
20
import eu.dnetlib.rmi.object.manager.ProcessImageDesc;
21
import eu.dnetlib.rmi.soap.ManagerWorkerService;
22

    
23
public class ManagerWorkerServiceImpl extends AbstractBaseService implements ManagerWorkerService {
24

    
25
	@Autowired
26
	private LocalWorkflowRegistry registry;
27
	private static final Log log = LogFactory.getLog(ManagerWorkerServiceImpl.class);
28

    
29
	@Override
30
	public ProcessImageDesc getProcessImageDesc(final String procId) {
31
		final GraphProcess proc = registry.findProcess(procId);
32
		byte[] imgBytes;
33
		String mapContent = "";
34
		try {
35
			if (proc != null) {
36
				final ByteArrayOutputStream bos = new ByteArrayOutputStream();
37
				try {
38
					final ProcessImageMapCreator imageMapCreator = new ProcessImageMapCreator(proc, new ProcessToImageMapHelper(procId));
39
					final BufferedImage image = imageMapCreator.getImage();
40
					ImageIO.write(image, "png", bos);
41
					imgBytes = bos.toByteArray();
42
					mapContent = imageMapCreator.getMapContents();
43
				} catch (Throwable e) {
44
					imgBytes = IOUtils.toByteArray(getClass().getResourceAsStream("/eu/dnetlib/msro/worker/ui/wf_problem.png"));
45
				} finally {
46
					bos.close();
47
				}
48
			} else {
49
				imgBytes = IOUtils.toByteArray(getClass().getResourceAsStream("/eu/dnetlib/msro/worker/ui/wf_not_found.png"));
50
			}
51
		} catch (IOException e) {
52
			log.error("Error generating wf image for process: " + procId, e);
53
			return null;
54
		}
55
		return new ProcessImageDesc("image/png", new String(Base64.encodeBase64(imgBytes)), mapContent);
56
	}
57

    
58
}
(3-3/5)