Created
August 14, 2010 19:03
-
-
Save ussy/524594 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.collection.JavaConversions._ | |
import java.lang.reflect.{Type, ParameterizedType} | |
import jp.sf.amateras.mirage._ | |
import jp.sf.amateras.mirage.session._ | |
abstract class AbstractService[T] { | |
private def findEntityClass() : Option[Class[_]] = { | |
def genericParameter(t: Type) : scala.Array[Type] = { | |
if (classOf[ParameterizedType].isInstance(t)) { | |
return classOf[ParameterizedType].cast(t).getActualTypeArguments() | |
} | |
scala.Array[Type]() | |
} | |
var c = getClass() | |
while (c != classOf[Any]) { | |
if (c.getSuperclass() == classOf[AbstractService[_]]) { | |
val t = c.getGenericSuperclass() | |
return genericParameter(t) match { | |
case arrays if (arrays.length > 0) => Some(classOf[Class[_]].cast(arrays(0))) | |
case _ => None | |
} | |
} | |
c = c.getSuperclass() | |
} | |
None | |
} | |
val entityClass = findEntityClass() match { | |
case Some(clazz) => clazz.asInstanceOf[Class[T]] | |
case _ => throw new Exception("Not Support Generic Type") | |
} | |
val sqlFilePathPrefix = "/META-INF/sql/" + entityClass.getName().replaceAll("\\.", "/") + "/" | |
def sqlManager = { | |
val session = SessionFactory.getSession() | |
session.getSqlManager() | |
} | |
def sqlFilePath(sqlPath: String) = sqlFilePathPrefix + sqlPath | |
def find(id: AnyVal*) : T = { | |
sqlManager.findEntity(entityClass, id.map { _.asInstanceOf[AnyRef] }: _*) | |
} | |
def getCount(sqlPath: String, param: Map[String, AnyRef]) : Int = { | |
sqlManager.getCount(sqlFilePath(sqlPath), convertJMap(param)) | |
} | |
def getSingleResult(sqlPath: String, param: Map[String, AnyRef] = Map()) : Option[T] = { | |
sqlManager.getSingleResult(entityClass, sqlFilePath(sqlPath), convertJMap(param)) match { | |
case null => None | |
case entity => Some(entity) | |
} | |
} | |
def getResultList(sqlPath: String, param: Map[String, AnyRef] = Map()) : List[T] = { | |
sqlManager.getResultList(entityClass, sqlFilePath(sqlPath), convertJMap(param)).toList | |
} | |
def insert(entity: T) = sqlManager.insertEntity(entity) | |
def update(entity: T) = sqlManager.updateEntity(entity) | |
def delete(entity: T) = sqlManager.deleteEntity(entity) | |
def call(functionName: String, param: Map[String, AnyRef]) : Option[T] = { | |
sqlManager.call(entityClass, functionName, convertJMap(param)) match { | |
case null => None | |
case entity => Some(entity) | |
} | |
} | |
def callForList(functionName: String, param: Map[String, AnyRef] = Map()) : List[T] = { | |
sqlManager.callForList(entityClass, functionName, convertJMap(param)).toList | |
} | |
def executeUpdate(sqlPath: String, param: Map[String, AnyRef] = Map()) : Int = { | |
sqlManager.executeUpdate(sqlFilePath(sqlPath), convertJMap(param)) | |
} | |
def iterate[R](sqlPath: String, param: Map[String, AnyRef] = Map())(callback: T => R) : R = { | |
sqlManager.iterate(entityClass, new IterationCallback[T, R]() { | |
def iterate(entity: T) : R = callback(entity) | |
}, sqlFilePath(sqlPath), convertJMap(param)) | |
} | |
private def convertJMap(param: Map[String, AnyRef]) : java.util.Map[String, Object] = param | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment