Skip to content

Commit

Permalink
Merge pull request #66 from lat-lon/feature/setPropertiesInDb
Browse files Browse the repository at this point in the history
Add support for mapping column names from dictionary
  • Loading branch information
tfr42 authored Nov 21, 2023
2 parents 30efc9a + 4759e71 commit 628f111
Show file tree
Hide file tree
Showing 16 changed files with 223 additions and 76 deletions.
6 changes: 6 additions & 0 deletions db.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ URL=jdbc:postgresql://localhost/petroparser
USER=petroparser
PASSWORD=petroparser
WOERTERBUCH=woerterbuch.woerterbuch
WBKUERZEL=kuerzel
WBKLARTEXT=klartext
WBTYP=typ
WBATTRIBUTE=attribute
SCHLUESSELTYPEN=woerterbuch.schluesseltypen
STNEBENTYPBEZ=nebentypbez
STDATENFELD=datenfeld
SCHLUESSELMAPPING=bml.bml_schluesselmapping
DATEFIELD=PETRO
130 changes: 107 additions & 23 deletions src/main/java/org/sep3tools/JavaConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.logging.Logger;

Expand All @@ -29,8 +33,20 @@ public final class JavaConnector {

private static String wb;

private static String wbKuerzel;

private static String wbKlartext;

private static String wbTyp;

private static String wbAttribute;

private static String st;

private static String stDatenfeld;

private static String stNebentypbez;

private static String sm;

private static String df;
Expand All @@ -53,7 +69,13 @@ public static void setPropertiesFile(String filename) {
setUser(properties.getProperty("USER"));
setPass(properties.getProperty("PASSWORD"));
setWb(properties.getProperty("WOERTERBUCH"));
setWbAttribute(properties.getProperty("WBATTRIBUTE"));
setWbKlartext(properties.getProperty("WBKLARTEXT"));
setWbKuerzel(properties.getProperty("WBKUERZEL"));
setWbTyp(properties.getProperty("WBTYP"));
setSt(properties.getProperty("SCHLUESSELTYPEN"));
setStDatenfeld(properties.getProperty("STDATENFELD"));
setStNebentypbez(properties.getProperty("STNEBENTYPBEZ"));
setSm(properties.getProperty("SCHLUESSELMAPPING"));
setDf(properties.getProperty("DATEFIELD"));
}
Expand All @@ -65,14 +87,6 @@ public static void setPropertiesFile(String filename) {
}
}

public static void setWb(String wb) {
JavaConnector.wb = wb;
}

public static void setSt(String st) {
JavaConnector.st = st;
}

public static void setSm(String sm) {
JavaConnector.sm = sm;
}
Expand All @@ -96,12 +110,44 @@ public static void setUrl(String newUrl) {
JavaConnector.m_url = newUrl;
}

public static void setWb(String wb) {
JavaConnector.wb = wb;
}

public static void setWbKuerzel(String wbKuerzel) {
JavaConnector.wbKuerzel = wbKuerzel;
}

public static void setWbKlartext(String wbKlartext) {
JavaConnector.wbKlartext = wbKlartext;
}

public static void setWbTyp(String wbTyp) {
JavaConnector.wbTyp = wbTyp;
}

public static void setWbAttribute(String wbAttribute) {
JavaConnector.wbAttribute = wbAttribute;
}

public static void setSt(String st) {
JavaConnector.st = st;
}

public static void setStDatenfeld(String stDatenfeld) {
JavaConnector.stDatenfeld = stDatenfeld;
}

public static void setStNebentypbez(String stNebentypbez) {
JavaConnector.stNebentypbez = stNebentypbez;
}

// String query = "SELECT Klartext from woerterbuch.Woerterbuch where Kuerzel=";

