Home » apache-openwebbeans-1.0.0-incubating-M3-binary » org.apache.webbeans.reservation » beans » 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.beans.user;
   18   
   19   import java.io.Serializable;
   20   import java.text.SimpleDateFormat;
   21   import java.util.ArrayList;
   22   import java.util.Date;
   23   import java.util.HashMap;
   24   import java.util.List;
   25   import java.util.Map;
   26   
   27   import javax.enterprise.context.Conversation;
   28   import javax.enterprise.context.ConversationScoped;
   29   import javax.enterprise.inject.Default;
   30   import javax.inject.Inject;
   31   import javax.inject.Named;
   32   import javax.faces.component.html.HtmlDataTable;
   33   import javax.faces.model.SelectItem;
   34   
   35   import org.apache.webbeans.reservation.controller.admin.AdminController;
   36   import org.apache.webbeans.reservation.controller.user.UserController;
   37   import org.apache.webbeans.reservation.entity.Hotel;
   38   import org.apache.webbeans.reservation.model.ReservationModel;
   39   import org.apache.webbeans.reservation.session.SessionTracker;
   40   import org.apache.webbeans.reservation.util.JSFUtility;
   41   
   42   @Named
   43   @ConversationScoped
   44   public class UserReservationBean implements Serializable
   45   {
   46       private static final long serialVersionUID = -5860989760497059459L;
   47   
   48       private List<SelectItem> reservations = new ArrayList<SelectItem>();
   49       
   50       private @Inject @Default UserController controller;
   51       
   52       private List<Hotel> hotels = new ArrayList<Hotel>();
   53       
   54       private @Inject @Default AdminController adminController;
   55       
   56       private String reservationDate;
   57       
   58       private HtmlDataTable model;
   59       
   60       private @Inject @Default Conversation conversation;
   61       
   62       private @Inject @Default SessionTracker tracker;
   63           
   64       private Integer[] itemSelected = new Integer[0];
   65       
   66       private Map<String, ReservationModel> models = new HashMap<String, ReservationModel>();
   67       
   68       public UserReservationBean()
   69       {
   70       }
   71   
   72       
   73       public String addReservation()
   74       {
   75           if(getReservationDate() == null)
   76           {
   77               JSFUtility.addErrorMessage("Reservation date can not be empty!", "");
   78               return null;
   79           }
   80                
   81           Date date = null;
   82           try
   83           {
   84               date = new SimpleDateFormat("dd/MM/yyyy").parse(getReservationDate());
   85               
   86           }catch(Exception e){
   87               
   88               JSFUtility.addErrorMessage("Please give a date with dd/MM/yyyy", "");
   89               return null;
   90           }
   91           
   92           if(!conversation.isLongRunning())
   93           {
   94               conversation.begin();
   95               
   96               JSFUtility.addInfoMessage("Reservation conversation with started with id : " + conversation.getId(), "");            
   97           }
   98           
   99           Hotel hotel = (Hotel)model.getRowData();
  100           
  101           SelectItem item = new SelectItem();
  102           item.setValue(hotel.getId());
  103           item.setLabel(hotel.getName());
  104           
  105           if(contains(item.getValue()) != null)
  106           {
  107               JSFUtility.addErrorMessage("Given hotel is already added", "");
  108               
  109               return null;
  110           }
  111                   
  112           reservations.add(item);
  113           
  114           
  115           ReservationModel model = new ReservationModel(item,date);
  116           models.put(item.getValue().toString(), model);
  117           
  118           return null;
  119       }
  120       
  121       private SelectItem contains(Object id)
  122       {
  123           for(SelectItem i : reservations)
  124           {
  125               if(i.getValue().toString().equals(id.toString()))
  126               {
  127                   return i;
  128               }
  129           }
  130           
  131           return null;
  132           
  133       }
  134       
  135       public String delete()
  136       {
  137           if(this.itemSelected.length == 0)
  138           {
  139               JSFUtility.addErrorMessage("Please select reservation to remove", "");
  140               
  141               return null;
  142           }
  143           
  144           for(Integer i : this.itemSelected)
  145           {
  146               SelectItem item = null;
  147               if((item = contains(i)) != null)
  148               {
  149                   this.reservations.remove(item);   
  150                   
  151                   this.models.remove(item);
  152               }    
  153           }
  154           
  155           
  156           return null;
  157       }
  158       
  159       public String checkout()
  160       {
  161           if(!conversation.isLongRunning())
  162           {
  163               JSFUtility.addErrorMessage("Conversation is not running! Please add hotel for reservation", "");
  164               this.reservations.clear();
  165               this.reservationDate = null;
  166           }
  167           else
  168           {
  169               this.controller.addReservation(models,tracker.getUser().getId());
  170               
  171               conversation.end();
  172               
  173               JSFUtility.addInfoMessage("Reservation are completed succesfully. Conversation with id "+conversation.getId() + " is ended ", "");
  174               
  175               this.reservations.clear();
  176               
  177               this.reservationDate = null;            
  178           }        
  179           
  180           return null;
  181       }
  182   
  183   
  184       /**
  185        * @return the reservations
  186        */
  187       public List<SelectItem> getReservations()
  188       {
  189           return reservations;
  190       }
  191   
  192       
  193       public String clear()
  194       {
  195           this.reservations.clear();
  196                   
  197           this.reservationDate = null;
  198           
  199           if(conversation.isLongRunning())
  200           {
  201               this.conversation.end();
  202               JSFUtility.addInfoMessage("Reservation are deleted succesfully. Conversation with id "+conversation.getId() + "is ended ", "");
  203           }
  204           
  205           return null;
  206       }
  207   
  208       /**
  209        * @param reservations the reservations to set
  210        */
  211       public void setReservations(List<SelectItem> reservations)
  212       {
  213           this.reservations = reservations;
  214       }
  215   
  216   
  217       /**
  218        * @return the hotels
  219        */
  220       public List<Hotel> getHotels()
  221       {
  222           this.hotels = this.adminController.getHotels();
  223           
  224           return hotels;
  225       }
  226   
  227   
  228       /**
  229        * @param hotels the hotels to set
  230        */
  231       public void setHotels(List<Hotel> hotels)
  232       {
  233           this.hotels = hotels;
  234       }
  235   
  236   
  237       /**
  238        * @return the reservationDate
  239        */
  240       public String getReservationDate()
  241       {
  242           return reservationDate;
  243       }
  244   
  245   
  246       /**
  247        * @param reservationDate the reservationDate to set
  248        */
  249       public void setReservationDate(String reservationDate)
  250       {
  251           this.reservationDate = reservationDate;
  252       }
  253   
  254   
  255       /**
  256        * @return the model
  257        */
  258       public HtmlDataTable getModel()
  259       {
  260           return model;
  261       }
  262   
  263   
  264       /**
  265        * @param model the model to set
  266        */
  267       public void setModel(HtmlDataTable model)
  268       {
  269           this.model = model;
  270       }
  271   
  272   
  273       /**
  274        * @return the itemSelected
  275        */
  276       public Integer[] getItemSelected()
  277       {
  278           
  279           return itemSelected;
  280       }
  281   
  282   
  283       /**
  284        * @param itemSelected the itemSelected to set
  285        */
  286       public void setItemSelected(Integer[] itemSelected)
  287       {
  288           this.itemSelected = itemSelected;
  289       }
  290       
  291       
  292   }

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