Project

General

Profile

1
package eu.dnetlib.data.mdstore.utils;
2

    
3
import java.io.IOException;
4
import java.io.StringWriter;
5
import java.io.Writer;
6
import java.util.Map;
7

    
8
import com.google.common.collect.Maps;
9
import eu.dnetlib.clients.is.InformationServiceClient;
10
import eu.dnetlib.miscutils.datetime.DateUtils;
11
import eu.dnetlib.msro.controllers.MsroWorkerController;
12
import freemarker.template.Configuration;
13
import freemarker.template.Template;
14
import freemarker.template.TemplateException;
15
import org.apache.commons.logging.Log;
16
import org.apache.commons.logging.LogFactory;
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.stereotype.Component;
19

    
20
/**
21
 * Created by claudio on 24/03/2017.
22
 */
23
@Component
24
public class MDStoreProfileManager {
25

    
26
	private static final Log log = LogFactory.getLog(MDStoreProfileManager.class);
27

    
28
	@Autowired
29
	private InformationServiceClient isClient;
30

    
31
	@Autowired
32
	private MsroWorkerController msroWorkerController;
33

    
34
	@Autowired
35
	private Configuration freemakerCfg;
36

    
37
	public String registerProfile(String format, String layout, String interpretation) throws MDStoreException {
38

    
39
		final Map<String, Object> input = Maps.newHashMap();
40

    
41
		input.put("serviceId", msroWorkerController.getProfileId());
42
		input.put("format", format);
43
		input.put("layout", layout);
44
		input.put("interpretation", interpretation);
45

    
46
		log.debug(String.format("registering new mdstore profile %s-%s-%s", format, layout, interpretation));
47

    
48
		return isClient.register(processTemplate(input));
49
	}
50

    
51
	public void deleteProfile(final String mdId) throws MDStoreException {
52
		isClient.deleteProfile(mdId);
53
	}
54

    
55
	public void updateSize(final String mdId, final long size) {
56
		final String now = DateUtils.now_ISO8601();
57

    
58
		isClient.xupdate("for $x in //RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value = '" + mdId + "']"
59
				+ "return update value $x//LAST_STORAGE_DATE with '" + now + "'");
60

    
61
		isClient.xupdate("for $x in //RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value = '" + mdId + "']"
62
				+ "return update value $x//NUMBER_OF_RECORDS with '" + size + "'");
63
	}
64

    
65
	// HELPERS
66

    
67
	private String processTemplate(final Map<String, Object> input) throws MDStoreException {
68
		try(Writer writer = new StringWriter()) {
69
			final Template template = freemakerCfg.getTemplate("mdstore.ftl");
70

    
71
			template.process(input, writer);
72

    
73
			return writer.toString();
74
		} catch (IOException | TemplateException e) {
75
			throw new MDStoreException(e);
76
		}
77
	}
78

    
79
}
(3-3/5)