Book Review: Spring Data

5 12 2012

I’m reviewing this book thanks to the good people at Packt Publishing that give me a free copy

Spring Data in is own words is:

…an umbrella open source project which contains many subprojects that are specific to a given database. The projects are developed by working together with many of the companies and developers that are behind these exciting technologies.

So, if you already know the kind of support that Spring Framework give to JDBC you could have an idea: Unified Runtime Exceptions Hierarchy, Templates implementation and so on; but for MongoDB, Redis, HBase and others, including extensions for already supported technologies like the afore mentioned JDBC and JPA

The Spring Data book from Packt Publishing, written by Petri Kainulainen covers the JPA extension and Redis support.

First the good parts:

  • The book is very short (161 pages) you can read all in less than a week
  • The author covers well both topics from the Spring perspective
  • The example code is very well written, well formated and concise (very rare on other Packt books)
  • The author approach is more practice than theory, some people love this approach

Now the bad parts

  • The examples use Spring Java Config. Java Config is a special configuration type that let you write Spring Applications without XML. Although more trendy, this is the least used configuration type and not all Spring users feels comfortable with this configuration, and the tooling support isn’t the best
  • The layer architecture isn’t well defined. The service layer have a lot of implementation details that depends on the data layer, so from one example to the other the services implementations change radically.

    This is “understandable” in JPA examples (Spring Data JPA adds dynamic implementations for Repositories) but in the Redis examples, There’s not a Data layer!!

In conclusion

The book cover well both topics, the code is well written, but have some oddities

Advertisements




KotlinPrimavera 0.1

3 11 2012

The first release for KotlinPrimavera is hot, right from the oven

Check the repo and the docs

KotlinPrimavera is a framework that provide support for Spring Frameworkk inside Kotlin Language





Book Review: Spring Web Services 2 Cookbook

3 05 2012

I’m reviewing this book thanks to the good people at Packt publishing that give me a PDF copy to read

I’m a SpringSource Certified Instructor and Spring WS is one of the most difficult part to teach en every course. And not because the framework is complicated, is the technology (SOAP) that is very complicated. So, having a book in the topic at hand is a very good thing.

First of all the good parts:

  • This is the first book on Spring WS and do a very good work covering every corner case. Is, by far, more complete than the official documentation
  • And when I say “every corner case” is TRUE (seriously, the whole enchilada), from things like using XMPP to send a SOAP message to secure a WS using WSS4J, including Spring Security and many more things.
  • The book also cover Web Service with REST (without covering security ) and Spring Remoting

Now, the (very minor) bad parts:

  • This format (a Cookbook) is not the best way to teach topics in a deep way
  • The code looks horrible (at least in the PDF version) and is very hard to read. This is epidemic in other Packt publishing books (Hey Packt’s friends, take note). Reading the code from the example code is better, but, the code have little or no documentation at all and is poorly formated.
  • The implementation details in some examples aren’t the best. The use of some deprecated classes, building new instances just to call static methods, using XPath without XPathOperations (a nice interface that define some very helpful XPath methods) and so on
  • In the Spring Remoting part the authors don’t cover HttpInvoker that is the more natural way to communicate two Spring applications. (In fact HttpInvoker is nicely cover in the Spring documentation, so is just a matter of taste)

In conclusion

This a very good book, the authoritative reference for Spring WS





Flex on Java:Review

8 09 2010

The people of Manning books give a MEAP copy of “Flex on Java” from Bernerd Allmon and Jeremy Anderson

In my company we are developing an ERP for a mid-size company dedicated to sale construction supplies on Leticia, Colombia. The application is a port from a Seam based application to a Flex/Spring application (BTW, Seam is a HORRIBLE framework, avoid it  if you can). So this is a great opportunity to review this book

First of all, the copy that I have is MEAP version so, this is not the final book and can contain some rough cuts and unpolished parts (if I can obtain the final version I’ll update this review)

