summaryrefslogtreecommitdiff
path: root/src/main/java/seu/se/Application.java
blob: 928bc28a3d672b2f781cac632e21829133c55623 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package seu.se;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.session.MapSessionRepository;
import org.springframework.session.config.annotation.web.http.EnableSpringHttpSession;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@EnableTransactionManagement
@EnableNeo4jRepositories
@SpringBootApplication
public class Application {
    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @ControllerAdvice
    public static class ExHandler {
        @ExceptionHandler(Exception.class)
        public ResponseEntity<Object> handle(Exception ex, HttpServletRequest request, HttpServletResponse response) {
            HttpStatus code = HttpStatus.INTERNAL_SERVER_ERROR;
            String status = "internal server error";
            if (ex instanceof NullPointerException) {
                code = HttpStatus.BAD_REQUEST;
                status = "bad request";
            }
            if (ex instanceof NoHandlerFoundException) {
                code = HttpStatus.NOT_FOUND;
                status = "not found";
            }
            var retObj = new ObjectMapper().createObjectNode();
            retObj.put("status", status);
            retObj.put("message", ex.getMessage());
            return ResponseEntity.status(code).body(retObj);
        }
    }

    // in-mem session
    @EnableSpringHttpSession
    @Configuration(proxyBeanMethods = false)
    public static class SpringHttpSessionConfig {
        @Bean
        public MapSessionRepository sessionRepository() {
            return new MapSessionRepository(new ConcurrentHashMap<>());
        }
    }

    @Bean
    public CommandLineRunner init(UserRepository ur, TreeNodeRepository tnr) {
        return (args) -> {
            // Load users
            try {
                List<User> users = new ObjectMapper().readValue(
                        new ClassPathResource("users.json").getFile(),
                        new TypeReference<>() {
                        }
                );
                ur.saveAll(users);
            } catch (IOException e) {
                log.error("Error loading users from JSON file", e);
            }

            // Load tree nodes
            TreeNode.tnRepo = tnr;
            try {
                List<TreeNode> treeNodes = new ObjectMapper().readValue(
                        new ClassPathResource("treeNodes.json").getFile(),
                        new TypeReference<>() {
                        }
                );
                tnr.saveAll(treeNodes);
            } catch (IOException e) {
                log.error("Error loading tree nodes from JSON file", e);
            }
        };
    }

}