]> WPIA git - gigi.git/blob - src/org/cacert/gigi/User.java
Fix user.equals (for dob datewise foo)
[gigi.git] / src / org / cacert / gigi / User.java
1 package org.cacert.gigi;
2
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6 import java.sql.Date;
7
8 import org.cacert.gigi.database.DatabaseConnection;
9 import org.cacert.gigi.util.PasswordHash;
10
11 public class User {
12
13         private int id;
14         Name name = new Name(null, null, null, null);
15
16         Date dob;
17         String email;
18
19         public User(int id) {
20                 this.id = id;
21                 try {
22                         PreparedStatement ps = DatabaseConnection
23                                         .getInstance()
24                                         .prepare(
25                                                         "SELECT `fname`, `lname`,`mname`, `suffix`, `dob`, `email` FROM `users` WHERE id=?");
26                         ps.setInt(1, id);
27                         ResultSet rs = ps.executeQuery();
28                         if (rs.next()) {
29                                 name = new Name(rs.getString(1), rs.getString(2),
30                                                 rs.getString(3), rs.getString(4));
31                                 dob = rs.getDate(5);
32                                 email = rs.getString(6);
33                         }
34                         rs.close();
35                 } catch (SQLException e) {
36                         e.printStackTrace();
37                 }
38         }
39         public User() {
40         }
41         public int getId() {
42                 return id;
43         }
44         public String getFname() {
45                 return name.fname;
46         }
47         public String getLname() {
48                 return name.lname;
49         }
50         public String getMname() {
51                 return name.mname;
52         }
53         public Name getName() {
54                 return name;
55         }
56         public void setMname(String mname) {
57                 this.name.mname = mname;
58         }
59         public String getSuffix() {
60                 return name.suffix;
61         }
62         public void setSuffix(String suffix) {
63                 this.name.suffix = suffix;
64         }
65         public Date getDob() {
66                 return dob;
67         }
68         public void setDob(Date dob) {
69                 this.dob = dob;
70         }
71         public String getEmail() {
72                 return email;
73         }
74         public void setEmail(String email) {
75                 this.email = email;
76         }
77         public void setId(int id) {
78                 this.id = id;
79         }
80         public void setFname(String fname) {
81                 this.name.fname = fname;
82         }
83         public void setLname(String lname) {
84                 this.name.lname = lname;
85         }
86         public void insert(String password) throws SQLException {
87                 if (id != 0) {
88                         throw new Error("refusing to insert");
89                 }
90                 PreparedStatement query = DatabaseConnection.getInstance().prepare(
91                                 "insert into `users` set `email`=?, `password`=?, "
92                                                 + "`fname`=?, `mname`=?, `lname`=?, "
93                                                 + "`suffix`=?, `dob`=?, `created`=NOW(), locked=0");
94                 query.setString(1, email);
95                 query.setString(2, PasswordHash.hash(password));
96                 query.setString(3, name.fname);
97                 query.setString(4, name.mname);
98                 query.setString(5, name.lname);
99                 query.setString(6, name.suffix);
100                 query.setDate(7, new java.sql.Date(dob.getTime()));
101                 query.execute();
102                 id = DatabaseConnection.lastInsertId(query);
103                 System.out.println("Inserted: " + id);
104         }
105
106         public boolean canAssure() throws SQLException {
107                 if (getAssurancePoints() < 100) {
108                         return false;
109                 }
110
111                 return hasPassedCATS();
112
113         }
114         public boolean hasPassedCATS() throws SQLException {
115                 PreparedStatement query = DatabaseConnection.getInstance().prepare(
116                                 "SELECT 1 FROM `cats_passed` where `user_id`=?");
117                 query.setInt(1, id);
118                 ResultSet rs = query.executeQuery();
119                 if (rs.next()) {
120                         return true;
121                 } else {
122                         return false;
123                 }
124         }
125         public int getAssurancePoints() throws SQLException {
126                 PreparedStatement query = DatabaseConnection
127                                 .getInstance()
128                                 .prepare(
129                                                 "SELECT sum(points) FROM `notary` where `to`=? AND `deleted`=0");
130                 query.setInt(1, id);
131                 ResultSet rs = query.executeQuery();
132                 int points = 0;
133                 if (rs.next()) {
134                         points = rs.getInt(1);
135                 }
136                 rs.close();
137                 return points;
138         }
139         public int getExperiencePoints() throws SQLException {
140                 PreparedStatement query = DatabaseConnection.getInstance().prepare(
141                                 "SELECT count(*) FROM `notary` where `from`=? AND `deleted`=0");
142                 query.setInt(1, id);
143                 ResultSet rs = query.executeQuery();
144                 int points = 0;
145                 if (rs.next()) {
146                         points = rs.getInt(1) * 2;
147                 }
148                 rs.close();
149                 return points;
150         }
151         @Override
152         public boolean equals(Object obj) {
153                 if (!(obj instanceof User)) {
154                         return false;
155                 }
156                 User s = (User) obj;
157                 return name.equals(s.name) && email.equals(s.email)
158                                 && dob.toString().equals(s.dob.toString()); // This is due to
159                                                                                                                         // day cutoff
160         }
161         public int getMaxAssurePoints() throws SQLException {
162                 int exp = getExperiencePoints();
163                 int points = 10;
164                 if (exp >= 10) {
165                         points += 5;
166                 }
167                 if (exp >= 20) {
168                         points += 5;
169                 }
170                 if (exp >= 30) {
171                         points += 5;
172                 }
173                 if (exp >= 40) {
174                         points += 5;
175                 }
176                 if (exp >= 50) {
177                         points += 5;
178                 }
179                 return points;
180         }
181 }