Home » apache-openwebbeans-1.0.0-incubating-M3-binary » org.apache.webbeans.reservation » controller » user » [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.user;
   18   
   19   import java.util.ArrayList;
   20   import java.util.List;
   21   import java.util.Map;
   22   import java.util.Set;
   23   
   24   import javax.annotation.PostConstruct;
   25   import javax.annotation.PreDestroy;
   26   import javax.enterprise.context.RequestScoped;
   27   import javax.enterprise.inject.Default;
   28   import javax.faces.model.SelectItem;
   29   import javax.inject.Inject;
   30   import javax.persistence.EntityManager;
   31   
   32   import org.apache.commons.logging.Log;
   33   import org.apache.webbeans.reservation.bindings.ApplicationLog;
   34   import org.apache.webbeans.reservation.bindings.intercep.Transactional;
   35   import org.apache.webbeans.reservation.entity.Hotel;
   36   import org.apache.webbeans.reservation.entity.Reservation;
   37   import org.apache.webbeans.reservation.entity.User;
   38   import org.apache.webbeans.reservation.model.ReservationModel;
   39   
   40   @RequestScoped
   41   public class UserController
   42   {
   43       private @Inject @Default EntityManager entityManager;
   44       
   45       private @Inject @ApplicationLog Log logger;
   46   
   47       public UserController()
   48       {
   49           
   50       }
   51       
   52       @PostConstruct
   53       public void postConstruct()
   54       {
   55           System.out.println("Post Construct Sample .... " + UserController.class.getName() + " is instantiated");
   56           
   57       }
   58       
   59       @PreDestroy
   60       public void preDestroy()
   61       {
   62           System.out.println("Pre Destroy Construct Sample .... " + UserController.class.getName() + " is destroyed by the container");
   63       }
   64       
   65       public User getUser(int id)
   66       {
   67           User user = this.entityManager.find(User.class, id);
   68           
   69           return user;
   70       }
   71       
   72       @Transactional
   73       public void updateUserInfo(int userId, String name, String surname, int age, String userName, String password)
   74       {
   75           logger.debug("Updating user with id : " + userId);
   76           
   77           User user = this.entityManager.find(User.class, userId);
   78           
   79           user.setName(name);
   80           user.setSurname(surname);
   81           user.setAge(age);
   82           user.setUserName(userName);
   83           user.setPassword(password);
   84       }
   85       
   86       @Transactional
   87       public void addReservation(Map<String, ReservationModel> reservations, int userId)
   88       {
   89           User user = this.entityManager.find(User.class, userId);
   90           
   91           Set<String> keys = reservations.keySet();
   92           
   93           for(String item : keys)
   94           {
   95               ReservationModel rm = reservations.get(item);
   96               SelectItem si = rm.getItem();
   97               Hotel hotel = this.entityManager.find(Hotel.class, si.getValue());
   98               
   99               Reservation reservation = new Reservation();
  100               
  101               user.addHotel(reservation);
  102               
  103               reservation.setHotel(hotel);
  104               reservation.setReservationDate(rm.getDate());
  105               
  106               this.entityManager.persist(reservation);            
  107           }
  108                   
  109       }
  110       
  111       @Transactional
  112       public void deleteReservation(int reservsitonId)
  113       {
  114           Reservation res = this.entityManager.find(Reservation.class, reservsitonId);
  115           User user = res.getUser();
  116           
  117           user.getReservations().remove(res);
  118           
  119           this.entityManager.remove(res);
  120       }
  121       
  122       public List<Reservation> getReservations(int id)
  123       {
  124           User user = this.entityManager.find(User.class, id);
  125           
  126           Set<Reservation> res = user.getReservations();
  127           
  128           List<Reservation> l = new ArrayList<Reservation>();
  129           for(Reservation r : res)
  130           {
  131               l.add(r);
  132           }
  133           
  134           return l;
  135       }
  136   }

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