Skip to main content

Part 3 - Introducing the domain objects and first lead create view

As I decided that I will go with static forms instead of dynamic ones, I set sail creating the createlead.html page with Thymeleaf support and Spring MVC.The listing below shows the form:

Listing 1 - createlead.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">

<head th:include="common :: headerFragment">

<title>effectiv:Home</title>

</head>

<body>

<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">

<div class="container-fluid">
<a class="btn btn-navbar" data-toggle="collapse"
data-target=".nav-collapse"> <span class="icon-bar"></span> <span
class="icon-bar"></span> <span class="icon-bar"></span>
</a> <a class="brand" href="#">Project name</a>
<div class="nav-collapse collapse">

<p class="navbar-text pull-right">
Logged in as <a href="#" class="navbar-link">Username</a>
</p>
<ul class="nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>

</ul>
</div>
<!--/.nav-collapse -->
</div>
</div>
</div>

<div class="container-fluid">
<div class="row-fluid">
<div class="span3">

<div class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header">Sidebar</li>
<li class="active"><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>

<li class="nav-header">Sidebar</li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>

<li><a href="#">Link</a></li>
<li class="nav-header">Sidebar</li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>

</div>
<!--/.well -->
</div>
<!--/span-->

<div class="span9">

<div class="well">

<form class="form-horizontal" action="#" th:action="@{/savelead}" th:object="${lead}" method="post">

<h4>New Lead</h4>

<div class="form-actions">
<button type="submit" class="btn btn-primary">Save</button>
<button type="button" class="btn">Cancel</button>
</div>

<div class="control-group">

<div class="row-fluid">
<div class="span4">
<label class="control-label" for="firstName">First Name</label>
<div class="controls">
<input type="text" th:field="*{firstName}" placeholder="First Name"></input>
</div>
</div>

<div class="span4">
<label class="control-label" for="lastName">Last Name</label>
<div class="controls">
<input type="text" placeholder="Last Name" th:field="*{lastName}"></input>
</div>
</div>
</div>
</div>

<div class="control-group">

<div class="row-fluid">
<div class="span4">
<label class="control-label" for="opportunityAmount">Opportunity Amount</label>
<div class="controls">
<input type="text" placeholder="Opportunity Amount" th:field="*{opportunityAmount}"></input>
</div>
</div>

<div class="span4">
<label class="control-label" for="converted">Converted</label>
<div class="controls">
<input type="checkbox" disabled="true" th:field="*{converted}"></input>
</div>
</div>
</div>
</div>

<div class="control-group">

<div class="row-fluid">
<div class="span4">
<label class="control-label" for="email">Email</label>
<div class="controls">
<div class="input-prepend">
<span class="add-on"><i class="icon-envelope"></i></span>
<input type="text" placeholder="Email" th:field="*{email}"></input>
</div>
</div>
</div>

<div class="span4">
<label class="control-label" for="mobile">Mobile</label>
<div class="controls">
<input type="text" placeholder="Mobile" th:field="*{mobile}"></input>
</div>
</div>
</div>
</div>

<div class="control-group">

<div class="row-fluid">
<div class="span4">
<label class="control-label" for="workPhone">Work Phone</label>
<div class="controls">
<input type="text" placeholder="Work Phone" th:field="*{workPhone}"></input>
</div>
</div>

<div class="span4">
<label class="control-label" for="fax">Fax</label>
<div class="controls">
<input type="text" placeholder="Fax" th:field="*{fax}"></input>
</div>
</div>
</div>
</div>

<div class="control-group">

<div class="row-fluid">
<div class="span8">
<label class="control-label" for="description">Description</label>
<div class="controls">

<textarea rows="4" cols="20" placeholder="Description" th:field="*{description}"></textarea>
</div>
</div>

</div>
</div>

<hr></hr>
<h5>Address</h5>

<div class="control-group">

<div class="row-fluid">
<div class="span4">
<label class="control-label" for="city">City</label>
<div class="controls">

<input type="text" id="city" placeholder="City" th:field="*{address.city}"></input>
</div>
</div>

<div class="span4">
<label class="control-label" for="country">Country</label>
<div class="controls">
<input type="text" id="country" placeholder="country" th:field="*{address.country}"></input>
</div>
</div>
</div>
</div>

<div class="control-group">

<div class="row-fluid">
<div class="span4">
<label class="control-label" for="postcode">Post Code</label>
<div class="controls">

<input type="text" id="postcode" placeholder="Post Code" th:field="*{address.postcode}"></input>
</div>
</div>

<div class="span4">
<label class="control-label" for="state">State</label>
<div class="controls">
<input type="text" id="state" placeholder="State" th:field="*{address.state}"></input>
</div>
</div>
</div>
</div>

<div class="control-group">

<div class="row-fluid">
<div class="span8">
<label class="control-label" for="street">Street</label>
<div class="controls">

<textarea rows="4" cols="60" placeholder="Street" th:field="*{address.street}"></textarea>
</div>
</div>

