diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java index 72a4e7e387a3e9b792d7a02778c1bf15fb06c236..a6885d013d4b27d9bc63bfaa933736640fa521f1 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java @@ -73,11 +73,11 @@ public class WelcomePage implements MouseListener, ActionListener { String selected = (String) comboBox.getSelectedItem(); Node node = Nodes.findByLocation(selected); - if(node == null) return; - - node.updateData(HttpRequests.getNodeData(node.getId())); - nodeDataFrame.updateData(node); - nodeSettingFrame.updateInformation(node); + if(node != null) { + node.updateData(HttpRequests.getNodeData(node.getId())); + nodeDataFrame.updateData(node); + nodeSettingFrame.updateInformation(node); + } comboBox.addActionListener(this); comboBox.setBounds(500, 10, 200, 50); @@ -188,8 +188,6 @@ public class WelcomePage implements MouseListener, ActionListener { cl.show(stagePanel, "1"); frame.add(stagePanel); - nodeSettingFrame.updateInformation(Nodes.findByLocation(comboBox.getSelectedItem().toString())); - frame.setVisible(true); } @@ -333,4 +331,25 @@ public class WelcomePage implements MouseListener, ActionListener { String node_name = ((JComboBox)e.getSource()).getSelectedItem().toString(); updateDataByName(node_name); } + + + // Default error message dialog + public static void ErrorDialog() { + String message = "Something went wrong."; + String title = "ERROR!"; + JOptionPane.showConfirmDialog(frame, message, title, + JOptionPane.ERROR_MESSAGE); + + } + + public static void TimedOutSession() { + String message = "\"The session has timed out.\"\n" + + "You will be logged out..."; + String title = "TIMEOUT!"; + JOptionPane.showConfirmDialog(frame, message, title, + JOptionPane.ERROR_MESSAGE); + User.clear(); + frame.dispose(); + LoginPage login = new LoginPage(); + } } \ No newline at end of file diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeSettingsFrame.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeSettingsFrame.java index 065027c5adb8b946b1b52ffc6966b3f39dbab23a..643e0e488547bc038f7033fdba777b607e171e8d 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeSettingsFrame.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeSettingsFrame.java @@ -91,12 +91,18 @@ public class NodeSettingsFrame extends JPanel implements ActionListener{ // A confirmation window is prompted private void saveInformation() { boolean save = WelcomePage.handleSave(); - if(save) HttpRequests.updateNode(new Node(descField.getText(), - Float.parseFloat(latField.getText()), - Float.parseFloat(lonField.getText()), - locationField.getText(), - current_node.getId())); - WelcomePage.updateComboBox(); + if(save && current_node != null) { + boolean success = HttpRequests.updateNode(new Node(descField.getText(), + Float.parseFloat(latField.getText()), + Float.parseFloat(lonField.getText()), + locationField.getText(), + current_node.getId())); + if(!success) { + WelcomePage.ErrorDialog(); + return; + } + WelcomePage.updateComboBox(); + } } /* diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java index bce7f5a36dfcd619144fa0f780c7337bd2db1ea7..4baefa7a367f473751be521ab4bab033d21401e2 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java @@ -20,6 +20,7 @@ import org.json.JSONObject; import com.joelhelkala.watcherGui.Nodes.Node.Node; import com.joelhelkala.watcherGui.User.User; +import com.joelhelkala.watcherGui.frames.WelcomePage; /* * Class to handle httpRequests to the server @@ -261,9 +262,13 @@ public class HttpRequests { } int status = con.getResponseCode(); - if(status < 300) success = true; - con.disconnect(); + if(status < 300) success = true; + else if (status == 403) { + WelcomePage.TimedOutSession(); + // Set success to true so error dialog wont be shown + success = true; + } } catch (MalformedURLException e) { e.printStackTrace(); diff --git a/watcherServer/src/main/java/com/joelhelkala/watcherServer/WatcherServerApplication.java b/watcherServer/src/main/java/com/joelhelkala/watcherServer/WatcherServerApplication.java index 49d019b48705e7332259575c9170a5b0b94ec101..ef8a38ae52131c1b9f6df83b7876c6f02f552807 100644 --- a/watcherServer/src/main/java/com/joelhelkala/watcherServer/WatcherServerApplication.java +++ b/watcherServer/src/main/java/com/joelhelkala/watcherServer/WatcherServerApplication.java @@ -1,12 +1,7 @@ package com.joelhelkala.watcherServer; -import com.joelhelkala.watcherServer.appuser.AppUser; -import com.joelhelkala.watcherServer.appuser.AppUserRole; -import com.joelhelkala.watcherServer.appuser.AppUserService; -import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; @SpringBootApplication public class WatcherServerApplication { @@ -14,14 +9,4 @@ public class WatcherServerApplication { public static void main(String[] args) { SpringApplication.run(WatcherServerApplication.class, args); } - - @Bean - CommandLineRunner run(AppUserService appUserService) { - return args -> { - appUserService.addNewUser(new AppUser("masa","masa@gmail.com","1234", AppUserRole.USER)); - appUserService.addNewUser(new AppUser("pena","pena@gmail.com","1234", AppUserRole.USER)); - appUserService.addNewUser(new AppUser("tero","tero@gmail.com","1234", AppUserRole.USER)); - appUserService.addNewUser(new AppUser("arska","arska@gmail.com","1234", AppUserRole.ADMIN)); - }; - } } \ No newline at end of file diff --git a/watcherServer/src/main/java/com/joelhelkala/watcherServer/appuser/AppUserRepository.java b/watcherServer/src/main/java/com/joelhelkala/watcherServer/appuser/AppUserRepository.java index 354b90848a2619293c874e4297db9ab6f6c4a7b3..6129d35ca05a5c9c44fd30804154d74e06c74540 100644 --- a/watcherServer/src/main/java/com/joelhelkala/watcherServer/appuser/AppUserRepository.java +++ b/watcherServer/src/main/java/com/joelhelkala/watcherServer/appuser/AppUserRepository.java @@ -11,6 +11,7 @@ import java.util.Optional; @Repository public interface AppUserRepository extends JpaRepository<AppUser, Long> { + @Query("SELECT u FROM AppUser u WHERE u.email = ?1") Optional<AppUser> findByEmail(String email); @Transactional diff --git a/watcherServer/src/main/java/com/joelhelkala/watcherServer/filter/CustomAuthorizationFilter.java b/watcherServer/src/main/java/com/joelhelkala/watcherServer/filter/CustomAuthorizationFilter.java index b0a01f723a4abdf4040a799cf0e9d8cc8ad78a10..86aadd7d1eebe0b8b76cd51a06ff015b0c03d6b3 100644 --- a/watcherServer/src/main/java/com/joelhelkala/watcherServer/filter/CustomAuthorizationFilter.java +++ b/watcherServer/src/main/java/com/joelhelkala/watcherServer/filter/CustomAuthorizationFilter.java @@ -39,7 +39,7 @@ public class CustomAuthorizationFilter extends OncePerRequestFilter { if(request.getServletPath().equals("/api/v1/login") || request.getServletPath().equals("/api/v1/registration")) { filterChain.doFilter(request, response); } - // Everything else does need + // Everything else does else { String authorization_header = request.getHeader(AUTHORIZATION); if (authorization_header != null && authorization_header.startsWith("Bearer ")) { @@ -65,6 +65,7 @@ public class CustomAuthorizationFilter extends OncePerRequestFilter { Map<String, String> error = new HashMap<>(); error.put("error_msg", e.getMessage()); + error.put("code", "401"); response.setContentType("application/json"); new ObjectMapper().writeValue(response.getOutputStream(), error); } diff --git a/watcherServer/src/main/java/com/joelhelkala/watcherServer/node/NodeRepository.java b/watcherServer/src/main/java/com/joelhelkala/watcherServer/node/NodeRepository.java index fa3b709710ef136caecb864c9563e71cc0a5da71..88c88158f9bbe1a7a65bfe7066d34ea5e68ec21a 100644 --- a/watcherServer/src/main/java/com/joelhelkala/watcherServer/node/NodeRepository.java +++ b/watcherServer/src/main/java/com/joelhelkala/watcherServer/node/NodeRepository.java @@ -1,9 +1,15 @@ package com.joelhelkala.watcherServer.node; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import java.util.Optional; public interface NodeRepository extends JpaRepository<Node, Long> { + + @Query("SELECT n FROM Node n WHERE n.location = ?1") + Optional<Object> findByLocation(String location); + + @Query("SELECT n FROM Node n WHERE n.id = ?1") Optional<Node> findById(Long id); } diff --git a/watcherServer/src/main/java/com/joelhelkala/watcherServer/node/NodeService.java b/watcherServer/src/main/java/com/joelhelkala/watcherServer/node/NodeService.java index 6e5035cd4a588f7433e7f70e10cd447393354f57..a4f0bfb5d746cc3d721af2568e3f1509feea75c0 100644 --- a/watcherServer/src/main/java/com/joelhelkala/watcherServer/node/NodeService.java +++ b/watcherServer/src/main/java/com/joelhelkala/watcherServer/node/NodeService.java @@ -17,6 +17,10 @@ public class NodeService { // Saves a given node to database public Node saveNode(Node node) { + log.info("Adding new node with name : {}, to the database", node.getLocation()); + + boolean exists = nodeRepository.findByLocation(node.getLocation()).isPresent(); + if (exists) throw new ApiRequestException("Node with name " + node.getLocation() + " already exists"); Node entity = nodeRepository.save(node); return entity; } diff --git a/watcherServer/src/main/resources/application.properties b/watcherServer/src/main/resources/application.properties index 0731cf6f32f1894f33ff3f80d2050ccbbbd13811..514230e42b2cff684e6a2c217ffeff97e603c222 100644 --- a/watcherServer/src/main/resources/application.properties +++ b/watcherServer/src/main/resources/application.properties @@ -1,7 +1,7 @@ -spring.datasource.url=jdbc:postgresql://localhost:5432/person +spring.datasource.url=jdbc:postgresql://localhost:5432/watcher spring.datasource.username= spring.datasource.password= -spring.jpa.hibernate.ddl-auto=create-drop +spring.jpa.hibernate.ddl-auto=none spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect spring.jpa.properties.hibernate.format_sql=true