aboutsummaryrefslogtreecommitdiff
path: root/juick-demo/src/main/java/com/juick/demo/Demo.java
blob: 0ed180791cc8d67842b961ae9c76349fc1b9d03d (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
77
package com.juick.demo;

import ch.vorburger.exec.ManagedProcessException;
import ch.vorburger.mariadb4j.DB;
import com.juick.Tag;
import com.juick.User;
import com.juick.server.MessagesQueries;
import com.juick.server.PMQueries;
import com.juick.server.TagQueries;
import com.juick.server.UserQueries;
import com.juick.server.protocol.JuickProtocol;
import com.juick.server.protocol.ProtocolReply;
import jline.UnsupportedTerminal;
import jline.console.ConsoleReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

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

/**
 * Created by vitalyster on 30.08.2016.
 */
public class Demo {
    private static final Logger logger = LoggerFactory.getLogger(Demo.class);
    public static void main(String ...args) throws ManagedProcessException {
        DB db = DB.newEmbeddedDB(33306);
        db.start();
        db.createDB("juick");
        db.source("schema.sql");
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:33306/juick?autoReconnect=true&user=root");
        JdbcTemplate jdbc = new JdbcTemplate(dataSource);
        logger.info("initializing demo database");

        Integer ugnich_id = UserQueries.createUser(jdbc, "ugnich", "secret");
        Integer ugn_mid = MessagesQueries.createMessage(jdbc, ugnich_id, "Hi, I am ugnich", null,
                new ArrayList<Tag>() {{add(TagQueries.getTag(jdbc, "yo", true)); add(TagQueries.getTag(jdbc, "people", true));}});

        Integer juick_id = UserQueries.createUser(jdbc, "juick", "secret");
        MessagesQueries.createMessage(jdbc, juick_id, "New demo juick was created", null,
                new ArrayList<Tag>() {{add(TagQueries.getTag(jdbc, "yo", true)); add(TagQueries.getTag(jdbc, "readonly", true));}});
        Integer freefd_id = UserQueries.createUser(jdbc, "freefd", "secret");
        MessagesQueries.createReply(jdbc, ugn_mid, 0, freefd_id, "I am freefd and this is my reply", null);
        MessagesQueries.recommendMessage(jdbc, ugn_mid, freefd_id);
        MessagesQueries.recommendMessage(jdbc, ugn_mid, juick_id);
        MessagesQueries.setMessagePopular(jdbc, ugn_mid, 1);
        PMQueries.createPM(jdbc, freefd_id, ugnich_id, "Hi ugnich!");
        jdbc.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 = UserQueries.getUserByUID(jdbc, ugnich_id).get();
            out.println(String.format("logged in as @%s", ugnich.getUName()));
            out.flush();
            String input;
            JuickProtocol protocol = new JuickProtocol(jdbc, "http://localhost/");
            while ((input = reader.readLine()) != null) {
                ProtocolReply reply = protocol.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();
        }
    }
}