📄 rentingsessionbeanbean.java
字号:
package BusinessSessionBeans;import Entities.RentingEntityBean;import Entities.CarEntityBean;import exception.CarNotFreeException;import exception.CarNotFoundException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import javax.ejb.Stateless;import javax.persistence.EntityManager;import javax.persistence.PersistenceContext;/** * * @author kiriashoo */@Statelesspublic class RentingSessionBeanBean implements BusinessSessionBeans.RentingSessionBeanRemote { @PersistenceContext private EntityManager em; public RentingSessionBeanBean() { } public void create(Integer carId, Integer userId, String sDate, String eDate) throws CarNotFreeException, CarNotFoundException { CarSessionBeanRemote carSBean; try { javax.naming.Context c = new javax.naming.InitialContext(); carSBean=(CarSessionBeanRemote)c.lookup(CarSessionBeanRemote.class.getName()); } catch(javax.naming.NamingException ne) { java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne); throw new RuntimeException(ne); } CarEntityBean car= carSBean.find(carId);//se cauta masina dupa id if (car== null){ throw new CarNotFoundException(); } //cauta in bd o inchiriere pentru id-ul masinii dat ca parametru, o inchiriere in perioada specificata //daca gaseste inseamna ca masina nu este libera List rentings= em.createQuery("select object(o) from RentingEntityBean o where o.carId = "+ carId+" and ((o.sDate<='"+sDate+"' and o.eDate>'"+sDate+"' ) or (o.sDate<'"+eDate+"' and o.eDate>='"+eDate+"' )) ").getResultList(); //daca lista nu e goala arunca exceptie if (!rentings.isEmpty()) throw new CarNotFreeException(carId, sDate, eDate); //daca lista e goala creaza o inchiriere noua RentingEntityBean rentBean= new RentingEntityBean(); rentBean.setCarId(carId); rentBean.setUserId(userId); rentBean.setSDate(sDate); rentBean.setEDate(eDate); em.persist(rentBean); } public void edit(RentingEntityBean rentBean) { em.merge(rentBean); } public void destroy(RentingEntityBean rentBean) { em.remove(rentBean); } public RentingEntityBean find(Object pk) { return (RentingEntityBean) em.find(RentingEntityBean.class, pk); } public List findAvailabeCars(String sDate, String eDate) { List result= new ArrayList(); List bookings; CarSessionBeanRemote carSBean; CarEntityBean carBean; RentingEntityBean rentBean; String[] rand= new String [6]; try { javax.naming.Context c = new javax.naming.InitialContext(); carSBean=(CarSessionBeanRemote)c.lookup(CarSessionBeanRemote.class.getName()); } catch(javax.naming.NamingException ne) { java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne); throw new RuntimeException(ne); } List car= carSBean.findAll();//se citesc toate masinile din bd Iterator it= car.iterator();//pt fiecare masina while (it.hasNext()){ carBean= (CarEntityBean)it.next(); rand[0]= carBean.getId().toString(); rand[1]= carBean.getMake(); rand[2]= carBean.getModel(); rand[3]= String.valueOf(carBean.getYearOfFabr()); //se interogheaza dupa id-ul masinii. se afla daca masina cu id-ul dat este disponibila bookings= em.createQuery("select object(o) from RentingEntityBean o where o.carId = "+ rand[0]+" and ((o.sDate<='"+sDate+"' and o.eDate>'"+sDate+"' ) or (o.sDate<'"+eDate+"' and o.eDate>='"+eDate+"' ))").getResultList(); if(bookings.isEmpty()){ // the apartment has not been booked result.add(new String[]{rand[0], rand[1], rand[2], rand[3],rand[4]}); } } return result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -