migrated all dates to joda time

This commit is contained in:
Mic 2013-01-19 02:53:27 +08:00
parent c4b5a98ac8
commit d88b565d28
13 changed files with 63 additions and 38 deletions

20
pom.xml
View file

@ -14,6 +14,7 @@
<hibernate.version>4.1.4.Final</hibernate.version> <hibernate.version>4.1.4.Final</hibernate.version>
<aspectj.version>1.7.1</aspectj.version> <aspectj.version>1.7.1</aspectj.version>
<hibernate.validator.version>4.2.0.Final</hibernate.validator.version> <hibernate.validator.version>4.2.0.Final</hibernate.validator.version>
<joda.version>2.0</joda.version>
</properties> </properties>
<dependencies> <dependencies>
@ -196,7 +197,26 @@
<version>1.1</version> <version>1.1</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda.version}</version>
</dependency>
<dependency>
<groupId>org.jadira.usertype</groupId>
<artifactId>usertype.core</artifactId>
<version>3.0.0.CR1</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time-jsptags</artifactId>
<version>1.1</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>

View file

@ -2,7 +2,6 @@ package org.springframework.samples.petclinic;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -15,6 +14,8 @@ import javax.persistence.ManyToOne;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.Table; import javax.persistence.Table;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;
import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator; import org.springframework.beans.support.PropertyComparator;
@ -29,7 +30,8 @@ import org.springframework.beans.support.PropertyComparator;
public class Pet extends NamedEntity { public class Pet extends NamedEntity {
@Column(name="birth_date") @Column(name="birth_date")
private Date birthDate; @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime birthDate;
@ManyToOne @ManyToOne
@JoinColumn(name = "type_id") @JoinColumn(name = "type_id")
@ -43,11 +45,11 @@ public class Pet extends NamedEntity {
private Set<Visit> visits; private Set<Visit> visits;
public void setBirthDate(Date birthDate) { public void setBirthDate(DateTime birthDate) {
this.birthDate = birthDate; this.birthDate = birthDate;
} }
public Date getBirthDate() { public DateTime getBirthDate() {
return this.birthDate; return this.birthDate;
} }

View file

@ -1,7 +1,5 @@
package org.springframework.samples.petclinic; package org.springframework.samples.petclinic;
import java.util.Date;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
@ -10,6 +8,9 @@ import javax.persistence.Table;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;
/** /**
* Simple JavaBean domain object representing a visit. * Simple JavaBean domain object representing a visit.
* *
@ -20,7 +21,8 @@ public class Visit extends BaseEntity {
/** Holds value of property date. */ /** Holds value of property date. */
@Column(name="visit_date") @Column(name="visit_date")
private Date date; @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime date;
/** Holds value of property description. */ /** Holds value of property description. */
@NotNull @Size(min = 1) @NotNull @Size(min = 1)
@ -35,21 +37,21 @@ public class Visit extends BaseEntity {
/** Creates a new instance of Visit for the current date */ /** Creates a new instance of Visit for the current date */
public Visit() { public Visit() {
this.date = new Date(); this.date = new DateTime();
} }
/** Getter for property date. /** Getter for property date.
* @return Value of property date. * @return Value of property date.
*/ */
public Date getDate() { public DateTime getDate() {
return this.date; return this.date;
} }
/** Setter for property date. /** Setter for property date.
* @param date New value of property date. * @param date New value of property date.
*/ */
public void setDate(Date date) { public void setDate(DateTime date) {
this.date = date; this.date = date;
} }

View file

@ -106,7 +106,7 @@ public class JdbcPetRepositoryImpl implements PetRepository {
return new MapSqlParameterSource() return new MapSqlParameterSource()
.addValue("id", pet.getId()) .addValue("id", pet.getId())
.addValue("name", pet.getName()) .addValue("name", pet.getName())
.addValue("birth_date", pet.getBirthDate()) .addValue("birth_date", pet.getBirthDate().toDate())
.addValue("type_id", pet.getType().getId()) .addValue("type_id", pet.getType().getId())
.addValue("owner_id", pet.getOwner().getId()); .addValue("owner_id", pet.getOwner().getId());
} }

View file

@ -2,7 +2,9 @@ package org.springframework.samples.petclinic.repository.jdbc;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Date;
import org.joda.time.DateTime;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper; import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
/** /**
@ -15,7 +17,8 @@ class JdbcPetRowMapper implements ParameterizedRowMapper<JdbcPet> {
JdbcPet pet = new JdbcPet(); JdbcPet pet = new JdbcPet();
pet.setId(rs.getInt("id")); pet.setId(rs.getInt("id"));
pet.setName(rs.getString("name")); pet.setName(rs.getString("name"));
pet.setBirthDate(rs.getDate("birth_date")); Date birthDate = rs.getDate("birth_date");
pet.setBirthDate(new DateTime(birthDate));
pet.setTypeId(rs.getInt("type_id")); pet.setTypeId(rs.getInt("type_id"));
pet.setOwnerId(rs.getInt("owner_id")); pet.setOwnerId(rs.getInt("owner_id"));
return pet; return pet;

View file

@ -2,10 +2,12 @@ package org.springframework.samples.petclinic.repository.jdbc;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Date;
import java.util.List; import java.util.List;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
@ -69,7 +71,7 @@ public class JdbcVisitRepositoryImpl implements VisitRepository {
private MapSqlParameterSource createVisitParameterSource(Visit visit) { private MapSqlParameterSource createVisitParameterSource(Visit visit) {
return new MapSqlParameterSource() return new MapSqlParameterSource()
.addValue("id", visit.getId()) .addValue("id", visit.getId())
.addValue("visit_date", visit.getDate()) .addValue("visit_date", visit.getDate().toDate())
.addValue("description", visit.getDescription()) .addValue("description", visit.getDescription())
.addValue("pet_id", visit.getPet().getId()); .addValue("pet_id", visit.getPet().getId());
} }
@ -84,7 +86,8 @@ public class JdbcVisitRepositoryImpl implements VisitRepository {
public Visit mapRow(ResultSet rs, int row) throws SQLException { public Visit mapRow(ResultSet rs, int row) throws SQLException {
Visit visit = new Visit(); Visit visit = new Visit();
visit.setId(rs.getInt("id")); visit.setId(rs.getInt("id"));
visit.setDate(rs.getTimestamp("visit_date")); Date visitDate = rs.getDate("visit_date");
visit.setDate(new DateTime(visitDate));
visit.setDescription(rs.getString("description")); visit.setDescription(rs.getString("description"));
return visit; return visit;
} }

View file

@ -1,10 +1,6 @@
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.web;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor; import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.samples.petclinic.PetType; import org.springframework.samples.petclinic.PetType;
import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.samples.petclinic.service.ClinicService;
@ -27,9 +23,6 @@ public class ClinicBindingInitializer implements WebBindingInitializer {
private ClinicService clinicService; private ClinicService clinicService;
public void initBinder(WebDataBinder binder, WebRequest request) { public void initBinder(WebDataBinder binder, WebRequest request) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
binder.registerCustomEditor(String.class, new StringTrimmerEditor(false)); binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
binder.registerCustomEditor(PetType.class, new PetTypeEditor(this.clinicService)); binder.registerCustomEditor(PetType.class, new PetTypeEditor(this.clinicService));
} }

View file

@ -46,7 +46,7 @@ public class VisitsAtomView extends AbstractAtomFeedView {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<Visit> visits = (List<Visit>) model.get("visits"); List<Visit> visits = (List<Visit>) model.get("visits");
for (Visit visit : visits) { for (Visit visit : visits) {
Date date = visit.getDate(); Date date = visit.getDate().toDate();
if (feed.getUpdated() == null || date.compareTo(feed.getUpdated()) > 0) { if (feed.getUpdated() == null || date.compareTo(feed.getUpdated()) > 0) {
feed.setUpdated(date); feed.setUpdated(date);
} }
@ -63,11 +63,11 @@ public class VisitsAtomView extends AbstractAtomFeedView {
for (Visit visit : visits) { for (Visit visit : visits) {
Entry entry = new Entry(); Entry entry = new Entry();
String date = String.format("%1$tY-%1$tm-%1$td", visit.getDate()); String date = String.format("%1$tY-%1$tm-%1$td", visit.getDate().toDate());
// see http://diveintomark.org/archives/2004/05/28/howto-atom-id#other // see http://diveintomark.org/archives/2004/05/28/howto-atom-id#other
entry.setId(String.format("tag:springsource.com,%s:%d", date, visit.getId())); entry.setId(String.format("tag:springsource.com,%s:%d", date, visit.getId()));
entry.setTitle(String.format("%s visit on %s", visit.getPet().getName(), date)); entry.setTitle(String.format("%s visit on %s", visit.getPet().getName(), date));
entry.setUpdated(visit.getDate()); entry.setUpdated(visit.getDate().toDate());
Content summary = new Content(); Content summary = new Content();
summary.setValue(visit.getDescription()); summary.setValue(visit.getDescription());

View file

@ -104,7 +104,7 @@
<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource" /> <constructor-arg ref="dataSource" />
</bean> </bean>
<context:component-scan base-package="org.springframework.samples.petclinic.repository.jdbc"/> <context:component-scan base-package="org.springframework.samples.petclinic.repository.jdbc"/>
</beans> </beans>

View file

@ -2,6 +2,7 @@
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="joda" uri="http://www.joda.org/joda/time/tags" %>
<html lang="en"> <html lang="en">
@ -58,7 +59,7 @@
<dt>Name</dt> <dt>Name</dt>
<dd>${pet.name}</dd> <dd>${pet.name}</dd>
<dt>Birth Date</dt> <dt>Birth Date</dt>
<dd><fmt:formatDate value="${pet.birthDate}" pattern="yyyy-MM-dd"/></dd> <dd><joda:format value="${pet.birthDate}" pattern="yyyy-MM-dd" /></dd>
<dt>Type</dt> <dt>Type</dt>
<dd>${pet.type.name}</dd> <dd>${pet.type.name}</dd>
</dl> </dl>
@ -73,7 +74,7 @@
</thead> </thead>
<c:forEach var="visit" items="${pet.visits}"> <c:forEach var="visit" items="${pet.visits}">
<tr> <tr>
<td><fmt:formatDate value="${visit.date}" pattern="yyyy-MM-dd"/></td> <td><joda:format value="${visit.date}" pattern="yyyy-MM-dd"/></td>
<td>${visit.description}</td> <td>${visit.description}</td>
</tr> </tr>
</c:forEach> </c:forEach>

View file

@ -2,7 +2,7 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="joda" uri="http://www.joda.org/joda/time/tags" %>
<html lang="en"> <html lang="en">
@ -26,7 +26,7 @@
</thead> </thead>
<tr> <tr>
<td>${visit.pet.name}</td> <td>${visit.pet.name}</td>
<td><fmt:formatDate value="${visit.pet.birthDate}" pattern="yyyy-MM-dd"/></td> <td><joda:format value="${visit.pet.birthDate}" pattern="yyyy-MM-dd"/></td>
<td>${visit.pet.type.name}</td> <td>${visit.pet.type.name}</td>
<td>${visit.pet.owner.firstName} ${visit.pet.owner.lastName}</td> <td>${visit.pet.owner.firstName} ${visit.pet.owner.lastName}</td>
</tr> </tr>
@ -35,7 +35,7 @@
<table class="table"> <table class="table">
<tr> <tr>
<th> <th>
Date: Date
<br/><form:errors path="date" cssClass="errors"/> <br/><form:errors path="date" cssClass="errors"/>
</th> </th>
<td> <td>
@ -44,7 +44,7 @@
<tr/> <tr/>
<tr> <tr>
<th valign="top"> <th valign="top">
Description: Description
<br/><form:errors path="description" cssClass="errors"/> <br/><form:errors path="description" cssClass="errors"/>
</th> </th>
<td> <td>
@ -61,7 +61,7 @@
</form:form> </form:form>
<br/> <br/>
<b>Previous Visits:</b> <b>Previous Visits</b>
<table style="width: 333px;"> <table style="width: 333px;">
<tr> <tr>
<th>Date</th> <th>Date</th>
@ -70,7 +70,7 @@
<c:forEach var="visit" items="${visit.pet.visits}"> <c:forEach var="visit" items="${visit.pet.visits}">
<c:if test="${!visit['new']}"> <c:if test="${!visit['new']}">
<tr> <tr>
<td><fmt:formatDate value="${visit.date}" pattern="yyyy-MM-dd"/></td> <td><joda:format value="${visit.date}" pattern="yyyy-MM-dd"/></td>
<td>${visit.description}</td> <td>${visit.description}</td>
</tr> </tr>
</c:if> </c:if>

View file

@ -4,8 +4,8 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.util.Collection; import java.util.Collection;
import java.util.Date;
import org.joda.time.DateTime;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.repository.OwnerRepository; import org.springframework.samples.petclinic.repository.OwnerRepository;
@ -124,7 +124,7 @@ public abstract class AbstractPetRepositoryTests {
pet.setName("bowser"); pet.setName("bowser");
Collection<PetType> types = this.petRepository.findPetTypes(); Collection<PetType> types = this.petRepository.findPetTypes();
pet.setType(EntityUtils.getById(types, PetType.class, 2)); pet.setType(EntityUtils.getById(types, PetType.class, 2));
pet.setBirthDate(new Date()); pet.setBirthDate(new DateTime());
o6.addPet(pet); o6.addPet(pet);
assertEquals(found + 1, o6.getPets().size()); assertEquals(found + 1, o6.getPets().size());
// both storePet and storeOwner are necessary to cover all ORM tools // both storePet and storeOwner are necessary to cover all ORM tools

View file

@ -25,6 +25,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.joda.time.DateTime;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.samples.petclinic.Pet; import org.springframework.samples.petclinic.Pet;
@ -55,14 +56,14 @@ public class VisitsAtomViewTest {
bello.setType(dog); bello.setType(dog);
Visit belloVisit = new Visit(); Visit belloVisit = new Visit();
belloVisit.setPet(bello); belloVisit.setPet(bello);
belloVisit.setDate(new Date(2009, 0, 1)); belloVisit.setDate(new DateTime(2009, 1, 1,1,1));
belloVisit.setDescription("Bello visit"); belloVisit.setDescription("Bello visit");
Pet wodan = new Pet(); Pet wodan = new Pet();
wodan.setName("Wodan"); wodan.setName("Wodan");
wodan.setType(dog); wodan.setType(dog);
Visit wodanVisit = new Visit(); Visit wodanVisit = new Visit();
wodanVisit.setPet(wodan); wodanVisit.setPet(wodan);
wodanVisit.setDate(new Date(2009, 0, 2)); wodanVisit.setDate(new DateTime(2009, 1, 2,1,1));
wodanVisit.setDescription("Wodan visit"); wodanVisit.setDescription("Wodan visit");
List<Visit> visits = new ArrayList<Visit>(); List<Visit> visits = new ArrayList<Visit>();
visits.add(belloVisit); visits.add(belloVisit);
@ -80,7 +81,7 @@ public class VisitsAtomViewTest {
assertNotNull("No id set", feed.getId()); assertNotNull("No id set", feed.getId());
assertNotNull("No title set", feed.getTitle()); assertNotNull("No title set", feed.getTitle());
assertEquals("Invalid update set", new Date(2009, 0, 2), feed.getUpdated()); assertEquals("Invalid update set", new DateTime(2009, 1, 2,1,1).toDate(), feed.getUpdated());
} }
@Test @Test