Home » apache-openwebbeans-1.0.0-incubating-M3-binary » org.apache.webbeans.reservation » controller » admin » [javadoc | source]

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    * 
    9    *       http://www.apache.org/licenses/LICENSE-2.0
   10    * 
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.webbeans.reservation.controller.admin;
   18   
   19   import java.util.HashSet;
   20   import java.util.List;
   21   import java.util.Set;
   22   
   23   import javax.enterprise.context.RequestScoped;
   24   import javax.enterprise.inject.Default;
   25   import javax.inject.Inject;
   26   import javax.inject.Named;
   27   import javax.persistence.EntityManager;
   28   import javax.persistence.Query;
   29   
   30   import org.apache.commons.logging.Log;
   31   import org.apache.commons.validator.GenericValidator;
   32   import org.apache.webbeans.reservation.bindings.ApplicationLog;
   33   import org.apache.webbeans.reservation.bindings.intercep.Transactional;
   34   import org.apache.webbeans.reservation.entity.Hotel;
   35   import org.apache.webbeans.reservation.entity.Reservation;
   36   import org.apache.webbeans.reservation.entity.User;
   37   
   38   /**
   39    * Contains admin related activities.
   40    */
   41   @Named
   42   @RequestScoped
   43   public class AdminController
   44   {
   45       private @Inject @ApplicationLog Log logger;
   46       
   47       private @Inject @Default EntityManager entityManager;
   48       
   49       @Transactional
   50       public void createNewHotel(String name, int star, String city, String country)
   51       {        
   52           
   53           if(GenericValidator.isBlankOrNull(name) ||
   54                   GenericValidator.isBlankOrNull(city) ||
   55                   GenericValidator.isBlankOrNull(country))
   56           {
   57               logger.debug("Some of the parameters are missing to define hotel.");
   58               
   59               return;
   60           }
   61           
   62           Hotel hotel = new Hotel();
   63           hotel.setCity(city);
   64           hotel.setCountry(country);
   65           hotel.setName(name);
   66           hotel.setStar(star);
   67           
   68           entityManager.persist(hotel);
   69           
   70       }
   71       
   72       @SuppressWarnings("unchecked")
   73       public List<Hotel> getHotels()
   74       {
   75           Query query = this.entityManager.createQuery("select h from Hotel h");
   76           
   77           return  (List<Hotel>)query.getResultList();
   78       }
   79       
   80       @SuppressWarnings("unchecked")
   81       public List<User> getUsers()
   82       {
   83           Query query = this.entityManager.createQuery("select u from User u");
   84           
   85           return  (List<User>)query.getResultList();
   86           
   87       }
   88   
   89       /**
   90        * Returns hotel with given id.
   91        * 
   92        * @param id hotel id
   93        * @return hotel
   94        */
   95       public Hotel getHotelWithId(int id)
   96       {
   97           Hotel hotel = this.entityManager.find(Hotel.class, id);
   98           
   99           return hotel;
  100       }
  101       
  102       @SuppressWarnings("unchecked")
  103       public List<User> getReservationsWithHotel(int hotelId)
  104       {
  105           Query query = this.entityManager.createQuery("select u from User u join fetch u.reservations r where r.id=:id");
  106           query.setParameter("id", hotelId);
  107           
  108           List<User> users = query.getResultList();
  109           
  110           return users;
  111       }
  112       
  113       public Set<Hotel> getReservationsWithUser(int userId)
  114       {
  115           Query query = this.entityManager.createQuery("select u from User u where u.id=:id");
  116           query.setParameter("id", userId);
  117           
  118           User user = (User)query.getSingleResult();
  119           
  120           Set<Hotel> hotels = new HashSet<Hotel>();
  121           
  122           Set<Reservation> reservations = user.getReservations();
  123           
  124           for(Reservation reserve : reservations)
  125           {
  126               hotels.add(reserve.getHotel());
  127           }
  128           
  129           return hotels;
  130       }
  131       
  132       
  133       @Transactional
  134       public void updateHotel(int id, String name, int star, String city, String country)
  135       {
  136           Hotel hotel = this.entityManager.find(Hotel.class, id);
  137           
  138           hotel.setName(name);
  139           hotel.setStar(star);
  140           hotel.setCountry(country);
  141           hotel.setCity(city);
  142       }
  143       
  144       @Transactional
  145       @SuppressWarnings("unchecked")
  146       public void deleteHotel(int id)
  147       {
  148           Hotel hotel = this.entityManager.find(Hotel.class, id);
  149           
  150           this.entityManager.remove(hotel);
  151           
  152           Query query = this.entityManager.createQuery("select r from Reservation r where r.hotel.id=:id");
  153           query.setParameter("id",hotel.getId());
  154           
  155           List<Reservation> res = query.getResultList();
  156           
  157           for(Reservation r : res)
  158           {
  159               this.entityManager.remove(r);
  160           }
  161           
  162       }
  163   }

Home » apache-openwebbeans-1.0.0-incubating-M3-binary » org.apache.webbeans.reservation » controller » admin » [javadoc | source]