diff options
author | Vitaly Takmazov | 2016-06-27 10:33:52 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-06-27 10:33:52 +0300 |
commit | 53fb0b3e0e790df007a198deef21c115ed236b3e (patch) | |
tree | 7dcf33649f36b64d8240bf9a48242c7e5ceed24b /src/main/java/com/juick/http/www/SignUp.java | |
parent | 83a3c5f721411bc463fea4c209e6fc2537c50417 (diff) |
merge with spring-jdbc
Diffstat (limited to 'src/main/java/com/juick/http/www/SignUp.java')
-rw-r--r-- | src/main/java/com/juick/http/www/SignUp.java | 197 |
1 files changed, 41 insertions, 156 deletions
diff --git a/src/main/java/com/juick/http/www/SignUp.java b/src/main/java/com/juick/http/www/SignUp.java index 64b62e48..1ee23386 100644 --- a/src/main/java/com/juick/http/www/SignUp.java +++ b/src/main/java/com/juick/http/www/SignUp.java @@ -18,16 +18,17 @@ package com.juick.http.www; import com.juick.server.UserQueries; -import java.io.IOException; -import java.io.PrintWriter; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import org.apache.commons.lang3.tuple.Pair; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; + import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.List; /** * @@ -35,7 +36,7 @@ import javax.servlet.http.HttpServletResponse; */ public class SignUp { - protected void doGet(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); String type = request.getParameter("type"); @@ -61,8 +62,7 @@ public class SignUp { } response.setContentType("text/html; charset=UTF-8"); - PrintWriter out = response.getWriter(); - try { + try (PrintWriter out = response.getWriter()) { PageTemplates.pageHead(out, "Новый пользователь", null); PageTemplates.pageNavigation(out, visitor, null); @@ -110,12 +110,10 @@ public class SignUp { PageTemplates.pageFooter(request, out, visitor, false); PageTemplates.pageEnd(out); - } finally { - out.close(); } } - protected void doPost(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doPost(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); int uid = 0; @@ -204,170 +202,57 @@ public class SignUp { response.sendRedirect("/"); } - private boolean setUserRef(Connection sql, int uid, int ref) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("INSERT INTO users_refs(user_id,ref) VALUES (?,?)"); - stmt.setInt(1, uid); - stmt.setInt(2, ref); - stmt.executeUpdate(); - ret = true; - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - return ret; + private boolean setUserRef(JdbcTemplate sql, int uid, int ref) { + return sql.update("INSERT INTO users_refs(user_id,ref) VALUES (?,?)", uid, ref) > 0; } - private String getFacebookNameByHash(Connection sql, String hash) { - String ret = null; - - PreparedStatement stmt = null; - ResultSet rs = null; + private String getFacebookNameByHash(JdbcTemplate sql, String hash) { try { - stmt = sql.prepareStatement("SELECT fb_name,fb_link FROM facebook WHERE loginhash=?"); - stmt.setString(1, hash); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = "<a href=\"" + rs.getString(2) + "\" rel=\"nofollow\">" + rs.getString(1) + "</a>"; - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); + return sql.queryForObject("SELECT fb_name,fb_link FROM facebook WHERE loginhash=?", String.class, hash); + } catch (EmptyResultDataAccessException e) { + return null; } - - return ret; } - private String getTelegramNameByHash(Connection sql, String hash) { - String ret = null; - - PreparedStatement stmt = null; - ResultSet rs = null; + private String getTelegramNameByHash(JdbcTemplate sql, String hash) { try { - stmt = sql.prepareStatement("SELECT tg_name FROM telegram WHERE loginhash=?"); - stmt.setString(1, hash); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = "<a href=\"https://telegram.me/" + rs.getString(1) + "\" rel=\"nofollow\">" + rs.getString(1) + "</a>"; - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); + String name = sql.queryForObject("SELECT tg_name FROM telegram WHERE loginhash=?", String.class, hash); + return "<a href=\"https://telegram.me/" + name + "\" rel=\"nofollow\">" + name + "</a>"; + } catch (EmptyResultDataAccessException e) { + return null; } - - return ret; } - private boolean setFacebookUser(Connection sql, String hash, int uid) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("UPDATE facebook SET user_id=?,loginhash=NULL WHERE loginhash=?"); - stmt.setInt(1, uid); - stmt.setString(2, hash); - stmt.executeUpdate(); - ret = true; - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - return ret; + private boolean setFacebookUser(JdbcTemplate sql, String hash, int uid) { + return sql.update("UPDATE facebook SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0; } - private String getVKNameByHash(Connection sql, String hash) { - String ret = null; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT vk_name,vk_link FROM vk WHERE loginhash=?"); - stmt.setString(1, hash); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = "<a href=\"http://vk.com/" + rs.getString(2) + "\" rel=\"nofollow\">" + rs.getString(1) + "</a>"; - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); + private String getVKNameByHash(JdbcTemplate sql, String hash) { + List<Pair<String, String>> logins = sql.query("SELECT vk_name,vk_link FROM vk WHERE loginhash=?", + (rs, num) -> { + return Pair.of(rs.getString(1), rs.getString(2)); + }, hash); + if (logins.size() > 0) { + return "<a href=\"http://vk.com/" + logins.get(0).getRight() + "\" rel=\"nofollow\">" + logins.get(0).getLeft() + "</a>"; } - - return ret; + return null; } - private boolean setVKUser(Connection sql, String hash, int uid) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("UPDATE vk SET user_id=?,loginhash=NULL WHERE loginhash=?"); - stmt.setInt(1, uid); - stmt.setString(2, hash); - stmt.executeUpdate(); - ret = true; - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - return ret; + private boolean setVKUser(JdbcTemplate sql, String hash, int uid) { + return sql.update("UPDATE vk SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0; } - private boolean setTelegramUser(Connection sql, String hash, int uid) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("UPDATE telegram SET user_id=?,loginhash=NULL WHERE loginhash=?"); - stmt.setInt(1, uid); - stmt.setString(2, hash); - stmt.executeUpdate(); - ret = true; - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - return ret; + private boolean setTelegramUser(JdbcTemplate sql, String hash, int uid) { + return sql.update("UPDATE telegram SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0; } - private String getJIDByHash(Connection sql, String hash) { - String ret = null; - - PreparedStatement stmt = null; - ResultSet rs = null; + private String getJIDByHash(JdbcTemplate sql, String hash) { try { - stmt = sql.prepareStatement("SELECT jid FROM jids WHERE loginhash=?"); - stmt.setString(1, hash); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = rs.getString(1); - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); + return sql.queryForObject("SELECT jid FROM jids WHERE loginhash=?", String.class, hash); + } catch (EmptyResultDataAccessException e) { + return null; } - - return ret; } - private boolean setJIDUser(Connection sql, String hash, int uid) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("UPDATE jids SET user_id=?,loginhash=NULL WHERE loginhash=?"); - stmt.setInt(1, uid); - stmt.setString(2, hash); - stmt.executeUpdate(); - ret = true; - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - return ret; + private boolean setJIDUser(JdbcTemplate sql, String hash, int uid) { + return sql.update("UPDATE jids SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0; } } |