1
|
package eu.dnetlib.validator.web.api.impls;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.HashMap;
|
5
|
import java.util.HashSet;
|
6
|
import java.util.LinkedHashMap;
|
7
|
import java.util.List;
|
8
|
import java.util.Map;
|
9
|
import java.util.Set;
|
10
|
|
11
|
import org.apache.log4j.Logger;
|
12
|
import org.springframework.transaction.annotation.Propagation;
|
13
|
import org.springframework.transaction.annotation.Transactional;
|
14
|
|
15
|
import eu.dnetlib.api.functionality.ValidatorService;
|
16
|
import eu.dnetlib.api.functionality.ValidatorServiceException;
|
17
|
import eu.dnetlib.domain.functionality.validator.JobForValidation;
|
18
|
import eu.dnetlib.domain.functionality.validator.RuleSet;
|
19
|
import eu.dnetlib.domain.functionality.validator.StoredJob;
|
20
|
import eu.dnetlib.validator.web.api.ValidatorWebApi;
|
21
|
import eu.dnetlib.validator.web.api.ValidatorWebException;
|
22
|
import gr.uoa.di.driver.util.ServiceLocator;
|
23
|
|
24
|
|
25
|
/**
|
26
|
*
|
27
|
* @author Nikon Gasparis
|
28
|
*
|
29
|
*/
|
30
|
|
31
|
@Transactional(propagation = Propagation.REQUIRED)
|
32
|
public class ValidatorWebApiImpl implements ValidatorWebApi {
|
33
|
|
34
|
|
35
|
private ServiceLocator<ValidatorService> validatorService = null;
|
36
|
|
37
|
private List<String> masterAdmins = new ArrayList<String>();
|
38
|
|
39
|
private List<String> secondaryAdmins = new ArrayList<String>();
|
40
|
|
41
|
private List<String> blacklistedUsers = new ArrayList<String>();
|
42
|
|
43
|
|
44
|
private static Logger logger = Logger.getLogger(ValidatorWebApiImpl.class);
|
45
|
|
46
|
public void start(){
|
47
|
}
|
48
|
|
49
|
@Override
|
50
|
public boolean userOverridesRepoRegistration(String user)
|
51
|
throws ValidatorWebException {
|
52
|
logger.debug("checking if user " + user + " overrides repo registration procedures");
|
53
|
if (this.masterAdmins.contains(user) || this.secondaryAdmins.contains(user)) {
|
54
|
logger.debug("overrides");
|
55
|
return true;
|
56
|
} else {
|
57
|
logger.debug("not overrides");
|
58
|
return false;
|
59
|
}
|
60
|
}
|
61
|
|
62
|
@Override
|
63
|
public StoredJob getJobSummary(int jobId, String groupBy)
|
64
|
throws ValidatorWebException {
|
65
|
try {
|
66
|
logger.info("getting job summary for id: " + jobId);
|
67
|
return validatorService.getService().getStoredJob(jobId, groupBy);
|
68
|
} catch (ValidatorServiceException e) {
|
69
|
logger.error("Error getting job summary for id: " + jobId, e);
|
70
|
throw new ValidatorWebException(e);
|
71
|
}
|
72
|
}
|
73
|
|
74
|
@Override
|
75
|
public List<StoredJob> getJobsOfUser(String user, String jobType, Integer offset, Integer limit, String dateFrom, String dateTo) throws ValidatorWebException {
|
76
|
try {
|
77
|
logger.debug("getting jobs of user " + user);
|
78
|
List<StoredJob> jobs;
|
79
|
if (this.userOverridesRepoRegistration(user))
|
80
|
jobs = validatorService.getService().getStoredJobs(null, jobType, offset, limit, null, null);
|
81
|
else
|
82
|
jobs = validatorService.getService().getStoredJobs(user, jobType, offset, limit, null, null);
|
83
|
return jobs;
|
84
|
} catch (Exception e) {
|
85
|
logger.error("Error getting jobs of user " + user, e);
|
86
|
throw new ValidatorWebException(e);
|
87
|
}
|
88
|
}
|
89
|
|
90
|
@Override
|
91
|
public int getJobsTotalNumberOfUser(String user, String jobType) throws ValidatorWebException {
|
92
|
try {
|
93
|
logger.debug("getting jobs of user " + user);
|
94
|
int sum;
|
95
|
if (this.userOverridesRepoRegistration(user))
|
96
|
sum = validatorService.getService().getStoredJobsTotalNumber(null, jobType);
|
97
|
else
|
98
|
sum = validatorService.getService().getStoredJobsTotalNumber(user, jobType);
|
99
|
return sum;
|
100
|
} catch (Exception e) {
|
101
|
logger.error("Error getting jobs of user " + user, e);
|
102
|
throw new ValidatorWebException(e);
|
103
|
}
|
104
|
}
|
105
|
|
106
|
@Deprecated
|
107
|
public Map<String, List<StoredJob>> getJobsOfUserSplitted(String user) throws ValidatorWebException {
|
108
|
try {
|
109
|
logger.info("getting jobs of user " + user);
|
110
|
Map< String, List<StoredJob>> jobsSplitted = new HashMap< String, List<StoredJob>>();
|
111
|
List<StoredJob> jobs = null;
|
112
|
// userMail,jobType, offset, limit
|
113
|
if (this.userOverridesRepoRegistration(user))
|
114
|
jobs = validatorService.getService().getStoredJobs(null, null, 0, 5, null, null);
|
115
|
else
|
116
|
jobs = validatorService.getService().getStoredJobs(user, null, 0, 5, null, null);
|
117
|
|
118
|
List<StoredJob> regList, compList, workflowList;
|
119
|
regList = new ArrayList<StoredJob>();
|
120
|
compList = new ArrayList<StoredJob>();
|
121
|
workflowList = new ArrayList<StoredJob>();
|
122
|
for (StoredJob job : jobs) {
|
123
|
if (job.getJobType().equals("Registration Request"))
|
124
|
regList.add(job);
|
125
|
else if (job.getJobType().equals("Compatibility Test"))
|
126
|
compList.add(job);
|
127
|
else if (job.getJobType().equals("Workflow Request"))
|
128
|
workflowList.add(job);
|
129
|
}
|
130
|
jobsSplitted.put("Registration Request", regList);
|
131
|
jobsSplitted.put("Compatibility Test", compList);
|
132
|
jobsSplitted.put("Workflow Request", workflowList);
|
133
|
return jobsSplitted;
|
134
|
} catch (Exception e) {
|
135
|
logger.error("Error getting jobs of user " + user, e);
|
136
|
throw new ValidatorWebException(e);
|
137
|
}
|
138
|
}
|
139
|
|
140
|
@Override
|
141
|
public Map<String, String> getRuleSetsMap(String deployEnvironment) throws ValidatorWebException {
|
142
|
Map<String, String> map = new LinkedHashMap<String, String>();
|
143
|
logger.info("Getting rulesets map");
|
144
|
List<RuleSet> ruleSets;
|
145
|
try {
|
146
|
ruleSets = validatorService.getService().getRuleSets();
|
147
|
for(RuleSet ruleSet : ruleSets)
|
148
|
{
|
149
|
if (ruleSet.getVisibility() != null && ruleSet.getVisibility().contains(deployEnvironment))
|
150
|
map.put(ruleSet.getGuidelinesAcronym(), ruleSet.getName());
|
151
|
}
|
152
|
} catch (Exception e) {
|
153
|
logger.error("Error getting rulesets map", e);
|
154
|
throw new ValidatorWebException(e);
|
155
|
}
|
156
|
return map;
|
157
|
}
|
158
|
|
159
|
@Override
|
160
|
public RuleSet getRuleSet(String acronym) throws ValidatorWebException {
|
161
|
logger.info("Getting ruleset with acronym: " + acronym);
|
162
|
RuleSet ruleSet = null;
|
163
|
try {
|
164
|
for(RuleSet rSet : validatorService.getService().getRuleSets())
|
165
|
{
|
166
|
if (rSet.getGuidelinesAcronym().equals(acronym))
|
167
|
ruleSet = rSet;
|
168
|
|
169
|
}
|
170
|
return ruleSet;
|
171
|
} catch (Exception e) {
|
172
|
logger.error("Error getting ruleset", e);
|
173
|
throw new ValidatorWebException(e);
|
174
|
}
|
175
|
}
|
176
|
|
177
|
@Override
|
178
|
public void submitValidationJob(JobForValidation job)
|
179
|
throws ValidatorWebException {
|
180
|
logger.info("Submitting validation job");
|
181
|
try {
|
182
|
validatorService.getService().submitValidationJob(job);
|
183
|
} catch (Exception e) {
|
184
|
logger.error("Submitting validation job", e);
|
185
|
throw new ValidatorWebException(e);
|
186
|
}
|
187
|
}
|
188
|
|
189
|
@Override
|
190
|
public void reSubmitValidationJob(JobForValidation job, Set<Integer> rules)
|
191
|
throws ValidatorWebException {
|
192
|
logger.info("Re-Submitting validation job");
|
193
|
try {
|
194
|
Set<Integer> contentRules = new HashSet<Integer>();
|
195
|
Set<Integer> usageRules = new HashSet<Integer>();
|
196
|
RuleSet ruleSet = this.getRuleSet(job.getDesiredCompatibilityLevel());
|
197
|
for (int ruleId : rules) {
|
198
|
if (ruleSet.getContentRulesIds().contains(ruleId))
|
199
|
contentRules.add(ruleId);
|
200
|
else if (ruleSet.getUsageRulesIds().contains(ruleId))
|
201
|
usageRules.add(ruleId);
|
202
|
}
|
203
|
if (!contentRules.isEmpty())
|
204
|
job.setSelectedContentRules(contentRules);
|
205
|
if (!usageRules.isEmpty())
|
206
|
job.setSelectedUsageRules(usageRules);
|
207
|
validatorService.getService().submitValidationJob(job);
|
208
|
} catch (Exception e) {
|
209
|
logger.error("Re-Submitting validation job", e);
|
210
|
throw new ValidatorWebException(e);
|
211
|
}
|
212
|
}
|
213
|
|
214
|
|
215
|
public ServiceLocator<ValidatorService> getValidatorService() {
|
216
|
return validatorService;
|
217
|
}
|
218
|
|
219
|
public void setValidatorService(
|
220
|
ServiceLocator<ValidatorService> validatorService) {
|
221
|
this.validatorService = validatorService;
|
222
|
}
|
223
|
|
224
|
@Override
|
225
|
public boolean userIsMasterAdmin(String user)
|
226
|
throws ValidatorWebException {
|
227
|
logger.debug("checking if user " + user + " is master admin");
|
228
|
if (this.masterAdmins.contains(user)) {
|
229
|
logger.debug("is master admin");
|
230
|
return true;
|
231
|
} else {
|
232
|
logger.debug("isn't master admin");
|
233
|
return false;
|
234
|
}
|
235
|
}
|
236
|
|
237
|
@Override
|
238
|
public boolean userIsSecondaryAdmin(String user)
|
239
|
throws ValidatorWebException {
|
240
|
logger.debug("checking if user " + user + " is secondary admin");
|
241
|
if (this.secondaryAdmins.contains(user)) {
|
242
|
logger.debug("is secondary admin");
|
243
|
return true;
|
244
|
} else {
|
245
|
logger.debug("isn't secondary admin");
|
246
|
return false;
|
247
|
}
|
248
|
}
|
249
|
|
250
|
|
251
|
public void setMasterAdmins(String masterAdmins) {
|
252
|
String[] recps = masterAdmins.split(",");
|
253
|
|
254
|
for (String recp : recps) {
|
255
|
recp = recp.trim();
|
256
|
this.masterAdmins.add(recp);
|
257
|
}
|
258
|
}
|
259
|
|
260
|
public void setSecondaryAdmins(String secondaryAdmins) {
|
261
|
String[] recps = secondaryAdmins.split(",");
|
262
|
|
263
|
for (String recp : recps) {
|
264
|
recp = recp.trim();
|
265
|
this.secondaryAdmins.add(recp);
|
266
|
}
|
267
|
}
|
268
|
|
269
|
public void setBlacklistedUsers(String blacklistedUsers) {
|
270
|
String[] recps = blacklistedUsers.split(",");
|
271
|
|
272
|
for (String recp : recps) {
|
273
|
recp = recp.trim();
|
274
|
this.blacklistedUsers.add(recp);
|
275
|
}
|
276
|
}
|
277
|
|
278
|
public boolean isUserBlacklisted(String email) throws ValidatorWebException {
|
279
|
logger.debug("checking if user " + email + " is blacklisted");
|
280
|
if (this.blacklistedUsers.contains(email)) {
|
281
|
logger.debug("is blacklisted");
|
282
|
return true;
|
283
|
} else {
|
284
|
logger.debug("isn't blacklisted");
|
285
|
return false;
|
286
|
}
|
287
|
}
|
288
|
}
|
289
|
|