1
|
package eu.dnetlib.msro.openaireplus.workflows.nodes;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.Map;
|
5
|
import javax.annotation.Resource;
|
6
|
import javax.xml.ws.wsaddressing.W3CEndpointReference;
|
7
|
|
8
|
import com.googlecode.sarasvati.Arc;
|
9
|
import com.googlecode.sarasvati.NodeToken;
|
10
|
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
|
11
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
12
|
import eu.dnetlib.enabling.resultset.client.ResultSetClientFactory;
|
13
|
import eu.dnetlib.msro.workflows.nodes.ProgressJobNode;
|
14
|
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
|
15
|
import eu.dnetlib.msro.workflows.resultset.ProcessCountingResultSetFactory;
|
16
|
import eu.dnetlib.msro.workflows.util.ProgressProvider;
|
17
|
import eu.dnetlib.msro.workflows.util.ResultsetProgressProvider;
|
18
|
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
|
19
|
import org.apache.commons.logging.Log;
|
20
|
import org.apache.commons.logging.LogFactory;
|
21
|
import org.dom4j.Document;
|
22
|
import org.dom4j.io.SAXReader;
|
23
|
|
24
|
public class UpdateRepositoryProfilesJobNode extends SimpleJobNode implements ProgressJobNode {
|
25
|
|
26
|
private static final String REPOSITORY_SERVICE_RESOURCE_TYPE = "RepositoryServiceResourceType";
|
27
|
|
28
|
@Resource
|
29
|
private ResultSetClientFactory resultSetClientFactory;
|
30
|
|
31
|
@Resource
|
32
|
private UniqueServiceLocator serviceLocator;
|
33
|
|
34
|
@Resource
|
35
|
private ProcessCountingResultSetFactory processCountingResultSetFactory;
|
36
|
|
37
|
private String eprParam = "repoEpr";
|
38
|
private String existingReposParam = "existingRepos";
|
39
|
private ResultsetProgressProvider progressProvider;
|
40
|
|
41
|
private static final Log log = LogFactory.getLog(UpdateRepositoryProfilesJobNode.class); // NOPMD by marko on 11/24/08 5:02 PM
|
42
|
|
43
|
@Override
|
44
|
protected String execute(final NodeToken token) throws Exception {
|
45
|
// datasourceID -> profileID
|
46
|
@SuppressWarnings("unchecked") final Map<String, String> existingRepos =
|
47
|
(Map<String, String>) token.getFullEnv().getTransientAttribute(getExistingReposParam());
|
48
|
final W3CEndpointReference epr = (W3CEndpointReference) token.getFullEnv().getTransientAttribute(getEprParam());
|
49
|
|
50
|
this.progressProvider = processCountingResultSetFactory.createProgressProvider(token.getProcess(), epr);
|
51
|
|
52
|
final SAXReader reader = new SAXReader();
|
53
|
|
54
|
int countUpdated = 0;
|
55
|
int countInserted = 0;
|
56
|
int countProfileErrors = 0;
|
57
|
|
58
|
log.info("Adding/updating repository profiles...");
|
59
|
|
60
|
final ISRegistryService registry = serviceLocator.getService(ISRegistryService.class);
|
61
|
|
62
|
for (String profile : resultSetClientFactory.getClient(this.progressProvider.getEpr())) {
|
63
|
try {
|
64
|
final Document doc = reader.read(new StringReader(profile));
|
65
|
final String dsId = doc.valueOf("//EXTRA_FIELDS/FIELD[./key='OpenAireDataSourceId']/value");
|
66
|
|
67
|
log.debug("Registering/updating profile:\n " + profile + "\n");
|
68
|
|
69
|
if (existingRepos.containsKey(dsId)) {
|
70
|
final String profId = existingRepos.get(dsId);
|
71
|
doc.selectSingleNode("//RESOURCE_IDENTIFIER/@value").setText(profId);
|
72
|
registry.updateProfile(profId, doc.asXML(), REPOSITORY_SERVICE_RESOURCE_TYPE);
|
73
|
log.debug("Profile " + profId + " UPDATED for ds " + dsId);
|
74
|
countUpdated++;
|
75
|
} else {
|
76
|
final String profId = registry.registerProfile(doc.asXML());
|
77
|
log.debug("Valid Profile " + profId + " REGISTERED for ds " + dsId);
|
78
|
countInserted++;
|
79
|
}
|
80
|
} catch (Exception e) {
|
81
|
log.error("INVALID PROFILE: " + profile, e);
|
82
|
countProfileErrors++;
|
83
|
}
|
84
|
}
|
85
|
|
86
|
log.info(" - updated_profiles: " + countUpdated);
|
87
|
log.info(" - inserted_profiles: " + countInserted);
|
88
|
log.info(" - profiles_with_errors: " + countProfileErrors);
|
89
|
|
90
|
token.getEnv().setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX + "updated_profiles", countUpdated);
|
91
|
token.getEnv().setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX + "inserted_profiles", countInserted);
|
92
|
token.getEnv().setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX + "profiles_with_errors", countProfileErrors);
|
93
|
|
94
|
return Arc.DEFAULT_ARC;
|
95
|
}
|
96
|
|
97
|
public String getEprParam() {
|
98
|
return eprParam;
|
99
|
}
|
100
|
|
101
|
public void setEprParam(final String eprParam) {
|
102
|
this.eprParam = eprParam;
|
103
|
}
|
104
|
|
105
|
public String getExistingReposParam() {
|
106
|
return existingReposParam;
|
107
|
}
|
108
|
|
109
|
public void setExistingReposParam(final String existingReposParam) {
|
110
|
this.existingReposParam = existingReposParam;
|
111
|
}
|
112
|
|
113
|
@Override
|
114
|
public ProgressProvider getProgressProvider() {
|
115
|
return progressProvider;
|
116
|
}
|
117
|
|
118
|
}
|