Skip to content

Commit

Permalink
NashTech-Labs#27 more code re-factoring and added audit controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Sushil committed Mar 31, 2015
1 parent d604c2a commit 6e5d239
Show file tree
Hide file tree
Showing 23 changed files with 275 additions and 274 deletions.
96 changes: 18 additions & 78 deletions app/controllers/AdminController.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package controllers

import model.repository.AuditForm
import model.repository.AuditRepository
import model.repository.Mobile
import model.repository.MobileRepository
import model.repository.MobileStatus
import model.repository.User
import model.repository._
import net.liftweb.json.DefaultFormats
import net.liftweb.json.JsonAST._
import net.liftweb.json.JsonDSL._
Expand All @@ -23,21 +18,13 @@ import play.twirl.api.Html

class AdminController(mobileRepo: MobileRepository, auditRepo: AuditRepository, mail: MailUtil, s3Util: S3UtilComponent) extends Controller with Secured {

implicit val formats = DefaultFormats
/**
* Describes the mobile status form
*/
val mobilestatus = Form(
mapping(
"imeiMeid" -> nonEmptyText)(MobileStatus.apply)(MobileStatus.unapply))

/**
* Describe mobile audit form
*/
val timestampform = Form(
mapping(
"imeiMeid" -> nonEmptyText)(AuditForm.apply)(AuditForm.unapply))

/**
* @param status, mobile status(pending, approved and proofdemanded)
* @return mobiles page with mobile user according to status
Expand Down Expand Up @@ -183,27 +170,27 @@ class AdminController(mobileRepo: MobileRepository, auditRepo: AuditRepository,
}
}

private def tweet(mobileuser: Mobile, msg: String)={
private def tweet(mobileuser: Mobile, msg: String) = {
val post = Play.current.configuration.getBoolean("tweetsWithEmail.post")
if(post.get){
if (post.get) {
Logger.info("AdminController:tweet -> disabled")
}else{
msg match {
case "approve" =>
if (mobileuser.regType == "stolen") {
TwitterTweet.tweetAMobileRegistration(TwitterTweet.tweetForStolen(mobileuser.imeiMeid))
} else {
TwitterTweet.tweetAMobileRegistration(TwitterTweet.tweetForClean(mobileuser.imeiMeid))
}
case "changeMobileRegType" =>
if (mobileuser.regType == "stolen") {
TwitterTweet.tweetAMobileRegistration(TwitterTweet.tweetForStolen(mobileuser.imeiMeid))
} else {
TwitterTweet.tweetAMobileRegistration(TwitterTweet.tweetForClean(mobileuser.imeiMeid))
}
} else {
msg match {
case "approve" =>
if (mobileuser.regType == "stolen") {
TwitterTweet.tweetAMobileRegistration(TwitterTweet.tweetForStolen(mobileuser.imeiMeid))
} else {
TwitterTweet.tweetAMobileRegistration(TwitterTweet.tweetForClean(mobileuser.imeiMeid))
}
case "changeMobileRegType" =>
if (mobileuser.regType == "stolen") {
TwitterTweet.tweetAMobileRegistration(TwitterTweet.tweetForStolen(mobileuser.imeiMeid))
} else {
TwitterTweet.tweetAMobileRegistration(TwitterTweet.tweetForClean(mobileuser.imeiMeid))
}
}
}
}
}

/**
* Deletes existed mobile
Expand All @@ -230,52 +217,5 @@ class AdminController(mobileRepo: MobileRepository, auditRepo: AuditRepository,
Ok("error in Delete ajax call")
}
}

/**
* Display audit page
*/
def auditPage: Action[AnyContent] = withAuth { username =>
implicit request =>
val user: Option[User] = Cache.getAs[User](username)
val list = List()
Ok(views.html.admin.audit("imeid", list, user))
}

/**
* Display timestamp records of particular imei number
*/
def getTimestampByIMEI: Action[AnyContent] = Action {
implicit request =>
Logger.info("AdminController:audit -> called")
val email = request.session.get(Security.username).getOrElse("")
val user: Option[User] = Cache.getAs[User](email)
val audit = timestampform.bindFromRequest()
audit.fold(
hasErrors = { form =>
val list = List()
Ok(views.html.admin.audit("imeid", list, user)).flashing("error" -> "Please correct the errors in the form")
},
success = { timestamp =>
val list = auditRepo.getAllTimestampsByIMEID(timestamp.imeiMeid)
Ok(views.html.admin.audit("imeid", list, user))
})
}

/**
* Display all timestamp records for all mobiles
*/
def getAllTimestamp: Action[AnyContent] = withAuth { username =>
implicit request =>
val user: Option[User] = Cache.getAs[User](username)
val list = auditRepo.getAllTimestamps
Ok(views.html.admin.audit("all", list, user))
}

def analytics(date: String): Action[AnyContent] = withAuth { username =>
implicit request =>
val user: Option[User] = Cache.getAs[User](username)
val list = mobileRepo.getRecordByDate(date)
Ok(views.html.admin.analytics(user, list))
}
}
object AdminController extends AdminController(MobileRepository, AuditRepository, MailUtil, S3Util)
77 changes: 77 additions & 0 deletions app/controllers/AuditController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package controllers

