Project

General

Profile

1
package eu.dnetlib.efg.workflows.nodes.thumbnail;
2

    
3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.util.concurrent.BlockingQueue;
6
import java.util.concurrent.Callable;
7

    
8
import eu.dnetlib.data.objectstore.ObjectStoreRecord;
9
import eu.dnetlib.data.objectstore.connector.ObjectStore;
10
import eu.dnetlib.rmi.data.ObjectStoreFile;
11
import org.apache.commons.logging.Log;
12
import org.apache.commons.logging.LogFactory;
13

    
14
/**
15
 * Created by sandro on 4/8/16.
16
 */
17
public class WorkerMap implements Callable<Boolean> {
18

    
19
	private static final Log log = LogFactory.getLog(WorkerMap.class);
20

    
21
	private final ObjectStore thumb256;
22

    
23
	private final ObjectStore thumb96;
24

    
25
	private final ObjectStore nativeObjectStore;
26

    
27
	private final BlockingQueue<ObjectStoreFile> queue;
28

    
29
	private final ThumbnailGenerator thumbnailGenerator;
30

    
31
	public WorkerMap(final ObjectStore nativeObjectStore, final ObjectStore thumb256, final ObjectStore thumb96, final BlockingQueue<ObjectStoreFile> queue)
32
			throws IOException {
33
		this.thumb96 = thumb96;
34
		this.thumb256 = thumb256;
35
		this.queue = queue;
36
		this.nativeObjectStore = nativeObjectStore;
37
		this.thumbnailGenerator = new ThumbnailGenerator();
38
	}
39

    
40
	@Override
41
	public Boolean call() throws Exception {
42
		ObjectStoreFile currentFile = queue.take();
43
		while (currentFile != GenerateThumbnailJobNode.END_QUEUE) {
44
			try {
45
				final ObjectStoreRecord record = new ObjectStoreRecord();
46
				if (!thumb96.alreadyExist(currentFile.getObjectID())) {
47
					final InputStream convert = thumbnailGenerator.convert(nativeObjectStore.deliverStream(currentFile.getObjectID()), 150, 96, "96", true);
48

    
49
					record.setFileMetadata(currentFile);
50
					record.setInputStream(convert);
51
					thumb96.feedObjectRecord(record);
52
				}
53
				if (!thumb256.alreadyExist(currentFile.getObjectID())) {
54
					record.setInputStream(thumbnailGenerator.convert(nativeObjectStore.deliverStream(currentFile.getObjectID()), 600, 250, "250", false));
55
					thumb256.feedObjectRecord(record);
56
				}
57
			} catch (Throwable e) {
58
				log.error("Error on generating thumbnail", e);
59
			} finally {
60
                currentFile = queue.take();
61
            }
62

    
63
		}
64

    
65
		if (currentFile == GenerateThumbnailJobNode.END_QUEUE)
66
			queue.put(currentFile);
67

    
68
		return true;
69
	}
70
}
(3-3/3)