Skip to content

Commit ae1fbab

Browse files
authored
Merge pull request #457 from ncats/ep_extensions3
DBClassificationsCodeProcessor and SetAccessCodeProcessor
2 parents e7817be + f56b135 commit ae1fbab

2 files changed

Lines changed: 218 additions & 0 deletions

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package gsrs.module.substance.processors;
2+
3+
import gsrs.springUtils.StaticContextAccessor;
4+
import ix.core.EntityProcessor;
5+
import ix.ginas.models.v1.Code;
6+
import ix.ginas.models.v1.Reference;
7+
8+
import java.sql.Connection;
9+
import java.sql.PreparedStatement;
10+
import java.sql.ResultSet;
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
import javax.sql.DataSource;
14+
15+
import lombok.extern.slf4j.Slf4j;
16+
17+
import org.springframework.boot.jdbc.DataSourceBuilder;
18+
19+
/**
20+
* This EntityProcessor will create a Classifications comments string
21+
* for the Code using Database as the source
22+
*
23+
* @author Egor Puzanov
24+
*/
25+
26+
@Slf4j
27+
public class DBClassificationsCodeProcessor implements EntityProcessor<Code> {
28+
29+
private DataSource datasource = StaticContextAccessor.getBean(DataSource.class);
30+
private String codeSystem;
31+
private String query;
32+
33+
public DBClassificationsCodeProcessor() {
34+
this(new HashMap<String, Object>());
35+
}
36+
37+
public DBClassificationsCodeProcessor(Map<String, Object> m) {
38+
if (m.containsKey("dataSourceQualifier")) {
39+
setDataSourceQualifier((String) m.get("dataSourceQualifier"));
40+
}
41+
if (m.get("datasource") instanceof Map) {
42+
@SuppressWarnings("unchecked")
43+
Map<String, String> dsconfig = (Map<String, String>) m.get("datasource");
44+
setDatasource(dsconfig);
45+
}
46+
if (m.containsKey("codeSystem")) {
47+
setCodeSystem((String) m.get("codeSystem"));
48+
}
49+
if (m.containsKey("query")) {
50+
setQuery((String) m.get("query"));
51+
}
52+
}
53+
54+
public void setDataSourceQualifier(String dataSourceQualifier) {
55+
this.datasource = StaticContextAccessor.getBeanQualified(DataSource.class, dataSourceQualifier);
56+
}
57+
58+
public void setDatasource(Map<String, String> dsconfig) {
59+
this.datasource = DataSourceBuilder.create()
60+
.url((String)dsconfig.get("url"))
61+
.username((String)dsconfig.get("username"))
62+
.password((String)dsconfig.get("password"))
63+
.build();
64+
}
65+
66+
public void setCodeSystem(String codeSystem) {
67+
this.codeSystem = codeSystem;
68+
}
69+
70+
public void setQuery(String query) {
71+
this.query = query;
72+
}
73+
74+
@Override
75+
public void prePersist(Code obj) throws EntityProcessor.FailProcessingException {
76+
if (codeSystem.equals(obj.codeSystem) && obj.code != null && !obj.code.isEmpty()) {
77+
try (Connection c = datasource.getConnection()) {
78+
try (PreparedStatement s = c.prepareStatement(query)) {
79+
s.setString(1, obj.getCode());
80+
try (ResultSet rs = s.executeQuery()) {
81+
while (rs.next()) {
82+
String value = rs.getString(1);
83+
if (value != null && value.contains("|") && !value.equals(obj.comments)) {
84+
obj.comments = value;
85+
value = rs.getString(2);
86+
if (value != null && value.startsWith("http") && !value.equals(obj.url)) {
87+
obj.url = value;
88+
}
89+
value = rs.getString(3);
90+
if (value != null && !value.isEmpty() && obj.getReferences().size() == 0) {
91+
Reference r = new Reference();
92+
r.docType = value;
93+
value = rs.getString(4);
94+
if (value != null && !value.isEmpty()) {
95+
r.citation = value;
96+
obj.addReference(r, obj.getOwner());
97+
}
98+
}
99+
}
100+
break;
101+
}
102+
}
103+
}
104+
} catch (Exception ex) {
105+
log.error(ex.toString());
106+
} finally {
107+
}
108+
}
109+
}
110+
111+
@Override
112+
public void preUpdate(Code obj) throws EntityProcessor.FailProcessingException {
113+
prePersist(obj);
114+
}
115+
116+
@Override
117+
public Class<Code> getEntityClass() {
118+
return Code.class;
119+
}
120+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package gsrs.module.substance.processors;
2+
3+
import gov.nih.ncats.common.util.CachedSupplier;
4+
import gsrs.services.GroupService;
5+
6+
import ix.core.EntityProcessor;
7+
import ix.core.models.Group;
8+
import ix.ginas.models.v1.Code;
9+
10+
import java.util.HashMap;
11+
import java.util.LinkedHashSet;
12+
import java.util.Map;
13+
import java.util.Set;
14+
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
17+
/**
18+
*
19+
* @author Egor Puzanov
20+
*/
21+
22+
public class SetAccessCodeProcessor implements EntityProcessor<Code>{
23+
24+
private CachedSupplier<Void> initializer = CachedSupplier.runOnceInitializer(this::addGroupsIfNeeded);
25+
26+
@Autowired
27+
private GroupService groupService;
28+
29+
private final Map<String, Map<Object, Object>> with;
30+
private final Map<String, Set<Group>> codeSystemAccess = new HashMap<String, Set<Group>>();
31+
private final Set<Group> defaultAccess = new LinkedHashSet<Group>();
32+
33+
public SetAccessCodeProcessor(Map<String, Map<Object, Object>> with){
34+
this.with = with;
35+
}
36+
37+
public SetAccessCodeProcessor(){
38+
this(new HashMap<String, Map<Object, Object>>());
39+
}
40+
41+
public void setCodeSystemAccess(Map<Object, Object> o) {
42+
this.with.put("codeSystemAccess", o);
43+
}
44+
45+
public Map<String, Set<Group>> getCodeSystemAccess() {
46+
return this.codeSystemAccess;
47+
}
48+
49+
public void setDefaultAccess(String o) {
50+
setDefaultAccess(new HashMap<Object, Object>());
51+
}
52+
53+
public void setDefaultAccess(Map<Object, Object> o) {
54+
this.with.put("defaultAccess", o);
55+
}
56+
57+
public Set<Group> getDefaultAccess() {
58+
return this.defaultAccess;
59+
}
60+
61+
public void addGroupsIfNeeded(){
62+
defaultAccess.addAll(parseAccess(with.get("defaultAccess")));
63+
64+
for (Map.Entry<Object, Object> e : with.getOrDefault("codeSystemAccess", new HashMap<Object, Object>()).entrySet()) {
65+
codeSystemAccess.put(String.valueOf(e.getKey()), parseAccess(e.getValue()));
66+
}
67+
}
68+
69+
private Set<Group> parseAccess(Object accessObj) {
70+
Set<Group> access = new LinkedHashSet<Group>();
71+
if (accessObj instanceof Map) {
72+
@SuppressWarnings("unchecked")
73+
Map<Object, Object> accessMap = (Map<Object, Object>) accessObj;
74+
for (Object groupName : accessMap.values()) {
75+
access.add(groupService.registerIfAbsent(String.valueOf(groupName)));
76+
}
77+
}
78+
return access;
79+
}
80+
81+
@Override
82+
public void initialize() throws EntityProcessor.FailProcessingException{
83+
initializer.getSync();
84+
}
85+
86+
@Override
87+
public void prePersist(Code obj) throws EntityProcessor.FailProcessingException {
88+
Set<Group> access = codeSystemAccess.getOrDefault(obj.codeSystem, defaultAccess);
89+
if(!access.equals(obj.getAccess())){
90+
obj.setAccess(access);
91+
}
92+
}
93+
94+
@Override
95+
public Class<Code> getEntityClass() {
96+
return Code.class;
97+
}
98+
}

0 commit comments

Comments
 (0)