private static void setConn(String url, String user, String pass) throws SQLException {
private static void setConn() throws SQLException {
if (conn != null && !conn.isClosed())
conn.close();
JavaConnector.conn = DriverManager.getConnection(url, user, pass);
JavaConnector.conn = DriverManager.getConnection(m_url, user, pass);
credChanged = false;
}

Expand All @@ -115,7 +161,7 @@ public static String getS3Name(String sep3Code) throws SQLException {
String query = getQueryString(wb, st, df);

if (credChanged)
setConn(m_url, user, pass);
setConn();
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, sep3Code);
LOG.fine("Executing statement: " + stmt);
Expand All @@ -135,9 +181,9 @@ public static String getS3Name(String sep3Code) throws SQLException {
}

private static String getQueryString(String woerterbuch, String schluesselypen, String datenfeld) {
return "select kuerzel, klartext from " + woerterbuch + " w join " + schluesselypen + " s "
+ "on w.typ = s.nebentypbez where (s.datenfeld = '" + datenfeld + "' "
+ "OR s.datenfeld = 'diverse') AND kuerzel= ?";
return "select " + wbKuerzel + ", " + wbKlartext + " from " + woerterbuch + " w join " + schluesselypen + " s "
+ "on w." + wbTyp + " = s." + stNebentypbez + " where (s." + stDatenfeld + " = '" + datenfeld + "' "
+ "OR s." + stDatenfeld + " = 'diverse') AND " + wbKuerzel + " = ?";
}

public static String getS3inDfName(String datefield, String sep3Code) throws SQLException {
Expand All @@ -163,7 +209,7 @@ public static String getS3inDfName(String datefield, String sep3Code) throws SQL
}

if (credChanged)
setConn(m_url, user, pass);
setConn();
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, sep3Code);
LOG.fine("Executing statement: " + stmt);
Expand Down Expand Up @@ -192,7 +238,7 @@ public static String getS3AsBMmlLitho(String sep3Code) throws SQLException {
String query = "select bml_code from " + sm + " where sep3_codelist = 'S3PETRO' AND sep3_code = ?";

if (credChanged)
setConn(m_url, user, pass);
setConn();
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, sep3Code);
LOG.fine("Executing statement: " + stmt);
Expand All @@ -211,20 +257,57 @@ public static String getS3AsBMmlLitho(String sep3Code) throws SQLException {
}
}

/**
* sets db properties for table and column names from database table
* @throws SQLException if DB error occurs
*/
public static void setPropertiesFromDB() throws SQLException {
String dbpart = "part";
String dbvalue = "value";
String mdtable = "public.sep3tools";
Properties properties = new Properties();

String query = "select " + dbpart + ", " + dbvalue + " from " + mdtable;
if (credChanged)
setConn();
PreparedStatement stmt = conn.prepareStatement(query);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
properties.setProperty(rs.getString(1), rs.getString(2));
}
setWb(properties.getProperty("WOERTERBUCH"));
setWbAttribute(properties.getProperty("WBATTRIBUTE"));
setWbKlartext(properties.getProperty("WBKLARTEXT"));
setWbKuerzel(properties.getProperty("WBKUERZEL"));
setWbTyp(properties.getProperty("WBTYP"));
setSt(properties.getProperty("SCHLUESSELTYPEN"));
setStDatenfeld(properties.getProperty("STDATENFELD"));
setStNebentypbez(properties.getProperty("STNEBENTYPBEZ"));
setSm(properties.getProperty("SCHLUESSELMAPPING"));
setDf(properties.getProperty("DATEFIELD"));
// System.out.println (properties.toString());

rs.close();
stmt.close();
}
}

/**
* Retrieves allowed attributes for a given SEP3 code
* @param sep3Code for which attributes are requested
* @return String containing information about allowed attributes, quantifiers, etc.
* @throws SQLException if DB error occurs
*/
public static String getAllowedAttribs(String sep3Code) throws SQLException {
String query = "select kuerzel, attribute from " + wb + " w join " + st + " s " + "on w.typ = s.nebentypbez "
+ "where (s.datenfeld = 'PETRO' OR s.datenfeld = 'diverse') AND kuerzel= ?";
String query = "select " + wbKuerzel + ", " + wbAttribute + " from " + wb + " w join " + st + " s " + "on w."
+ wbTyp + " = s." + stNebentypbez + " where (s." + stDatenfeld + " = 'PETRO' OR s." + stDatenfeld
+ " = 'diverse') AND " + wbKuerzel + " = ?";
if (credChanged)
setConn(m_url, user, pass);
setConn();
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, sep3Code);
LOG.fine("Executing statement: " + stmt);

