Skip to content

Commit

Permalink
Updated exception handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasmiosczka committed May 3, 2018
1 parent 5b9d51a commit dc8a929
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
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;
import com.github.tobiasmiosczka.cinema.KDMManager.pojo.*;
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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -247,18 +260,15 @@ private void loadKdms() {
btLoadKdms.setEnabled(false);
setUiEnabled(false);
new Thread(() -> {
Collection<KDM> kdms = null;
Collection<KDM> 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()) {
Expand All @@ -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.");
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<FtpLogin> 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<EmailLogin> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ public FtpException(int code) {
public int getCode() {
return code;
}

@Override
public String getMessage() {
return "FTP Error Code: " + getCode();
}
}

0 comments on commit dc8a929

Please sign in to comment.