-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJobService.java
More file actions
602 lines (555 loc) · 26.5 KB
/
Copy pathJobService.java
File metadata and controls
602 lines (555 loc) · 26.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
package de.tum.cit.aet.job.service;
import de.tum.cit.aet.ai.domain.ComplianceIssue;
import de.tum.cit.aet.application.constants.ApplicationState;
import de.tum.cit.aet.application.domain.Application;
import de.tum.cit.aet.application.repository.ApplicationRepository;
import de.tum.cit.aet.core.constants.Language;
import de.tum.cit.aet.core.domain.DepartmentImage;
import de.tum.cit.aet.core.domain.Image;
import de.tum.cit.aet.core.dto.PageDTO;
import de.tum.cit.aet.core.dto.SortDTO;
import de.tum.cit.aet.core.exception.AccessDeniedException;
import de.tum.cit.aet.core.exception.EntityNotFoundException;
import de.tum.cit.aet.core.service.CurrentUserService;
import de.tum.cit.aet.core.service.ImageService;
import de.tum.cit.aet.core.util.HtmlSanitizer;
import de.tum.cit.aet.core.util.PageUtil;
import de.tum.cit.aet.core.util.StringUtil;
import de.tum.cit.aet.evaluation.constants.RejectReason;
import de.tum.cit.aet.interview.service.InterviewService;
import de.tum.cit.aet.job.constants.JobState;
import de.tum.cit.aet.job.constants.RecommendationType;
import de.tum.cit.aet.job.constants.SubjectArea;
import de.tum.cit.aet.job.domain.Job;
import de.tum.cit.aet.job.dto.*;
import de.tum.cit.aet.job.repository.JobRepository;
import de.tum.cit.aet.notification.constants.EmailType;
import de.tum.cit.aet.notification.dto.JobPublicationEmailContextDTO;
import de.tum.cit.aet.notification.service.AsyncEmailSender;
import de.tum.cit.aet.notification.service.EmailSettingService;
import de.tum.cit.aet.notification.service.mail.Email;
import de.tum.cit.aet.usermanagement.domain.User;
import de.tum.cit.aet.usermanagement.dto.ResearchGroupSummaryDTO;
import de.tum.cit.aet.usermanagement.repository.ApplicantRepository;
import de.tum.cit.aet.usermanagement.repository.ResearchGroupRepository;
import de.tum.cit.aet.usermanagement.repository.UserRepository;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class JobService {
private final JobRepository jobRepository;
private final UserRepository userRepository;
private final ApplicantRepository applicantRepository;
private final ResearchGroupRepository researchGroupRepository;
private final CurrentUserService currentUserService;
private final AsyncEmailSender sender;
private final EmailSettingService emailSettingService;
private final ApplicationRepository applicationRepository;
private final InterviewService interviewService;
private final JobImageHelper jobImageHelper;
private final ImageService imageService;
/**
* Creates a new job using the provided job form data.
*
* @param dto the job details used to create the job
* @return the created job as a {@link JobFormDTO}
*/
public JobFormDTO createJob(JobFormDTO dto) {
Job job = new Job();
return updateJobEntity(job, dto);
}
/**
* Updates an existing job with the new form data.
*
* @param jobId the ID of the job to update
* @param dto the {@link JobFormDTO} containing updated job details
* @return the updated job as a {@link JobFormDTO}
*/
public JobFormDTO updateJob(UUID jobId, JobFormDTO dto) {
Job job = assertCanManageJob(jobId);
return updateJobEntity(job, dto);
}
/**
* Changes the state of a job to the specified target state.
* If the job is being closed or if explicitly requested, all pending
* application states
* (i.e., in 'SAVED', 'SENT' or 'IN_REVIEW' state) for the job will be
* automatically updated.
*
* @param jobId the ID of the job whose state is to
* be changed
* @param targetState the new {@link JobState} to apply to
* the job
* @param shouldRejectRemainingApplications flag indicating whether remaining
* pending applications should be
* rejected
* @return the updated job as a {@link JobFormDTO}
*/
public JobFormDTO changeJobState(UUID jobId, JobState targetState, boolean shouldRejectRemainingApplications) {
Job job = assertCanManageJob(jobId);
JobState oldState = job.getState();
job.setState(targetState);
if (targetState == JobState.CLOSED) {
// send emails stating that the job has been closed, to all applicants whose
// application was 'SENT' or 'IN_REVIEW'
Set<Application> applicationsToNotify = applicationRepository.findApplicantsToNotify(jobId);
// update the state of all submitted and unsubmitted applications to
// 'JOB_CLOSED'
applicationRepository.updateApplicationsForJob(jobId, targetState.getValue());
notifyApplicants(applicationsToNotify, RejectReason.JOB_OUTDATED);
} else if (targetState == JobState.APPLICANT_FOUND && shouldRejectRemainingApplications) {
// send rejection emails to all applicants whose application was 'SENT' or
// 'IN_REVIEW'
Set<Application> applicationsToNotify = applicationRepository.findApplicantsToNotify(jobId);
// update the state of all submitted applications to 'REJECTED', all unsubmitted
// applications to 'JOB_CLOSED'
applicationRepository.updateApplicationsForJob(jobId, targetState.getValue());
notifyApplicants(applicationsToNotify, RejectReason.JOB_FILLED);
}
Job savedJob = jobRepository.save(job);
if (savedJob.getState() == JobState.PUBLISHED && oldState != JobState.PUBLISHED) {
notifySubjectAreaSubscribers(savedJob);
}
return JobFormDTO.getFromEntity(savedJob);
}
private void notifyApplicants(Set<Application> applications, RejectReason reason) {
for (Application application : applications) {
User user = application.getApplicant().getUser();
Email email = Email.builder()
.to(user)
.language(Language.fromCode(user.getSelectedLanguage()))
.emailType(reason.toEmailType())
.content(application)
.build();
sender.sendAsync(email);
}
}
/**
* Deletes a job posting by ID.
*
* @param jobId the ID of the job to delete
*/
public void deleteJob(UUID jobId) {
assertCanManageJob(jobId);
// Get the job to check if it has an associated image
Job job = jobRepository.findById(jobId).orElseThrow(() -> new EntityNotFoundException("Job not found"));
// Delete associated image if it exists and is not a default image
if (job.getImage() != null && !(job.getImage() instanceof DepartmentImage)) {
try {
imageService.deleteWithoutChecks(job.getImage().getImageId());
} catch (Exception _) {}
}
jobRepository.deleteById(jobId);
}
/**
* Returns a JobDTO given the job id.
* Job description fields are sanitized on read before sending to the client.
*
* @param jobId the ID of the job
* @return the job DTO with general job information
*/
public JobDTO getJobById(UUID jobId) {
Job job = assertCanManageJob(jobId);
return new JobDTO(
job.getJobId(),
job.getTitle(),
job.getResearchArea(),
job.getSubjectArea(),
job.getSupervisingProfessor().getUserId(),
job.getLocation(),
job.getStartDate(),
job.getEndDate(),
job.getWorkload(),
job.getContractDuration(),
job.getFundingType(),
job.getTvlGrade(),
HtmlSanitizer.sanitize(job.getJobDescriptionEN()),
HtmlSanitizer.sanitize(job.getJobDescriptionDE()),
job.getState(),
job.getImage() != null ? job.getImage().getImageId() : null,
job.getImage() != null ? job.getImage().getUrl() : null,
job.getSuitableForDisabled(),
job.getStartDateByArrangement(),
job.getReferenceLettersRequired(),
job.getRecommendationType(),
job.getGenderBiasScore(),
job.getComplianceIssues()
);
}
/**
* Returns a JobDetailDTO given the job id.
* Job description fields are sanitized on read before sending to the client.
*
* @param jobId the ID of the job
* @return the job detail DTO with detailed job information
*/
public JobDetailDTO getJobDetails(UUID jobId) {
// CurrentUserService is a request-scoped proxy; calling it from a
// background task thread (e.g. the admin bulk export running on
// taskExecutor) throws BeanCreationException at proxy-resolution
// time — before getUserIdIfAvailable's own try/catch runs. Treat
// "no active request" as "anonymous caller": the userId is only
// used downstream to look up the caller's own application state
// for the "already applied" indicator, which is irrelevant in an
// off-request context.
UUID userId;
try {
userId = currentUserService.getUserIdIfAvailable().orElse(null);
} catch (Exception e) {
userId = null;
}
Job job = jobRepository.findById(jobId).orElseThrow(() -> EntityNotFoundException.forId("Job", jobId));
UUID applicationId = null;
ApplicationState applicationState = null;
if (userId != null) {
Application application = applicationRepository.getByApplicantByUserIdAndJobId(userId, jobId);
if (application != null) {
applicationId = application.getApplicationId();
applicationState = application.getState();
}
}
return new JobDetailDTO(
job.getJobId(),
job.getSupervisingProfessor().getFirstName() + " " + job.getSupervisingProfessor().getLastName(),
ResearchGroupSummaryDTO.getFromEntity(job.getResearchGroup()),
job.getTitle(),
job.getSubjectArea(),
job.getResearchArea(),
job.getLocation(),
job.getWorkload(),
job.getContractDuration(),
job.getFundingType(),
job.getTvlGrade(),
HtmlSanitizer.sanitize(job.getJobDescriptionEN()),
HtmlSanitizer.sanitize(job.getJobDescriptionDE()),
job.getStartDate(),
job.getEndDate(),
job.getCreatedAt(),
job.getLastModifiedAt(),
job.getState(),
applicationId,
applicationState,
job.getSuitableForDisabled(),
job.getStartDateByArrangement(),
job.getReferenceLettersRequired(),
job.getRecommendationType(),
job.getImage() != null ? job.getImage().getImageId() : null
);
}
/**
* Returns a paginated list of all available (PUBLISHED) jobs.
* Supports filtering by multiple fields and dynamic sorting, including manual
* sort for professor name.
*
* @param pageDTO pagination configuration
* @param availableJobsFilterDTO DTO containing all optionally filterable fields
* @param sortDTO sort configuration (by field and direction)
* @param searchQuery string to search for job title, subject area
* or supervisor name
* @return a page of {@link JobCardDTO} matching the criteria
*/
public Page<JobCardDTO> getAvailableJobs(
PageDTO pageDTO,
AvailableJobsFilterDTO availableJobsFilterDTO,
SortDTO sortDTO,
String searchQuery
) {
UUID userId = currentUserService.getUserIdIfAvailable().orElse(null);
Pageable pageable;
String normalizedSearchQuery = StringUtil.normalizeSearchQuery(searchQuery);
List<SubjectArea> searchSubjectAreas = normalizedSearchQuery != null ? SubjectArea.search(normalizedSearchQuery) : null;
if (searchSubjectAreas != null && searchSubjectAreas.isEmpty()) {
searchSubjectAreas = null;
}
List<SubjectArea> subjectAreas = availableJobsFilterDTO.subjectAreas();
if (subjectAreas != null && subjectAreas.isEmpty()) {
subjectAreas = null;
}
if (sortDTO.sortBy() != null && sortDTO.sortBy().equals("professorName")) {
// Use pageable without sort: Sorting will be handled manually in @Query
pageable = PageUtil.createPageRequest(pageDTO, null, null, false);
return jobRepository.findAllJobCardsByState(
JobState.PUBLISHED,
subjectAreas,
availableJobsFilterDTO.locations(), // filter for campus location
availableJobsFilterDTO.professorNames(), // filter for supervising professor's full name
sortDTO.sortBy(),
sortDTO.direction().name(),
userId,
normalizedSearchQuery,
searchSubjectAreas,
pageable
);
} else {
// Sort dynamically via Pageable
pageable = PageUtil.createPageRequest(pageDTO, sortDTO, PageUtil.ColumnMapping.AVAILABLE_JOBS, true);
return jobRepository.findAllJobCardsByState(
JobState.PUBLISHED,
subjectAreas,
availableJobsFilterDTO.locations(), // optional filter for campus location
availableJobsFilterDTO.professorNames(), // optional filter for supervising professor's full name
userId,
normalizedSearchQuery,
searchSubjectAreas,
pageable
);
}
}
/**
* Retrieves all unique subject areas.
* This is used for filter dropdown options and should not be affected by
* current filters.
*
* @return a list of all unique subject areas sorted
* alphabetically
*/
public List<SubjectArea> getAllSubjectAreas() {
return jobRepository.findAllUniqueSubjectAreas(JobState.PUBLISHED);
}
/**
* Retrieves all unique supervisor names
* This is used for filter dropdown options and should not be affected by
* current filters.
*
* @return a list of all unique supervisor names sorted
* alphabetically
*/
public List<String> getAllSupervisorNames() {
return jobRepository.findAllUniqueSupervisorNames(JobState.PUBLISHED);
}
/**
* Returns a paginated list of jobs for the current user's research group (professor or employee).
* Supports optional filtering and dynamic sorting.
*
* @param pageDTO pagination configuration
* @param professorJobsFilterDTO DTO containing all optionally filterable fields
* @param sortDTO sorting configuration
* @param searchQuery search string for supervising professor or job
* title
* @return a page of {@link CreatedJobDTO} for the research group's jobs
*/
public Page<CreatedJobDTO> getJobsForCurrentResearchGroup(
PageDTO pageDTO,
ProfessorJobsFilterDTO professorJobsFilterDTO,
SortDTO sortDTO,
String searchQuery
) {
UUID researchGroupId = currentUserService.getResearchGroupIdIfMember();
Pageable pageable = PageUtil.createPageRequest(pageDTO, sortDTO, PageUtil.ColumnMapping.PROFESSOR_JOBS, true);
List<JobState> enumStates = null;
if (professorJobsFilterDTO.states() != null && !professorJobsFilterDTO.states().isEmpty()) {
enumStates = professorJobsFilterDTO.states().stream().map(JobState::fromValue).filter(Objects::nonNull).toList();
}
String normalizedSearchQuery = StringUtil.normalizeSearchQuery(searchQuery);
return jobRepository.findAllJobsByResearchGroup(researchGroupId, enumStates, normalizedSearchQuery, pageable);
}
/**
* Returns a paginated list of jobs across every research group for admin views.
* Supports optional filters for state, research group, and supervising professor,
* plus a search string matching job title or professor full name.
*
* @param pageDTO pagination configuration
* @param adminFilter DTO containing all optionally filterable fields
* @param sortDTO sorting configuration
* @param searchQuery search string for supervising professor or job title
* @return a page of {@link AdminCreatedJobDTO} matching the criteria
*/
public Page<AdminCreatedJobDTO> getAllJobs(PageDTO pageDTO, AdminJobsFilterDTO adminFilter, SortDTO sortDTO, String searchQuery) {
Pageable pageable = PageUtil.createPageRequest(pageDTO, sortDTO, PageUtil.ColumnMapping.PROFESSOR_JOBS, true);
List<JobState> enumStates = null;
if (adminFilter.states() != null && !adminFilter.states().isEmpty()) {
enumStates = adminFilter.states().stream().map(JobState::fromValue).filter(Objects::nonNull).toList();
}
List<UUID> researchGroupIds = (adminFilter.researchGroupIds() == null || adminFilter.researchGroupIds().isEmpty())
? null
: adminFilter.researchGroupIds();
List<UUID> supervisingProfessorIds = (adminFilter.supervisingProfessorIds() == null ||
adminFilter.supervisingProfessorIds().isEmpty())
? null
: adminFilter.supervisingProfessorIds();
String normalizedSearchQuery = StringUtil.normalizeSearchQuery(searchQuery);
return jobRepository.findAllJobsForAdmin(enumStates, researchGroupIds, supervisingProfessorIds, normalizedSearchQuery, pageable);
}
private JobFormDTO updateJobEntity(Job job, JobFormDTO dto) {
User supervisingProfessor = userRepository.findWithResearchGroupRolesByUserIdElseThrow(dto.supervisingProfessor());
// Ensure that the current user is either an admin or a research group member of
// the supervising professor
currentUserService.isAdminOrMemberOfResearchGroupOfProfessor(supervisingProfessor);
JobState oldState = job.getState();
// 1. Resolve the active research group of the editor: a job belongs to the group
// the user is currently acting on behalf of, not to some implicit "primary" group.
UUID activeResearchGroupId = currentUserService.getActiveResearchGroupId();
// 2. The supervising professor must be a member of that same active group.
boolean professorIsMember = supervisingProfessor
.getResearchGroupRoles()
.stream()
.anyMatch(
role -> role.getResearchGroup() != null && activeResearchGroupId.equals(role.getResearchGroup().getResearchGroupId())
);
if (!professorIsMember) {
throw new AccessDeniedException("Supervising professor is not a member of the active research group");
}
job.setSupervisingProfessor(supervisingProfessor);
job.setResearchGroup(
researchGroupRepository
.findById(activeResearchGroupId)
.orElseThrow(() -> EntityNotFoundException.forId("ResearchGroup", activeResearchGroupId))
);
job.setTitle(dto.title());
job.setResearchArea(dto.researchArea());
job.setSubjectArea(dto.subjectArea());
job.setLocation(dto.location());
job.setStartDate(dto.startDate());
job.setEndDate(dto.endDate());
job.setWorkload(dto.workload());
job.setContractDuration(dto.contractDuration());
job.setFundingType(dto.fundingType());
job.setTvlGrade(dto.tvlGrade());
job.setJobDescriptionEN(HtmlSanitizer.sanitize(dto.jobDescriptionEN()));
job.setJobDescriptionDE(HtmlSanitizer.sanitize(dto.jobDescriptionDE()));
job.setState(dto.state());
job.setSuitableForDisabled(dto.suitableForDisabled());
job.setStartDateByArrangement(Boolean.TRUE.equals(dto.startDateByArrangement()));
int referenceLettersRequired = Objects.requireNonNullElse(dto.referenceLettersRequired(), 0);
job.setReferenceLettersRequired(referenceLettersRequired);
job.setRecommendationType(
referenceLettersRequired > 0
? Objects.requireNonNullElse(dto.recommendationType(), RecommendationType.LETTER_AND_EVALUATION)
: null
);
// Capture old image before any modifications
Image oldImage = job.getImage();
// Update image reference (read-only lookup from imageRepository)
if (dto.imageId() != null) {
job.setImage(jobImageHelper.getImageForJob(dto.imageId()));
} else {
job.setImage(null);
}
// Save job entity first (single repository write)
Job savedJob = jobRepository.save(job);
if (dto.state() == JobState.PUBLISHED && oldState != JobState.PUBLISHED) {
interviewService.createInterviewProcessForJob(savedJob.getJobId());
notifySubjectAreaSubscribers(savedJob);
}
// Clean up old image after job is persisted (separate from job persistence)
jobImageHelper.replaceJobImage(oldImage, savedJob.getImage());
return JobFormDTO.getFromEntity(savedJob);
}
private void notifySubjectAreaSubscribers(Job job) {
Set<User> candidates = applicantRepository.findAllBySubjectAreaSubscription(job.getSubjectArea());
if (candidates.isEmpty()) {
return;
}
// 1) Collect candidate user IDs in one pass.
// 2) Resolve the enabled subset in a single query.
// 3) Fan out the async emails only to that subset.
Set<UUID> candidateIds = candidates.stream().map(User::getUserId).collect(Collectors.toSet());
Set<UUID> enabledIds = emailSettingService.filterEnabledUserIds(EmailType.JOB_PUBLISHED_SUBJECT_AREA, candidateIds);
candidates
.stream()
.filter(user -> enabledIds.contains(user.getUserId()))
.forEach(user ->
sender.sendAsync(
Email.builder()
.to(user)
.emailType(EmailType.JOB_PUBLISHED_SUBJECT_AREA)
.content(JobPublicationEmailContextDTO.fromEntities(user, job))
.language(Language.fromCode(user.getSelectedLanguage()))
.sendAlways(true)
.build()
)
);
}
/**
* Asserts that the current user can manage the job with the given ID.
*
* @param jobId the ID of the job to check
* @return the job entity if the user can manage it
*/
private Job assertCanManageJob(UUID jobId) {
Job job = jobRepository.findByIdWithCompliance(jobId).orElseThrow(() -> EntityNotFoundException.forId("Job", jobId));
currentUserService.isAdminOrMemberOf(job.getResearchGroup());
return job;
}
/**
* Updates the job description of a job in the specified language.
* The translated text is sanitized to remove unsafe HTML before persisting.
*
* @param jobId the ID of the job to update
* @param toLang the target language ("de" or "en")
* @param translatedText the translated job description text
*/
public void updateJobDescriptionLanguage(String jobId, String toLang, String translatedText) {
Job job = jobRepository.findById(UUID.fromString(jobId)).orElseThrow(() -> EntityNotFoundException.forId("Job", jobId));
String sanitized = HtmlSanitizer.sanitize(translatedText);
if ("de".equalsIgnoreCase(toLang)) {
job.setJobDescriptionDE(sanitized);
} else if ("en".equalsIgnoreCase(toLang)) {
job.setJobDescriptionEN(sanitized);
}
jobRepository.save(job);
}
/**
* Updates AI-generated analysis fields for a job: replaces the compliance
* issues for the given language and overwrites the combined gender bias score.
*
* @param jobId the job identifier
* @param score the combined AI score to persist
* @param complianceAnalysis compliance issues detected for the given language
* @param lang the analyzed language ("de" or "en")
*/
public void updateAiAnalysis(UUID jobId, int score, List<ComplianceIssue> complianceAnalysis, String lang) {
applyJobChangeForAnalysis(jobId, job -> {
replaceComplianceIssuesForLanguage(job, complianceAnalysis, lang);
job.setGenderBiasScore(score);
});
}
/**
* Replaces the compliance issues for a single language without touching the
* gender bias score. Used by the snippet-mapping flow, where the score has
* already been written by the source-language analysis and must not be reset.
*
* @param jobId the job identifier
* @param complianceAnalysis compliance issues for the target language
* @param lang the target language ("de" or "en")
*/
public void updateComplianceIssues(UUID jobId, List<ComplianceIssue> complianceAnalysis, String lang) {
applyJobChangeForAnalysis(jobId, job -> replaceComplianceIssuesForLanguage(job, complianceAnalysis, lang));
}
/**
* Loads the job, applies the given change, and persists in a single repository write.
*/
private void applyJobChangeForAnalysis(UUID jobId, Consumer<Job> changes) {
if (jobId == null) {
return;
}
Job job = jobRepository.findByIdWithCompliance(jobId).orElseThrow(() -> EntityNotFoundException.forId("Job", jobId));
currentUserService.isAdminOrMemberOf(job.getResearchGroup());
changes.accept(job);
jobRepository.save(job);
}
/**
* Replaces compliance issues for the given language.
* Issues from other languages stay unchanged.
* Updates the job in place and caller saves it.
*/
private void replaceComplianceIssuesForLanguage(Job job, List<ComplianceIssue> complianceAnalysis, String lang) {
List<ComplianceIssue> issuesToSave = job
.getComplianceIssues()
.stream()
.filter(issue -> !Objects.equals(issue.getLanguage(), lang))
.collect(Collectors.toCollection(ArrayList::new));
issuesToSave.addAll(complianceAnalysis);
job.setComplianceIssues(issuesToSave);
}
}