-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathExecutableScriptService.java
More file actions
92 lines (66 loc) · 3.74 KB
/
ExecutableScriptService.java
File metadata and controls
92 lines (66 loc) · 3.74 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
package edu.mit.cci.pogs.service;
import edu.mit.cci.pogs.model.dao.executablescript.ExecutableScriptDao;
import edu.mit.cci.pogs.model.dao.executablescripthasresearchgroup.ExecutableScriptHasResearchGroupDao;
import edu.mit.cci.pogs.model.jooq.tables.pojos.ExecutableScript;
import edu.mit.cci.pogs.model.jooq.tables.pojos.ExecutableScriptHasResearchGroup;
import edu.mit.cci.pogs.service.base.ServiceBase;
import edu.mit.cci.pogs.utils.ObjectUtils;
import edu.mit.cci.pogs.view.executablescript.beans.ExecutableScriptBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class ExecutableScriptService extends ServiceBase {
private final ExecutableScriptHasResearchGroupDao executableScriptHasResearchGroupDao;
private final ExecutableScriptDao executableScriptDao;
@Autowired
public ExecutableScriptService(ExecutableScriptHasResearchGroupDao executableScriptHasResearchGroupDao, ExecutableScriptDao executableScriptDao) {
this.executableScriptHasResearchGroupDao = executableScriptHasResearchGroupDao;
this.executableScriptDao = executableScriptDao;
}
public List<ExecutableScriptHasResearchGroup> listExecutableScriptHasResearchGroupByDictionaryId(Long executableScriptId) {
return this.executableScriptHasResearchGroupDao.listByExecutableScriptId(executableScriptId);
}
private void createOrUpdateUserGroups(ExecutableScriptBean executableScriptBean) {
if (executableScriptBean.getResearchGroupRelationshipBean() == null && executableScriptBean.getResearchGroupRelationshipBean().getSelectedValues() == null) {
return;
}
List<Long> toCreate = new ArrayList<>();
List<Long> toDelete = new ArrayList<>();
List<ExecutableScriptHasResearchGroup> currentlySelected = listExecutableScriptHasResearchGroupByDictionaryId(executableScriptBean.getId());
List<Long> currentResearchGroups = currentlySelected
.stream()
.map(ExecutableScriptHasResearchGroup::getResearchGroupId)
.collect(Collectors.toList());
String[] newSelectedValues = executableScriptBean.getResearchGroupRelationshipBean().getSelectedValues();
UpdateResearchGroups(toCreate, toDelete, currentResearchGroups, newSelectedValues);
for (Long toCre : toCreate) {
ExecutableScriptHasResearchGroup rghau = new ExecutableScriptHasResearchGroup();
rghau.setExecutableScriptId(executableScriptBean.getId());
rghau.setResearchGroupId(toCre);
executableScriptHasResearchGroupDao.create(rghau);
}
for (Long toDel : toDelete) {
ExecutableScriptHasResearchGroup rghau = currentlySelected
.stream()
.filter(a -> (a.getExecutableScriptId() == executableScriptBean.getId() && a.getResearchGroupId() == toDel))
.findFirst().get();
executableScriptHasResearchGroupDao.delete(rghau);
}
}
public ExecutableScript createOrUpdate(ExecutableScriptBean executableScriptBean) {
ExecutableScript executableScript = new ExecutableScript();
ObjectUtils.Copy(executableScript, executableScriptBean);
if (executableScript.getId() == null) {
executableScript = executableScriptDao.create(executableScript);
executableScriptBean.setId(executableScript.getId());
createOrUpdateUserGroups(executableScriptBean);
} else {
executableScriptDao.update(executableScript);
createOrUpdateUserGroups(executableScriptBean);
}
return executableScript;
}
}