From 3e07dc20bfc6a64f34992be4201f8a0761322f25 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Fri, 20 Feb 2015 01:08:17 -0600 Subject: [PATCH] Created model objects and DB layer. --- .gitignore | 1 + build.gradle | 15 ++++ .../nlsongs/db/.NLSongsDB.groovy.swp | Bin 0 -> 12288 bytes .../com/jdbernard/nlsongs/db/NLSongsDB.groovy | 66 ++++++++++++++++++ .../com/jdbernard/nlsongs/model/ApiKey.groovy | 7 ++ .../nlsongs/model/Performance.groovy | 13 ++++ .../jdbernard/nlsongs/model/Service.groovy | 8 +++ .../jdbernard/nlsongs/model/ServiceType.java | 3 + .../com/jdbernard/nlsongs/model/Song.groovy | 8 +++ .../com/jdbernard/nlsongs/model/User.groovy | 8 +++ .../.NLSongsContextListener.groovy.swp | Bin 0 -> 12288 bytes .../servlet/NLSongsContextListener.groovy | 42 +++++++++++ src/main/sql/create-db.sql | 59 ++++++++++++++++ .../nlsongs/db/GenerateQueries.groovy | 32 +++++++++ 14 files changed, 262 insertions(+) create mode 100644 src/main/groovy/com/jdbernard/nlsongs/db/.NLSongsDB.groovy.swp create mode 100644 src/main/groovy/com/jdbernard/nlsongs/db/NLSongsDB.groovy create mode 100644 src/main/groovy/com/jdbernard/nlsongs/model/ApiKey.groovy create mode 100644 src/main/groovy/com/jdbernard/nlsongs/model/Performance.groovy create mode 100644 src/main/groovy/com/jdbernard/nlsongs/model/Service.groovy create mode 100644 src/main/groovy/com/jdbernard/nlsongs/model/ServiceType.java create mode 100644 src/main/groovy/com/jdbernard/nlsongs/model/Song.groovy create mode 100644 src/main/groovy/com/jdbernard/nlsongs/model/User.groovy create mode 100644 src/main/groovy/com/jdbernard/nlsongs/servlet/.NLSongsContextListener.groovy.swp create mode 100644 src/main/groovy/com/jdbernard/nlsongs/servlet/NLSongsContextListener.groovy create mode 100644 src/main/sql/create-db.sql create mode 100644 src/tools/groovy/com/jdbernard/nlsongs/db/GenerateQueries.groovy diff --git a/.gitignore b/.gitignore index 9f2a078..d0639b9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build/ .gradle/ +*.sw? diff --git a/build.gradle b/build.gradle index 80547c9..0ace28d 100644 --- a/build.gradle +++ b/build.gradle @@ -13,6 +13,21 @@ dependencies { compile 'org.slf4j:slf4j-api:1.7.10' compile 'ch.qos.logback:logback-core:1.1.2' compile 'ch.qos.logback:logback-classic:1.1.2' + compile 'javax.ws.rs:javax.ws.rs-api:2.0.1' + compile 'javax.servlet:javax.servlet-api:3.1.0' + compile 'com.zaxxer:HikariCP-java6:2.3.2' + compile 'com.impossibl.pgjdbc-ng:pgjdbc-ng:0.3' + runtime 'org.glassfish.jersey.containers:jersey-container-servlet:2.16' testCompile 'junit:junit:4.12' + + /* + compile 'org.hibernate:hibernate-core:4.3.8.Final' + compile 'org.hibernate:hibernate-validator:5.1.3.Final' + copmile 'javax.el:javax.el-api:2.2.4' + compile 'org.glassfish.web:javax.el:2.2.4' + compile 'org.modelmapper:modelmapper:0.7.3' + compile 'org.springframework:spring-context:4.1.4.RELEASE' + compile 'com.mchange:c3p0:0.9.5' + */ } diff --git a/src/main/groovy/com/jdbernard/nlsongs/db/.NLSongsDB.groovy.swp b/src/main/groovy/com/jdbernard/nlsongs/db/.NLSongsDB.groovy.swp new file mode 100644 index 0000000000000000000000000000000000000000..314c8d3979376e551d537229b8050c7ae5649379 GIT binary patch literal 12288 zcmeI2O>Z1E7{^^WlokpIBu)sKv{Gk7){{^WD3G=^3A7?1D4P_C?W&rc@g`1ZXQsAy z^TvXNkPza^7pTMq1jlmXhTtPW#i2n6xBz!9RQx@!$+itbJ)zjrKa;Va$Is)RUskJi zd;H>sdHMo-j-WhB$k*Thw)FPlr^t`b6S5Yxxr{s+n6`Zi?CMHtuO9IJK4#?IvO_nK z@f!EFawEQ84@HO9RUEA-S4rRPdLnXHWE}T5T|e%+l~)vIRzcf+W3FKwX3nw9a=*B? z%@R291bVttfAJVO_3XsBVR3xqY5MqM%Lg8d1z-tS0+xU!UIypq% z!+`J02YoJo9vXV)ul8XHSOS)SC143y0+xU!U z`~m$AeFa^Iu0b2n9CSbQ?L&lo4t)lF3|)j6RELg2I}f4_YC~tC8stE?juP@K^b2$y z`Uvu&C!xQN5b_hW1Kof=fdsS)%|J(>pE3SVq4%M8pdMtHoPunz1S|ndz!I=IvCuWKcXaBH76LWPqdgHhav6}(rqd< z^Swl9FBDtcsnG#ji$&zrh{>xc4J}2qlBJ0Rk)P>nyhWcOg^77)X3dsqg^lB66*Vfk zAIkuqHKvkKXy@eRlT`#6uQTrXtH`D_90P&+X-dle7ux3HW0nH?_Cs%Li{TdJg-B^H z@{xLKiWe<>np5u#C@D2!_?uCbwy6!nT~g$r1y=?A!Z(9oL8zi5 zbaS$mc9fGgZK>Z&6Y7V>CCuNqhr z^h^1sl^WUa@@Bn!7DommWu<%UWQ2ZPHFJ^0kN~ zRmJ9^;`u_LQ7;U4O@diB*bK!wp-zL#zVLamaf=Mn4O6u3VKma1otvFrqNDWH#f5pQ zGJi$iIzPKOOGSY3o*5aVO%b%Hr|6te`sHkop03V;JM2&RsjDID2&ptSu9F5$dhHN< zJWqsnL=@!zJ0jAER&L~!N@19@$i4e9vQn!(9mMN6?r`=5p7t7XPx_qgsvqE!3yUBv zdQ~Wfe2^7R9@@AXI+YWSPLkWv)UaxTef5$WsOe!k*-T87S}a=yr?gIEwHQgIxn+o`1z#;dkK>w8=lQvcLKO literal 0 HcmV?d00001 diff --git a/src/main/groovy/com/jdbernard/nlsongs/db/NLSongsDB.groovy b/src/main/groovy/com/jdbernard/nlsongs/db/NLSongsDB.groovy new file mode 100644 index 0000000..c99b805 --- /dev/null +++ b/src/main/groovy/com/jdbernard/nlsongs/db/NLSongsDB.groovy @@ -0,0 +1,66 @@ +package com.jdbernard.nlsongs.db + +import java.sql.Connection +import java.sql.PreparedStatement +import java.sql.ResultSet +import com.zaxxer.hikari.HikariDataSource + +import groovy.sql.GroovyRowResult +import groovy.sql.Sql +import groovy.transform.CompileStatic + +import com.jdbernard.nlsongs.model.* + +//@CompileStatic +public class NLSongsDB { + + private HikariDataSource dataSource + private Sql sql + + public NLSongsDB(HikariDataSource dataSource) { + this.dataSource = dataSource + this.sql = new Sql(dataSource) } + + + public void shutdown() { dataSource.shutdown() } + + // ### Services + public Service findService(int id) { + GroovyRowResult row = sql.firstRow( + "SELECT * FROM services WHERE id = ?", [id] as List) + if (row) return (Service) recordToModel(row, Service) + else return null } + + // #### Utility functions + static def recordToModel(def row, Class clazz) { + def model = clazz.newInstance() + + row.each { recordKey, v -> + def pts = recordKey.split('_') + def modelKey = pts.length == 1 ? pts[0] : + pts[0] + pts[1..-1].collect { it.capitalize() }.join() + model[modelKey] = v } + return model } + + static def modelToRecord(def model) { + def record = [:] + + model.properties.each { modelKey, v -> + if (modelKey == "class") return + def recordKey = modelKey. + replaceAll(/(\p{javaUpperCase})/, /_$1/).toLowerCase() + record[recordKey] = v } + return record } + + /* + static Object recordToModel(GroovyRowResult row, Class clazz) { + Object model = clazz.newInstance() + + row.each { recordKey, v -> + String[] pts = ((String) recordKey).split('_') + String modelKey = pts[0] + + pts[1..-1].collect { it.capitalize() }.join() + model[modelKey] = v } + } + */ +} diff --git a/src/main/groovy/com/jdbernard/nlsongs/model/ApiKey.groovy b/src/main/groovy/com/jdbernard/nlsongs/model/ApiKey.groovy new file mode 100644 index 0000000..06d1f02 --- /dev/null +++ b/src/main/groovy/com/jdbernard/nlsongs/model/ApiKey.groovy @@ -0,0 +1,7 @@ +package com.jdbernard.nlsongs.model + +public class ApiKey implements Serializable { + + String key + String description +} diff --git a/src/main/groovy/com/jdbernard/nlsongs/model/Performance.groovy b/src/main/groovy/com/jdbernard/nlsongs/model/Performance.groovy new file mode 100644 index 0000000..d1f548b --- /dev/null +++ b/src/main/groovy/com/jdbernard/nlsongs/model/Performance.groovy @@ -0,0 +1,13 @@ +package com.jdbernard.nlsongs.model + +public class Performance implements Serializable { + + int serviceId + int songId + String pianist + String organist + String bassist + String drummer + String guitarist + String leader +} diff --git a/src/main/groovy/com/jdbernard/nlsongs/model/Service.groovy b/src/main/groovy/com/jdbernard/nlsongs/model/Service.groovy new file mode 100644 index 0000000..5929482 --- /dev/null +++ b/src/main/groovy/com/jdbernard/nlsongs/model/Service.groovy @@ -0,0 +1,8 @@ +package com.jdbernard.nlsongs.model + +public class Service implements Serializable { + + int id + Date date + ServiceType serviceType +} diff --git a/src/main/groovy/com/jdbernard/nlsongs/model/ServiceType.java b/src/main/groovy/com/jdbernard/nlsongs/model/ServiceType.java new file mode 100644 index 0000000..1777242 --- /dev/null +++ b/src/main/groovy/com/jdbernard/nlsongs/model/ServiceType.java @@ -0,0 +1,3 @@ +package com.jdbernard.nlsongs.model; + +public enum ServiceType { SUN_AM, SUN_PM, WED } diff --git a/src/main/groovy/com/jdbernard/nlsongs/model/Song.groovy b/src/main/groovy/com/jdbernard/nlsongs/model/Song.groovy new file mode 100644 index 0000000..112a7c9 --- /dev/null +++ b/src/main/groovy/com/jdbernard/nlsongs/model/Song.groovy @@ -0,0 +1,8 @@ +package com.jdbernard.nlsongs.model + +public class Song implements Serializable { + + int id + String name + List artists +} diff --git a/src/main/groovy/com/jdbernard/nlsongs/model/User.groovy b/src/main/groovy/com/jdbernard/nlsongs/model/User.groovy new file mode 100644 index 0000000..18b17f5 --- /dev/null +++ b/src/main/groovy/com/jdbernard/nlsongs/model/User.groovy @@ -0,0 +1,8 @@ +package com.jdbernard.nlsongs.model + +public class User { + + int id + String username + String pwd +} diff --git a/src/main/groovy/com/jdbernard/nlsongs/servlet/.NLSongsContextListener.groovy.swp b/src/main/groovy/com/jdbernard/nlsongs/servlet/.NLSongsContextListener.groovy.swp new file mode 100644 index 0000000000000000000000000000000000000000..28b960ecd20bd02685951468d64132b3388c5fec GIT binary patch literal 12288 zcmeI2%Wl&^6ov}R(Etl4sIddIq6@m92@*)Ku<4v9zL*m0bsgtTG@J(hl1&YatPGh-?8sh4kGxysMmvkb2zjO~8^ zefj2|@uKS)5}$NiUk8N0I&i^uQ(ga7|u4m0){ z^$zs}bq{qHHHVr-9YTFO#MoEVbJR1`GOC1{LG40)IEenJ7bqX)pne`;>qr~Qn5M7>76LV2hK)CJTJtnV%A1?nm49xAQ*A7oA?69FPX1c(3;AOb|-|0VE{ z@$7Q@J({+HAnL$HW0b774HT@jqqDkb75UIO_sRxs-D(>z>V}14K9u!-Ru_3@#25G= zNfXSZ47so4@LCp;n-=i2i+=2+Y@3E);E+$IJ3! z1<--e*t*}Jn)RioW^udjOP6;d=?%)Pf;K8z1FujUv%i$Y=tnB@2ft;(=#yqQGvJVb z%qyEj30rP?{(GV2=^<1EQ&Aucq_a(p^az%bDPNYWLdh!fsHHeK#=-d~u{RDCT5h9R z9M+UP+QRdO&NoyPgEDIctF0k{0qTZ=xMl*Qtr>0^)dp)=<+K2C=2sDJ3u^cMB^gMQ z#5h}?o&T#Cavf6tx$+j>-6Zv<^(R}YkFMeit*?myke{`Dy?WismoJ@ZjKkeXd4Cv2 zWZ0xV$-BQ0xC9l{Z5;slKLeG8y Ddu}o4 literal 0 HcmV?d00001 diff --git a/src/main/groovy/com/jdbernard/nlsongs/servlet/NLSongsContextListener.groovy b/src/main/groovy/com/jdbernard/nlsongs/servlet/NLSongsContextListener.groovy new file mode 100644 index 0000000..828078a --- /dev/null +++ b/src/main/groovy/com/jdbernard/nlsongs/servlet/NLSongsContextListener.groovy @@ -0,0 +1,42 @@ +package com.jdbernard.nlsongs.servlet + +import javax.servlet.ServletContext +import javax.servlet.ServletContextEvent +import javax.servlet.ServletContextListener + +import com.jdbernard.nlsongs.db.NLSongsDB + +import com.zaxxer.hikari.HikariConfig +import com.zaxxer.hikari.HikariDataSource + +public final class NLSongsContextListener implements ServletContextListener { + + public void contextInitialized(ServletContextEvent event) { + context = event.servletContext + + // Create the pooled data source + HikariConfig hcfg = new HikariConfig() + hcfg.username = 'TODO' + hcfg.password = 'TODO' + hcfg.dataSourceClassName = 'TODO' + hcfg.addDataSourceProperty('cachePrepStmts', 'true') + hcfg.addDataSourceProperty('prepStmtCacheSize', '250') + hcfg.addDataSourceProperty('prepStmtCacheSqlLimit', '2048') + hcfg.addDataSourceProperty('useServerPrepStmts', 'true') + + HikariDataSource hds = new HikariDataSource(hcfg) + + // Create the NLSonsDB instance. + NLSongsDB songsDB = new NLSongsDB(hds) + + context.setAttribute('songsDB', songsDB) } + + public void contextDestroyed(ServletContextEvent event) { + context = event.servletContext + + // Shutdown the Songs DB instance (it will shut down the data source). + NLSongsDB songsDB = context.getAttribute('songsDB') + songsDB.shutdown() + + context.removeAttribute('songsDB') } +} diff --git a/src/main/sql/create-db.sql b/src/main/sql/create-db.sql new file mode 100644 index 0000000..0245745 --- /dev/null +++ b/src/main/sql/create-db.sql @@ -0,0 +1,59 @@ +-- # New Life Songs DB +-- @author Jonathan Bernard +-- +-- PostgreSQL database creation sript. + +-- DROP DATABASE IF EXISTS nlsongs; +CREATE DATABASE IF NOT EXISTS nlsongs + ENCODING = 'UTF8' + LC_COLLATE = 'en_US.UTF-8' + LC_CTYPE = 'en_US.UTF-8' + CONNECTION LIMIT = 1; + + +-- Services table +DROP TABLE IF EXISTS services; +CREATE TABLE IF NOT EXISTS services ( + id SERIAL, + date DATE NOT NULL, + service_type VARCHAR(16) DEFAULT NULL, + PRIMARY KEY (id)); + + +-- Songs table +DROP TABLE IF EXISTS songs; +CREATE TABLE IF NOT EXISTS songs ( + id SERIAL, + name VARCHAR(128) NOT NULL, + artists VARCHAR(256) DEFAULT NULL, + PRIMARY KEY (id)); + + +-- performances table +DROP TABLE IF EXISTS performances; +CREATE TABLE IF NOT EXISTS performances ( + service_id INTEGER NOT NULL, + song_id INTEGER NOT NULL, + pianist VARCHAR(64) DEFAULT NULL, + organist VARCHAR(64) DEFAULT NULL, + bassist VARCHAR(64) DEFAULT NULL, + drummer VARCHAR(64) DEFAULT NULL, + guitarist VARCHAR(64) DEFAULT NULL, + leader VARCHAR(64) DEFAULT NULL, + PRIMARY KEY (service_id, song_id), + FOREIGN KEY (service_id) REFERENCES services (id), + FOREIGN KEY (song_id) REFERENCES songs (id)); + + +DROP TABLE IF EXISTS api_keys; +CREATE TABLE IF NOT EXISTS api_keys ( + key VARCHAR(32) NOT NULL, + description VARCHAR(256) NOT NULL, + PRIMARY KEY (key)); + + +DROP TABLE IF EXISTS users; +CREATE TABLE IF NOT EXISTS users ( + id SERIAL, + username VARCHAR(64), + pwd VARCHAR(80)); diff --git a/src/tools/groovy/com/jdbernard/nlsongs/db/GenerateQueries.groovy b/src/tools/groovy/com/jdbernard/nlsongs/db/GenerateQueries.groovy new file mode 100644 index 0000000..93148e4 --- /dev/null +++ b/src/tools/groovy/com/jdbernard/nlsongs/db/GenerateQueries.groovy @@ -0,0 +1,32 @@ +package com.jdbernard.nlsongs.db + +public class GenerateQueries { + + public static void main(String[] args) { + + } + + public static Map > generateQueries(String ddl) { + + def tables = [:] + + // Find the table definitions + String tableRegex = /(?ms)(?:CREATE TABLE (?:IF NOT EXISTS )?([^\s]+) \(([^\s]+);.+?)+/ + + ddl.eachMatch(tableRegex) { matchGroups -> + String tableName = matchGroups[1] + + // Parse the column definitions. + + // Create new record insert statements. + + // Create insert queries. + + // Create update queries. + + // Create delete queries. + + // Create ID lookup queries. + + } +}