Chapter 1:Some Flex with your Java?: A general introduction to the main topics of Flex and, of course, Java. A nice introduction for the rookie, but I don’t think that a rookie want to read this book. Flex and Java are big Behemoths and think to dominate both with just one book is silly. Also we have our first “Hello World!”, and IMHO the main problem of this book: (actually, not so relevant) The majority of the example codes aren’t in the last versions of the frameworks.

Chapter 2:Beginning with Java: This chapter cover the development of a Java Web Application with AppFuse (I read about this framework but I never use before) and Maven, once more time good information for the Rookie (Including how to install a JDK, yes, you read well). The application expose some WebServices and have the DAO Layer with Hibernate, the IoC with Spring and the Web Layer with Struts and JSP. Thumbs Up for no including IDE related content

Chapter 3:Getting Rich with Flex: Very good chapter, nice examples, and a valuable information for all the developers that are new in Flex, although don’t replace a good book in Flex, contains enough information to start and see the power of this framework. Thumbs Up for don’t use Flash Builder.

Chapter 4:Connecting to Web Services: This chapter is worth of the total costs of the book. The code quality are amazing, as never see before on others Flex books. The authors introduce the MVP (Model View Presenter) Pattern, a good way of organizing the code of a RIA. Although I never use the MVP is very nice to see a good example of how you can organize the code to call WebService/RemoteObject.

If you, like me, are not new to Flex and Server communication you’ll know that all the examples in the web and in the books use the <mx:RemoteObject> tag, very well for small examples but when your application grows up you’ll have a lot of copy-n-paste code in all the application. Well this chapter teach you how avoid this nightmare.

Chapter 5:BlazeDS Remoting and Logging: This chapter cover the Spring Flex project in very detailed way. For me this is OK, I’m a fan of Spring ( I’m  SpringSource Certified Spring Professional) but not all the developers love Spring. So, will be nice if the book cover another options like GraniteDS and integrations with EJB3 and Seam (The HORROR). Thumbs down for don’t including information about Exception Translators and the proper way to manage them

Chapter 6: Flex Messaging: Messaging is one of the more powerful features on Flex. This chapter have a LOT of technical details, is very deep and not for the faint of heart. But if you’re using Flex and Java, surely you’re not of them, right?

Chapter 7:Securing and personalizing your application: Spring Security deserve a book for his own, but this chapter makes a good work explaining the basics of Spring Security and his integration with Flex. Good examples and good theory, much better than the Spring Flex documentation

Chapter 8:Charting with Degrafa: This is the chapter that I enjoy less, Why? ’cause Degrada only works in Flex 2 and 3, and my current projects are on Flex 4, however AFAIK Degrafa is integrated now with Flex 4, so, hypothetically speaking, the theory in this chapter will help you with graphics on Flex 4.

Chapter 9:Desktop 2.0 with AIR: Great Chapter, AIR is a very compelling technology, and is huge plus if you can turn your Flex Web App in AIR Desktop App, but don’t lies yourself, most of the times this is Phantom requirement (A requirement that don’t exists), so Choosing Flex ’cause you can make Desktop apps easily when your app don’t have this requirement is a no-no.

Chapter 10:Testing your Flex application:Unit Testing is a requirement on all modern application development cycle. Flex Unit Testing is not very different from Unit Testing on other languages, so if you already knows how to do Unit Testing, you will dominate Flex Unit Testing in a breeze. This Chapter also covers Continuous Integration with Hudson Server, another hot topic (BTW I prefer TeamCity from JetBrains)

Chapter 11:Flex on Grails: Grails, my deadliest enemy, we meet again. You hate me, and I hate you… jokes apart, seriously, I’m not able to run any Grails example, and for my own mental health I don’t make the example in this chapter, but the code seems pretty easy, (Grails code is very easy, if it works is another question) but don’t expect that Grails auto-magically generates MXML code, so it’s not reduce drastically the amount of work that you need to do a mid size app.

Conclusion: Flex on Java is a very well written book, the code examples have a great quality and are production-ready, a very rare characteristic in programming books.

I like:

  • Code quality
  • Explanations on some hard topics
  • IDE-Agnostic

