+
+ public static User getById(int id) {
+ return new User(id);
+ }
+
+ public EmailAddress[] getEmails() {
+ try {
+ PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM email WHERE memid=?");
+ ps.setInt(1, id);
+ ResultSet rs = ps.executeQuery();
+ rs.last();
+ int count = rs.getRow();
+ EmailAddress[] data = new EmailAddress[count];
+ rs.beforeFirst();
+ for (int i = 0; i < data.length; i++) {
+ if (!rs.next()) {
+ throw new Error("Internal sql api violation.");
+ }
+ data[i] = EmailAddress.getById(rs.getInt(1));
+ }
+ rs.close();
+ return data;
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+
+ return null;
+ }
+
+ public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
+ try {
+ EmailAddress[] adrs = getEmails();
+ for (int i = 0; i < adrs.length; i++) {
+ if (adrs[i].getAddress().equals(newMail.getAddress())) {
+ if (!adrs[i].isVerified()) {
+ throw new GigiApiException("Email not verified.");
+ }
+ PreparedStatement ps = DatabaseConnection.getInstance().prepare(
+ "UPDATE users SET email=? WHERE id=?");
+ ps.setString(1, newMail.getAddress());
+ ps.setInt(2, getId());
+ ps.execute();
+ email = newMail.getAddress();
+ return;
+ }
+ }
+ throw new GigiApiException("Given address not an address of the user.");
+ } catch (SQLException e) {
+ throw new GigiApiException(e);
+ }
+ }
+
+ public void deleteEmail(EmailAddress mail) throws GigiApiException {
+ if (getEmail().equals(mail.getAddress())) {
+ throw new GigiApiException("Can't delete user's default e-mail.");
+ }
+ try {
+ PreparedStatement ps = DatabaseConnection.getInstance().prepare("DELETE FROM email WHERE id=?");
+ ps.setInt(1, mail.getId());
+ ps.execute();
+ } catch (SQLException e) {
+ throw new GigiApiException(e);
+ }
+ }