Java tutorial
/* * Copyright 2014-2018 NTT Corporation. * * 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 jp.co.ntt.atrs.domain.service.b1; import jp.co.ntt.atrs.domain.common.exception.AtrsBusinessException; import jp.co.ntt.atrs.domain.common.masterdata.BoardingClassProvider; import jp.co.ntt.atrs.domain.common.masterdata.FareTypeProvider; import jp.co.ntt.atrs.domain.common.masterdata.FlightMasterProvider; import jp.co.ntt.atrs.domain.common.masterdata.RouteProvider; import jp.co.ntt.atrs.domain.common.util.DateTimeUtil; import jp.co.ntt.atrs.domain.common.util.FareTypeUtil; import jp.co.ntt.atrs.domain.model.BoardingClassCd; import jp.co.ntt.atrs.domain.model.FareType; import jp.co.ntt.atrs.domain.model.FareTypeCd; import jp.co.ntt.atrs.domain.model.Flight; import jp.co.ntt.atrs.domain.model.FlightMaster; import jp.co.ntt.atrs.domain.model.FlightType; import jp.co.ntt.atrs.domain.model.Route; import jp.co.ntt.atrs.domain.repository.flight.FlightRepository; import jp.co.ntt.atrs.domain.repository.flight.VacantSeatSearchCriteriaDto; import jp.co.ntt.atrs.domain.service.b0.TicketSharedService; import org.joda.time.LocalDate; import org.joda.time.Days; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; import org.terasoluna.gfw.common.date.jodatime.JodaTimeDateFactory; import org.terasoluna.gfw.common.exception.BusinessException; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Date; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.inject.Inject; /** * * * @author NTT */ @Service @Transactional public class TicketSearchServiceImpl implements TicketSearchService { /** * ?? */ @Inject JodaTimeDateFactory dateFactory; /** * ? */ @Inject FlightRepository flightRepository; /** * ?? */ @Inject RouteProvider routeProvider; /** * ??? */ @Inject FareTypeProvider fareTypeProvider; /** * ?? */ @Inject FlightMasterProvider flightMasterProvider; /** * ??? */ @Inject BoardingClassProvider boardingClassProvider; /** * ? */ @Inject TicketSharedService ticketSharedService; /** * {@inheritDoc} */ @Override public List<FlightVacantInfoDto> searchFlight(TicketSearchCriteriaDto searchCriteria) throws BusinessException { // ? Assert.notNull(searchCriteria); Date depDate = searchCriteria.getDepDate(); BoardingClassCd boardingClassCd = searchCriteria.getBoardingClassCd(); String depAirportCd = searchCriteria.getDepartureAirportCd(); String arrAirportCd = searchCriteria.getArrivalAirportCd(); FlightType flightType = searchCriteria.getFlightType(); Assert.notNull(depDate); Assert.notNull(boardingClassCd); Assert.hasText(depAirportCd); Assert.hasText(arrAirportCd); Assert.notNull(flightType); // ?????? ticketSharedService.validateDepatureDate(depDate); // ???????????? Route route = routeProvider.getRouteByAirportCd(depAirportCd, arrAirportCd); if (route == null) { throw new AtrsBusinessException(TicketSearchErrorCode.E_AR_B1_2002); } // ????? LocalDate sysLocalDate = dateFactory.newDateTime().toLocalDate(); LocalDate depLocalDate = new LocalDate(depDate); int beforeDayNum = Days.daysBetween(sysLocalDate, depLocalDate).getDays(); // ?????Dto? List<FareTypeCd> fareTypeList = FareTypeUtil.getFareTypeCdList(flightType); VacantSeatSearchCriteriaDto criteria = new VacantSeatSearchCriteriaDto(depDate, route, boardingClassCd, beforeDayNum, fareTypeList); // ???? List<Flight> flightList = flightRepository.findByVacantSeatSearchCriteria(criteria); // ?? if (flightList.isEmpty()) { throw new FlightNotFoundException(); } // ????? for (Flight flight : flightList) { FareTypeCd fareTypeCd = flight.getFareType().getFareTypeCd(); flight.setFareType(fareTypeProvider.getFareType(fareTypeCd)); flight.setFlightMaster(flightMasterProvider.getFlightMaster(flight.getFlightMaster().getFlightName())); flight.setBoardingClass( boardingClassProvider.getBoardingClass(flight.getBoardingClass().getBoardingClassCd())); } // ?? int basicFare = ticketSharedService.calculateBasicFare(route.getBasicFare(), boardingClassCd, depDate); // ??? List<FlightVacantInfoDto> flightVacantInfoList = createFlightVacantInfoList(flightList, basicFare); return flightVacantInfoList; } /** * ???? * * @param flightList * @param basicFare ? * @return ? */ private List<FlightVacantInfoDto> createFlightVacantInfoList(List<Flight> flightList, int basicFare) { // ?Map Map<String, FlightVacantInfoDto> vacantInfoMap = new LinkedHashMap<>(); DecimalFormat fareFormatter = new DecimalFormat("###,###"); for (Flight flight : flightList) { FlightMaster flightMaster = flight.getFlightMaster(); String departureTime = flightMaster.getDepartureTime(); FlightVacantInfoDto vacantInfo = vacantInfoMap.get(departureTime); if (vacantInfo == null) { vacantInfo = createFlightVacantInfo(flight); vacantInfoMap.put(departureTime, vacantInfo); } // ? FareType fareType = flight.getFareType(); int fare = ticketSharedService.calculateFare(basicFare, fareType.getDiscountRate()); FareTypeVacantInfoDto fareTypeVacantInfo = new FareTypeVacantInfoDto(fareType.getFareTypeName(), fareFormatter.format(fare), flight.getVacantNum()); vacantInfo.addFareTypeVacantInfo(fareType.getFareTypeCd(), fareTypeVacantInfo); } // ????? return new ArrayList<>(vacantInfoMap.values()); } /** * ??? * * @param flight * @return ? */ private FlightVacantInfoDto createFlightVacantInfo(Flight flight) { FlightVacantInfoDto vacantInfo = new FlightVacantInfoDto(); FlightMaster flightMaster = flight.getFlightMaster(); vacantInfo.setFlightName(flightMaster.getFlightName()); Route route = flightMaster.getRoute(); vacantInfo.setDepAirportName(route.getDepartureAirport().getName()); vacantInfo.setArrAirportName(route.getArrivalAirport().getName()); String depTime = DateTimeUtil.toFormatTimeString(flightMaster.getDepartureTime()); vacantInfo.setDepTime(depTime); String arrTime = DateTimeUtil.toFormatTimeString(flightMaster.getArrivalTime()); vacantInfo.setArrTime(arrTime); vacantInfo.setDepDate(DateTimeUtil.toFormatDateString(flight.getDepartureDate())); vacantInfo.setBoardingClassCd(flight.getBoardingClass().getBoardingClassCd()); return vacantInfo; } }