Don’t Like:

  • The book use some outdated libraries
  • Some important topics aren’t well covered
  • Don’t cover other connection options apart from WS and BlazeDS

NOTE: 9/10





Play, Roo and a little of Django

12 01 2010

I start my own development and consulting startup-one-man-army company, a risky move on this times, but I hope that will generate profits in this year and in the nexts.

Weapons and choices

As developers, all we have a set of weapons to defeat different enemies, seasoned developers know that non exists a golden hammer, and different problems require different tools, even different mindsets.

Coming from the “Enterprise” world I develop a skills set for kill “big behemoth” projects, my tools include Stripes/Flex/Spring and I’m very comfortable with all them.

But in the small startup kingdom is very hard that you will develop a “big behemoth” projects in the first months or years (until you build a good name for your company), so you will develop small web apps with little requirements, and this is not bad at all, you can make a good profit, if you are smart and have good connections.

But trying to develop a small app, with Stripes/Flex/Spring, seems to be a little… overkill and don’t lie yourself, is possible that you spend a day (even a couple of days) setting the whole enchilada (xmls, libs and all the stuff that you need); this is not the worst thing, (but could be better) in a “big behemoth” with lot of tricky requirements (this tools give you incredible power and flexibility with a reasonable cost).  But in the small apps kingdom a couple of days is too much, and when you realize that you don’t need all the fancy stuff is worst, so I start to evaluate some other options.

The usual suspects

In the Java developer community mindset when you talk about productivity (wherever that means) two names shine among others JRuby on Rails and Grails.

Few years ago Ruby on Rails was the king of the hype, conferences, book, developers of all the world screaming “Java is Dead” and praising his new productivity messiah (wherever that means) whom would lead to the promise land. But them few stories from the trenches show that “The Emperor’s New Clothes” are too invisible for some guys. Right now the RoR community is more mature (or I think so) and Ruby on Rails seems to be good enough, but I think that I’ll wait to the Rails/Merb merge to take a look. In the other hand the JRuby on Rails seems to have less “Rock Star” attitude (Disclaimer: I’m writing this based on the blogs and articles that I read, I’m trying RoR but no more than a few tutorials and no serious project) .

Grails is the Java (or JVM) community response to RoR phenomenon, Grails right now have a lot of momentum and now is under the SpringSource umbrella, with lot of press attention and good community support. Take a look, I say; Groovy is nice language, I say; but, shame on me, I’m not able to run the simplest tutorials, lot of strange (and the worst, non-deterministic) errors spawn everywhere and my confidence in Grails died drowned in a pool of EOF exceptions (and other random things), Maybe in the future I’ll revisited Grails, maybe.

The new kids on the block

So I take a look to other JVM alternatives:

Django/Jython

I like Python, is good language, and the Jython implementation is really nice too, a lot of Python libs run very well with Jython including Django.

Django is a very nice framework, extremely well documented, very logical, little or no magic, nice ORM (I don’t like the ORM’s anyway, but this is nice),and have a set of apps (plugins in other frameworks) that let you build a standard web app with little cost.

The Django/Jython integration is very good, and his author (@leosoto) is a smart and nice guy, the main problem for me is his performance; each request represent a small peak in the processor performance, that obviously will be corrected with little hacks in the next versions. Django/Jython is good option to Java developers looking for a good RAD (when I was young the first thing that you think when you read this word was Visual Basic 6, ah the good ol’ times) framework.

Spring Roo

First of all I’m a huge fan of Spring related projects, even I take the Spring Core certification exam and pass with a shiny 94% (seems that I’m the first Colombian with this certification), so my opinion I could be a little biased.

Spring Roo is a project that aims to give productivity (wherever that means) to the Java ecosystem.

Roo have a nice shell interface with lot of options and good documentation, This shell let you to create a new maven project in matter of seconds, all correctly configured, nice start.

