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>
<aspectj.version>1.7.1</aspectj.version>
<hibernate.validator.version>4.2.0.Final</hibernate.validator.version>
<joda.version>2.0</joda.version>
</properties>
<dependencies>
@ -197,6 +198,25 @@
<scope>test</scope>
</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>
<build>
<plugins>

View file

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

View file

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

View file

@ -106,7 +106,7 @@ public class JdbcPetRepositoryImpl implements PetRepository {
return new MapSqlParameterSource()
.addValue("id", pet.getId())
.addValue("name", pet.getName())
.addValue("birth_date", pet.getBirthDate())
.addValue("birth_date", pet.getBirthDate().toDate())
.addValue("type_id", pet.getType().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.SQLException;
import java.util.Date;
import org.joda.time.DateTime;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
/**
@ -15,7 +17,8 @@ class JdbcPetRowMapper implements ParameterizedRowMapper<JdbcPet> {
JdbcPet pet = new JdbcPet();
pet.setId(rs.getInt("id"));
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.setOwnerId(rs.getInt("owner_id"));
return pet;

View file

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

View file

@ -1,10 +1,6 @@
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.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.samples.petclinic.PetType;
import org.springframework.samples.petclinic.service.ClinicService;
@ -27,9 +23,6 @@ public class ClinicBindingInitializer implements WebBindingInitializer {
private ClinicService clinicService;
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(PetType.class, new PetTypeEditor(this.clinicService));
}

View file

@ -46,7 +46,7 @@ public class VisitsAtomView extends AbstractAtomFeedView {
@SuppressWarnings("unchecked")
List<Visit> visits = (List<Visit>) model.get("visits");
for (Visit visit : visits) {
Date date = visit.getDate();
Date date = visit.getDate().toDate();
if (feed.getUpdated() == null || date.compareTo(feed.getUpdated()) > 0) {
feed.setUpdated(date);
}
@ -63,11 +63,11 @@ public class VisitsAtomView extends AbstractAtomFeedView {
for (Visit visit : visits) {
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
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.setUpdated(visit.getDate());
entry.setUpdated(visit.getDate().toDate());
Content summary = new Content();
summary.setValue(visit.getDescription());

View file

@ -2,6 +2,7 @@
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ 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">
@ -58,7 +59,7 @@
<dt>Name</dt>
<dd>${pet.name}</dd>
<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>
<dd>${pet.type.name}</dd>
</dl>
@ -73,7 +74,7 @@
</thead>
<c:forEach var="visit" items="${pet.visits}">
<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>
</tr>
</c:forEach>

View file

@ -2,7 +2,7 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ 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">
@ -26,7 +26,7 @@
</thead>
<tr>
<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.owner.firstName} ${visit.pet.owner.lastName}</td>
</tr>
@ -35,7 +35,7 @@
<table class="table">
<tr>
<th>
Date:
Date
<br/><form:errors path="date" cssClass="errors"/>
</th>
<td>
@ -44,7 +44,7 @@
<tr/>
<tr>
<th valign="top">
Description:
Description
<br/><form:errors path="description" cssClass="errors"/>
</th>
<td>
@ -61,7 +61,7 @@
</form:form>
<br/>
<b>Previous Visits:</b>
<b>Previous Visits</b>
<table style="width: 333px;">
<tr>
<th>Date</th>
@ -70,7 +70,7 @@
<c:forEach var="visit" items="${visit.pet.visits}">
<c:if test="${!visit['new']}">
<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>
</tr>
</c:if>

View file

@ -4,8 +4,8 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Collection;
import java.util.Date;
import org.joda.time.DateTime;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.repository.OwnerRepository;
@ -124,7 +124,7 @@ public abstract class AbstractPetRepositoryTests {
pet.setName("bowser");
Collection<PetType> types = this.petRepository.findPetTypes();
pet.setType(EntityUtils.getById(types, PetType.class, 2));
pet.setBirthDate(new Date());
pet.setBirthDate(new DateTime());
o6.addPet(pet);
assertEquals(found + 1, o6.getPets().size());
// 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.Map;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;
import org.springframework.samples.petclinic.Pet;
@ -55,14 +56,14 @@ public class VisitsAtomViewTest {
bello.setType(dog);
Visit belloVisit = new Visit();
belloVisit.setPet(bello);
belloVisit.setDate(new Date(2009, 0, 1));
belloVisit.setDate(new DateTime(2009, 1, 1,1,1));
belloVisit.setDescription("Bello visit");
Pet wodan = new Pet();
wodan.setName("Wodan");
wodan.setType(dog);
Visit wodanVisit = new Visit();
wodanVisit.setPet(wodan);
wodanVisit.setDate(new Date(2009, 0, 2));
wodanVisit.setDate(new DateTime(2009, 1, 2,1,1));
wodanVisit.setDescription("Wodan visit");
List<Visit> visits = new ArrayList<Visit>();
visits.add(belloVisit);
@ -80,7 +81,7 @@ public class VisitsAtomViewTest {
assertNotNull("No id set", feed.getId());
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