-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManageAccount.java
More file actions
159 lines (141 loc) · 5 KB
/
Copy pathManageAccount.java
File metadata and controls
159 lines (141 loc) · 5 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
import java.util.List;
import java.util.Date;
import java.util.Iterator;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class ManageAccount {
private static SessionFactory factory;
// Method to CREATE an account by email in the database
public Integer addAccountByEmail(String email, String pw){
Session session = factory.openSession();
Transaction tx = null;
Integer uid = null;
try {
tx = session.beginTransaction();
Account account = new Account(email, pw);
uid = (Integer) session.save(account);
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
return uid;
}
// Method to CREATE an account by phone number in the database
public Integer addAccountByPhone(int pNum, String pw){
Session session = factory.openSession();
Transaction tx = null;
Integer uid = null;
try {
tx = session.beginTransaction();
Account account = new Account(pNum, pw);
uid = (Integer) session.save(account);
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
return uid;
}
// Method to CREATE an account by both email and phone number in the database
public Integer addAccountByEmailAndPhone(String email, int pNum, String pw){
Session session = factory.openSession();
Transaction tx = null;
Integer uid = null;
try {
tx = session.beginTransaction();
Account account = new Account(pNum, email, pw);
uid = (Integer) session.save(account);
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
return uid;
}
/* Method to READ all the accounts */
public void listAccounts( ){
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
// TODO: confirm Accounts or Account?
List accounts = session.createQuery("FROM Accounts").list();
for (Iterator iterator = accounts.iterator(); iterator.hasNext();){
Account account = (Account) iterator.next();
System.out.print("phone: " + account.getPhoneNumber());
System.out.print(" email: " + account.getEmailAddress());
}
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
/* Method to UPDATE password for an account */
public void updatePassword(String email, String pw ){
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Account account = (Account)session.get(Account.class, emailAddress);
account.setPassword( pw );
session.update(account);
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
/* Method to UPDATE password for an account */
public void updatePassword(Integer pNum, String pw ){
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Account account = (Account)session.get(Account.class, phoneNumber);
account.setPassword( pw );
session.update(account);
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
/* Method to UPDATE email for an account */
// TODO: confirm can user update email when they already has one?
// or is it only allowed when current email is NULL
public void updatePassword(Int pNum, String email ){
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Account account = (Account)session.get(Account.class, phoneNumber);
account.setEmailAddress( email );
session.update(account);
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
/* Method to DELETE an account from the records */
// TODO: do we need this?
}