Then you can build your entities with the shell (a la RoR) and the horror begins. Roo will generate your class, annotated with Roo annotations and a bunch of *.aj files (AspectJ files) that seems (I’m not expert in AspectJ) that only add methods (getter, setter, toString and some static methods for queries with JPA) to the class (not AOP stuff). I never like code generation tools (if you, like me, try the JBuilder 9-X ($5000) EJB 2.0 app-from-database stuff, you know what I’m talking about), for me is like a glorified “copy-n-paste”. The only valuable thing that code generation tools bring is for teach to junior programmers what is smell code.

But the *.aj code that Roo generate seems to be not bad all, but who know? all the code analysis tools that I know don’t work on *.aj, shame on me. So you can make changes on Java class, but don’t expect that compile outside Roo (or maven), but I’m too afraid to change the *.aj files (one of the worst things of code generation tools) that seems that a dead end road

Not only that, you could generate a CRUD GUI based on JSPX and Spring @MVC (Obviously with all the *.aj stuff), but IMHO, Even with the Spring @MVC (that corrected many things like the horrible inheritance tree) included in the 2.5 version, Spring MVC is by far the most unnecessary part of Spring Framework, if Java have a thing this is MVC frameworks (good or bad), we don’t need another one, PERIOD. For me, used to Stripes, Spring @MVC seems very bloated with lot of code even for simple things. In theory you can make an Add On that build your  GUI in other technology, but theres no documentation right now.

Other thing that I don’t like is that the dynamic client parts are implemented with Spring JS that seems like a Dojo wrapper or something like that, well is pretty hard to know ’cause the documentation is near to nothing (a rare thing in a Spring project).

For me Roo will be almost perfect with a Stripes Add On (maybe I’ll make it in the future) and an option that “push” all the *.aj methods in the Java File… but wait a minute this seems a lot like my “BFG 9000” stack to kill “big behemoth”…. never mind

Play Framework

Play is a mind-blowing/fresh-air for Java ecosystem, totally different from all the stuff that exists in Java.

First of all don’t need a Servlet container to run, or compile, and you don’t see the Servlet API (????) and break some doctrinal rules that all Java developers know (public fields, Model classes extends from a Framework class, strong procedural flavor and others). Those engineering trade-offs could scare some guys, but at the end the framework is very solid

IMHO Play Framework is heavily inspired on Django, but have some things that are more polished that Django for example the absence of Forms (that are a little rough) , the routes seem more polished and the template language is a little more powerful (based on Groovy).

In the other hand, Play don’t have all the modules (apps) that Django have, and of course have less momentum.

In future releases, Play Framework will support Scala as development language, that seems like a very intelligent movement, and who knows maybe we’ll see Groovy, JRuby or Jython as development language (or crazy things like Jaskell or Clojure)

Play is a really good framework and I see myself in the future using Play a lot, (maybe I’ll write a plugin for IntelliJ).

Conclusions

I’ll invest some time on Play Framework in the next few months, but is a community driven framework, so is possible that don’t have the fuel to be a mainstream Framework. In the other hand Roo could be better in a near future with some additions, and have the financial support from VMWare, so is almost guaranteed that Roo will be a success in the middle size projects.





Scala on Stripes/Spring(II)

26 07 2009

Well the past chapter was very well received

Now we will continue with the DAO layer, and will learn more advanced Scala concepts

The DAO Layer

My friends know that I’m a “SQL Boy” I never need to learn ORM (I try many times, but the laziness beat me) so I’ll use Spring JDBC. If you don’t know this technology I’ll encourage you too take a look, this is one of the best parts of Spring. Spring JDBC take out the pain of JDBC in a very easy way, in fact I participate on a project where we migrate a bunch of Classic JDBC DAO’s for Spring DAO’s and the code was reduced in a 70%.

ScalaSimpleJdbcDaoSupport.scala

This class is a Support Class that extend SimpleJdbcDaoSupport to make more easy the work with Scala. In this class we are writing a little utility function in order to provide support for the methods with (Object… args) that SimpleJdbcTemplate have. Also we are writing some functions to wrap the SimpleJdbcTemplate; in a Real Case Scenario you need to write a lot of methods more

