Browse Source

First commit with working H2 database and Hibernate configuration\! Persistance here we come\!\!\!

master
Drew Short 11 years ago
parent
commit
0d71dbe334
  1. 7
      pom.xml
  2. 2
      src/main/resources/application.conf
  3. 56
      src/main/resources/hibernate.cfg.xml
  4. 16
      src/main/resources/hibernate/Image.hbm.xml
  5. 18
      src/main/resources/hibernate/ImageHash.hbm.xml
  6. 36
      src/main/scala/com/sothr/imagetools/dao/HibernateUtil.scala
  7. 40
      src/main/scala/com/sothr/imagetools/dao/ImageDAO.scala
  8. 24
      src/main/scala/com/sothr/imagetools/dto/ImageHashDTO.scala
  9. 27
      src/main/scala/com/sothr/imagetools/image/Image.scala
  10. 39
      src/main/scala/com/sothr/imagetools/image/ImageService.scala
  11. 2
      src/main/scala/com/sothr/imagetools/util/PropertiesEnum.scala
  12. 2
      src/test/resources/application.conf
  13. 35
      src/test/resources/hibernate.cfg.xml
  14. 16
      src/test/resources/hibernate/Image.hbm.xml
  15. 18
      src/test/resources/hibernate/ImageHash.hbm.xml

7
pom.xml

@ -121,9 +121,9 @@
<version>${lib.jta.version}</version> <version>${lib.jta.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>${lib.ehcache.version}</version>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>${lib.ehcache.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.typesafe.akka</groupId> <groupId>com.typesafe.akka</groupId>
@ -351,6 +351,7 @@
<phase>process-resources</phase> <phase>process-resources</phase>
<configuration> <configuration>
<tasks> <tasks>
<!-- Copy hibernate configuration files -->
<copy todir="${basedir}/src/test/resources/hibernate"> <copy todir="${basedir}/src/test/resources/hibernate">
<fileset dir="${basedir}/src/main/resources/hibernate" includes="**/*" /> <fileset dir="${basedir}/src/main/resources/hibernate" includes="**/*" />
</copy> </copy>

2
src/main/resources/application.conf

@ -54,7 +54,7 @@ app {
} }
//Default Database Settings //Default Database Settings
database { database {
location = ".cache/database.db"
connectionURL = "jdbc:h2:.cache/imageTools"
inMemory = false inMemory = false
} }
} }

56
src/main/resources/hibernate.cfg.xml

@ -3,32 +3,34 @@
"-//Hibernate/Hibernate Configuration DTD//EN" "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration> <hibernate-configuration>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory
name="java:hibernate/SessionFactory">
<!-- properties -->
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.datasource">jdbc:h2:.cache/database.db</property>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="show_sql">false</property>
<property name="transaction.factory_class">
org.hibernate.transaction.JTATransactionFactory
</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- mapping files -->
<!--<mapping resource="hibernate/"/>-->
<!-- cache settings -->
<!--<class-cache class="org.hibernate.auction.Item" usage="read-write"/>
<class-cache class="org.hibernate.auction.Bid" usage="read-only"/>
<collection-cache collection="org.hibernate.auction.Item.bids" usage="read-write"/>-->
</session-factory>
<!-- properties -->
<property name="connection.driver_class">org.h2.Driver</property>
<!--<property name="connection.connection.url">jdbc:h2:imageTools.db</property>-->
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="show_sql">false</property>
<!--<property name="transaction.factory_class">
org.hibernate.transaction.JTATransactionFactory
</property>-->
<property name="jta.UserTransaction">java:comp/UserTransaction</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<!--<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>-->
<!-- mapping files -->
<mapping resource="hibernate/Image.hbm.xml"/>
<mapping resource="hibernate/ImageHash.hbm.xml"/>
<!-- cache settings -->
<!--<class-cache class="org.hibernate.auction.Item" usage="read-write"/>
<class-cache class="org.hibernate.auction.Bid" usage="read-only"/>
<collection-cache collection="org.hibernate.auction.Item.bids" usage="read-write"/>-->
</session-factory>
</hibernate-configuration> </hibernate-configuration>

