Change package names zero->boot
* actuator -> boot-ops * cli -> boot-cli * launcher -> boot-load * autoconfig -> boot-config * bootstrap -> boot-strap * starters -> boot-up [#54095231] [bs-253] Refactor Zero->Boot
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.sample.data.jpa;
|
||||
|
||||
import org.springframework.boot.config.EnableAutoConfiguration;
|
||||
import org.springframework.boot.strap.SpringApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan
|
||||
@EnableAutoConfiguration
|
||||
public class SampleDataJpaApplication {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(SampleDataJpaApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.sample.data.jpa.domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class City implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String state;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String country;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String map;
|
||||
|
||||
protected City() {
|
||||
}
|
||||
|
||||
public City(String name, String country) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public String getState() {
|
||||
return this.state;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return this.country;
|
||||
}
|
||||
|
||||
public String getMap() {
|
||||
return this.map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getName() + "," + getState() + "," + getCountry();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.sample.data.jpa.domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
|
||||
import org.hibernate.annotations.NaturalId;
|
||||
|
||||
@Entity
|
||||
public class Hotel implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@ManyToOne(optional = false)
|
||||
@NaturalId
|
||||
private City city;
|
||||
|
||||
@Column(nullable = false)
|
||||
@NaturalId
|
||||
private String name;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String address;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String zip;
|
||||
|
||||
@OneToMany(fetch = FetchType.LAZY, mappedBy = "hotel")
|
||||
private Set<Review> reviews;
|
||||
|
||||
protected Hotel() {
|
||||
}
|
||||
|
||||
public Hotel(City city, String name) {
|
||||
this.city = city;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public City getCity() {
|
||||
return this.city;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return this.address;
|
||||
}
|
||||
|
||||
public String getZip() {
|
||||
return this.zip;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.sample.data.jpa.domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
public class HotelSummary implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private static final MathContext MATH_CONTEXT = new MathContext(2,
|
||||
RoundingMode.HALF_UP);
|
||||
|
||||
private City city;
|
||||
|
||||
private String name;
|
||||
|
||||
private Double averageRating;
|
||||
|
||||
private Integer averageRatingRounded;
|
||||
|
||||
public HotelSummary(City city, String name, Double averageRating) {
|
||||
this.city = city;
|
||||
this.name = name;
|
||||
this.averageRating = averageRating == null ? null : new BigDecimal(averageRating,
|
||||
MATH_CONTEXT).doubleValue();
|
||||
this.averageRatingRounded = averageRating == null ? null : (int) Math
|
||||
.round(averageRating);
|
||||
}
|
||||
|
||||
public City getCity() {
|
||||
return this.city;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public Double getAverageRating() {
|
||||
return this.averageRating;
|
||||
}
|
||||
|
||||
public Integer getAverageRatingRounded() {
|
||||
return this.averageRatingRounded;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.sample.data.jpa.domain;
|
||||
|
||||
public enum Rating {
|
||||
TERRIBLE, POOR, AVERAGE, GOOD, EXCELLENT,
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.sample.data.jpa.domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class RatingCount implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Rating rating;
|
||||
|
||||
private long count;
|
||||
|
||||
public RatingCount(Rating rating, long count) {
|
||||
this.rating = rating;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public Rating getRating() {
|
||||
return this.rating;
|
||||
}
|
||||
|
||||
public long getCount() {
|
||||
return this.count;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.sample.data.jpa.domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Temporal;
|
||||
import javax.persistence.TemporalType;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@Entity
|
||||
public class Review implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@ManyToOne(optional = false)
|
||||
private Hotel hotel;
|
||||
|
||||
@Column(nullable = false)
|
||||
private int index;
|
||||
|
||||
@Column(nullable = false)
|
||||
@Enumerated(EnumType.ORDINAL)
|
||||
private Rating rating;
|
||||
|
||||
@Column(nullable = false)
|
||||
@Temporal(TemporalType.DATE)
|
||||
private Date checkInDate;
|
||||
|
||||
@Column(nullable = false)
|
||||
@Enumerated(EnumType.ORDINAL)
|
||||
private TripType tripType;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String title;
|
||||
|
||||
@Column(nullable = false, length = 5000)
|
||||
private String details;
|
||||
|
||||
protected Review() {
|
||||
}
|
||||
|
||||
public Review(Hotel hotel, int index, ReviewDetails details) {
|
||||
Assert.notNull(hotel, "Hotel must not be null");
|
||||
Assert.notNull(details, "Details must not be null");
|
||||
this.hotel = hotel;
|
||||
this.index = index;
|
||||
this.rating = details.getRating();
|
||||
this.checkInDate = details.getCheckInDate();
|
||||
this.tripType = details.getTripType();
|
||||
this.title = details.getTitle();
|
||||
this.details = details.getDetails();
|
||||
}
|
||||
|
||||
public Hotel getHotel() {
|
||||
return this.hotel;
|
||||
}
|
||||
|
||||
public int getIndex() {
|
||||
return this.index;
|
||||
}
|
||||
|
||||
public Rating getRating() {
|
||||
return this.rating;
|
||||
}
|
||||
|
||||
public void setRating(Rating rating) {
|
||||
this.rating = rating;
|
||||
}
|
||||
|
||||
public Date getCheckInDate() {
|
||||
return this.checkInDate;
|
||||
}
|
||||
|
||||
public void setCheckInDate(Date checkInDate) {
|
||||
this.checkInDate = checkInDate;
|
||||
}
|
||||
|
||||
public TripType getTripType() {
|
||||
return this.tripType;
|
||||
}
|
||||
|
||||
public void setTripType(TripType tripType) {
|
||||
this.tripType = tripType;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return this.title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getDetails() {
|
||||
return this.details;
|
||||
}
|
||||
|
||||
public void setDetails(String details) {
|
||||
this.details = details;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.sample.data.jpa.domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
//FIXME Hibernate bug HHH-5792 prevents this being embedded
|
||||
|
||||
public class ReviewDetails implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Rating rating;
|
||||
|
||||
private Date checkInDate;
|
||||
|
||||
private TripType tripType;
|
||||
|
||||
private String title;
|
||||
|
||||
private String details;
|
||||
|
||||
public ReviewDetails() {
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public ReviewDetails(String title, Rating rating) {
|
||||
this.title = title;
|
||||
this.rating = rating;
|
||||
}
|
||||
|
||||
public Rating getRating() {
|
||||
return this.rating;
|
||||
}
|
||||
|
||||
public void setRating(Rating rating) {
|
||||
this.rating = rating;
|
||||
}
|
||||
|
||||
public Date getCheckInDate() {
|
||||
return this.checkInDate;
|
||||
}
|
||||
|
||||
public void setCheckInDate(Date checkInDate) {
|
||||
this.checkInDate = checkInDate;
|
||||
}
|
||||
|
||||
public TripType getTripType() {
|
||||
return this.tripType;
|
||||
}
|
||||
|
||||
public void setTripType(TripType tripType) {
|
||||
this.tripType = tripType;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return this.title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getDetails() {
|
||||
return this.details;
|
||||
}
|
||||
|
||||
public void setDetails(String details) {
|
||||
this.details = details;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.sample.data.jpa.domain;
|
||||
|
||||
public enum TripType {
|
||||
BUSINESS, COUPLES, FAMILY, FRIENDS, SOLO
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.sample.data.jpa.domain.repository;
|
||||
|
||||
import org.springframework.boot.sample.data.jpa.domain.City;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
public interface CityRepository extends Repository<City, Long> {
|
||||
|
||||
Page<City> findAll(Pageable pageable);
|
||||
|
||||
Page<City> findByNameLikeAndCountryLikeAllIgnoringCase(String name, String country,
|
||||
Pageable pageable);
|
||||
|
||||
City findByNameAndCountryAllIgnoringCase(String name, String country);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.sample.data.jpa.domain.repository;
|
||||
|
||||
import org.springframework.boot.sample.data.jpa.domain.City;
|
||||
import org.springframework.boot.sample.data.jpa.domain.Hotel;
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
public interface HotelRepository extends Repository<Hotel, Long> {
|
||||
|
||||
Hotel findByCityAndName(City city, String name);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.sample.data.jpa.domain.repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import org.springframework.boot.sample.data.jpa.domain.City;
|
||||
import org.springframework.boot.sample.data.jpa.domain.Hotel;
|
||||
import org.springframework.boot.sample.data.jpa.domain.HotelSummary;
|
||||
import org.springframework.boot.sample.data.jpa.domain.RatingCount;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.domain.Sort.Order;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class HotelSummaryRepository {
|
||||
|
||||
private static final String AVERAGE_REVIEW_FUNCTION = "avg(r.rating)";
|
||||
|
||||
private static final String FIND_BY_CITY_QUERY = "select new "
|
||||
+ HotelSummary.class.getName() + "(h.city, h.name, "
|
||||
+ AVERAGE_REVIEW_FUNCTION
|
||||
+ ") from Hotel h left outer join h.reviews r where h.city = ?1 group by h";
|
||||
|
||||
private static final String FIND_BY_CITY_COUNT_QUERY = "select count(h) from Hotel h where h.city = ?1";
|
||||
|
||||
private static final String FIND_RATING_COUNTS_QUERY = "select new "
|
||||
+ RatingCount.class.getName() + "(r.rating, count(r)) "
|
||||
+ "from Review r where r.hotel = ?1 group by r.rating order by r.rating DESC";
|
||||
|
||||
private EntityManager entityManager;
|
||||
|
||||
public Page<HotelSummary> findByCity(City city, Pageable pageable) {
|
||||
StringBuilder queryString = new StringBuilder(FIND_BY_CITY_QUERY);
|
||||
applySorting(queryString, pageable == null ? null : pageable.getSort());
|
||||
|
||||
Query query = this.entityManager.createQuery(queryString.toString());
|
||||
query.setParameter(1, city);
|
||||
query.setFirstResult(pageable.getOffset());
|
||||
query.setMaxResults(pageable.getPageSize());
|
||||
|
||||
Query countQuery = this.entityManager.createQuery(FIND_BY_CITY_COUNT_QUERY);
|
||||
countQuery.setParameter(1, city);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
List<HotelSummary> content = query.getResultList();
|
||||
|
||||
Long total = (Long) countQuery.getSingleResult();
|
||||
|
||||
return new PageImpl<HotelSummary>(content, pageable, total);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<RatingCount> findRatingCounts(Hotel hotel) {
|
||||
Query query = this.entityManager.createQuery(FIND_RATING_COUNTS_QUERY);
|
||||
query.setParameter(1, hotel);
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
private void applySorting(StringBuilder query, Sort sort) {
|
||||
if (sort != null) {
|
||||
query.append(" order by");
|
||||
for (Order order : sort) {
|
||||
String aliasedProperty = getAliasedProperty(order.getProperty());
|
||||
query.append(String.format(" %s %s,", aliasedProperty, order
|
||||
.getDirection().name().toLowerCase(Locale.US)));
|
||||
}
|
||||
query.deleteCharAt(query.length() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
private String getAliasedProperty(String property) {
|
||||
if (property.equals("averageRating")) {
|
||||
return AVERAGE_REVIEW_FUNCTION;
|
||||
}
|
||||
return "h." + property;
|
||||
}
|
||||
|
||||
@PersistenceContext
|
||||
public void setEntityManager(EntityManager entityManager) {
|
||||
this.entityManager = entityManager;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.sample.data.jpa.domain.repository;
|
||||
|
||||
import org.springframework.boot.sample.data.jpa.domain.Hotel;
|
||||
import org.springframework.boot.sample.data.jpa.domain.Review;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
public interface ReviewRepository extends Repository<Review, Long> {
|
||||
|
||||
Page<Review> findByHotel(Hotel hotel, Pageable pageable);
|
||||
|
||||
Review findByHotelAndIndex(Hotel hotel, int index);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.sample.data.jpa.service;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
public class CitySearchCriteria implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String name;
|
||||
|
||||
public CitySearchCriteria() {
|
||||
}
|
||||
|
||||
public CitySearchCriteria(String name) {
|
||||
Assert.notNull(name, "Name must not be null");
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.sample.data.jpa.service;
|
||||
|
||||
import org.springframework.boot.sample.data.jpa.domain.City;
|
||||
import org.springframework.boot.sample.data.jpa.domain.HotelSummary;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
public interface CityService {
|
||||
|
||||
Page<City> findCities(CitySearchCriteria criteria, Pageable pageable);
|
||||
|
||||
City getCity(String name, String country);
|
||||
|
||||
Page<HotelSummary> getHotels(City city, Pageable pageable);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.sample.data.jpa.service;
|
||||
|
||||
import org.springframework.boot.sample.data.jpa.domain.City;
|
||||
import org.springframework.boot.sample.data.jpa.domain.Hotel;
|
||||
import org.springframework.boot.sample.data.jpa.domain.Review;
|
||||
import org.springframework.boot.sample.data.jpa.domain.ReviewDetails;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
public interface HotelService {
|
||||
|
||||
Hotel getHotel(City city, String name);
|
||||
|
||||
Page<Review> getReviews(Hotel hotel, Pageable pageable);
|
||||
|
||||
Review getReview(Hotel hotel, int index);
|
||||
|
||||
Review addReview(Hotel hotel, ReviewDetails details);
|
||||
|
||||
ReviewsSummary getReviewSummary(Hotel hotel);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.sample.data.jpa.service;
|
||||
|
||||
import org.springframework.boot.sample.data.jpa.domain.Rating;
|
||||
|
||||
public interface ReviewsSummary {
|
||||
|
||||
public long getNumberOfReviewsWithRating(Rating rating);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.sample.data.jpa.service.impl;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.sample.data.jpa.domain.City;
|
||||
import org.springframework.boot.sample.data.jpa.domain.HotelSummary;
|
||||
import org.springframework.boot.sample.data.jpa.domain.repository.CityRepository;
|
||||
import org.springframework.boot.sample.data.jpa.domain.repository.HotelSummaryRepository;
|
||||
import org.springframework.boot.sample.data.jpa.service.CitySearchCriteria;
|
||||
import org.springframework.boot.sample.data.jpa.service.CityService;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@Component("cityService")
|
||||
@Transactional
|
||||
public class CityServiceImpl implements CityService {
|
||||
|
||||
// FIXME deal with null repository return values
|
||||
|
||||
private CityRepository cityRepository;
|
||||
|
||||
private HotelSummaryRepository hotelSummaryRepository;
|
||||
|
||||
@Override
|
||||
public Page<City> findCities(CitySearchCriteria criteria, Pageable pageable) {
|
||||
Assert.notNull(criteria, "Criteria must not be null");
|
||||
String name = criteria.getName();
|
||||
if (!StringUtils.hasLength(name)) {
|
||||
return this.cityRepository.findAll(null);
|
||||
}
|
||||
String country = "";
|
||||
int splitPos = name.lastIndexOf(",");
|
||||
if (splitPos >= 0) {
|
||||
country = name.substring(splitPos + 1);
|
||||
name = name.substring(0, splitPos);
|
||||
}
|
||||
name = "%" + name.trim() + "%";
|
||||
country = "%" + country.trim() + "%";
|
||||
return this.cityRepository.findByNameLikeAndCountryLikeAllIgnoringCase(name,
|
||||
country, pageable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public City getCity(String name, String country) {
|
||||
Assert.notNull(name, "Name must not be null");
|
||||
Assert.notNull(country, "Country must not be null");
|
||||
return this.cityRepository.findByNameAndCountryAllIgnoringCase(name, country);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<HotelSummary> getHotels(City city, Pageable pageable) {
|
||||
Assert.notNull(city, "City must not be null");
|
||||
return this.hotelSummaryRepository.findByCity(city, pageable);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setCityRepository(CityRepository cityRepository) {
|
||||
this.cityRepository = cityRepository;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setHotelSummaryRepository(HotelSummaryRepository hotelSummaryRepository) {
|
||||
this.hotelSummaryRepository = hotelSummaryRepository;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.sample.data.jpa.service.impl;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.sample.data.jpa.domain.City;
|
||||
import org.springframework.boot.sample.data.jpa.domain.Hotel;
|
||||
import org.springframework.boot.sample.data.jpa.domain.Rating;
|
||||
import org.springframework.boot.sample.data.jpa.domain.RatingCount;
|
||||
import org.springframework.boot.sample.data.jpa.domain.Review;
|
||||
import org.springframework.boot.sample.data.jpa.domain.ReviewDetails;
|
||||
import org.springframework.boot.sample.data.jpa.domain.repository.HotelRepository;
|
||||
import org.springframework.boot.sample.data.jpa.domain.repository.HotelSummaryRepository;
|
||||
import org.springframework.boot.sample.data.jpa.domain.repository.ReviewRepository;
|
||||
import org.springframework.boot.sample.data.jpa.service.HotelService;
|
||||
import org.springframework.boot.sample.data.jpa.service.ReviewsSummary;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@Component("hotelService")
|
||||
@Transactional
|
||||
public class HotelServiceImpl implements HotelService {
|
||||
|
||||
// FIXME deal with null repository return values
|
||||
|
||||
private HotelRepository hotelRepository;
|
||||
|
||||
private HotelSummaryRepository hotelSummaryRepository;
|
||||
|
||||
private ReviewRepository reviewRepository;
|
||||
|
||||
@Override
|
||||
public Hotel getHotel(City city, String name) {
|
||||
Assert.notNull(city, "City must not be null");
|
||||
Assert.hasLength(name, "Name must not be empty");
|
||||
return this.hotelRepository.findByCityAndName(city, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<Review> getReviews(Hotel hotel, Pageable pageable) {
|
||||
Assert.notNull(hotel, "Hotel must not be null");
|
||||
return this.reviewRepository.findByHotel(hotel, pageable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Review getReview(Hotel hotel, int reviewNumber) {
|
||||
Assert.notNull(hotel, "Hotel must not be null");
|
||||
return this.reviewRepository.findByHotelAndIndex(hotel, reviewNumber);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Review addReview(Hotel hotel, ReviewDetails details) {
|
||||
// FIXME
|
||||
System.out.println(details.getTitle());
|
||||
return new Review(hotel, 1, details);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReviewsSummary getReviewSummary(Hotel hotel) {
|
||||
List<RatingCount> ratingCounts = this.hotelSummaryRepository
|
||||
.findRatingCounts(hotel);
|
||||
return new ReviewsSummaryImpl(ratingCounts);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setHotelRepository(HotelRepository hotelRepository) {
|
||||
this.hotelRepository = hotelRepository;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setHotelSummaryRepository(HotelSummaryRepository hotelSummaryRepository) {
|
||||
this.hotelSummaryRepository = hotelSummaryRepository;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setReviewRepository(ReviewRepository reviewRepository) {
|
||||
this.reviewRepository = reviewRepository;
|
||||
}
|
||||
|
||||
private static class ReviewsSummaryImpl implements ReviewsSummary {
|
||||
|
||||
private Map<Rating, Long> ratingCount;
|
||||
|
||||
public ReviewsSummaryImpl(List<RatingCount> ratingCounts) {
|
||||
this.ratingCount = new HashMap<Rating, Long>();
|
||||
for (RatingCount ratingCount : ratingCounts) {
|
||||
this.ratingCount.put(ratingCount.getRating(), ratingCount.getCount());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getNumberOfReviewsWithRating(Rating rating) {
|
||||
Long count = this.ratingCount.get(rating);
|
||||
return count == null ? 0 : count;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.sample.data.jpa.web;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.sample.data.jpa.service.CityService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@Controller
|
||||
public class SampleController {
|
||||
|
||||
@Autowired
|
||||
private CityService cityService;
|
||||
|
||||
@RequestMapping("/")
|
||||
@ResponseBody
|
||||
public String helloWorld() {
|
||||
return this.cityService.getCity("Bath", "UK").getName();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user