1
|
package eu.dnetlib.data.objectstore.filesystem;
|
2
|
|
3
|
import java.io.UnsupportedEncodingException;
|
4
|
|
5
|
import com.mongodb.DBObject;
|
6
|
import eu.dnetlib.data.objectstore.rmi.ObjectStoreFile;
|
7
|
import eu.dnetlib.data.objectstore.rmi.Protocols;
|
8
|
import eu.dnetlib.miscutils.functional.UnaryFunction;
|
9
|
import org.apache.commons.logging.Log;
|
10
|
import org.apache.commons.logging.LogFactory;
|
11
|
|
12
|
/**
|
13
|
* The Class ObjectStoreFileBuilder generates an objectStoreFile bean
|
14
|
*
|
15
|
*/
|
16
|
public class ObjectStoreFileUtility {
|
17
|
|
18
|
private static final int KB_SIZE = 1024;
|
19
|
|
20
|
/** The Constant log. */
|
21
|
private static final Log log = LogFactory.getLog(ObjectStoreFileUtility.class);
|
22
|
|
23
|
public static ObjectStoreFile build(final DBObject metadata, final String baseURI, final String objectStoreID, final String basePath) {
|
24
|
|
25
|
String originalFile = (String) metadata.get("originalObject");
|
26
|
ObjectStoreFile original = ObjectStoreFile.createObject(originalFile);
|
27
|
ObjectStoreFile newFile = new ObjectStoreFile();
|
28
|
newFile.setObjectID((String) metadata.get("id"));
|
29
|
newFile.setAccessProtocol(Protocols.HTTP);
|
30
|
newFile.setMimeType((String) metadata.get("mime"));
|
31
|
newFile.setMd5Sum((String) metadata.get("md5Sum"));
|
32
|
try{
|
33
|
newFile.setFileSizeKB(Integer.parseInt(metadata.get("size").toString()) / KB_SIZE);
|
34
|
} catch (Throwable e) {
|
35
|
log.error("Error on getting size",e);
|
36
|
}
|
37
|
if (originalFile != null) {
|
38
|
newFile.setMetadataRelatedID(original.getMetadataRelatedID());
|
39
|
if ((original.getDownloadedURL() == null) || (original.getDownloadedURL().length() == 0)) {
|
40
|
newFile.setDownloadedURL(original.getURI());
|
41
|
} else {
|
42
|
newFile.setDownloadedURL(original.getDownloadedURL());
|
43
|
}
|
44
|
}
|
45
|
try {
|
46
|
newFile.setURI(ModularObjectStoreRESTService.retrieveURL(baseURI, basePath, objectStoreID, newFile.getObjectID()));
|
47
|
} catch (UnsupportedEncodingException e) {
|
48
|
log.error("Error on Build objectStoreFile ", e);
|
49
|
}
|
50
|
return newFile;
|
51
|
}
|
52
|
|
53
|
public static UnaryFunction<String, DBObject> asJSON(final String baseURI, final String objectStoreID, final String basePath) {
|
54
|
return new UnaryFunction<String, DBObject>() {
|
55
|
|
56
|
@Override
|
57
|
public String evaluate(final DBObject input) {
|
58
|
return build(input, baseURI, objectStoreID, basePath).toJSON();
|
59
|
|
60
|
}
|
61
|
};
|
62
|
}
|
63
|
}
|