16
src/main/resources/hibernate/Image.hbm.xml

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.sothr.imagetools.image.Image" table="Image">
<meta attribute="class-description">
This class contains the image hashes and meta data
</meta>
<id name="imagePath" type="string" column="path"/>
<property name="thumbnailPath" column="thumbnail_path" type="string"/>
<property name="width" column="width" type="int"/>
<property name="height" column="height" type="int"/>
<one-to-one name="hashes" class="com.sothr.imagetools.dto.ImageHashDTO"/>
</class>
</hibernate-mapping>

18
src/main/resources/hibernate/ImageHash.hbm.xml

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.sothr.imagetools.dto.ImageHashDTO" table="ImageHash">
<meta attribute="class-description">
This class contains the image hashes
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="ahash" column="ahash" type="long"/>
<property name="dhash" column="dhash" type="long"/>
<property name="phash" column="phash" type="long"/>
<property name="md5" column="md5" type="string"/>
</class>
</hibernate-mapping>

36
src/main/scala/com/sothr/imagetools/dao/HibernateUtil.scala

@ -0,0 +1,36 @@
package com.sothr.imagetools.dao
import grizzled.slf4j.Logging
import org.hibernate.SessionFactory
import org.hibernate.cfg.Configuration
import com.sothr.imagetools.util.{PropertiesEnum, PropertiesService}
/**
* Created by drew on 2/8/14.
*/
object HibernateUtil extends Logging {
private val sessionFactory:SessionFactory = buildSessionFactory()
private def buildSessionFactory():SessionFactory = {
try {
// Create the SessionFactory from hibernate.cfg.xml
val configuration = new Configuration().configure("hibernate.cfg.xml")
//set the database location
info(s"Connecting to database at: \'${PropertiesService.get(PropertiesEnum.DatabaseConnectionURL.toString)}\'")
configuration.setProperty("hibernate.connection.url", PropertiesService.get(PropertiesEnum.DatabaseConnectionURL.toString))
return configuration.buildSessionFactory
} catch {
case ex:Throwable =>
// Make sure you log the exception, as it might be swallowed
error("Initial SessionFactory creation failed.", ex)
throw new ExceptionInInitializerError(ex)
}
}
def getSessionFactory():SessionFactory = {
sessionFactory
}
}

40
src/main/scala/com/sothr/imagetools/dao/ImageDAO.scala

@ -0,0 +1,40 @@
package com.sothr.imagetools.dao
import org.hibernate.{Session, SessionFactory}
import com.sothr.imagetools.image.Image
/**
* Created by drew on 2/8/14.
*/
class ImageDAO {
private val sessionFactory:SessionFactory = HibernateUtil.getSessionFactory()
private val example:Image = new Image()
def find(path:String):Image = {
val session:Session = sessionFactory.getCurrentSession
session.beginTransaction
val result = session.get(example.getClass, path).asInstanceOf[Image]
session.getTransaction.commit()
result
}
def save(image:Image) = {
val session:Session = sessionFactory.getCurrentSession
session.beginTransaction
session.saveOrUpdate(image)
session.getTransaction.commit()
}
def save(images:List[Image]) = {
val session:Session = sessionFactory.getCurrentSession
session.beginTransaction
for (image <- images) session.saveOrUpdate(image)
session.getTransaction.commit()
}
}

24
src/main/scala/com/sothr/imagetools/dto/ImageHashDTO.scala