package com.simpletasks.dao.impl
import java.util.List
import org.springframework.jdbc.core.simple.{SimpleJdbcDaoSupport, ParameterizedRowMapper}
abstract class ScalaSimpleJdbcDaoSupport extends SimpleJdbcDaoSupport {
//
private def buildObjectArray(params: Any*): Array[Object] =     params.map(_.asInstanceOf[Object]).toArray
//
def queryForObject[T](sql: String, mapper: ParameterizedRowMapper[T], params: Any*): T =
getSimpleJdbcTemplate.queryForObject(sql, mapper, buildObjectArray(params: _*))
//
def query[T](sql: String, mapper: ParameterizedRowMapper[T], params: Any*): List[T] =
getSimpleJdbcTemplate.query(sql, mapper, buildObjectArray(params: _*))
//
def update(sql: String, params: Any*): Int =
getSimpleJdbcTemplate.update(sql, buildObjectArray(params: _*))
}

  • line 04 abstract: Same as Java
  • line 07 Any: Any is Scala as Object is a Java, all the Scala Classes including Basic Types are subclasses of Any
  • line 07 Repeated Parameters: The Asterisk (*) symbol is like Ellipsis/Varags (…) in Java. This means that you could add the parameters that you want, but is different in Java in some ways first of all, In Java you can call a method with an Array of the same type of the varargs argument, this isn’t possible in Scala; the second difference, In Java, in the method body the varargs argument is an Array, in Scala is a Seq. Seq is the base for a bunch of Collections in Scala, and provide some nice functions
  • line 07 Arrays: the Array in Scala is different from the Java Array; first of all the declaration type:
    In JavaString[]in ScalaArray[String]second, the access
    In JavamyArray[0]In ScalamyArray(0)Third, the implicit constructor
    In Java{0,1,2,3,4,5}In ScalaArray(0,1,2,3,4,5)
  • line 08 map function: the map function return a new Seq (remember that Scala is a OO/functional language, so you will be a lot of immutable classes like this) after applying some algorithm to all his members
  • line 08 High order functions:: A HOT feature, High order functions are lovely functions that take functions as parameters and/or return functions. (Geek Fun). In this case map take a function that will be applied in all the Seq’s members, (in our case, we are casting all the members to Object) this type of functions are very common in Scala Collections and produce a very neat and elegant code
  • line 08 The Placeholder strikes back: Our friend is representing the Seq member that we are casting.
  • line 08 Casting: The casting in Scala is a function invocation
  • line 08 Square Braces []: Most of the time that you’ll see Square Braces ([]) in Scala are around a Type, in fact this is the way that we define Type parameterization (Generics in Java) for that reason we declare Array[String] instead of String[]
  • line 08 (again) toArray function: toArray is a function that convert this little Seq in an Array (pretty evident), Look that I call this function without parenthesis.
  • line 10 Type parameterization [T]: This is the equivalent to <T> in Java, but Scala Type parameterization have enough power (and beautiful concepts like, variance, co-variance and contra-variance, I told you Scala is a funny language) too put Java Generics in shame (Martin Odersky, The Scala Father was the co-designer of Java Generics). But… sorry the whole power of Type Parameterization is outside of the scope of this humble tutorial.
  • line 13 : _* This is a little “syntactic hack”, if we call our function buildObjectArray with the params Seq, we are passing the Seq and not the Seq members. with : _*, we are passing the members one by one.

Well A lot of concepts for a simple class. This is all for now, in the next chapter we will use this class and learn about Traits, Bye, bye.





Scala on Stripes/Spring (I)

12 07 2009

All the cool kids on the block are talking about Scala. Is Scala the next big thing? the Golden Hammer?

The Hype is justified

First of all, yes, the hype is justified, Scala is a gorgeous language: powerful, elegant and fun.

Not the same “fun” that bring us Python or Groovy, is more an “Intellectual Fun”. Scala have many features and constructs that can teach a lot of Computer Science and language design, maybe more than another language, and for a Geek like me this is a lot of fun

