-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabase.java
More file actions
176 lines (158 loc) · 4.63 KB
/
Database.java
File metadata and controls
176 lines (158 loc) · 4.63 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package com.flight.dao;//Packages
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.flight.bean.Address;
import com.flight.bean.AvailableTransport;
import com.flight.bean.Customer;
import com.flight.bean.Passenger;
import com.flight.bean.Person;
import com.flight.bean.Reservation;
import com.flight.bean.Transportation;
import com.flight.entity.AddressEntity;
import com.flight.entity.CustomerEntity;
import com.flight.entity.PassengerEntity;
import com.flight.entity.PersonEntity;
public class Database {
private static SessionFactory factory;
public static synchronized SessionFactory getDBTable()
{
if(factory==null)
{
factory=new Configuration().configure().buildSessionFactory();
}
return factory;
}
public List<Reservation> addReservationtToDb(Reservation reservation)
{
return null;
}
@SuppressWarnings("finally")
public boolean addCustomerToDb(Customer customer)
{
try{
factory=getDBTable();
Session session=factory.getCurrentSession();
session.beginTransaction();
CustomerEntity ce=new CustomerEntity();
ce.setDate(customer.getDate());
ce.setEmail(customer.getEmail());
ce.setFirstName(customer.getFirstName());
ce.setGender(customer.getGender());
ce.setLastName(customer.getLastName());
ce.setNoOfReservation(customer.getNoOfReservation());
ce.setPassword(customer.getPassword());
List<Passenger> pass=customer.getPassenger();
List<PassengerEntity> passentity=new ArrayList<>();
if(pass!=null)
{
for(int i=0;i<pass.size();i++)
{
Passenger p=pass.get(i);
PassengerEntity pe=new PassengerEntity();
pe.setDate(p.getDate());
pe.setEmail(p.getEmail());
pe.setFirstName(p.getFirstName());
pe.setGender(p.getGender());
pe.setLastName(p.getLastName());
pe.setMealType(p.getMealType());
pe.setPassportNo(p.getPassportNo());
pe.setPassword(p.getPassword());
pe.setTicketType(p.getTicketType());
pe.setVisaType(p.getVisaType());
AddressEntity ae=new AddressEntity();
ae.setCity(p.getAddress().getCity());
ae.setCountry(p.getAddress().getCountry());
ae.setState(p.getAddress().getState());
ae.setStreet(p.getAddress().getStreet());
ae.setUnit(p.getAddress().getUnit());
ae.setZipCode(p.getAddress().getZipCode());
pe.setAddress(ae);
passentity.add(pe);
}
}
ce.setPassenger(passentity);
AddressEntity ae=new AddressEntity();
ae.setCity(customer.getAddress().getCity());
ae.setCountry(customer.getAddress().getCountry());
ae.setState(customer.getAddress().getState());
ae.setStreet(customer.getAddress().getStreet());
ae.setUnit(customer.getAddress().getUnit());
ae.setZipCode(customer.getAddress().getZipCode());
ce.setAddress(ae);
session.save(ae);
session.save(ce);
session.getTransaction().commit();
return true;
}
catch(Exception e)
{
e.printStackTrace();
}
finally {
factory.close();
}
return false;
}
public boolean checkCustomerInDb(Customer customer)
{
try
{
factory=getDBTable();
Session session=factory.getCurrentSession();
session.beginTransaction();
CustomerEntity ce=session.get(CustomerEntity.class,customer.getEmail());
if(ce!=null)
{
return true;
}
else
{
return false;
}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally {
factory.close();
}
return false;
}
public AvailableTransport getFlightListFromDb(List<Transportation> transports)
{
return null;
}
public boolean signIn(Person person)
{
// PersonEntity pe=new PersonEntity();
// pe.setEmail(person.getEmail());
// pe.setPassword(person.getPassword());
factory=getDBTable();
Session session=factory.getCurrentSession();
session.beginTransaction();
PersonEntity pe=session.get(PersonEntity.class,person.getEmail());
if(pe!=null)
{
Address a=new Address();
a.setCity(pe.getAddress().getCity());
a.setCountry(pe.getAddress().getCountry());
a.setState(pe.getAddress().getState());
a.setStreet(pe.getAddress().getStreet());
a.setUnit(pe.getAddress().getUnit());
a.setZipCode(pe.getAddress().getUnit());
person.setAddress(a);
person.setDate(pe.getDate());
person.setFirstName(pe.getFirstName());
person.setGender(pe.getGender());
return (pe.getPassword()==person.getPassword());
}
else
{
return false;
}
}
}