|
| 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 | +} |
0 commit comments