Project

General

Profile

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

    
3
import java.io.StringReader;
4
import java.util.Map;
5

    
6
import javax.annotation.Resource;
7
import javax.xml.ws.wsaddressing.W3CEndpointReference;
8

    
9
import org.apache.commons.logging.Log;
10
import org.apache.commons.logging.LogFactory;
11
import org.dom4j.Document;
12
import org.dom4j.io.SAXReader;
13

    
14
import com.googlecode.sarasvati.Arc;
15
import com.googlecode.sarasvati.NodeToken;
16

    
17
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
18
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
19
import eu.dnetlib.enabling.resultset.client.ResultSetClientFactory;
20
import eu.dnetlib.msro.workflows.nodes.ProgressJobNode;
21
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
22
import eu.dnetlib.msro.workflows.resultset.ProcessCountingResultSetFactory;
23
import eu.dnetlib.msro.workflows.util.ProgressProvider;
24
import eu.dnetlib.msro.workflows.util.ResultsetProgressProvider;
25
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
26

    
27
public class UpdateRepositoryProfilesJobNode extends SimpleJobNode implements ProgressJobNode {
28

    
29
	private static final String REPOSITORY_SERVICE_RESOURCE_TYPE = "RepositoryServiceResourceType";
30

    
31
	@Resource
32
	private ResultSetClientFactory resultSetClientFactory;
33

    
34
	@Resource
35
	private UniqueServiceLocator serviceLocator;
36

    
37
	@Resource
38
	private ProcessCountingResultSetFactory processCountingResultSetFactory;
39

    
40
	private String eprParam = "repoEpr";
41
	private String existingReposParam = "existingRepos";
42
	private ResultsetProgressProvider progressProvider;
43

    
44
	private static final Log log = LogFactory.getLog(UpdateRepositoryProfilesJobNode.class); // NOPMD by marko on 11/24/08 5:02 PM
45

    
46
	@Override
47
	protected String execute(final NodeToken token) throws Exception {
48
		// datasourceID -> profileID
49
		@SuppressWarnings("unchecked")
50
		final Map<String, String> existingRepos = (Map<String, String>) token.getFullEnv().getTransientAttribute(getExistingReposParam());
51
		final W3CEndpointReference epr = (W3CEndpointReference) token.getFullEnv().getTransientAttribute(getEprParam());
52

    
53
		this.progressProvider = processCountingResultSetFactory.createProgressProvider(token.getProcess(), epr);
54

    
55
		final SAXReader reader = new SAXReader();
56

    
57
		int countUpdated = 0;
58
		int countInserted = 0;
59
		int countProfileErrors = 0;
60

    
61
		log.info("Adding/updating repository profiles...");
62

    
63
		final ISRegistryService registry = serviceLocator.getService(ISRegistryService.class);
64

    
65
		for (String profile : resultSetClientFactory.getClient(this.progressProvider.getEpr())) {
66
			try {
67
				final Document doc = reader.read(new StringReader(profile));
68
				final String dsId = doc.valueOf("//EXTRA_FIELDS/FIELD[./key='OpenAireDataSourceId']/value");
69

    
70
				log.debug("Registering/updating profile:\n " + profile + "\n");
71

    
72
				if (existingRepos.containsKey(dsId)) {
73
					final String profId = existingRepos.get(dsId);
74
					doc.selectSingleNode("//RESOURCE_IDENTIFIER/@value").setText(profId);
75
					registry.updateProfile(profId, doc.asXML(), REPOSITORY_SERVICE_RESOURCE_TYPE);
76
					log.debug("Profile " + profId + " UPDATED for ds " + dsId);
77
					countUpdated++;
78
				} else {
79
					final String profId = registry.registerProfile(doc.asXML());
80
					log.debug("Valid Profile " + profId + " REGISTERED for ds " + dsId);
81
					countInserted++;
82
				}
83
			} catch (Exception e) {
84
				log.error("INVALID PROFILE: " + profile, e);
85
				countProfileErrors++;
86
			}
87
		}
88

    
89
		log.info("   - updated_profiles: " + countUpdated);
90
		log.info("   - inserted_profiles: " + countInserted);
91
		log.info("   - profiles_with_errors: " + countProfileErrors);
92

    
93
		token.getEnv().setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX + "updated_profiles", countUpdated);
94
		token.getEnv().setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX + "inserted_profiles", countInserted);
95
		token.getEnv().setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX + "profiles_with_errors", countProfileErrors);
96

    
97
		return Arc.DEFAULT_ARC;
98
	}
99

    
100
	public String getEprParam() {
101
		return eprParam;
102
	}
103

    
104
	public void setEprParam(final String eprParam) {
105
		this.eprParam = eprParam;
106
	}
107

    
108
	public String getExistingReposParam() {
109
		return existingReposParam;
110
	}
111

    
112
	public void setExistingReposParam(final String existingReposParam) {
113
		this.existingReposParam = existingReposParam;
114
	}
115

    
116
	@Override
117
	public ProgressProvider getProgressProvider() {
118
		return progressProvider;
119
	}
120

    
121
}
(20-20/21)