But, beware, as the Uncle Ben Parker says: “With great power comes great responsability”. Scala is a very complex language, some times Scala is overwhelming. Personally I (think that) dominate Java in 3 years including a SCJP: easily Scala will take me 2 or 3 times to tame. Scala have features that, poorly used, can make your code a mess, and is probably that you can’t read your own code in six months. So is necesary a good amount of discipline and good praxis

In the other side of the coin, this mean a long (and fun) way of learning and auto discovery that’s will keep you interested for a long, long time

Why Stripes/Spring if exists Lift?

I most confess that I don’t know Lift more than a couple of quicks looks at Lift’s Site, seems good but the Maven thing scare me a lot, but the most important reason is simple; I’m a Big Fan of Stripes/Spring. I spend the last 3 years on both frameworks, So, Why throw away all my hard earned experience, if I can use both frameworks with Scala?

Lets code

Many tutorials have an academic approach, but for some people is more easy with an example project, this is the approach with this tutorial. Although I’m not a Scala expert, I’ll try to do the best I can

The Project

In IntelliStripes’s repository we have a toy project that we use to make functional test, you can see the java code
in http://code.google.com/p/intellistripes/source/browse/#svn/trunk/functional-test
or use a SVN Client to checkout

This project is a “bare bones” web app for Task Management (very original), nothing fancy here just create an account, create your tasks and delete them

The Domain (or is the model??)

We have 2 simples entities User and Task, just simple java beans

User.scala

package com.simpletasks.domain
/**
* Created by IntelliJ IDEA.
* User: Mario Arias
* Date: 15/05/2009
* Time: 10:09:57 PM
*/
class User {
  private var id: Int = 0
  private var email: String = _
  private var password: String = _
  private var name: String = _
  private var lastName: String = _
  def getEmail: String = email
  def setEmail(email: String) = this.email = email
  def getId: Int = id
  def setId(id: Int) = this.id = id
  def getLastName: String = lastName
  def setLastName(lastName: String) = this.lastName = lastName
  def getName: String = name
  def setName(name: String) = this.name = name
  def getPassword: String = password
  def setPassword(password: String) = this.password = password
  override def toString: String = "User{" +
    "email='" + email + '\'' +
    ", id=" + id +
    ", password='" + password + '\'' +
    ", name='" + name + '\'' +
    ", lastName='" + lastName + '\'' +
    '}'
}

Java version

