aboutsummaryrefslogtreecommitdiff
path: root/juick-demo/src/main/java/com/juick/demo/DemoApp.java
blob: 0c9390dd6db200b54cd43053527750e9c34320ef (plain) (blame)
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
package com.juick.demo;

import com.juick.Tag;
import com.juick.User;
import com.juick.server.protocol.JuickProtocol;
import com.juick.server.protocol.ProtocolReply;
import com.juick.service.MessagesService;
import com.juick.service.PMQueriesService;
import com.juick.service.TagService;
import com.juick.service.UserService;
import jline.UnsupportedTerminal;
import jline.console.ConsoleReader;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

/**
 * Created by vitalyster on 19.12.2016.
 */
public class DemoApp {
    UserService userService;
    MessagesService messagesService;
    PMQueriesService pmQueriesService;
    TagService tagService;
    JdbcTemplate jdbcTemplate;
    JuickProtocol juickProtocol;

    public DemoApp(ApplicationContext context) {
        userService = context.getBean(UserService.class);
        messagesService = context.getBean(MessagesService.class);
        pmQueriesService = context.getBean(PMQueriesService.class);
        tagService = context.getBean(TagService.class);
        jdbcTemplate = context.getBean(JdbcTemplate.class);
        juickProtocol = context.getBean(JuickProtocol.class);
    }
    public void start() {
        Integer ugnich_id = userService.createUser("ugnich", "secret");
        Integer ugn_mid = messagesService.createMessage(ugnich_id, "Hi, I am ugnich", null,
                new ArrayList<Tag>() {{add(tagService.getTag("yo", true)); add(tagService.getTag("people", true));}});

        Integer juick_id = userService.createUser("juick", "secret");
        messagesService.createMessage(juick_id, "New demo juick was created", null,
                new ArrayList<Tag>() {{add(tagService.getTag("yo", true)); add(tagService.getTag("readonly", true));}});
        Integer freefd_id = userService.createUser("freefd", "secret");
        messagesService.createReply(ugn_mid, 0, freefd_id, "I am freefd and this is my reply", null);
        messagesService.recommendMessage(ugn_mid, freefd_id);
        messagesService.recommendMessage(ugn_mid, juick_id);
        messagesService.setMessagePopular(ugn_mid, 1);
        pmQueriesService.createPM(freefd_id, ugnich_id, "Hi ugnich!");
        jdbcTemplate.execute("UPDATE tags SET top=1");
        jline.TerminalFactory.registerFlavor(jline.TerminalFactory.Flavor.WINDOWS, UnsupportedTerminal.class);
        try (ConsoleReader reader = new ConsoleReader()) {
            reader.setPrompt("ugnich>");
            PrintWriter out = new PrintWriter(reader.getOutput());
            User ugnich = userService.getUserByUID(ugnich_id).get();
            out.println(String.format("logged in as @%s", ugnich.getName()));
            out.flush();
            String input;
            while ((input = reader.readLine()) != null) {
                ProtocolReply reply = juickProtocol.getReply(ugnich, input);
                out.println(reply.getDescription());
                out.flush();
            }
        } catch (IOException | NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}