diff --git a/src/main/java/com/github/tobiasmiosczka/cinema/KDMManager/Starter.java b/src/main/java/com/github/tobiasmiosczka/cinema/KDMManager/Starter.java index 0871976..1833bb4 100644 --- a/src/main/java/com/github/tobiasmiosczka/cinema/KDMManager/Starter.java +++ b/src/main/java/com/github/tobiasmiosczka/cinema/KDMManager/Starter.java @@ -1,11 +1,10 @@ package com.github.tobiasmiosczka.cinema.KDMManager; import com.github.tobiasmiosczka.cinema.KDMManager.gui.Window; -import org.jdom2.JDOMException; public class Starter { - public static void main(String[] args) throws JDOMException { + public static void main(String[] args) { Window window = new Window(); window.setVisible(true); } diff --git a/src/main/java/com/github/tobiasmiosczka/cinema/KDMManager/gui/Window.java b/src/main/java/com/github/tobiasmiosczka/cinema/KDMManager/gui/Window.java index 0a96064..6f5f7f6 100644 --- a/src/main/java/com/github/tobiasmiosczka/cinema/KDMManager/gui/Window.java +++ b/src/main/java/com/github/tobiasmiosczka/cinema/KDMManager/gui/Window.java @@ -1,5 +1,6 @@ package com.github.tobiasmiosczka.cinema.KDMManager.gui; +import com.github.tobiasmiosczka.cinema.KDMManager.helper.ConfigParseException; import com.github.tobiasmiosczka.cinema.KDMManager.helper.EmailHelper; import com.github.tobiasmiosczka.cinema.KDMManager.helper.FtpHelper; import com.github.tobiasmiosczka.cinema.KDMManager.helper.XmlHelper; @@ -7,14 +8,7 @@ import org.jdom2.JDOMException; import javax.mail.MessagingException; -import javax.swing.DefaultListModel; -import javax.swing.JButton; -import javax.swing.JCheckBox; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JList; -import javax.swing.JProgressBar; -import javax.swing.SwingConstants; +import javax.swing.*; import javax.swing.border.LineBorder; import java.awt.Color; import java.awt.Container; @@ -52,11 +46,14 @@ public class Window extends JFrame implements IUpdate { private Config config = new Config(); - private void loadConfig() throws JDOMException { + private void loadConfig(String filename) { try { - this.config = XmlHelper.loadConfig(new FileInputStream("config.xml")); + this.config = XmlHelper.loadConfig(new FileInputStream(filename)); } catch (IOException e) { this.config = new Config(); + JOptionPane.showMessageDialog(this, "Couldn't find " + filename + ". Starting with default configuration."); + } catch (JDOMException|ConfigParseException e) { + JOptionPane.showMessageDialog(this, "Error occurred while loading config.xml: " + e.getMessage()); } updateEmailLoginList(); updateFtpLoginList(); @@ -71,8 +68,8 @@ private void saveConfig() { } } - public Window() throws JDOMException { - loadConfig(); + public Window() { + loadConfig("config.xml"); this.init(); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.pack(); @@ -167,9 +164,17 @@ private void init() { btDeleteEmailLogin = new JButton("Delete"); btDeleteEmailLogin.addActionListener(a -> { - lEmailLoginList.getSelectedValuesList().forEach(config.getEmailLogins()::remove); - saveConfig(); - updateEmailLoginList(); + int count = lEmailLoginList.getSelectedValuesList().size(); + int result = JOptionPane.showConfirmDialog( + this, + "This action will delete the selected " + count + " Email login(s). Are you sure?", + "Delete email logins", + JOptionPane.YES_NO_OPTION); + if (result == JOptionPane.YES_OPTION) { + lEmailLoginList.getSelectedValuesList().forEach(config.getEmailLogins()::remove); + saveConfig(); + updateFtpLoginList(); + } }); btDeleteEmailLogin.setBounds(235, 145, 110, 30); c.add(btDeleteEmailLogin); @@ -213,9 +218,17 @@ private void init() { btDeleteFtpLogin = new JButton("Delete"); btDeleteFtpLogin.addActionListener(a -> { - lFtpLoginList.getSelectedValuesList().forEach(config.getFtpLogins()::remove); - saveConfig(); - updateFtpLoginList(); + int count = lFtpLoginList.getSelectedValuesList().size(); + int result = JOptionPane.showConfirmDialog( + this, + "This action will delete the selected " + count + " FTP login(s). Are you sure?", + "Delete FTP logins", + JOptionPane.YES_NO_OPTION); + if (result == JOptionPane.YES_OPTION) { + lFtpLoginList.getSelectedValuesList().forEach(config.getFtpLogins()::remove); + saveConfig(); + updateFtpLoginList(); + } }); btDeleteFtpLogin.setBounds(235, 320, 110, 30); c.add(btDeleteFtpLogin); @@ -247,18 +260,15 @@ private void loadKdms() { btLoadKdms.setEnabled(false); setUiEnabled(false); new Thread(() -> { - Collection kdms = null; + Collection kdms; long start = System.currentTimeMillis(); pbMajor.setVisible(true); pbMinor.setVisible(true); try { kdms = EmailHelper.getKdmsFromEmail(config.getEmailLogins(), this); } catch (MessagingException | JDOMException | ParseException | IOException e) { - //TODO: implement - e.printStackTrace(); - } - if(kdms == null) { - //TODO: implement + JOptionPane.showMessageDialog(this, "Error occurred while loading KDMs from Email: " + e.getMessage()); + setUiEnabled(true); return; } if (cbIgnoreExpiredKdms.isSelected()) { @@ -269,8 +279,8 @@ private void loadKdms() { FtpHelper ftpHelper = new FtpHelper(config.getFtpLogins()); ftpHelper.uploadFiles(kdms, this); } catch (IOException | FtpException e) { - //TODO: implement - e.printStackTrace(); + JOptionPane.showMessageDialog(this, "Error occurred while sending KDMs: " + e.getMessage()); + setUiEnabled(true); } long diff = System.currentTimeMillis() - start; pbMajor.setString("Loaded " + kdms.size() + " KDMs after " + diff / 1000 + " seconds."); diff --git a/src/main/java/com/github/tobiasmiosczka/cinema/KDMManager/helper/ConfigParseException.java b/src/main/java/com/github/tobiasmiosczka/cinema/KDMManager/helper/ConfigParseException.java new file mode 100644 index 0000000..42ee3d0 --- /dev/null +++ b/src/main/java/com/github/tobiasmiosczka/cinema/KDMManager/helper/ConfigParseException.java @@ -0,0 +1,24 @@ +package com.github.tobiasmiosczka.cinema.KDMManager.helper; + +import org.jdom2.Element; + +import java.util.LinkedList; +import java.util.List; + +public class ConfigParseException extends Throwable { + + private final List stack = new LinkedList<>(); + + public ConfigParseException(Element element, String name) { + stack.add(name); + while (element != null) { + stack.add(element.getName()); + element = element.getParentElement(); + } + } + + @Override + public String getMessage() { + return stack.stream().reduce("", (a, b) -> "<" + b + "> " + a); + } +} diff --git a/src/main/java/com/github/tobiasmiosczka/cinema/KDMManager/helper/XmlHelper.java b/src/main/java/com/github/tobiasmiosczka/cinema/KDMManager/helper/XmlHelper.java index 39844f0..33bf0f0 100644 --- a/src/main/java/com/github/tobiasmiosczka/cinema/KDMManager/helper/XmlHelper.java +++ b/src/main/java/com/github/tobiasmiosczka/cinema/KDMManager/helper/XmlHelper.java @@ -26,13 +26,41 @@ public class XmlHelper { private static final Namespace ns2 = Namespace.getNamespace("", "http://www.smpte-ra.org/schemas/430-1/2006/KDM"); private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX"); private static final SAXBuilder saxBuilder = new SAXBuilder(); - private static final XMLOutputter xmlOutputter = new XMLOutputter(); static { xmlOutputter.setFormat(Format.getPrettyFormat()); } + private static String getStringValue(Element element, String name) throws ConfigParseException { + try { + return element.getChild(name).getValue(); + } catch (NullPointerException e) { + throw new ConfigParseException(element, name); + } + } + + private static int getIntegerValue(Element element, String name) throws ConfigParseException { + try { + return Integer.parseInt(element.getChild(name).getValue()); + } catch (NullPointerException|NumberFormatException e) { + throw new ConfigParseException(element, name); + } + } + + private static boolean getBooleanValue(Element element, String name) throws ConfigParseException { + try { + String string = element.getChild(name).getValue(); + switch (string) { + case "true": return true; + case "false": return false; + default: throw new ConfigParseException(element, name); + } + } catch (NullPointerException e) { + throw new ConfigParseException(element, name); + } + } + public static Document getDocument(InputStream inputStream) throws JDOMException, IOException { return saxBuilder.build(inputStream); } @@ -88,38 +116,42 @@ private static Element emailLoginToElement(EmailLogin emailLogin) { return emailLoginElement; } - private static FtpLogin elementToFtpLogin(Element element) { + private static FtpLogin elementToFtpLogin(Element element) throws ConfigParseException { return new FtpLogin( - element.getChild("host").getValue(), - Integer.parseInt(element.getChild("port").getValue()), - element.getChild("user").getValue(), - element.getChild("password").getValue(), - element.getChild("serial").getValue() + getStringValue(element, "host"), + getIntegerValue(element, "port"), + getStringValue(element, "user"), + getStringValue(element, "password"), + getStringValue(element, "serial") ); } - private static EmailLogin elementToEmailLogin(Element element) { + private static EmailLogin elementToEmailLogin(Element element) throws ConfigParseException { return new EmailLogin( - element.getChild("host").getValue(), - Integer.parseInt(element.getChild("port").getValue()), - element.getChild("user").getValue(), - element.getChild("password").getValue(), - element.getChild("protocol").getValue(), - element.getChild("folder").getValue(), - Boolean.parseBoolean(element.getChild("tls").getValue()) + getStringValue(element, "host"), + getIntegerValue(element, "port"), + getStringValue(element, "user"), + getStringValue(element, "password"), + getStringValue(element, "protocol"), + getStringValue(element, "folder"), + getBooleanValue(element, "tls") ); } - public static Config loadConfig(Document document) { + public static Config loadConfig(Document document) throws ConfigParseException { Config config = new Config(); Collection ftpLoginMap = config.getFtpLogins(); - document.getRootElement().getChild("ftpLogins").getChildren().forEach(element -> ftpLoginMap.add(elementToFtpLogin(element))); + for (Element element1 : document.getRootElement().getChild("ftpLogins").getChildren()) { + ftpLoginMap.add(elementToFtpLogin(element1)); + } Collection emailLogins = config.getEmailLogins(); - document.getRootElement().getChild("emailLogins").getChildren().forEach(element -> emailLogins.add(elementToEmailLogin(element))); + for (Element element : document.getRootElement().getChild("emailLogins").getChildren()) { + emailLogins.add(elementToEmailLogin(element)); + } return config; } - public static Config loadConfig(InputStream inputStream) throws JDOMException, IOException { + public static Config loadConfig(InputStream inputStream) throws JDOMException, IOException, ConfigParseException { Document document = getDocument(inputStream); return loadConfig(document); } diff --git a/src/main/java/com/github/tobiasmiosczka/cinema/KDMManager/pojo/FtpException.java b/src/main/java/com/github/tobiasmiosczka/cinema/KDMManager/pojo/FtpException.java index 4297a43..5020b79 100644 --- a/src/main/java/com/github/tobiasmiosczka/cinema/KDMManager/pojo/FtpException.java +++ b/src/main/java/com/github/tobiasmiosczka/cinema/KDMManager/pojo/FtpException.java @@ -11,4 +11,9 @@ public FtpException(int code) { public int getCode() { return code; } + + @Override + public String getMessage() { + return "FTP Error Code: " + getCode(); + } }