-
Notifications
You must be signed in to change notification settings - Fork 0
/
DaoAdminImpl.java
152 lines (136 loc) · 4.3 KB
/
DaoAdminImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.dev.bss.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import com.dev.bbs.exceptions.CustomException;
import com.dev.bbs.exceptions.DeleteFailedException;
import com.dev.bbs.exceptions.UpdateFailedException;
import com.dev.bss.beans.Admin;
import com.dev.bss.beans.Available;
import com.dev.bss.beans.Bus;
import com.dev.bss.beans.Feedback;
public class DaoAdminImpl implements DaoAdmin {
//Creating Global EntityManagerFactory
EntityManagerFactory emf = Persistence.createEntityManagerFactory("MySQLUnit");
//Add Bus
@Override
public Bus createBus(Bus bus) {
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(bus);
em.getTransaction().commit();
em.close();
if(bus != null)
{
return bus;
}
} catch (Exception e) {
throw new CustomException("CreateBusFailed");
}
return null;
}
//UPDATE BUS
@Override
public Boolean updateBus(Bus bus) {
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
//FIND PARTICULAR BUS BASED ON ID TO UPDATE
Bus bus1 = em.find(Bus.class, bus.getBusId());
bus1.setBusName(bus.getBusName());
bus1.setBusType(bus.getBusType());
bus1.setDestination(bus.getDestination());
bus1.setSource(bus.getSource());
bus1.setPrice(bus.getPrice());
bus1.setTotalSeats(bus.getTotalSeats());
em.getTransaction().commit();
return true;
} catch (Exception e) {
throw new UpdateFailedException("FailedTOUpdate");
}
}
//SEARCH BUS ON ID
@Override
public Bus searchBus(int busId) {
try {
EntityManager em = emf.createEntityManager();
//SEARCH BUS BASED ON ID
Query query = em.createQuery("from Bus u where u.busId= :busid ");
query.setParameter("busid", busId );
em.getTransaction().begin();
List bus=query.getResultList();
em.getTransaction().commit();
Bus bus1= (Bus) bus.get(0);
return bus1;
} catch (Exception e) {
throw new CustomException("SearchBusException");
}
}
//DELETE BUS
@Override
public Boolean deletebus(int busId, String password) {
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Bus bus = em.find(Bus.class,busId );
em.remove(bus);
em.getTransaction().commit();
return true;
} catch (Exception e) {
throw new DeleteFailedException("DeleteFailed");
}
}
//GET BUSES BETWEEN SOURCE AND DESTINATION
@Override
public List<Bus> busBetween(String source, String destination) {
try {
EntityManager em = emf.createEntityManager();
Query query = em.createQuery("From Bus b where b.source = :source" + " and b.destination = :destination ");
query.setParameter("source", source);
query.setParameter("destination", destination);
List<Bus> buses = query.getResultList();
return buses;
} catch (Exception e) {
throw new CustomException("SearchBusException");
}
}
//ADMIN LOGIN
@Override
public Boolean adminLogin(int adminId, String password) {
try {
EntityManager em = emf.createEntityManager();
//CHECK FROM PASSWORD
Query query = em.createQuery("from Admin a where a.adminId= :adminid and a.password= :password ");
query.setParameter("adminid", adminId);
query.setParameter("password", password);
em.getTransaction().begin();
List admin = query.getResultList();
em.getTransaction().commit();
Admin admin1 = (Admin) admin.get(0);
return true;
} catch (Exception e) {
throw new com.dev.bbs.exceptions.LoginException("LoginFAILED");
}
}
//ADD AVAILABILITY
@Override
public Boolean addAvailability(Available available) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(available);
em.getTransaction().commit();
return true;
}
//SHOW ALL FEEDBACKS
@Override
public List<Feedback> showFeedback() {
EntityManager em = emf.createEntityManager();
TypedQuery<Feedback> query = em.createQuery("Select f from Feedback f", Feedback.class);
List<Feedback> feedbacks = query.getResultList();
return feedbacks;
}
}