Skip to content

Instantly share code, notes, and snippets.

@sergiopvilar
Last active August 29, 2015 14:12
Show Gist options
  • Save sergiopvilar/71201054eb7073e2924e to your computer and use it in GitHub Desktop.
Save sergiopvilar/71201054eb7073e2924e to your computer and use it in GitHub Desktop.
package com.scrumplus.core.framework.crud
import java.beans.Introspector
import java.lang.reflect.Field
import com.scrumplus.core.framework.annotation.{Private, WriteOnly, ReadOnly}
import scala.language.dynamics
import scala.collection.mutable.ListBuffer
/**
* @project simple-heroku-webapp
* @author sergiovilar
* @date: 12/28/14
*/
class RestEntity extends Dynamic{
/*def selectDynamic(methodName: String) = {
println("I'm calling " + methodName)
functionsMap(methodName).apply()
}*/
def applyDynamic(name: String)(args: Any*): Any = {
var getter = false
var setter = false
var prefix = ""
if(name.indexOf("set") == 0){
prefix = "set"
setter = true
}else if(name.indexOf("get") == 0){
getter = true
prefix = "get"
}else if(name.indexOf("is") == 0){
getter = true
prefix = "is"
}
var readOnly = false
var writeOnly = false
val fieldname = Introspector.decapitalize(name.replace(prefix, ""))
val clz = this.getClass
val field: Field = clz.getDeclaredField(fieldname);
field.setAccessible(true)
if(field.getAnnotation(classOf[ReadOnly]) != null)
readOnly = true
if(field.getAnnotation(classOf[WriteOnly]) != null)
writeOnly = true
if(field.getAnnotation(classOf[Private]) != null){
writeOnly = true
readOnly = true
}
if(getter && !writeOnly){
return field.get(this)
}
if(setter && !readOnly){
field.set(this, args.last)
}
}
}
package com.scrumplus.entity
import java.util.Date
import javax.persistence._
import javax.xml.bind.annotation.XmlRootElement
import com.scrumplus.core.framework.annotation.{WriteOnly, ReadOnly}
import com.scrumplus.core.framework.crud.RestEntity
/**
* @project simple-heroku-webapp
* @author sergiovilar
* @date: 12/21/14
*/
@Entity
@Table(name="user")
class User extends RestEntity(){
@Id
@Column
@GeneratedValue
var id: Long = 0
// ----- Dados pessoais
@Column
var name = ""
@Column
var lastName = ""
@Column(unique=true)
var email = ""
@Column
var username = ""
@Column
@WriteOnly
var password = ""
// ----- Status
@Column
var active = true
@Column
var lastLogin = new Date()
@Column
@ReadOnly
var created = new Date()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment