first commit

This commit is contained in:
Erik Bročko 2018-06-28 08:30:24 +02:00
parent f5149c7f16
commit 9e0976e30b
11 changed files with 428 additions and 0 deletions

View File

@ -0,0 +1,74 @@
package eu.lixko.ext.forraceexpander.components;
import java.util.ArrayList;
import java.util.LinkedHashMap;
public class Address extends SerializableComponent {
private ArrayList<String> addressLines = new ArrayList<>();
private String street, zipCode, city, district, country;
private String name, phone, mail;
public void setAddressLine(int linei, String val) throws IndexOutOfBoundsException {
this.addressLines.set(linei, val);
if(linei > 3)
throw new IndexOutOfBoundsException("Address can not have more than 4 lines! Tried to set #" + linei);
}
public void addAddressLine(String val) throws IndexOutOfBoundsException {
if(this.addressLines.size() == 4)
throw new IndexOutOfBoundsException("Address can not have more than 4 lines!");
this.addressLines.add(val);
}
public void setStreet(String street) {
this.street = street;
}
public void setZIP(String zipCode) {
this.zipCode = zipCode;
}
public void setCity(String city) {
this.city = city;
}
public void setDistrict(String district) {
this.district = district;
}
public void setCountry(String country) {
this.country = country;
}
public void setName(String name) {
this.name = name;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setMail(String mail) {
this.mail = mail;
}
@Override
public void prepare() {
super.prepare();
for (int i = 0; i < this.addressLines.size(); i++) {
this.putPreparedEntry("Name" + (i + 1), this.addressLines.get(i));
}
this.putPreparedEntry("Street", street);
this.putPreparedEntry("ZIP", zipCode);
this.putPreparedEntry("City", city);
this.putPreparedEntry("District", district);
this.putPreparedEntry("Country", country);
this.putPreparedEntry("ContactName", name);
this.putPreparedEntry("ContactPhone", phone);
this.putPreparedEntry("ContactMail", mail);
}
}

View File

@ -0,0 +1,83 @@
package eu.lixko.ext.forraceexpander.components;
public class Item extends SerializableComponent {
private long itemID = 0, productCode = 0, priceUnit = 0, priceTotal = 0, priceTotalWithTax = 0;
private int qty = 0;
private String name = "", ean = "";
public void setItemID(long itemID) {
this.itemID = itemID;
}
public void setProductCode(long productCode) {
this.productCode = productCode;
}
public void setQuantity(int qty) {
this.qty = qty;
}
public void setUnitPrice(long price) {
this.priceUnit = price;
}
public void setTotalPrice(long price) {
this.priceTotal = price;
}
public void setTotalPriceWithTax(long price) {
this.priceTotalWithTax = price;
}
public void setName(String name) {
this.name = name;
}
public void setEAN(String ean) {
this.ean = ean;
}
public long getItemID() {
return itemID;
}
public long getProductCode() {
return productCode;
}
public int getQuantity() {
return qty;
}
public long getUnitPrice() {
return priceUnit;
}
public long getTotalPrice() {
return priceTotal;
}
public long getTotalPriceWithTax() {
return priceTotalWithTax;
}
public String getName() {
return name;
}
public String getEAN() {
return ean;
}
@Override
public void prepare() {
super.prepare();
this.putPreparedEntry("ItemID", itemID + "");
this.putPreparedEntry("Qty", qty + "");
String priceStr = priceUnit + "";
this.putPreparedEntry("Price", priceStr.substring(0, priceStr.length() - 2) + "." + priceStr.substring(priceStr.length() - 2, priceStr.length()));
this.putPreparedEntry("ShipTxt", name);
}
}

View File

@ -0,0 +1,32 @@
package eu.lixko.ext.forraceexpander.components;
import java.util.ArrayList;
import java.util.Map;
public class Order extends SerializableComponent {
private ArrayList<Item> items = new ArrayList<>();
@Override
public void prepare() {
super.prepare();
for(Item it : items) {
it.prepare();
}
}
public void dump() {
for(Item it : items) {
if(it.getNameValuePairs() == null)
it.prepare();
for (Map.Entry<String, String> entry : it.getNameValuePairs().entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
System.out.println("-------------------------------");
}
}
public void addItem(Item item) {
this.items.add(item);
}
}

View File

@ -0,0 +1,35 @@
package eu.lixko.ext.forraceexpander.components;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.LinkedHashMap;
public class SerializableComponent {
LinkedHashMap<String, String> vals;
public LinkedHashMap<String, String> getNameValuePairs() {
return this.vals;
}
public void checkComponent() {
}
public void prepare() {
this.vals = new LinkedHashMap<>();
}
public void putPreparedEntry(String name, String value) {
this.putPreparedEntry(name, value, false);
}
public void putPreparedEntry(String name, String value, boolean allowEmpty) {
if(!value.isEmpty() || allowEmpty)
this.vals.put(name, value);
}
@Retention(RetentionPolicy.RUNTIME)
public @interface MaxLength {
int value() default -1;
}
}

View File

@ -0,0 +1,5 @@
package eu.lixko.ext.forraceexpander.exporter;
public class OrderExporter {
}

View File

@ -0,0 +1,31 @@
package eu.lixko.ext.forraceexpander.importer;
import java.util.ArrayList;
import java.util.Iterator;
import eu.lixko.ext.forraceexpander.components.Order;
public class OrderImporter {
public static OrderProvider orderProvider;
public static OrderParser orderParser;
public ArrayList<Order> orders = new ArrayList<>();
public OrderImporter() {
orderProvider = new TestOrderProvider();
orderParser = new TestOrderParser();
}
public void run() {
Iterator<String> orderIterator = orderProvider.iterator();
while(orderIterator.hasNext()) {
String rawOrder = (String) orderIterator.next();
Order ord = orderParser.parse(rawOrder);
orders.add(ord);
ord.dump();
}
}
public ArrayList<Order> getOrders() {
return this.orders;
}
}

View File

@ -0,0 +1,9 @@
package eu.lixko.ext.forraceexpander.importer;
import eu.lixko.ext.forraceexpander.components.Order;
public interface OrderParser {
public Order parse(String html);
}

View File

@ -0,0 +1,5 @@
package eu.lixko.ext.forraceexpander.importer;
public interface OrderProvider extends Iterable<String> {
}

View File

@ -0,0 +1,99 @@
package eu.lixko.ext.forraceexpander.importer;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Comment;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;
import eu.lixko.ext.forraceexpander.components.Item;
import eu.lixko.ext.forraceexpander.components.Order;
public class TestOrderParser implements OrderParser {
@Override
public Order parse(String html) {
Document doc = Jsoup.parse(html);
Order order = new Order();
List<Comment> comments = getComments(doc);
for (Comment c : comments) {
Pattern p = Pattern.compile("\\<!--\\s*(.*)\\s--\\>");
Matcher m = p.matcher(c.toString());
String label = "";
if (m.find()) {
label = m.group(1);
} else {
continue;
}
if(!label.equals("COMMODITIES"))
continue;
Element parent = (Element) c.parent();
Elements commodities = parent.select("tbody tr");
for(Element row : commodities) {
Elements itemEl = row.select("td");
Item it = new Item();
String nameRaw = itemEl.get(0).html();
if(nameRaw.contains(" <br> ")) {
String[] vals = nameRaw.split(" <br> ");
it.setName(vals[0]);
for(String val : vals) {
if(val.contains("Kód produktu: ")) {
it.setProductCode(Long.parseLong(val.replaceAll("[^0-9]", "")));
} else if(val.startsWith("EAN: ")) {
it.setEAN(val.substring(5));
}
}
} else {
it.setName(nameRaw);
}
it.setQuantity(Integer.parseInt(itemEl.get(1).text().replaceAll("\\D.*", ""), 10));
it.setTotalPrice(convertPrice(itemEl.get(3).text()));
it.setTotalPriceWithTax(convertPrice(itemEl.get(4).text()));
long unitPrice = convertPrice(itemEl.get(4).text());
if(unitPrice < 0)
it.setUnitPrice(it.getTotalPriceWithTax());
else
it.setUnitPrice(unitPrice);
order.addItem(it);
}
}
return order;
}
private List<Comment> getComments(Node node) {
List<Comment> comments = new ArrayList<Comment>();
int i = 0;
while (i < node.childNodes().size()) {
Node child = node.childNode(i);
if (child.nodeName().equals("#comment"))
comments.add((Comment) child);
else {
comments.addAll(getComments(child));
}
i++;
}
return comments;
}
private static long convertPrice(String raw) {
raw = raw.replaceAll("&nbsp;", " ");
if(raw.length() < 4)
return -1l;
raw = raw.replaceAll("[^0-9]", "");
return Long.parseLong(raw);
}
}

View File

@ -0,0 +1,43 @@
package eu.lixko.ext.forraceexpander.importer;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.Scanner;
public class TestOrderProvider implements OrderProvider {
private static final String testFilePath = "/home/erik/Dokumenty/ForRaceExpander/notes/original_html.txt";
@Override
public Iterator<String> iterator() {
return new Iterator<String>() {
private int currenti = 0;
@Override
public boolean hasNext() {
if (currenti == 1)
return false;
return true;
}
@Override
public String next() {
currenti++;
Scanner scanner = null;
try {
scanner = new Scanner(new File(testFilePath), "UTF-8");
return scanner.useDelimiter("\\A").next();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
scanner.close();
}
return "";
}
};
}
}

View File

@ -0,0 +1,12 @@
package eu.lixko.ext.forracexpander;
import eu.lixko.ext.forraceexpander.importer.OrderImporter;
public class Main {
public static void main(String[] args) {
OrderImporter orderImporter = new OrderImporter();
orderImporter.run();
}
}