@ -1,11 +1,31 @@
package com.sothr.imagetools.dto package com.sothr.imagetools.dto
import grizzled.slf4j.Logging import grizzled.slf4j.Logging
import javax.persistence._
class ImageHashDTO(val ahash:Long, val dhash:Long, val phash:Long, val md5:String) extends Serializable with Logging{
@Entity
@Table(name = "ImageHash")
class ImageHashDTO(var ahash:Long, var dhash:Long, var phash:Long, var md5:String) extends Serializable with Logging {
def this() = this (0l, 0l, 0l, "")
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
var id:Int = _
def getId:Int = id
def setId(newId:Int) = { id = newId }
def getAhash:Long = ahash
def setAhash(hash:Long) = { ahash = hash}
def getDhash:Long = dhash
def setDhash(hash:Long) = { dhash = hash}
def getPhash:Long = phash
def setPhash(hash:Long) = { phash = hash}
def getMd5:String = md5
def setMd5(hash:String) = { md5 = hash}
def cloneHashes:ImageHashDTO = { def cloneHashes:ImageHashDTO = {
return new ImageHashDTO(ahash,dhash,phash,md5)
new ImageHashDTO(ahash,dhash,phash,md5)
} }
override def hashCode():Int = { override def hashCode():Int = {

27
src/main/scala/com/sothr/imagetools/image/Image.scala

@ -3,8 +3,33 @@ package com.sothr.imagetools.image
import com.sothr.imagetools.dto.ImageHashDTO import com.sothr.imagetools.dto.ImageHashDTO
import com.sothr.imagetools.hash.HashService import com.sothr.imagetools.hash.HashService
import grizzled.slf4j.Logging import grizzled.slf4j.Logging
import javax.persistence._
class Image(val imagePath:String, val thumbnailPath:String, val imageSize:Tuple2[Int,Int], var hashes:ImageHashDTO = null) extends Serializable with Logging{
@Entity
@Table(name = "Image")
class Image(val image:String, val thumbnail:String, val size:Tuple2[Int,Int], val imageHashes:ImageHashDTO = null) extends Serializable with Logging {
def this() = this ("", "", (0,0), null)
@Id
var imagePath:String = image
def getImagePath:String = imagePath
def setImagePath(path:String) = { imagePath = path}
var thumbnailPath:String = thumbnail
def getThumbnailPath:String = thumbnailPath
def setThumbnailPath(path:String) = { thumbnailPath = path}
var width:Int = size._1
def getWidth:Int = width
def setWidth(size:Int) = { width = size}
var height:Int = size._2
def getHeight:Int = height
def setHeight(size:Int) = { height = size}
var hashes:ImageHashDTO = imageHashes
def getHashes:ImageHashDTO = hashes
def setHashes(newHashes:ImageHashDTO) = { hashes = newHashes}
@transient
var imageSize:Tuple2[Int,Int] = { new Tuple2(width, height) }
var imageType:ImageType = ImageType.SingleFrameImage var imageType:ImageType = ImageType.SingleFrameImage

39
src/main/scala/com/sothr/imagetools/image/ImageService.scala

@ -10,28 +10,47 @@ import javax.imageio.ImageIO
import java.io.IOException import java.io.IOException
import net.sf.ehcache.Element import net.sf.ehcache.Element
import com.sothr.imagetools.util.{PropertiesEnum, PropertiesService} import com.sothr.imagetools.util.{PropertiesEnum, PropertiesService}
import com.sothr.imagetools.dao.ImageDAO
object ImageService extends Logging { object ImageService extends Logging {
val imageCache = AppConfig.cacheManager.getCache("images") val imageCache = AppConfig.cacheManager.getCache("images")
private val imageDAO = new ImageDAO()
private def lookupImage(file:File):Image = { private def lookupImage(file:File):Image = {
var image:Image = null
//get from memory cache if possible
var image:Image = null
var found = false
//get from memory cache if possible
try {
if (imageCache.isKeyInCache(file.getAbsolutePath)) {
found = true
image = imageCache.get(file.getAbsolutePath).getObjectValue.asInstanceOf[Image]
}
} catch {
case npe:NullPointerException => error(s"Error grabbing \'${file.getAbsolutePath}\' from cache", npe)
}
//get from datastore if possible
if (!found) {
try { try {
if (imageCache.isKeyInCache(file.getAbsolutePath)) image = imageCache.get(file.getAbsolutePath).getObjectValue.asInstanceOf[Image]
val tempImage = imageDAO.find(file.getAbsolutePath)
if (tempImage != null) image = tempImage
} catch { } catch {
case npe:NullPointerException => error(s"Error grabbing \'${file.getAbsolutePath}\' from cache", npe)
case ex:Exception => error(s"Error looking up \'${file.getAbsolutePath}\' from database", ex)
} }
//get from datastore if possible
image
}
image
} }
private def saveImage(image:Image):Image = { private def saveImage(image:Image):Image = {
//save to cache
imageCache.put(new Element(image.imagePath, image))
//save to datastore
image
//save to cache
imageCache.put(new Element(image.imagePath, image))
//save to datastore
try {
imageDAO.save(image)
} catch {
case ex:Exception => error(s"Error saving up \'${image.imagePath}\' to database", ex)
}
image
} }
def getImage(file:File):Image = { def getImage(file:File):Image = {

2
src/main/scala/com/sothr/imagetools/util/PropertiesEnum.scala

@ -27,4 +27,6 @@ object PropertiesEnum extends Enumeration {
//Default Thumbnail Settings //Default Thumbnail Settings
val ThumbnailDirectory = Value("app.thumbnail.directory") val ThumbnailDirectory = Value("app.thumbnail.directory")
val ThumbnailSize = Value("app.thumbnail.size") val ThumbnailSize = Value("app.thumbnail.size")
//Default Database Settings
val DatabaseConnectionURL = Value("app.database.connectionURL")
} }

2
src/test/resources/application.conf

@ -54,7 +54,7 @@ app {
} }
//Default Database Settings //Default Database Settings
database { database {
location = ".cache/database.db"
connectionURL = "jdbc:h2:.cache/imageTools"
inMemory = false inMemory = false
} }
} }

35
src/test/resources/hibernate.cfg.xml

@ -0,0 +1,35 @@
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory>
<!-- properties -->
<property name="connection.driver_class">org.h2.Driver</property>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="show_sql">true</property>
<!--<property name="transaction.factory_class">
org.hibernate.transaction.JTATransactionFactory
</property>-->
<property name="jta.UserTransaction">java:comp/UserTransaction</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<!--<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>-->
<!-- mapping files -->
<mapping resource="hibernate/Image.hbm.xml"/>
<mapping resource="hibernate/ImageHash.hbm.xml"/>
<!-- cache settings -->
<!--<class-cache class="org.hibernate.auction.Item" usage="read-write"/>
<class-cache class="org.hibernate.auction.Bid" usage="read-only"/>
<collection-cache collection="org.hibernate.auction.Item.bids" usage="read-write"/>-->
</session-factory>
</hibernate-configuration>

16
src/test/resources/hibernate/Image.hbm.xml

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.sothr.imagetools.image.Image" table="Image">
<meta attribute="class-description">
This class contains the image hashes and meta data
</meta>
<id name="imagePath" type="string" column="path"/>
<property name="thumbnailPath" column="thumbnail_path" type="string"/>
<property name="width" column="width" type="int"/>
<property name="height" column="height" type="int"/>
<one-to-one name="hashes" class="com.sothr.imagetools.dto.ImageHashDTO"/>
</class>
</hibernate-mapping>

18
src/test/resources/hibernate/ImageHash.hbm.xml

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.sothr.imagetools.dto.ImageHashDTO" table="ImageHash">
<meta attribute="class-description">
This class contains the image hashes
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="ahash" column="ahash" type="long"/>
<property name="dhash" column="dhash" type="long"/>
<property name="phash" column="phash" type="long"/>
<property name="md5" column="md5" type="string"/>
</class>
</hibernate-mapping>
Loading…
Cancel
Save