Prepare for more Client Implementations (#56)

This commit is contained in:
2020-09-28 14:29:47 +02:00
committed by GitHub
parent 9ba508d8d5
commit 98d53ea161
5 changed files with 41 additions and 10 deletions

View File

@@ -1,6 +1,6 @@
package de.obermui.cabr2.cli;
import de.obermui.cabr2.clients.GESTIS;
import de.obermui.cabr2.clients.*;
import de.obermui.cabr2.intern.Sheet2Html;
import de.obermui.cabr2.intern.html2pdf;
import de.obermui.cabr2.models.Personal;
@@ -22,10 +22,12 @@ public class MainCLI {
SafetyDataSheet sheet = new SafetyDataSheet();
Client client = new GESTIS();
boolean next = true;
while (next) {
String[] content = GESTIS.SimpleSearch(query, false);
String[] content = client.SimpleSearch(query, false);
for (int i = 0; i < content.length; i++) {
System.out.println(i + ":\t" + content[i]);
}
@@ -36,7 +38,7 @@ public class MainCLI {
System.out.println("invalid number: '" + i + "'");
continue;
}
Substance s = GESTIS.getSubstance(content[i], null);
Substance s = client.GetSubstance(content[i], null);
if (s.CAS != null) {
sheet.addSubstance(s);
}

View File

@@ -0,0 +1,18 @@
package de.obermui.cabr2.clients;
import de.obermui.cabr2.models.Substance;
import de.obermui.cabr2.models.SubstanceShort;
import java.util.List;
public interface Client {
// GetSubstance return Substance based on ZVG and have a fallback to name
Substance GetSubstance(String name, String ZVG);
// Search return list of Substances (SubstanceShort) based on a keyword
// if exact is true it will look only for exact matches
List<SubstanceShort> Search(String keyword, boolean exact);
// SimpleSearch works similar to Search but only return names of substances in a string list
String[] SimpleSearch(String keyword, boolean exact);
}

View File

@@ -19,11 +19,12 @@ import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class GESTIS {
public class GESTIS implements Client {
private static final String gestis_en = "http://gestis-en.itrust.de";
private static final String gestis_de = "http://gestis.itrust.de";
public static String[] SimpleSearch(String keyword, boolean exact) {
@Override
public String[] SimpleSearch(String keyword, boolean exact) {
keyword = keyword.trim();
if (!exact) {
@@ -51,7 +52,8 @@ public class GESTIS {
return result;
}
public static List<SubstanceShort> Search(String keyword, boolean exact) {
@Override
public List<SubstanceShort> Search(String keyword, boolean exact) {
List<SubstanceShort> result = new ArrayList<>();
if (!exact) {
@@ -126,7 +128,8 @@ public class GESTIS {
return result;
}
public static Substance getSubstance(String name, String ZVG) {
@Override
public Substance GetSubstance(String name, String ZVG) {
Substance s = new Substance();
if (ZVG == null) {
@@ -339,7 +342,7 @@ public class GESTIS {
}
private static String getZVGfromName(String name) {
List<SubstanceShort> s = Search(name, true);
List<SubstanceShort> s = new GESTIS().Search(name, true);
if (s.size() == 0) {
return "";
}

View File

@@ -57,9 +57,14 @@ public class SearchSelectWindow implements ActionListener {
jlist_substances.setModel(listModel);
selected = new ArrayList<>();
if (Ctx.sheet == null) {
Ctx.sheet = new SafetyDataSheet();
}
if (Ctx.client == null) {
Ctx.client = new GESTIS();
}
l_searchResult = new ArrayList<>();
fm.setContentPane(SearchSelectPanel);
@@ -70,7 +75,7 @@ public class SearchSelectWindow implements ActionListener {
Dialogs.infoBox(this.fm, "Kein Suchbegriff eingegeben", "Error: no keyword");
return;
}
List<SubstanceShort> result = GESTIS.Search(tf_search.getText(), false);
List<SubstanceShort> result = Ctx.client.Search(tf_search.getText(), false);
listModel.clear();
l_searchResult.clear();
l_searchResult.addAll(result);
@@ -132,7 +137,7 @@ public class SearchSelectWindow implements ActionListener {
}
// get the whole substance data from GESTIS
Substance sub = GESTIS.getSubstance(slectedSub.Name, slectedSub.ZVG);
Substance sub = Ctx.client.GetSubstance(slectedSub.Name, slectedSub.ZVG);
if ((sub.CAS == null || sub.CAS.length() == 0) && (sub.ZVG == null || sub.ZVG.length() == 0)) {
Dialogs.infoBox(this.fm, "Ein Fehler trat beim laden der GESTIS daten auf", "Error: gestis return unexpected");
return;

View File

@@ -1,5 +1,6 @@
package de.obermui.cabr2.gui;
import de.obermui.cabr2.clients.Client;
import de.obermui.cabr2.models.SafetyDataSheet;
import javax.swing.*;
@@ -13,4 +14,6 @@ public class ctx {
protected SafetyDataSheet sheet;
protected String lastSavedFileDir;
protected Client client;
}