Created model objects and DB layer.
This commit is contained in:
		
							
								
								
									
										
											BIN
										
									
								
								src/main/groovy/com/jdbernard/nlsongs/db/.NLSongsDB.groovy.swp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/main/groovy/com/jdbernard/nlsongs/db/.NLSongsDB.groovy.swp
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										66
									
								
								src/main/groovy/com/jdbernard/nlsongs/db/NLSongsDB.groovy
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								src/main/groovy/com/jdbernard/nlsongs/db/NLSongsDB.groovy
									
									
									
									
									
										Normal file
									
								
							@@ -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<Object>)
 | 
			
		||||
        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 }
 | 
			
		||||
    }
 | 
			
		||||
    */
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,7 @@
 | 
			
		||||
package com.jdbernard.nlsongs.model
 | 
			
		||||
 | 
			
		||||
public class ApiKey implements Serializable {
 | 
			
		||||
 | 
			
		||||
    String key
 | 
			
		||||
    String description
 | 
			
		||||
}
 | 
			
		||||
@@ -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
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,8 @@
 | 
			
		||||
package com.jdbernard.nlsongs.model
 | 
			
		||||
 | 
			
		||||
public class Service implements Serializable {
 | 
			
		||||
 | 
			
		||||
    int id
 | 
			
		||||
    Date date
 | 
			
		||||
    ServiceType serviceType
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,3 @@
 | 
			
		||||
package com.jdbernard.nlsongs.model;
 | 
			
		||||
 | 
			
		||||
public enum ServiceType { SUN_AM, SUN_PM, WED }
 | 
			
		||||
							
								
								
									
										8
									
								
								src/main/groovy/com/jdbernard/nlsongs/model/Song.groovy
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/main/groovy/com/jdbernard/nlsongs/model/Song.groovy
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
package com.jdbernard.nlsongs.model
 | 
			
		||||
 | 
			
		||||
public class Song implements Serializable {
 | 
			
		||||
 | 
			
		||||
    int id
 | 
			
		||||
    String name
 | 
			
		||||
    List<String> artists
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										8
									
								
								src/main/groovy/com/jdbernard/nlsongs/model/User.groovy
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/main/groovy/com/jdbernard/nlsongs/model/User.groovy
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
package com.jdbernard.nlsongs.model
 | 
			
		||||
 | 
			
		||||
public class User {
 | 
			
		||||
 | 
			
		||||
    int id
 | 
			
		||||
    String username
 | 
			
		||||
    String pwd
 | 
			
		||||
}
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							@@ -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') }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user