Let’s see line by line and go deeper in Scala concepts on the way.

  • Scala Files: In Java you need to declare the file with the same name of a public class (interface, enum) in Scala you don´t have the same limitation. You can have several classes, even many packages, or an entire application in one file, I don’t know if you can want this, but is posbile, in the other side, navigate into this files could be frustrating
  • line 00 Packages: packages in scala have the same sintax that in java, but also you can declare as the namespaces in C#. Example:
    package com{
      package simpletaks.domain{
      }
    }
  • line 01 Comments: Same as Java.
  • line 07 Access Modifiers: All is public by default, but also exists private and protected. Access Modifiers are more powerful than in Java, but for now is enough. More on this later
  • line 07 Class Declaration:Similar to Java, More on this later
  • line 08 Look ma’ no semicolons:Scala don’t need semicolons, but you can add them if you want
  • line 08 Vars: The var keyword means that this field will change in the time, in other words is a variable (D’oh)
  • line 08 Type:The type of a field or a method is declared after the name and separated with colon
  • line 08 (Not So)Primitives:Scala don’t have primitives; all in Scala is an object, so the concept here is called basic types. The basic types are
    Byte 8-bit signed two’s complement integer (-27 to 27 – 1, inclusive)
    Short 16-bit signed two’s complement integer (-215 to 215 – 1, inclusive)
    Int 32-bit signed two’s complement integer (-231 to 231 – 1, inclusive)
    Long 64-bit signed two’s complement integer (-263 to 263 – 1, inclusive)
    Char 16-bit unsigned Unicode character (0 to 216 – 1, inclusive)
    String a sequence of Chars
    Float 32-bit IEEE 754 single-precision float
    Double 64-bit IEEE 754 double-precision float
    Boolean true or false
  • line 08 Assign: Same as Java
  • line 09 Placeholder:You’ll see the _ character a lot in Scala code, this is a placeholder and have many, many uses, in this case is the same as null. But why you want to assign null to a field?. ’cause in Scala when you don’t assign a value to a field is the same as have and abstract method in Java; In fact do you need to declare abstract your Scala class. In conclusion an abstract Scala class could have abstract fields or methods. This is weird coming from the Java world, but is pretty useful
  • line 13 Function declaration:All the functions (methods) in Scala start with the def keyword
  • line 13 Look ma’ no parenthesis (neither braces, nor return):Well that’s is less code, but all have and explanation, the lack of parenthesis = absence of parameters, the lack of braces = one-line function, the lack of return = pretty similar to Groovy, the last line of a function is the return; but like Groovy you can made the return explicit
  • line 14 Look ma’ no void:Yes and not, remember that scala is functional/object language, so all functions have a return type, in the case that function return nothing, the type of the function is Unit. So the same could be declared as def setEmail(email: String):Unit = this.email = email.
  • line 13 and 14 Getters and Setters and @BeanProperty Hot topic. Many of you will blame me for write myself Getters and Setter when I could use the @BeanProperty and write less code just like this @BeanProperty
    private var id:Int = 0
    and voila!! the compiler will generate a nice shiny get/set pair for me… you read well the compiler, thats mean that I cannot invoke this get/set pair on my own code, for me is almost useless, but a lot of people like this approach. Before you start to complain that I could declare the var as “public” and call it directly, I remember you that this is a Scala on Stripes/Spring tutorial and I need too use this class in the JSP so I need the get/set pair
  • line 23 Function override:When you need override a function, you must add the keyword override this must will protect you from accidentally override a function without know.
  • line 23 String concat:Same as Java, but with a great difference… in fact this is not a string concat… this is only a function invocation. "Hello " + "World" is the same as "Hello ".+("World"). So in Scala do you have operator overloading; and this is how the concat happens. Then you can call any one-parameter function without the dot and the parenthesis. if the function have many parameters you can call without the dot, but with parenthesis

Task.scala
package com.simpletasks.domain
import java.util.Date
class Task {
  private var id: Int = 0
  private var title: String = _
  private var detail: String = _
  private var finished: Boolean = false
  private var createDate: Date = _
  private var endDate: Date = _
  def getCreateDate: Date = createDate
  def setCreateDate(createDate: Date) = this.createDate = createDate
  def getDetail: String = detail
  def setDetail(detail: String) = this.detail = detail
  def getEndDate: Date = endDate
  def setEndDate(endDate: Date) = this.endDate = endDate
  def getId: Int = id
  def setId(id: Int) = this.id = id
  def getTitle: String = title
  def setTitle(title: String) = this.title = title
  def isFinished: Boolean = finished;
  def setFinished(finished: Boolean) = this.finished = finished
  override def toString: String = "Task{" +
    "createDate=" + createDate +
    ", id=" + id +
    ", title='" + title + '\'' +
    ", detail='" + detail + '\'' +
    ", finished=" + finished +
    ", endDate=" + endDate +
    '}';
}

Java Version

  • line 01 Imports Similar in to Java, but a little more powerful. If you need to import two class of the same package you only need a line import java.util.{Date,List} But if you need all the classes on a package, our old friend placeholder will help us import java.util._. More on this topic in the next chapters.

UserNotFoundException.scala
package com.simpletasks.domain.exceptions
class UserNotFoundException extends Exception

Java Version

  • line 01 extends: The extends keyword is similar to Java, but have little differences, but in this tutorial we don’t touch this concept very deeply.
  • line 01 Look ma’ no braces: You don’t need to add braces if your class don’t have a body

Is ok for now, maybe this chapter isn’t so exciting, but I promise you that the next chapters will be bring some OOOHHH, and many WOWWWW. In the next chapter we’ll use some Spring JDBC and take a closer look to the Scala Syntax.