sample.data.jpa.service.CityServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for sample.data.jpa.service.CityServiceImpl.java

Source

/*
 * 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 sample.data.jpa.service;

import org.springframework.beans.factory.annotation.Autowired;
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;

import sample.data.jpa.domain.City;
import sample.data.jpa.domain.Hotel;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@Component("cityService")
@Transactional
class CityServiceImpl implements CityService {
    private final CityRepository cityRepository;

    private final HotelRepository hotelRepository;

    @Autowired
    public CityServiceImpl(CityRepository cityRepository, HotelRepository hotelRepository) {
        this.cityRepository = cityRepository;
        this.hotelRepository = hotelRepository;
    }

    @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();
        }

        String country = "";
        int splitPos = name.lastIndexOf(",");

        if (splitPos >= 0) {
            country = name.substring(splitPos + 1);
            name = name.substring(0, splitPos);
        }

        return this.cityRepository.findByNameContainingAndCountryContainingAllIgnoringCase(name.trim(),
                country.trim(), 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 List<Hotel> getHotels(City city) {
        Assert.notNull(city, "City must not be null");
        return this.hotelRepository.findByCity(city);
    }

    @Override
    public Hotel modifyHotelAddress(String hotelName, String newName) {
        Hotel hotel = hotelRepository.findByName(hotelName);
        hotel.modifyAddress(newName);
        hotelRepository.save(hotel);
        return hotel;
    }

    @Override
    public Hotel findHotel(String hotelName) {
        return hotelRepository.findByName(hotelName);
    }

    @Override
    public Hotel findHotel(String hotelName, String cityName) {
        Hotel hotel = hotelRepository.findByCityAndName(cityRepository.findByName(cityName), hotelName);

        return hotel;
    }

    @Override
    public City findReference(String cityId) {
        return cityRepository.getOne(Long.parseLong(cityId));
    }
}