Permalink
Browse files

implementing saving

  • Loading branch information...
1 parent 04ea7e2 commit 9b0ed0eff51d308b3f360882ca94127e131bb878 @leonlee committed Dec 19, 2012
View
@@ -15,6 +15,7 @@ configure(subprojects.findAll { project -> !emptyProjects.find {it == project.na
repositories {
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
+ maven { url 'http://clojars.org/repo' }
mavenCentral()
}
@@ -0,0 +1,60 @@
+package org.riderzen.ogs.auth
+
+import hirondelle.date4j.DateTime
+import net.sf.oval.Validator
+import net.sf.oval.constraint.Email
+import net.sf.oval.constraint.MaxLength
+import net.sf.oval.constraint.MinLength
+import net.sf.oval.constraint.NotNull
+import org.riderzen.ogs.common.BaseModel
+import org.riderzen.ogs.common.Tools
+
+/**
+ * User: Leon Lee <mail.lgq@gmail.com>
+ * Date: 12-12-19
+ */
+class User extends BaseModel {
+ @MinLength(6)
+ @MaxLength(64)
+ String username
+ @NotNull
+ String password
+ @NotNull
+ UserType type
+ @NotNull
+ UserStatus status
+ @Email(when = "groovy:_value != null")
+ String email
+ @NotNull(when = "groovy:_this.email != null")
+ Long emailBindOn
+ String mobile
+ String deviceToken
+ String clientVersion
+ Long lastNodeId
+ Long lastLoginOn
+
+ static User register(String username, String password, String email = null,
+ String mobile = null, String deviceToken = null, String clientVersion = null) {
+ User user = new User()
+ user.username = username
+ user.password = Tools.encrypt(password)
+ user.email = email
+ user.mobile = mobile
+ user.deviceToken = deviceToken
+ user.clientVersion = clientVersion
+
+ if (email) {
+ user.emailBindOn = Tools.currentTime()
+ }
+
+ User.save()
+ }
+}
+
+enum UserType {
+ auto, register
+}
+
+enum UserStatus {
+ normal, blocked
+}
@@ -1,2 +1,5 @@
dependencies {
+ compile "net.sf.oval:oval:$ovalVsn"
+ compile "commons-codec:commons-codec:$commonsCodecVsn"
+ compile "date4j:date4j:$date4jVsn"
}
@@ -1 +1,5 @@
-modulename=org.riderzen.ogs.plat.common
+modulename=org.riderzen.ogs.platform.common
+
+ovalVsn=1.81
+commonsCodecVsn=1.7
+date4jVsn=1.2.0
@@ -1,10 +1,23 @@
package org.riderzen.ogs.common
+import hirondelle.date4j.DateTime
+import net.sf.oval.Validator
+import net.sf.oval.constraint.NotNull
+
/**
* User: Leon Lee <mail.lgq@gmail.com>
* Date: 12-12-7
*/
class BaseModel {
+ @NotNull
+ Long id
+ @NotNull
+ Long createdOn
+ @NotNull
+ Long updatedOn
+ @NotNull
+ Long vsn
+
static def transientAttributes = ['class', 'metaClass', 'transientAttributes', 'tableName']
BaseModel() {
@@ -24,4 +37,25 @@ class BaseModel {
propMap
}
+ def save() {
+ if (!id){
+ id = DBHelper.nextID()
+ createdOn = Tools.currentTime()
+ updatedOn = createdOn
+ vsn = 0
+ }
+
+ if (validate()) {
+
+ }
+ }
+
+ def validate() {
+ Validator validator = new Validator()
+ def errors = validator.validate(this)
+ if (errors) {
+ throw new InvalidModelException(errors)
+ }
+ true
+ }
}
@@ -0,0 +1,19 @@
+package org.riderzen.ogs.common
+
+import net.sf.oval.ConstraintViolation
+
+/**
+ * User: Leon Lee <mail.lgq@gmail.com>
+ * Date: 12-12-19
+ */
+class InvalidModelException extends Exception {
+ InvalidModelException(List<ConstraintViolation> errors) {
+ StringBuilder messageBuilder = new StringBuilder("Invalid Model:\n")
+ errors.each {
+ messageBuilder << it.message
+ messageBuilder << "\n"
+ }
+
+ this.message = messageBuilder.toString()
+ }
+}
@@ -0,0 +1,22 @@
+package org.riderzen.ogs.common
+
+import hirondelle.date4j.DateTime
+import org.apache.commons.codec.digest.DigestUtils
+
+import java.security.MessageDigest
+
+/**
+ * User: Leon Lee <mail.lgq@gmail.com>
+ * Date: 12-12-19
+ */
+class Tools {
+ static salt = "sJKVn3IGFwjdty37"
+
+ static encrypt(message) {
+ DigestUtils.md5Hex(message + salt)
+ }
+
+ static currentTime() {
+ DateTime.now(TimeZone.getDefault()).getMilliseconds(TimeZone.getDefault())
+ }
+}
@@ -1 +1 @@
-modulename=org.riderzen.ogs.plat.protocol
+modulename=org.riderzen.ogs.platform.protocol
@@ -1 +1 @@
-modulename=org.riderzen.ogs.plat.tcp.server
+modulename=org.riderzen.ogs.platform.tcp-server

0 comments on commit 9b0ed0e

Please sign in to comment.