</div>
</div>

<div class="form-actions">
<button type="submit" class="btn btn-primary">Save</button>
<button type="button" class="btn">Cancel</button>
</div>

</form>
</div>

</div>
<!--/span-->

</div>
<!--/row-->

<hr></hr>

<footer>
<p>&copy; Company 2012</p>
</footer>

</div>
<!--/.fluid-container-->

<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="../assets/js/jquery.js"></script>
<script src="../assets/js/bootstrap-transition.js"></script>
<script src="../assets/js/bootstrap-alert.js"></script>
<script src="../assets/js/bootstrap-modal.js"></script>

<script src="../assets/js/bootstrap-dropdown.js"></script>
<script src="../assets/js/bootstrap-scrollspy.js"></script>
<script src="../assets/js/bootstrap-tab.js"></script>
<script src="../assets/js/bootstrap-tooltip.js"></script>
<script src="../assets/js/bootstrap-popover.js"></script>
<script src="../assets/js/bootstrap-button.js"></script>

<script src="../assets/js/bootstrap-collapse.js"></script>
<script src="../assets/js/bootstrap-carousel.js"></script>
<script src="../assets/js/bootstrap-typeahead.js"></script>

</body>
</html>

 

Next step is to modify the guest controller and introduce a new savelead controller as shown in Listing 2

Listing 2 - SaveLeadController.java

package com.effectivcrm.controller;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.effectivcrm.domain.Address;
import com.effectivcrm.domain.Lead;

@Controller
@Slf4j
public class SaveLeadController {

@ModelAttribute
public Lead initLead(){
Lead lead = new Lead();
lead.setAddress(new Address());

log.info("Lead = {}", lead);
log.info("Address = {}", lead.getAddress());

return new Lead();
}

@RequestMapping("/savelead")
public String saveLead(Lead lead){
log.info("Lead for saving = {}", lead);
return "createlead";
}

@RequestMapping(value="/createlead")
public String createLead(){
return "createlead";
}

}

 

Now the most important thing we introduce the domain objects. For this we need the Hibernate dependencies. These are added in the parent pom.xml. The domain object project is created by Maven quickstart archetype. The domain object Lead extends from PersistentObject class and also embeds an Address object. Note the use of nested object with Spring EL and Thmyeleaf in Listing 1. All source code is available in SVN.

Listing 3 - PeristentObject.java

/**
*
*/
package com.effectivcrm.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
import org.hibernate.envers.Audited;
import org.joda.time.DateTime;

/**
* @author Dhrubo
* @param
* @param
*
*/
@Audited
@MappedSuperclass
@EqualsAndHashCode(exclude={"createdBy", "lastModifiedBy", "createdDate", "lastModifiedDate", "deleted", "version"})
@ToString(exclude={"createdBy", "lastModifiedBy", "createdDate", "lastModifiedDate", "deleted", "version"})
public abstract class PersistentObject implements Serializable{

private static final long serialVersionUID = 1L;

@Getter @Setter
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "id", updatable = false, nullable = false, unique = true)
private ID id;

@Getter @Setter
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "created_by", referencedColumnName = "user_id", nullable = false, updatable = false)
private U createdBy;

@Getter @Setter
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "last_modified_by", referencedColumnName = "user_id", nullable = false, updatable = true)
private U lastModifiedBy;

@Getter @Setter
@Temporal(TemporalType.TIMESTAMP)
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@Column(name = "created_date", nullable = false, updatable = false)
private DateTime createdDate;

@Getter @Setter
@Temporal(TemporalType.TIMESTAMP)
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@Column(name = "last_modified_date", nullable = false, updatable = true)
private DateTime lastModifiedDate;

@Getter @Setter
@Column(name = "deleted")
private boolean deleted;

@Getter @Setter
@Version
@Column(name = "version")
private int version;

}

 

Listing 4 - User.java

package com.effectivcrm.domain;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
import org.hibernate.envers.Audited;
import org.joda.time.DateTime;

/**
* @author Dhrubo
*
*/
@Audited
@Entity
@Table(name = "t_user")
public class User implements Serializable{
private static final long serialVersionUID = 1L;

@Getter @Setter
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "user_id", updatable = false, nullable = false, unique = true)
private String id;

@Getter @Setter
@Column(name="first_name" ,nullable=false , length=50)
private String firstName;

@Getter @Setter
@Column(name="last_name" ,nullable=false , length=50)
private String lastName;

@Getter @Setter
@Column(name="email",unique=true ,nullable=false, length=100)
private String email;

@Getter @Setter
@Column(name="password",unique=true ,nullable=false, length=100)
private String password;

@Getter @Setter
@Temporal(TemporalType.TIMESTAMP)
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@Column(name = "created_date", nullable = false, updatable = false)
private DateTime createdDate;