try (ResultSet rs = stmt.executeQuery()) {
boolean validRS = rs.next();
String result = "";
Expand Down Expand Up @@ -254,10 +337,11 @@ public static String getItemQuant(String sep3Code, String quant) throws SQLExcep
allowedAttributes = getAllowedAttribs(sep3Code);
quantBez = getQuantBezFromAttribs(allowedAttributes);

String query = "select w.kuerzel, w.klartext, s.nebentypbez from " + wb + " w join " + st + " s "
+ "on w.typ = s.nebentypbez where (s.nebentypbez = ? AND w.kuerzel = ?);";
String query = "select w." + wbKuerzel + ", w." + wbKlartext + ", s." + stNebentypbez + " from " + wb
+ " w join " + st + " s " + "on w." + wbTyp + " = s." + stNebentypbez + " where (s. " + stNebentypbez
+ " = ? AND w." + wbKuerzel + " = ?);";
if (credChanged)
setConn(m_url, user, pass);
setConn();
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, quantBez);
stmt.setString(2, quant);
Expand Down
50 changes: 43 additions & 7 deletions src/main/java/org/sep3tools/Launch.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.postgresql.pljava.annotation.Function;
import org.sep3tools.gen.*;

import java.sql.SQLException;
import org.sep3tools.gen.PetroGrammarLexer;
import org.sep3tools.gen.PetroGrammarParser;

/**
* Command line launcher for SEP3 tools
Expand Down Expand Up @@ -53,7 +52,7 @@ else if (args.length == 2) {
+ "\"G(fg-gg,ms-gs,mats,mata,grs(tw)),fX-mX(mata),mS(fs,grs,fg-mg2,mx(voe))\"");
return;
}
String visit = S3_AsText(sep3String);
String visit = convertS3ToText(sep3String);
System.out.println(visit);
}

Expand All @@ -62,7 +61,7 @@ else if (args.length == 2) {
* @param s3String coded SEP3 string parsing
* @return human readable format of SEP3 input
*/
protected static String S3_AsText(String s3String) {
protected static String convertS3ToText(String s3String) {
CharStream input = CharStreams.fromString(s3String);
PetroGrammarLexer lexer = new PetroGrammarLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
Expand All @@ -87,7 +86,44 @@ public static String S3_AsText(String s3String, String wb, String st, String df)
JavaConnector.setWb(wb);
JavaConnector.setSt(st);
JavaConnector.setDf(df);
return S3_AsText(s3String);
return convertS3ToText(s3String);
}
catch (Exception e) {
return "";
}
}

/**
* translates a coded SEP3 String to a human readable format. needed for in database
* use. Returns empty String, if exception is catched.
* @param s3String coded SEP3 string parsing
* @param df Datenfeld
* @return human readable format of SEP3 input
*/
@Function
public static String S3_AsText(String s3String, String df) {
try {
JavaConnector.setPropertiesFromDB();
JavaConnector.setDf(df);
return convertS3ToText(s3String);
}
catch (Exception e) {
return "";
}
}

/**
* translates a coded SEP3 String to a human readable format. Needed for in database
* use. Returns empty String, if exception is catched. Uses configured standard
* datefiled
* @param s3String coded SEP3 string parsing
* @return human readable format of SEP3 input
*/
@Function
public static String S3_AsText(String s3String) {
try {
JavaConnector.setPropertiesFromDB();
return convertS3ToText(s3String);
}
catch (Exception e) {
return "";
Expand All @@ -110,4 +146,4 @@ public static String S3_AsText_verbose(String s3String, String wb, String st, St
return S3_AsText(s3String);
}

}
}
2 changes: 1 addition & 1 deletion src/main/java/org/sep3tools/PetroVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.sep3tools.gen.*;

import java.sql.*;
import java.sql.SQLException;
import java.util.logging.Logger;

