1
|
package eu.dnetlib.msro.openaireplus.workflows.nodes.consistency;
|
2
|
|
3
|
import java.util.HashMap;
|
4
|
import java.util.List;
|
5
|
import java.util.Map;
|
6
|
import java.util.concurrent.atomic.AtomicInteger;
|
7
|
import java.util.stream.Collectors;
|
8
|
|
9
|
import org.apache.commons.lang.StringUtils;
|
10
|
import org.apache.commons.logging.Log;
|
11
|
import org.apache.commons.logging.LogFactory;
|
12
|
import org.springframework.beans.factory.annotation.Autowired;
|
13
|
|
14
|
import com.googlecode.sarasvati.Arc;
|
15
|
import com.googlecode.sarasvati.NodeToken;
|
16
|
|
17
|
import eu.dnetlib.enabling.datasources.LocalOpenaireDatasourceManager;
|
18
|
import eu.dnetlib.enabling.datasources.LocalOpenaireDatasourceManagerImpl;
|
19
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
20
|
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
|
21
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
22
|
import eu.dnetlib.msro.workflows.nodes.ProgressJobNode;
|
23
|
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
|
24
|
import eu.dnetlib.msro.workflows.util.ProgressProvider;
|
25
|
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
|
26
|
|
27
|
public class FindInvalidRepoProfilesJobNode extends SimpleJobNode implements ProgressJobNode {
|
28
|
|
29
|
@Autowired
|
30
|
private LocalOpenaireDatasourceManager dsManager;
|
31
|
|
32
|
@Autowired
|
33
|
private UniqueServiceLocator serviceLocator;
|
34
|
|
35
|
private int current = 0;
|
36
|
private int total = 0;
|
37
|
|
38
|
private boolean deleteInvalidProfiles = false;
|
39
|
private boolean registerMissingProfiles = false;
|
40
|
|
41
|
private static final Log log = LogFactory.getLog(FindInvalidRepoProfilesJobNode.class);
|
42
|
|
43
|
@Override
|
44
|
protected String execute(final NodeToken token) throws Exception {
|
45
|
|
46
|
final Map<String, AtomicInteger> validIds = ((LocalOpenaireDatasourceManagerImpl) dsManager).getDatasourceManagerClients()
|
47
|
.searchSQL("SELECT id FROM dsm_datasources", new HashMap<>())
|
48
|
.stream()
|
49
|
.map(m -> (String) m.get("id"))
|
50
|
.collect(Collectors.toMap(s -> s, s -> new AtomicInteger(0)));
|
51
|
|
52
|
final List<String> list = serviceLocator.getService(ISLookUpService.class).quickSearchProfile(
|
53
|
"for $x in collection('/db/DRIVER/RepositoryServiceResources/RepositoryServiceResourceType') return concat($x//DATASOURCE_ORIGINAL_ID, ' @@@ ', $x//RESOURCE_IDENTIFIER/@value)");
|
54
|
|
55
|
this.current = 0;
|
56
|
this.total = list.size() + validIds.size();
|
57
|
int errorInvalidProfiles = 0;
|
58
|
int errorUnregisteredProfiles = 0;
|
59
|
int errorTooManyProfiles = 0;
|
60
|
|
61
|
for (final String s : list) {
|
62
|
current++;
|
63
|
final String oid = StringUtils.substringBefore(s, "@@@").trim();
|
64
|
final String profId = StringUtils.substringAfter(s, "@@@").trim();
|
65
|
log.info("Evaluating ds: " + oid);
|
66
|
if (validIds.containsKey(oid)) {
|
67
|
validIds.get(oid).incrementAndGet();
|
68
|
} else {
|
69
|
log.warn("Invalid profile " + profId + ", openaireId " + oid + " not registered properly");
|
70
|
errorInvalidProfiles++;
|
71
|
// token.getEnv().setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX + "invalidProfile:" + oid, profId);
|
72
|
if (deleteInvalidProfiles) {
|
73
|
log.warn(" - Deleting profile " + profId);
|
74
|
serviceLocator.getService(ISRegistryService.class).deleteProfile(profId);
|
75
|
}
|
76
|
}
|
77
|
}
|
78
|
|
79
|
for (final Map.Entry<String, AtomicInteger> e : validIds.entrySet()) {
|
80
|
current++;
|
81
|
final String dsId = e.getKey();
|
82
|
final int n = e.getValue().get();
|
83
|
if (n == 0) {
|
84
|
errorUnregisteredProfiles++;
|
85
|
// token.getEnv().setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX + "unregisterdProfile:" + dsId, dsId);
|
86
|
log.warn("Missing profile for ds " + dsId);
|
87
|
if (registerMissingProfiles) {
|
88
|
dsManager.setManaged(dsId, dsManager.isManaged(dsId)); // This command should regenerate the repo profile
|
89
|
}
|
90
|
} else if (n > 1) {
|
91
|
errorTooManyProfiles++;
|
92
|
log.error("Too many profiles registerd " + n + " for ds " + dsId);
|
93
|
// token.getEnv().setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX + "tooManyProfiles:" + dsId, n);
|
94
|
}
|
95
|
}
|
96
|
|
97
|
token.getEnv().setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX + "errorInvalidProfiles", errorInvalidProfiles);
|
98
|
token.getEnv().setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX + "errorUnregisteredProfiles", errorUnregisteredProfiles);
|
99
|
token.getEnv().setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX + "errorTooManyProfiles", errorTooManyProfiles);
|
100
|
|
101
|
return Arc.DEFAULT_ARC;
|
102
|
}
|
103
|
|
104
|
@Override
|
105
|
public ProgressProvider getProgressProvider() {
|
106
|
return new ProgressProvider() {
|
107
|
|
108
|
@Override
|
109
|
public int getTotalValue() {
|
110
|
return total;
|
111
|
}
|
112
|
|
113
|
@Override
|
114
|
public int getCurrentValue() {
|
115
|
return current;
|
116
|
}
|
117
|
|
118
|
@Override
|
119
|
public boolean isInaccurate() {
|
120
|
return false;
|
121
|
}
|
122
|
};
|
123
|
}
|
124
|
|
125
|
public boolean isDeleteInvalidProfiles() {
|
126
|
return deleteInvalidProfiles;
|
127
|
}
|
128
|
|
129
|
public void setDeleteInvalidProfiles(final boolean deleteInvalidProfiles) {
|
130
|
this.deleteInvalidProfiles = deleteInvalidProfiles;
|
131
|
}
|
132
|
|
133
|
public boolean isRegisterMissingProfiles() {
|
134
|
return registerMissingProfiles;
|
135
|
}
|
136
|
|
137
|
public void setRegisterMissingProfiles(final boolean registerMissingProfiles) {
|
138
|
this.registerMissingProfiles = registerMissingProfiles;
|
139
|
}
|
140
|
}
|