@Getter @Setter
@Temporal(TemporalType.TIMESTAMP)
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@Column(name = "last_modified_date", nullable = false, updatable = false)
private DateTime lastModifiedDate;

@Getter @Setter
@Deprecated
@Temporal(TemporalType.TIMESTAMP)
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@Column(name = "activation_date", nullable = true, updatable = false)
private DateTime activationDate;

@Getter @Setter
@Column(name="deleted" ,nullable=false)
private boolean deleted;

@Getter @Setter
@Column(name="locked" ,nullable=false)
private boolean locked;

}

 

Listing 5 - Address.java

package com.effectivcrm.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

import lombok.Getter;
import lombok.Setter;

/**
* @author Dhrubo
*
*/
@Embeddable
public class Address implements Serializable {
private static final long serialVersionUID = 1L;

@Getter @Setter
@Column(name="city", insertable=true , updatable=true, length=50)
private String city;

@Getter @Setter
@Column(name="country", insertable=true , updatable=true, length=50)
private String country;

@Getter @Setter
@Column(name="postcode", insertable=true , updatable=true, length=50)
private String postcode;

@Getter @Setter
@Column(name="state", insertable=true , updatable=true, length=50)
private String state;

@Getter @Setter
@Column(name="street", insertable=true , updatable=true, length=50)
private String street;

}

 

Listing 6 - Lead.java

package com.effectivcrm.domain;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;

import org.hibernate.envers.Audited;

/**
* The persistent class for the leads database table.
*
*/
@Entity
@Table(name = "t_lead")
@Audited
public class Lead extends PersistentObject {
private static final long serialVersionUID = 1L;

@Getter @Setter
@Column(name = "first_name", length = 100)
private String firstName;

@Getter @Setter
@Column(name = "last_name", length = 100)
private String lastName;

@Getter @Setter
@Column(name = "opportunity_amount")
private double opportunityAmount;

@Getter @Setter
@Column(name = "converted")
private boolean converted;

@Getter @Setter
@Column(name = "description", length = 250)
private String description;

@Getter @Setter
@Column(name = "email", length = 100)
private String email;

@Getter @Setter
@Column(name = "mobile", length = 30)
private String mobile;

@Getter @Setter
@Column(name = "work_phone", length = 30)
private String workPhone;

@Getter @Setter
@Column(name="fax")
private String fax;

@Getter @Setter
@Embedded
private Address address;

}

So whats next in part 3

  • Tiles integration
  • Internationalized messages
  • Validation
  • Spring Security integration
  • Introduction of service layer
  • Introduction of repository layer

    Comments

    Popular posts from this blog

    Why do you need Spring Cloud Config server?

    Last month I wrote a primer on concepts around 12 factor app. Before getting into the details of the Spring Cloud Config Server, I must refresh on the principle #3 from the list presented in that post.

    3 – ConfigurationStore config in the environments
    Configuration information must be separate from the source code. This may seem so obvious, but often we are guilty of leaving critical configuration parameters in the scattered in the code. Instead, applications should have environment specific configuration files. The sensitive information like database password or API key should be stored in these environment configuration files in encrypted format.
     The key takeaways from this postulate for a cloud-native microservices application are:
    Do not store configuration as part of the deployable unit (in the case of lead microservice - inside the jar or war if you are still deploying war like the good old days). Instead, store it in an external location and make it easily accessible during run-…

    Upgrading Lead Microservice - Use MariaDB and Flyway with Spring Boot

    So far I have been using an in-memory H2 database or Mockito for testing the lead microservice. To make the transition towards using the Spring Cloud Config server, I need to upgrade the micro-application to use MariaDB. I will be adding the configuration in the application.yml  the file which in the subsequent post will move over to the config server store. I will also be using Flyway to make it easy to maintain the database schema changes in future. I will use this post to introduce Flyway in the mix. Spring Boot also provides first class integration with Flyway. I am using Flyway as its really quick and easy to get started, minimal learning curve (no DSL) and I am comfortable with it having used it in the past.

    Assumptions

    MariaDB 10 is installedBasic familiarity with FlywayHeidi SQL client is installed.
    Step 1 - Update build.gradle to include the MariaDB JDBC and Flyway dependencies.
    Do not forget to do a Gradle refresh on your IDE (I am using STS 3.8.4 on Java 8)

    Step 2 - Rename the…

    Part 3 - Integrating Tiles, Thymeleaf and Spring MVC 3

    In this post I will demonstrate how to integrate Apache Tiles with Thymeleaf. This is very simple. The first step is to include the tiles and thymeleaf-tiles extension dependencies. I will include them in the pom.xml. Note we wil lbe using Tiles 2.2.2Listing 1 - parent/pom.xml --- thymeleaf-tiles and tiles dependencies <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!-- Tiles -->
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->

    <dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-core</artifactId>
    <version>${tiles.version}</version>
    <scope>compile</scope>
    </dependency>
    <dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-template</artifactId>
    <version>${tiles.version}</version>
    <scope>compile</scope>