diff options
Diffstat (limited to 'juick-demo')
3 files changed, 187 insertions, 64 deletions
diff --git a/juick-demo/src/main/java/com/juick/demo/Demo.java b/juick-demo/src/main/java/com/juick/demo/Demo.java index b94c9830..db03e2e5 100644 --- a/juick-demo/src/main/java/com/juick/demo/Demo.java +++ b/juick-demo/src/main/java/com/juick/demo/Demo.java @@ -1,77 +1,28 @@ 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; +import com.juick.demo.configuration.DemoAppConfiguration; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.core.env.SimpleCommandLinePropertySource; /** * 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"); + final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); - 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));}}); + // setup configuration + applicationContext.register(DemoAppConfiguration.class); + // add CLI property source + applicationContext.getEnvironment().getPropertySources() + .addLast(new SimpleCommandLinePropertySource(args)); - 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.getName())); - 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(); - } + // setup all the dependencies (refresh) and make them run (start) + applicationContext.refresh(); + applicationContext.start(); + DemoApp demoApp = new DemoApp(applicationContext); + demoApp.start(); } } diff --git a/juick-demo/src/main/java/com/juick/demo/DemoApp.java b/juick-demo/src/main/java/com/juick/demo/DemoApp.java new file mode 100644 index 00000000..9d1d7926 --- /dev/null +++ b/juick-demo/src/main/java/com/juick/demo/DemoApp.java @@ -0,0 +1,82 @@ +package com.juick.demo; + +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 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 org.springframework.stereotype.Component; + +import javax.inject.Inject; +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(); + } + } +} diff --git a/juick-demo/src/main/java/com/juick/demo/configuration/DemoAppConfiguration.java b/juick-demo/src/main/java/com/juick/demo/configuration/DemoAppConfiguration.java new file mode 100644 index 00000000..6a7f697a --- /dev/null +++ b/juick-demo/src/main/java/com/juick/demo/configuration/DemoAppConfiguration.java @@ -0,0 +1,90 @@ +package com.juick.demo.configuration; + +import ch.vorburger.exec.ManagedProcessException; +import ch.vorburger.mariadb4j.DB; +import com.juick.server.protocol.JuickProtocol; +import com.juick.service.search.SearchService; +import org.apache.commons.dbcp2.BasicDataSource; +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.DependsOn; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.TransactionManagementConfigurer; + +import java.util.Collections; +import java.util.List; + +/** + * Created by vitalyster on 19.12.2016. + */ +@Configuration +@ComponentScan(basePackages = {"com.juick.demo", "com.juick.service", "com.juick.server.protocol"}) +public class DemoAppConfiguration implements ApplicationContextAware, TransactionManagementConfigurer { + private ApplicationContext applicationContext; + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } + @Bean + public BasicDataSource dataSource() { + try { + DB db = DB.newEmbeddedDB(33306); + db.start(); + db.createDB("juick"); + db.source("schema.sql"); + BasicDataSource dataSource = new BasicDataSource(); + dataSource.setDriverClassName("com.mysql.jdbc.Driver"); + dataSource.setUrl("jdbc:mysql://localhost:33306/juick?autoReconnect=true&user=root"); + + dataSource.setValidationQuery("select 1"); + + return dataSource; + } catch (ManagedProcessException e) { + return null; + } + } + + @Bean + public PlatformTransactionManager transactionManager() { + return new DataSourceTransactionManager(dataSource()); + } + + @Override + public PlatformTransactionManager annotationDrivenTransactionManager() { + return transactionManager(); + } + + @Bean + @DependsOn("dataSource") + public JdbcTemplate jdbcTemplate() { + return new JdbcTemplate(dataSource()); + } + @Bean + public SearchService emptySearchService() { + return new SearchService() { + @Override + public void setMaxResult(int maxResult) { + } + + @Override + public List<Integer> searchInAllMessages(String searchString, int messageIdBefore) { + return Collections.emptyList(); + } + + @Override + public List<Integer> searchByStringAndUser(String searchString, int userId, int messageIdBefore) { + return Collections.emptyList(); + } + }; + } + @Bean + public JuickProtocol juickProtocol() { + return new JuickProtocol("http://localhost:8080/"); + } +} |