import static java.util.Objects.isNull;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/beschbgtest.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
kb2,kb3(un)=schlechte Kornbindung, mäßige Kornbindung (unten)
kb2,kb3(un)=schlechte Kornbindung, mäßige Kornbindung (unten)
idm=Mitte
(stf-hfe)(ob),(stf)(idm),(stf-hfe,stf)(un)=(steif bis halbfest) (oben), (steif) (Mitte), (steif bis halbfest, steif) (unten)
2 changes: 1 addition & 1 deletion src/main/resources/beschbvtest.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
spvl(30%-40%,tv(29.02-29.23))=Spülverlust [Prozent] (30% bis 40%, Teufe von (29,02 bis 29,23))
spvl(30%-40%,tv(29.02-29.23))=Spülverlust [Prozent] (30% bis 40%, Teufe von (29,02 bis 29,23))
2 changes: 1 addition & 1 deletion src/main/resources/bgruppetest.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
GE=Kies, enggestuft
GE=Kies, enggestuft
8 changes: 4 additions & 4 deletions src/main/resources/farbetest.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ olst=olivstichig
sw=schwarz
ge=gelb
dgr=dunkelgrau
gn=grün
gn=grün
rs=rosa
ddro=sehr dunkelrot
holst=hellolivstichig
gnli=grünlich
gnli=grünlich
grst=graustichig
robn=rotbraun
blro=blaurot
mtol=mattoliv
mtolst=mattolivstichig
drohbngr,blro,ddro,hgrbn,mtgr,mtol,mtolst=dunkelrothellbraungrau, blaurot, sehr dunkelrot, hellgraubraun, mattgrau, mattoliv, mattolivstichig
dro-hgr=dunkelrot bis hellgrau
robn,sw,ge,dgr-gn,swgerobnrs=rotbraun, schwarz, gelb, dunkelgrau bis grün, schwarzgelbrotbraunrosa
gnli,grst=grünlich, graustichig
robn,sw,ge,dgr-gn,swgerobnrs=rotbraun, schwarz, gelb, dunkelgrau bis grün, schwarzgelbrotbraunrosa
gnli,grst=grünlich, graustichig
6 changes: 3 additions & 3 deletions src/main/resources/fremddatenfeldtest.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
G(F\:ro)=Kies [gerundet] (rot)
G(F\:ddro)=Kies [gerundet] (sehr dunkelrot)
U(hz(res),H(res),zg1,F\:mtolst)=Schluff (Holzreste (Reste), Torf (Reste), sehr schwach zersetzt, mattolivstichig)
G(F:ro)=Kies [gerundet] (rot)
G(F:ddro)=Kies [gerundet] (sehr dunkelrot)
U(hz(res),H(res),zg1,F:mtolst)=Schluff (Holzreste (Reste), Torf (Reste), sehr schwach zersetzt, mattolivstichig)

16 changes: 8 additions & 8 deletions src/main/resources/genesetest.properties
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
as,Lou(lok)=Abspülung, Schwemmlöss (lokal)
Mg,Gb(P\:^kkn)=Geschiebemergel, Geschiebe (Knollenkalk)
y=Auffüllung [allgemein]
as,Lou(lok)=Abspülung, Schwemmlöss (lokal)
Mg,Gb(P:^kkn)=Geschiebemergel, Geschiebe (Knollenkalk)
y=Auffüllung [allgemein]
ya=Asche (anthropogen)
lok=lokal
y,(ya,P\:fG)(lok)=Auffüllung [allgemein], (Asche (anthropogen), Feinkies [2,0-6,3 mm]) (lokal)
(P\:t4)=(stark tonig)
y,(ya,P:fG)(lok)=Auffüllung [allgemein], (Asche (anthropogen), Feinkies [2,0-6,3 mm]) (lokal)
(P:t4)=(stark tonig)
(Mg,lw)=(Geschiebemergel, lagenweise)
gf,g=glazifluviatil, glaziär
(P\:t4)(Mg,lw),gf,g=(stark tonig) (Geschiebemergel, lagenweise), glazifluviatil, glaziär
(P\:fs,P\:ms)(Mg),g=(feinsandig, mittelsandig) (Geschiebemergel), glaziär
gf,g=glazifluviatil, glaziär
(P:t4)(Mg,lw),gf,g=(stark tonig) (Geschiebemergel, lagenweise), glazifluviatil, glaziär
(P:fs,P:ms)(Mg),g=(feinsandig, mittelsandig) (Geschiebemergel), glaziär
Loading

0 comments on commit 628f111

Please sign in to comment.