import model.repository._
import net.liftweb.json.DefaultFormats
import net.liftweb.json.JsonAST._
import net.liftweb.json.JsonDSL._
import net.liftweb.json.Serialization.write
import play.api.Logger
import play.api._
import play.api.Play.current
import play.api.data.Form
import play.api.data.Forms.{ mapping, nonEmptyText }
import play.api.mvc._
import utils._
import play.api.cache.Cache
import play.twirl.api.Html
import java.util.Calendar

class AuditController(auditRepo: AuditRepository) extends Controller with Secured {

/**
* Describe mobile audit form
*/
val timestampform = Form(
mapping(
"imeiMeid" -> nonEmptyText)(AuditForm.apply)(AuditForm.unapply))

/**
* Display audit page
*/
def auditPage: Action[AnyContent] = withAuth { username =>
implicit request =>
val user: Option[User] = Cache.getAs[User](username)
val list = List()
Ok(views.html.audit.audit("imeid", list, user))
}

/**
* Display timestamp records of particular imei number
*/
def getTimestampByIMEI: Action[AnyContent] = Action {
implicit request =>
Logger.info("AdminController:audit -> called")
val email = request.session.get(Security.username).getOrElse("")
val user: Option[User] = Cache.getAs[User](email)
timestampform.bindFromRequest().fold(
hasErrors = { form =>
val list = List()
Ok(views.html.audit.audit("imeid", list, user)).flashing("error" -> "Please correct the errors in the form")
},
success = { timestamp =>
val list = auditRepo.getAllTimestampsByIMEID(timestamp.imeiMeid)
Ok(views.html.audit.audit("imeid", list, user))
})
}

/**
* Display all timestamp records for all mobiles
*/
def getAllTimestamp: Action[AnyContent] = withAuth { username =>
implicit request =>
val user: Option[User] = Cache.getAs[User](username)
val list = auditRepo.getAllTimestamps
Ok(views.html.audit.audit("all", list, user))
}

def getRegistrationRecordsByYear(year: String): Action[AnyContent] = withAuth { username =>
implicit request =>
val user: Option[User] = Cache.getAs[User](username)
val years = (2012 to Calendar.getInstance().get(Calendar.YEAR)).toList
val monthList = List("Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec")
val recordList = auditRepo.getRecordByDate(year)
val recordsList = monthList zip recordList
Ok(views.html.audit.analytics(user, recordsList, years))
}
}
object AuditController extends AuditController(AuditRepository)
5 changes: 2 additions & 3 deletions app/controllers/MobileController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import play.api.i18n.Messages
import play.api.libs.json.Json
import play.api.mvc._
import utils._
import utils.UtilDate._
import model.repository._
import java.util.Date
import java.sql.Timestamp
Expand Down Expand Up @@ -112,11 +111,11 @@ class MobileController(mobileRepo: MobileRepository, brandRepo: BrandRepository,
mobileregistrationform.bindFromRequest.fold(
formWithErrors => BadRequest(views.html.mobileRegistrationForm(formWithErrors, mobileBrands, user)),
mobileuser => {
val date = UtilDate.getDate()
val date = commonUtils.getUtilDate()
val index = mobileuser.document.indexOf(".")
val documentName = mobileuser.imeiMeid + mobileuser.document.substring(index)
val result = mobileRepo.insertMobileUser(Mobile(mobileuser.userName, mobileuser.brandId,
mobileuser.mobileModelId, mobileuser.imeiMeid, mobileuser.otherImeiMeid, UtilDate.getDate(mobileuser.purchaseDate), mobileuser.contactNo,
mobileuser.mobileModelId, mobileuser.imeiMeid, mobileuser.otherImeiMeid, commonUtils.getUtilDate(mobileuser.purchaseDate), mobileuser.contactNo,
mobileuser.email, mobileuser.regType, StatusUtil.Status.pending,
mobileuser.description, date, documentName, mobileuser.otherMobileBrand, mobileuser.otherMobileModel))
request.body.file("fileUpload").map { image =>
Expand Down
39 changes: 30 additions & 9 deletions app/model/repository/AuditRepository.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package model.repository
package model.repository

import scala.slick.driver.PostgresDriver.simple._
import scala.slick.driver
import scala.slick.lifted.ProvenShape
import utils.Connection
import model.repository._
import play.api.Logger
import java.util.Date
import java.sql.Timestamp
import scala.slick.driver
import scala.slick.lifted.ProvenShape
import utils._
import model.repository._
import play.api.Logger
import java.util.Date
import java.sql.Timestamp
import scala.collection.mutable.ListBuffer

trait AuditRepository extends AuditTable {
trait AuditRepository extends AuditTable with MobileRepository {
/*
* Inserts new timestamp when an imei number check
* @param timestamp, object of Audit
Expand Down Expand Up @@ -50,6 +52,25 @@ trait AuditRepository extends AuditTable {
}
}

def getRecordByDate(year: String): List[Int] = {
Connection.databaseObject().withSession { implicit session: Session =>
val empty: ListBuffer[Int] = ListBuffer()
for (i <- 1 to 12) {
if (i < 10) {
val to = CommonUtils.getUtilDate(("0" + i.toString() + "/01/" + year))
val from = CommonUtils.getUtilDate(("0" + i.toString() + "/31/" + year))
val s = mobiles.filter { mobile => mobile.registrationDate >= to && mobile.registrationDate <= from }
empty += s.list.length
} else {
val to = CommonUtils.getUtilDate((i.toString() + "/01/" + year))
val from = CommonUtils.getUtilDate((i.toString() + "/31/" + year))
val s = mobiles.filter { mobile => mobile.registrationDate >= to && mobile.registrationDate <= from }
empty += s.list.length
}
}
empty.toList
}
}
}

trait AuditTable {
Expand Down
11 changes: 2 additions & 9 deletions app/model/repository/MobileRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import scala.slick.lifted.ProvenShape
import utils.Connection
import model.repository._
import play.api.Logger
import utils.StatusUtil._
import utils._
import scala.collection.mutable.ListBuffer
import utils.StatusUtil.Status
import java.sql.Date
Expand Down Expand Up @@ -154,13 +154,6 @@ trait MobileRepository extends MobileTable {
}
}
}

def getRecordByDate(date: String): List[Int] = {
Connection.databaseObject().withSession { implicit session: Session =>
val empty: ListBuffer[Int] = ListBuffer()
empty.toList
}
}
}

// Mapping of mobile Table
Expand All @@ -187,7 +180,7 @@ trait MobileTable extends BrandTable with ModelTable {
regType, mobileStatus, description, registrationDate, document, otherMobileBrand, otherMobileModel, id) <> ((Mobile.apply _).tupled, Mobile.unapply)
def mobileIndex: scala.slick.lifted.Index = index("idx_imei", (imeiMeid), unique = true)

def fkeyBrand = foreignKey("brandId_FK", brandId, brands)(_.id.get, onUpdate = ForeignKeyAction.Restrict,
def fkeyBrand= foreignKey("brandId_FK", brandId, brands)(_.id.get, onUpdate = ForeignKeyAction.Restrict,
onDelete = ForeignKeyAction.Cascade)

def fkeyModel = foreignKey("ModelId_FK", mobileModelId, models)(_.id.get, onUpdate = ForeignKeyAction.Restrict,
Expand Down
16 changes: 13 additions & 3 deletions app/utils/CommonUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ trait CommonUtils {
val arr = imei.map(f => f.toString().toInt).toArray
val len = arr.length
val checksum = arr(len - 1)
if (len != 15)
false
if (len != 15) { false }
var mul = 2
var sum = 0
var i = len - 2
Expand All @@ -28,10 +27,21 @@ trait CommonUtils {
}
var m10 = sum % 10
if (m10 > 0) m10 = 10 - m10
if (m10 == checksum) true
if (m10 == checksum) { true }
else
false
}

def utilDate = new java.text.SimpleDateFormat("MM/dd/yyyy")

def getUtilDate() = {
val currentDate = utilDate.format(new java.util.Date())
new java.sql.Date(utilDate.parse(currentDate).getTime())
}

def getUtilDate(date:String) = {
new java.sql.Date(utilDate.parse(date).getTime())
}
}
object CommonUtils extends CommonUtils

2 changes: 1 addition & 1 deletion app/utils/Constants.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package utils
object Constants {
val ZERO = 0
val ONE = 1
}
}
18 changes: 1 addition & 17 deletions app/utils/S3Util.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,5 @@ trait S3UtilComponent {
false
}
}

/**
* Checks if the file exists on the standard bucket of S3
*/

/*def doesFileExist(fileKeyName: String): Boolean = {
try {
amazonS3Client.getObjectMetadata(bucketName, fileKeyName)
true
} catch {
case ex: Exception => Logger.error(ex.getMessage(), ex)
false
}
}*/

}

object S3Util extends S3UtilComponent
object S3Util extends S3UtilComponent
5 changes: 1 addition & 4 deletions app/utils/StatusUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package utils
import scala.slick.driver.PostgresDriver.simple._
import scala.slick.driver
import play.api.libs.json.Json
import model.repository.MobileDetail
import model.repository.Model
import model.repository.Model
import model.repository._

/**
* Provides commonly used database utilities
Expand All @@ -18,7 +16,6 @@ object StatusUtil {
val pending = Value("pending")
val approved = Value("approved")
val proofdemanded = Value("proofdemanded")

}

/**
Expand Down
Loading

0 comments on commit 6e5d239

Please sign in to comment.