Commit 8048cc45 authored by Marcel's avatar Marcel
Browse files

Add update checker

parent 84d23655
Pipeline #127184273 passed with stage
in 4 minutes
......@@ -5,7 +5,7 @@
<groupId>de.apis-stuttgart</groupId>
<artifactId>churchtools-to-songbeamer</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
<packaging>jar</packaging>
<name>churchtools-to-songbeamer</name>
......@@ -48,6 +48,7 @@
<includes>
<include>**/application.yml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
......
......@@ -4,15 +4,15 @@ import de.apisstuttgart.churchtoolstosongbeamer.config.ChurchtoolsConfig;
import de.apisstuttgart.churchtoolstosongbeamer.domain.CtEvent;
import de.apisstuttgart.churchtoolstosongbeamer.service.AgendaConverterService;
import de.apisstuttgart.churchtoolstosongbeamer.service.CtEventService;
import de.apisstuttgart.churchtoolstosongbeamer.service.UpdateService;
import de.apisstuttgart.churchtoolstosongbeamer.ui.ApplicationUI;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import javax.swing.*;
import java.awt.*;
import org.springframework.web.client.HttpServerErrorException;
@SpringBootApplication
@CommonsLog
......@@ -24,81 +24,56 @@ public class ChurchtoolsToSongbeamerApplication {
@Autowired
protected CtEventService ctEventService;
@Autowired
protected UpdateService updateService;
@Autowired
protected ChurchtoolsConfig churchtoolsConfig;
protected static Container contentPanel;
@Autowired
protected ApplicationUI appUI;
public static void main(String[] args) {
new SpringApplicationBuilder(ChurchtoolsToSongbeamerApplication.class).headless(false).run(args);
}
@Bean
public CommandLineRunner run() {
return args -> {
log.debug("Debug logging is active.");
initGUI();
if (updateService.isUpdateAvailable()) {
appUI.showUpdate(updateService.getDownloadUrl());
}
// Check config
if (churchtoolsConfig.getUrlBase().isEmpty()) {
showMessage("Bitte Churchtools Instanz richtig konfigurieren.");
appUI.showMessage("Bitte Churchtools Instanz richtig konfigurieren.");
} else if (churchtoolsConfig.getUserId().isEmpty()) {
showMessage("Bitte die User ID richtig konfigurieren.");
appUI.showMessage("Bitte die User ID richtig konfigurieren.");
} else if (churchtoolsConfig.getUserToken().isEmpty()) {
showMessage("Bitte die User Token richtig konfigurieren.");
appUI.showMessage("Bitte die User Token richtig konfigurieren.");
// Config seems correct
} else {
CtEvent[] ctEvents = ctEventService.getAllEvents();
if (ctEvents != null) {
showEventList(ctEvents, agendaConverterService);
} else {
showMessage("Daten konnten nicht geladen werden.");
try {
CtEvent[] ctEvents = ctEventService.getAllEvents();
if (ctEvents != null) {
appUI.showEventList(ctEvents, agendaConverterService);
} else {
appUI.showMessage("Daten konnten nicht geladen werden.");
}
} catch (HttpServerErrorException e) {
if (e instanceof HttpServerErrorException.InternalServerError) {
appUI.showMessage("Serverfehler, versuche es später wieder.");
} else {
appUI.showMessage("Allgemeiner Fehler, versuche es später wieder.");
}
throw e;
}
}
};
}
protected static void initGUI() {
JFrame jFrame = new JFrame("Churchtools to Songbeamer");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPanel = jFrame.getContentPane();
contentPanel.setLayout(new BorderLayout());
jFrame.setSize(400, 400);
jFrame.setVisible(true);
showMessage("Bitte warten, die Daten werden geladen.");
}
protected static void showMessage(String message) {
contentPanel.removeAll();
JLabel jLabel = new JLabel(message);
contentPanel.add(jLabel, BorderLayout.NORTH);
contentPanel.revalidate();
contentPanel.repaint();
}
protected static void showEventList(CtEvent[] ctEvents, AgendaConverterService agendaConverterService) {
showMessage("Agenda auswählen um diese im SongBeamer zu öffnen.");
JList list = new JList(ctEvents);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.addListSelectionListener(selectionEvent -> {
if (!selectionEvent.getValueIsAdjusting()) {
log.info("Selected object for translation is: " + list.getSelectedValue());
agendaConverterService.translate((CtEvent) list.getSelectedValue());
}
});
contentPanel.add(list);
contentPanel.revalidate();
contentPanel.repaint();
}
}
......@@ -50,7 +50,7 @@ public class CtAgendaItem implements Comparable {
}
public boolean isSong() {
return name.isEmpty() && !arrangementId.isEmpty();
return name.isEmpty() && arrangementId != null && !arrangementId.isEmpty();
}
public int getSortKey() {
......
package de.apisstuttgart.churchtoolstosongbeamer.service;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Service
@CommonsLog
public class UpdateService {
@Value("${application.version}")
protected String version;
@Value("${application.update-url}")
protected String updateUrl;
@Value("${application.download-url}")
protected String downloadUrl;
protected RestTemplate restTemplate = new RestTemplate();
public boolean isUpdateAvailable() {
try {
String response = restTemplate.getForObject(getUpdateUrl(), String.class);
Matcher matcher = Pattern.compile("<version>(.+?)</version>", Pattern.DOTALL).matcher(response);
if (matcher.find()) {
String responseVersion = matcher.group(1);
if (version != null && !version.equals(responseVersion)) {
log.warn("Update is available");
return true;
} else {
log.info("No update found");
}
}
} catch (HttpClientErrorException e) {
log.warn("Update check failed. " + e.getMessage());
}
return false;
}
private String getUpdateUrl() {
return updateUrl + "?version=" + version;
}
public String getDownloadUrl() {
return downloadUrl;
}
}
package de.apisstuttgart.churchtoolstosongbeamer.ui;
import de.apisstuttgart.churchtoolstosongbeamer.domain.CtEvent;
import de.apisstuttgart.churchtoolstosongbeamer.service.AgendaConverterService;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.stereotype.Service;
import javax.swing.*;
import java.awt.*;
import java.net.URI;
@Service
@CommonsLog
public class ApplicationUI {
protected Container contentPanel;
protected JLabel messageLabel;
private ApplicationUI() {
JFrame jFrame = new JFrame("Churchtools to Songbeamer");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPanel = jFrame.getContentPane();
contentPanel.setLayout(new BorderLayout());
JPanel messagePanel = new JPanel();
messageLabel = new JLabel();
messagePanel.add(messageLabel);
contentPanel.add(messagePanel, BorderLayout.NORTH);
jFrame.setSize(500, 500);
jFrame.setVisible(true);
showMessage("Bitte warten, die Daten werden geladen.");
}
private void repaintUI() {
contentPanel.revalidate();
contentPanel.repaint();
}
public void showMessage(String message) {
messageLabel.setText(message);
}
public void showUpdate(String url) {
JPanel downloadPanel = new JPanel();
downloadPanel.add(new JLabel("Ein Update ist verfügbar:"));
JButton downloadButton = new JButton("Jetzt downloaden!");
downloadButton.addActionListener(actionEvent -> {
try {
Desktop.getDesktop().browse(URI.create(url));
} catch (Exception e) {
log.error("Could not open browser with update URL");
}
});
downloadPanel.add(downloadButton);
contentPanel.add(downloadPanel, BorderLayout.SOUTH);
repaintUI();
}
public void showEventList(CtEvent[] ctEvents, AgendaConverterService agendaConverterService) {
showMessage("Ablauf auswählen um diesen in SongBeamer zu öffnen.");
JList list = new JList(ctEvents);
list.setFixedCellHeight(25);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.addListSelectionListener(selectionEvent -> {
if (!selectionEvent.getValueIsAdjusting()) {
log.info("Selected object for translation is: " + list.getSelectedValue());
agendaConverterService.translate((CtEvent) list.getSelectedValue());
}
});
contentPanel.add(list);
repaintUI();
}
}
application:
version: '@project.version@'
base-url: 'https://www.apis-stuttgart.de/ct2sb'
update-url: '${application.base-url}/update'
download-url: '${application.base-url}/download'
churchtools:
instance: ''
user-id: ''
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment