Project

General

Profile

1
package eu.dnetlib.msro.openaireplus.workflows.nodes.actions;
2

    
3
import java.io.StringReader;
4
import java.util.List;
5
import java.util.Map;
6
import javax.annotation.Resource;
7

    
8
import com.google.gson.Gson;
9
import com.googlecode.sarasvati.Arc;
10
import com.googlecode.sarasvati.NodeToken;
11
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
12
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
13
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
14
import eu.dnetlib.miscutils.datetime.DateUtils;
15
import eu.dnetlib.msro.rmi.MSROException;
16
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
17
import org.apache.commons.logging.Log;
18
import org.apache.commons.logging.LogFactory;
19
import org.dom4j.Attribute;
20
import org.dom4j.Document;
21
import org.dom4j.Element;
22
import org.dom4j.io.SAXReader;
23

    
24
public class UpdateSetsJobNode extends SimpleJobNode {
25

    
26
	/**
27
	 * logger.
28
	 */
29
	private static final Log log = LogFactory.getLog(UpdateSetsJobNode.class);
30

    
31
	@Resource
32
	private UniqueServiceLocator serviceLocator;
33

    
34
	@Override
35
	protected String execute(final NodeToken token) throws Exception {
36

    
37
		@SuppressWarnings("unchecked")		final List<Map<String, String>> sets = new Gson().fromJson(token.getEnv().getAttribute("sets"), List.class);
38

    
39
		final String lastUpdate = DateUtils.now_ISO8601();
40
		for (Map<String, String> set : sets) {
41

    
42
			// update only the enabled sets.
43
			if (isEnabled(set)) {
44
				log.info("updating set: " + set.toString());
45
				addLatestRawSet(set, lastUpdate);
46
			} else {
47
				log.info("skip set update: " + set.toString());
48
			}
49
		}
50

    
51
		return Arc.DEFAULT_ARC;
52
	}
53

    
54
	private boolean isEnabled(final Map<String, String> set) {
55
		return set.containsKey("enabled") && set.get("enabled").equals("true");
56
	}
57

    
58
	public void addLatestRawSet(final Map<String, String> set, final String lastUpdate) throws MSROException {
59
		final String q = "for $x in collection('/db/DRIVER/ActionManagerSetDSResources/ActionManagerSetDSResourceType') where $x//SET/@id = '" + set.get("set")
60
				+ "' return $x";
61
		try {
62
			final String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(q);
63
			final Document doc = new SAXReader().read(new StringReader(profile));
64
			final String profId = doc.valueOf("//RESOURCE_IDENTIFIER/@value");
65
			final Element latest = (Element) doc.selectSingleNode("//RAW_SETS/LATEST");
66
			final Element expired = ((Element) doc.selectSingleNode("//RAW_SETS")).addElement("EXPIRED");
67

    
68
			for (Object o : latest.attributes()) {
69
				final Attribute a = (Attribute) o;
70
				expired.addAttribute(a.getName(), a.getValue());
71
			}
72

    
73
			latest.addAttribute("id", set.get("rawset"));
74
			latest.addAttribute("creationDate", set.get("creationDate"));
75
			latest.addAttribute("lastUpdate", lastUpdate);
76

    
77
			serviceLocator.getService(ISRegistryService.class).updateProfile(profId, doc.asXML(), "ActionManagerSetDSResourceType");
78
		} catch (Exception e) {
79
			String msg = "Error updating profile of set: " + set;
80
			log.error(msg);
81
			throw new MSROException(msg, e);
82
		}
83
	}
84

    
85
}
(8-8/8)