Skip to content

Commit

Permalink
NashTech-Labs#27 refactored MailUtil; NashTech-Labs#21 more of automa…
Browse files Browse the repository at this point in the history
…tion tests added; refactored mail messege ordering in admincontroller;
  • Loading branch information
manishatGit committed Apr 2, 2015
2 parents fc732fa + 1abc78c commit 1129e97
Show file tree
Hide file tree
Showing 23 changed files with 158 additions and 122 deletions.
21 changes: 14 additions & 7 deletions app/controllers/AdminController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ class AdminController(mobileRepo: MobileRepository, auditRepo: AuditRepository,
implicit request =>
Logger.info("AdminController:changeMobileRegType - change Registration type : " + imeiId)
val mobileUser = mobileRepo.getMobileUserByIMEID(imeiId)
Logger.info("AdminController:changeMobileRegType - change Registration type: " + mobileUser)
val regType = if (mobileUser.get.regType == "stolen") "Clean" else "stolen"
val updatedMobile = Mobile(mobileUser.get.userName, mobileUser.get.brandId, mobileUser.get.mobileModelId,
mobileUser.get.imeiMeid, mobileUser.get.otherImeiMeid, mobileUser.get.purchaseDate, mobileUser.get.contactNo, mobileUser.get.email,
Expand All @@ -154,7 +153,7 @@ class AdminController(mobileRepo: MobileRepository, auditRepo: AuditRepository,
* @param mobileuser object of Mobile
* @param msg type of mail
*/
private def sendEmail(mobileuser: Mobile, msg: String) = {
private def sendEmail(mobileuser: Mobile, msg: String): Unit = {
val post = Play.current.configuration.getBoolean("Email.send")
if (!post.get) {
Logger.info("AdminController:tweet -> disabled")
Expand All @@ -167,17 +166,25 @@ class AdminController(mobileRepo: MobileRepository, auditRepo: AuditRepository,
case "delete" =>
mail.sendMail(mobileuser.imeiMeid + "<" + mobileuser.email + ">", "Delete mobile registration from MCWS", mail.deleteMessage(mobileuser.imeiMeid))
case "changeMobileRegType" =>
if (mobileuser.regType == "stolen")
mail.sendMail(mobileuser.imeiMeid + "<" + mobileuser.email + ">", "Change mobile status from MCWS", mail.changeMobileRegTypeStolen(mobileuser.imeiMeid))
else
mail.sendMail(mobileuser.imeiMeid + "<" + mobileuser.email + ">", "Change mobile status from MCWS", mail.changeMobileRegTypeClean(mobileuser.imeiMeid))
if (mobileuser.regType == "stolen") {
mail.sendMail(mobileuser.imeiMeid + "<" + mobileuser.email + ">", "Change mobile status from MCWS",
mail.changeMobileRegTypeStolen(mobileuser.imeiMeid))
} else {
mail.sendMail(mobileuser.imeiMeid + "<" + mobileuser.email + ">", "Change mobile status from MCWS",
mail.changeMobileRegTypeClean(mobileuser.imeiMeid))
}
case _ =>
Logger.info("AdminController:sendEmail -> failed")
}
}
}

private def tweet(mobileuser: Mobile, msg: String) = {
/**
* This function tweet the post on twitter page
* @param mobileuser object of mobile
* @param msg type of tweet
*/
private def tweet(mobileuser: Mobile, msg: String): Unit = {
val post = Play.current.configuration.getBoolean("Tweet.post")
if (!post.get) {
Logger.info("AdminController:tweet -> disabled")
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/Application.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ def index: Action[play.api.mvc.AnyContent] = Action { implicit request =>
}

/**
* Handle the calling of controllers methods from javascript ajax call
* Handle the calling of controllers methods from javascript ajax call
*/
def javascriptRoutes: Action[play.api.mvc.AnyContent] = Action { implicit request =>
import routes.javascript._
Ok(Routes.javascriptRouter("jsRoutes")(
routes.javascript.MobileController.checkStatus,
routes.javascript.MobileController.checkMobileStatus,
routes.javascript.MobileController.getModels,
routes.javascript.MobileController.isImeiExist,
routes.javascript.AdminController.pending,
Expand Down
42 changes: 26 additions & 16 deletions app/controllers/MobileController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import utils._
import model.repository._
import java.util.Date
import java.sql.Timestamp
import play.api.libs.Files

class MobileController(mobileRepo: MobileRepository, brandRepo: BrandRepository,
modelRepo: ModelRepository, auditRepo: AuditRepository, mail: MailUtil, s3Util: S3UtilComponent, commonUtils: CommonUtils)
Expand Down Expand Up @@ -104,18 +105,18 @@ class MobileController(mobileRepo: MobileRepository, brandRepo: BrandRepository,
/**
* Handle the new mobile registration form submission and add new mobile
*/
def mobileRegistration = Action(parse.multipartFormData) { implicit request =>
def mobileRegistration: Action[MultipartFormData[Files.TemporaryFile]] = Action(parse.multipartFormData) { implicit request =>
val username = request.session.get(Security.username).getOrElse("None")
val mobileBrands = brandRepo.getAllBrands
val user: Option[User] = Cache.getAs[User](username)
mobileregistrationform.bindFromRequest.fold(
formWithErrors => BadRequest(views.html.mobileRegistrationForm(formWithErrors, mobileBrands, user)),
mobileuser => {
val date = commonUtils.getUtilDate()
val date = commonUtils.getSqlDate()
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, commonUtils.getUtilDate(mobileuser.purchaseDate), mobileuser.contactNo,
mobileuser.mobileModelId, mobileuser.imeiMeid, mobileuser.otherImeiMeid, commonUtils.getSqlDate(mobileuser.purchaseDate), mobileuser.contactNo,
mobileuser.email, mobileuser.regType, StatusUtil.Status.pending,
mobileuser.description, date, documentName, mobileuser.otherMobileBrand, mobileuser.otherMobileModel))
result match {
Expand Down Expand Up @@ -162,7 +163,7 @@ class MobileController(mobileRepo: MobileRepository, brandRepo: BrandRepository,
* Getting mobile details by imei id
* @param imeid of mobile
*/
def checkStatus(imeid: String, user: String): Action[AnyContent] = Action { implicit request =>
def checkMobileStatus(imeid: String, user: String): Action[AnyContent] = Action { implicit request =>
Logger.info("MobileController: getMobileUser -> called")
val data = mobileRepo.getMobileUserByIMEID(imeid)
data match {
Expand Down Expand Up @@ -239,13 +240,18 @@ class MobileController(mobileRepo: MobileRepository, brandRepo: BrandRepository,
brandform.bindFromRequest.fold(
formWithErrors => BadRequest(views.html.createMobileNameForm(formWithErrors, user)),
brand => {
val insertedBrand = brandRepo.insertBrand(Brand(brand.name))
insertedBrand match {
case Right(Some(id)) =>
Redirect(routes.MobileController.brandRegisterForm).flashing("SUCCESS" -> Messages("messages.mobile.brand.added.success"))
case _ =>
Redirect(routes.MobileController.brandRegisterForm).flashing("ERROR" -> Messages("messages.mobile.brand.added.error"))
if (brandRepo.getAllBrands.filter { x => x.name.equalsIgnoreCase(brand.name) }.isEmpty) {
val insertedBrand = brandRepo.insertBrand(Brand(brand.name))
insertedBrand match {
case Right(Some(id)) =>
Redirect(routes.MobileController.brandRegisterForm).flashing("SUCCESS" -> Messages("messages.mobile.brand.added.success"))
case _ =>
Redirect(routes.MobileController.brandRegisterForm).flashing("ERROR" -> Messages("messages.mobile.brand.added.error"))
}
} else {
Redirect(routes.MobileController.brandRegisterForm).flashing("ERROR" -> "Brand allready exist")
}

})
}

Expand All @@ -261,12 +267,16 @@ class MobileController(mobileRepo: MobileRepository, brandRepo: BrandRepository,
modelform.bindFromRequest.fold(
formWithErrors => BadRequest(views.html.createMobileModelForm(formWithErrors, brands, user)),
modell => {
val insertedModel = modelRepo.insertModel(Model(modell.modelName, modell.brandName.toInt))
insertedModel match {
case Right(Some(id)) =>
Redirect(routes.MobileController.modelRegistrationForm).flashing("SUCCESS" -> Messages("messages.mobile.model.added.success"))
case _ =>
Redirect(routes.MobileController.modelRegistrationForm).flashing("ERROR" -> Messages("messages.mobile.model.added.error"))
if (modelRepo.getAllModelByBrandId(modell.brandName.toInt).filter { x => x.name.equalsIgnoreCase(modell.modelName) }.isEmpty) {
val insertedModel = modelRepo.insertModel(Model(modell.modelName, modell.brandName.toInt))
insertedModel match {
case Right(Some(id)) =>
Redirect(routes.MobileController.modelRegistrationForm).flashing("SUCCESS" -> Messages("messages.mobile.model.added.success"))
case _ =>
Redirect(routes.MobileController.modelRegistrationForm).flashing("ERROR" -> Messages("messages.mobile.model.added.error"))
}
} else {
Redirect(routes.MobileController.modelRegistrationForm).flashing("ERROR" -> "Model Allready exist")
}
})
}
Expand Down
8 changes: 4 additions & 4 deletions app/model/repository/AuditRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ trait AuditRepository extends AuditTable with MobileRepository {
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 to = CommonUtils.getSqlDate(("0" + i.toString() + "/01/" + year))
val from = CommonUtils.getSqlDate(("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 to = CommonUtils.getSqlDate((i.toString() + "/01/" + year))
val from = CommonUtils.getSqlDate((i.toString() + "/31/" + year))
val s = mobiles.filter { mobile => mobile.registrationDate >= to && mobile.registrationDate <= from }
empty += s.list.length
}
Expand Down
4 changes: 2 additions & 2 deletions app/model/repository/BrandsRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import play.api.Logger
import java.sql.Date

trait BrandRepository extends BrandTable {

/**
* Returns List of mobile Brands
*/
Expand Down Expand Up @@ -64,7 +64,7 @@ case class Brand(
name: String,
id: Option[Int] = None)

//Represents a brand name
//Represents a brand name
case class BrandForm(name: String)

//Trait companion object
Expand Down
2 changes: 1 addition & 1 deletion app/model/repository/MobileRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ case class MobileDetail(
//Represents Registered Mobile Status in the database
case class MobileStatus(imeiMeid: String)

//Represents Mobile registration Form
//Represents Mobile registration Form
case class MobileRegisterForm(
userName: String,
brandId: Int,
Expand Down
1 change: 1 addition & 0 deletions app/model/repository/ModelsRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import utils.Connection
import model.repository._
import play.api.Logger
import java.util.Date
import scala.slick.lifted.ForeignKeyQuery

trait ModelRepository extends ModelTable {

Expand Down
13 changes: 6 additions & 7 deletions app/utils/CommonUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,21 @@ trait CommonUtils {
if (mul == 2) mul = 1 else mul = 2
}
var m10 = sum % 10
if (m10 > 0) m10 = 10 - m10
if (m10 > 0) { m10 = 10 - m10 }
if (m10 == checksum) { true }
else
false
}

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

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

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

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

13 changes: 13 additions & 0 deletions app/utils/MailUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,37 @@ import play.api.Logger

trait MailUtil {

// message for stolen registration
def stolenRegisterMessage(imeid: String): String = {
Messages("messages.mobile.stolenregister", imeid, signature)
}

//message for clean registration
def cleanRegisterMessage(imeid: String): String = {
Messages("messages.mobile.cleanregister", imeid, signature)
}

// message for demand proof
def demandProofMessage(imeid: String): String = {
Messages("messages.mobile.demandProof", imeid, signature)
}

// message for approved of mobile user
def approvedMessage(imeid: String): String = {
Messages("messages.mobile.approved", imeid, signature)
}

// message for delete mobile user
def deleteMessage(imeid: String): String = {
Messages("messages.mobile.delete", imeid, signature)
}

// message for change mobile registration to stolen
def changeMobileRegTypeStolen(imeid: String): String = {
Messages("messages.mobile.changeMobileRegTypeStolen", imeid, signature)
}

//// message for change mobile registration to clean
def changeMobileRegTypeClean(imeid: String): String = {
Messages("messages.mobile.changeMobileRegTypeClean", imeid, signature)
}
Expand All @@ -40,6 +47,12 @@ trait MailUtil {
Messages("messages.signature")
}

/**
* Sends mail to mobile user
* @param email of mobile user
* @param subject of mail
* @param message
*/
def sendMail(email: String, subject: String, message: String): Unit = {
try {
val emailBuilder = Email(
Expand Down
41 changes: 25 additions & 16 deletions app/utils/S3Util.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.amazonaws.auth.BasicAWSCredentials
import com.amazonaws.services.s3.AmazonS3Client
import java.io.File
import play.api.Logger
import scala.concurrent._
import ExecutionContext.Implicits.global

trait S3UtilComponent {

Expand All @@ -15,27 +17,34 @@ trait S3UtilComponent {
val amazonS3Client = new AmazonS3Client(mcwsAWSCredentials)

/**
* Store file to standard bucket on S3
* Store file to standard bucket on amazonS3
* @param documentName, name of file
* @param fileToSave the file which you want to store
*/
def store(documentName: String, fileToSave: File) = {
try {
amazonS3Client.putObject(bucketName, documentName, fileToSave)
} catch {
case ex: Exception => Logger.error(ex.getMessage(), ex); false
}
def store(documentName: String, fileToSave: File): Future[Boolean] = {
Future {
try {
amazonS3Client.putObject(bucketName, documentName, fileToSave)
Logger.info(s"Successfully upload : [ Bucket : ${bucketName} ] [ document : ${documentName}]"); true
} catch {
case ex: Exception => Logger.info("UNABLE TO STORE FILE", ex); false
}
}
}

/**
* Delete file from standard bucket on S3
* Delete Object from standard bucket on amazonS3
* @param key key is the unique key of the object which you want to delete
*/
def delete(fileKeyName: String): Boolean = {
try {
amazonS3Client.deleteObject(bucketName, fileKeyName)
true
} catch {
case ex: Exception =>
Logger.error(ex.getMessage(), ex)
false
def delete(key: String): Future[Boolean] = {
Future {
try {
amazonS3Client.deleteObject(bucketName, key)
Logger.info(s" Successfully delete: [ Bucket : ${bucketName} ] [ Key : ${key}]"); true
} catch {
case ex: Exception =>
Logger.info("UNABLE TO DELETE FILE", ex); false
}
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions app/utils/StatusUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import play.api.libs.json.Json
import model.repository._

/**
* Provides commonly used database utilities
* Provides commonly used database utilities
*/
object StatusUtil {

Expand All @@ -17,7 +17,7 @@ object StatusUtil {
val approved = Value("approved")
val proofdemanded = Value("proofdemanded")
}

/**
* Maps the status to a slick Column Type
*/
Expand All @@ -32,4 +32,3 @@ object StatusUtil {
}
})
}

Loading

0 comments on commit 1129e97

Please sign in to comment.