Added timestamp to Person for testing Date conversion.

This commit is contained in:
Jon Brisbin
2012-10-09 09:15:39 -05:00
committed by Jon Brisbin
parent 68bf39749a
commit e8be1c1ded
5 changed files with 45 additions and 3 deletions

View File

@@ -29,7 +29,7 @@ class QueryMethodsSpec extends BaseSpec {
then:
response.statusCode == HttpStatus.OK
body.links.size() == 4
body.links.size() == 5
}

View File

@@ -1,5 +1,7 @@
package org.springframework.data.rest.test.webmvc;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.persistence.CascadeType;
@@ -8,6 +10,7 @@ import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.Version;
import org.codehaus.jackson.annotate.JsonManagedReference;
@@ -28,6 +31,7 @@ public class Person {
@OneToMany(cascade = CascadeType.REMOVE)
@MapKey(name = "type")
private Map<String, Profile> profiles;
private Date created;
public Person() {
}
@@ -90,4 +94,13 @@ public class Person {
this.profiles = profiles;
}
public Date getCreated() {
return created;
}
@PrePersist
private void setCreated() {
this.created = Calendar.getInstance().getTime();
}
}

View File

@@ -1,5 +1,6 @@
package org.springframework.data.rest.test.webmvc;
import java.util.Date;
import java.util.List;
import org.springframework.data.domain.Page;
@@ -7,6 +8,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.repository.annotation.ConvertWith;
import org.springframework.data.rest.repository.annotation.RestResource;
/**
@@ -35,4 +37,8 @@ public interface PersonRepository extends PagingAndSortingRepository<Person, Lon
@Query("select p from Person p where p.id in(:ids)")
@RestResource(path = "id") Page<Person> findById(@Param("ids") List<Long> ids, Pageable pageable);
@RestResource(path = "created") List<Person> findByCreatedGreaterThan(
@Param("startDate") @ConvertWith(StringToISODateConverter.class) Date startDate
);
}

View File

@@ -10,8 +10,7 @@ import org.springframework.validation.Validator;
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
*/
public class PersonValidator
implements Validator {
public class PersonValidator implements Validator {
private static final Logger LOG = LoggerFactory.getLogger(PersonValidator.class);

View File

@@ -0,0 +1,24 @@
package org.springframework.data.rest.test.webmvc;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.core.convert.converter.Converter;
/**
* @author Jon Brisbin
*/
public class StringToISODateConverter implements Converter<String[], Date> {
@Override public Date convert(String[] s) {
if(s.length == 1) {
try {
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").parse(s[0]);
} catch(ParseException e) {
throw new IllegalArgumentException(e);
}
}
throw new IllegalArgumentException("Can only parse a single date in the parameter.");
}
}