/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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.apache.webbeans.reservation.controller.admin; import java.util.HashSet; import java.util.List; import java.util.Set; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.inject.Named; import javax.persistence.EntityManager; import javax.persistence.Query; import org.apache.webbeans.reservation.bindings.EntityManagerQualifier; import org.apache.webbeans.reservation.bindings.intercep.Transactional; import org.apache.webbeans.reservation.entity.Hotel; import org.apache.webbeans.reservation.entity.Reservation; import org.apache.webbeans.reservation.entity.Users; /** * Contains admin related activities. */ @Named @RequestScoped public class AdminController { private @Inject @EntityManagerQualifier EntityManager entityManager; @Transactional public void createNewHotel(String name, int star, String city, String country) { if(name.equals("") || city.equals("") || country.equals("")) { return; } Hotel hotel = new Hotel(); hotel.setCity(city); hotel.setCountry(country); hotel.setName(name); hotel.setStar(star); entityManager.persist(hotel); } @SuppressWarnings("unchecked") public List getHotels() { Query query = this.entityManager.createQuery("select h from Hotel h"); return (List)query.getResultList(); } @SuppressWarnings("unchecked") public List getUsers() { Query query = this.entityManager.createQuery("select u from Users u"); return (List)query.getResultList(); } /** * Returns hotel with given id. * * @param id hotel id * @return hotel */ public Hotel getHotelWithId(int id) { Hotel hotel = this.entityManager.find(Hotel.class, id); return hotel; } @SuppressWarnings("unchecked") public List getReservationsWithHotel(int hotelId) { Query query = this.entityManager.createQuery("select u from Users u join fetch u.reservations r where r.id=:id"); query.setParameter("id", hotelId); List users = query.getResultList(); return users; } public Set getReservationsWithUser(int userId) { Query query = this.entityManager.createQuery("select u from Users u where u.id=:id"); query.setParameter("id", userId); Users user = (Users)query.getSingleResult(); Set hotels = new HashSet(); Set reservations = user.getReservations(); if (reservations != null) { for (Reservation reserve : reservations) { hotels.add(reserve.getHotel()); } } return hotels; } @Transactional public void updateHotel(int id, String name, int star, String city, String country) { Hotel hotel = this.entityManager.find(Hotel.class, id); hotel.setName(name); hotel.setStar(star); hotel.setCountry(country); hotel.setCity(city); } @Transactional @SuppressWarnings("unchecked") public void deleteHotel(int id) { Hotel hotel = this.entityManager.find(Hotel.class, id); this.entityManager.remove(hotel); Query query = this.entityManager.createQuery("select r from Reservation r where r.hotel.id=:id"); query.setParameter("id",hotel.getId()); List res = query.getResultList(); for(Reservation r : res) { this.entityManager.remove(r); } } }