mirror of
https://github.com/ZSCNetSupportDept/WechatTicketSystem.git
synced 2025-10-30 01:46:18 +08:00
Use Hibernate framework to operate pojo instance, bye bye SQL strings :)
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
package love.sola.netsupport;
|
||||
|
||||
import love.sola.netsupport.config.Settings;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
@@ -16,10 +14,9 @@ import java.io.PrintWriter;
|
||||
* Don't modify this source without my agreement
|
||||
* ***********************************************
|
||||
*/
|
||||
@WebServlet(name = "Index",urlPatterns = "/",loadOnStartup = 1)
|
||||
@WebServlet(name = "Index",urlPatterns = "/index",loadOnStartup = 1)
|
||||
public class Index extends HttpServlet {
|
||||
|
||||
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
doGet(request, response);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package love.sola.netsupport.api;
|
||||
import com.google.gson.Gson;
|
||||
import love.sola.netsupport.config.Settings;
|
||||
import love.sola.netsupport.sql.SQLCore;
|
||||
import love.sola.netsupport.util.JsonP;
|
||||
import love.sola.netsupport.wechat.Command;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
@@ -15,8 +16,6 @@ import java.io.PrintWriter;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import static love.sola.netsupport.config.Lang.lang;
|
||||
|
||||
/**
|
||||
* ***********************************************
|
||||
* Created by Sola on 2015/12/2.
|
||||
@@ -32,16 +31,21 @@ public class Authorize extends HttpServlet {
|
||||
public static Map<String, Command> fetchedCommand = new ConcurrentHashMap<>();
|
||||
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
doGet(request, response);
|
||||
}
|
||||
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
request.setCharacterEncoding("utf-8");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.addHeader("Content-type", "text/json;charset=utf-8");
|
||||
PrintWriter out = response.getWriter();
|
||||
String wechat = request.getParameter("wechat");
|
||||
out.println(gson.toJson(authorize(wechat)));
|
||||
String json = gson.toJson(authorize(request));
|
||||
out.println(JsonP.parse(request, json));
|
||||
out.close();
|
||||
}
|
||||
|
||||
private Response authorize(String wechat) {
|
||||
private Response authorize(HttpServletRequest request) {
|
||||
String wechat = request.getParameter("wechat");
|
||||
if (wechat == null) {
|
||||
return new Response(Response.ResponseCode.PARAMETER_REQUIRED);
|
||||
}
|
||||
@@ -50,28 +54,20 @@ public class Authorize extends HttpServlet {
|
||||
if (l == null || c == null) {
|
||||
return new Response(Response.ResponseCode.AUTHORIZE_FAILED);
|
||||
}
|
||||
if (l < System.currentTimeMillis() - Settings.I.User_Command_Timeout) {
|
||||
return new Response(Response.ResponseCode.AUTHORIZE_FAILED);
|
||||
if (l < System.currentTimeMillis() - Settings.I.User_Command_Timeout * 1000) {
|
||||
return new Response(Response.ResponseCode.REQUEST_EXPIRED);
|
||||
}
|
||||
switch (c) {
|
||||
case REGISTER:
|
||||
Register.authorized.put(wechat, System.currentTimeMillis());
|
||||
break;
|
||||
case QUERY:
|
||||
request.getSession(true).setAttribute("wechat", wechat);
|
||||
request.getSession(true).setAttribute("wechat", wechat);
|
||||
default:
|
||||
return new Response(Response.ResponseCode.AUTHORIZE_FAILED);
|
||||
}
|
||||
return new Response(Response.ResponseCode.OK);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("Duplicates")
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
request.setCharacterEncoding("utf-8");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.addHeader("Content-type", "text/plain;charset=utf-8");
|
||||
PrintWriter out = response.getWriter();
|
||||
out.println(lang("Illegal_Request"));
|
||||
out.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.google.gson.Gson;
|
||||
import love.sola.netsupport.pojo.User;
|
||||
import love.sola.netsupport.sql.SQLCore;
|
||||
import love.sola.netsupport.sql.TableUser;
|
||||
import love.sola.netsupport.util.JsonP;
|
||||
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.ServletException;
|
||||
@@ -39,28 +40,34 @@ public class GetUser extends HttpServlet {
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.addHeader("Content-type", "text/json;charset=utf-8");
|
||||
PrintWriter out = response.getWriter();
|
||||
String json = gson.toJson(query(request));
|
||||
out.println(JsonP.parse(request, json));
|
||||
out.close();
|
||||
}
|
||||
|
||||
private Response query(HttpServletRequest request) {
|
||||
String id = request.getParameter("id");
|
||||
String name = request.getParameter("name");
|
||||
if ((id == null || id.isEmpty()) && (name == null || name.isEmpty())) {
|
||||
out.println(gson.toJson(new Response(Response.ResponseCode.PARAMETER_REQUIRED)));
|
||||
} else if (id != null) {
|
||||
return new Response(Response.ResponseCode.PARAMETER_REQUIRED);
|
||||
}
|
||||
if (id != null) {
|
||||
try {
|
||||
User u = TableUser.getUserById(Long.parseLong(id));
|
||||
if (u == null)
|
||||
out.println(gson.toJson(new Response(Response.ResponseCode.USER_NOT_FOUND)));
|
||||
return new Response(Response.ResponseCode.USER_NOT_FOUND);
|
||||
else
|
||||
out.println(gson.toJson(new Response(Response.ResponseCode.OK, u)));
|
||||
return new Response(Response.ResponseCode.OK, u);
|
||||
} catch (NumberFormatException e) {
|
||||
out.println(gson.toJson(new Response(Response.ResponseCode.ILLEGAL_PARAMETER)));
|
||||
return new Response(Response.ResponseCode.ILLEGAL_PARAMETER);
|
||||
}
|
||||
} else {
|
||||
User u = TableUser.getUserByName(name);
|
||||
if (u == null)
|
||||
out.println(gson.toJson(new Response(Response.ResponseCode.USER_NOT_FOUND)));
|
||||
return new Response(Response.ResponseCode.USER_NOT_FOUND);
|
||||
else
|
||||
out.println(gson.toJson(new Response(Response.ResponseCode.OK, u)));
|
||||
return new Response(Response.ResponseCode.OK, u);
|
||||
}
|
||||
out.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
44
src/main/java/love/sola/netsupport/api/QueryTicket.java
Normal file
44
src/main/java/love/sola/netsupport/api/QueryTicket.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package love.sola.netsupport.api;
|
||||
|
||||
import love.sola.netsupport.sql.SQLCore;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.Session;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
/**
|
||||
* ***********************************************
|
||||
* Created by Sola on 2015/12/4.
|
||||
* Don't modify this source without my agreement
|
||||
* ***********************************************
|
||||
*/
|
||||
@WebServlet(name = "QueryTicket", urlPatterns = "/api/queryticket", loadOnStartup = 23)
|
||||
public class QueryTicket extends HttpServlet {
|
||||
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
|
||||
}
|
||||
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
request.setCharacterEncoding("utf-8");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.addHeader("Content-type", "text/json;charset=utf-8");
|
||||
PrintWriter out = response.getWriter();
|
||||
}
|
||||
|
||||
private Response query(HttpServletRequest request) {
|
||||
try (Session s = SQLCore.sf.openSession()) {
|
||||
// TODO: 2015/12/5 TICKET QUERY
|
||||
} catch (HibernateException e) {
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,7 +12,6 @@ import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@@ -32,27 +31,31 @@ public class Register extends HttpServlet {
|
||||
public static final String STUDENT_ID_REGEX = "^(2010|2012|2013|2014|2015)[0-9]{9}$";
|
||||
public static final String PHONE_NUMBER_REGEX = "^1[34578][0-9]{9}$";
|
||||
|
||||
public static final String REDIRECT_PAGE = "http://topaz.sinaapp.com/nm/result.html?";
|
||||
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
request.setCharacterEncoding("utf-8");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.addHeader("Content-type", "text/plain;charset=utf-8");
|
||||
PrintWriter out = response.getWriter();
|
||||
|
||||
ISP isp = checkISP(request.getParameter("isp"));
|
||||
Block block = checkBlock(request.getParameter("block"));
|
||||
out.println(
|
||||
register(
|
||||
checkStudentId(request.getParameter("sid")),
|
||||
request.getParameter("name"),
|
||||
isp,
|
||||
checkNetAccount(request.getParameter("username"), isp),
|
||||
block,
|
||||
checkRoom(request.getParameter("room"), block),
|
||||
checkPhoneNumber(request.getParameter("phone")),
|
||||
checkWechat(request.getParameter("wechatid"))
|
||||
String result = register(
|
||||
checkStudentId(request.getParameter("sid")),
|
||||
request.getParameter("name"),
|
||||
isp,
|
||||
checkNetAccount(request.getParameter("username"), isp),
|
||||
block,
|
||||
checkRoom(request.getParameter("room"), block),
|
||||
checkPhoneNumber(request.getParameter("phone")),
|
||||
checkWechat(request.getParameter("wechatid"))
|
||||
);
|
||||
response.sendRedirect(
|
||||
response.encodeRedirectURL(REDIRECT_PAGE +
|
||||
"msg=" + result + "" +
|
||||
"&type=" + (result.equals("Register_Success") ? 1 : 0)
|
||||
)
|
||||
);
|
||||
out.close();
|
||||
}
|
||||
|
||||
@SuppressWarnings("Duplicates")
|
||||
@@ -60,31 +63,35 @@ public class Register extends HttpServlet {
|
||||
request.setCharacterEncoding("utf-8");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.addHeader("Content-type", "text/plain;charset=utf-8");
|
||||
PrintWriter out = response.getWriter();
|
||||
out.println(lang("Illegal_Request"));
|
||||
out.close();
|
||||
response.sendRedirect(
|
||||
response.encodeRedirectURL(REDIRECT_PAGE +
|
||||
"msg=" + lang("Illegal_Request") +
|
||||
"&type=-1"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private String register(long sid, String name, ISP isp, String netAccount, Block block, int room, long phone, String wechat) {
|
||||
if (wechat == null) return lang("Illegal_Request");
|
||||
if (sid == -1) return lang("Invalid_Student_Id");
|
||||
if (name == null) return lang("Invalid_Name");
|
||||
if (isp == null) return lang("Invalid_ISP");
|
||||
if (netAccount == null) return lang("Invalid_Account");
|
||||
if (block == null) return lang("Invalid_Block");
|
||||
if (room == -1) return lang("Invalid_Room");
|
||||
if (phone == -1) return lang("Invalid_Phone_Number");
|
||||
if (wechat == null) return "Illegal_Request";
|
||||
if (sid == -1) return "Invalid_Student_Id";
|
||||
if (name == null) return "Invalid_Name";
|
||||
if (isp == null) return "Invalid_ISP";
|
||||
if (netAccount == null) return "Invalid_Account";
|
||||
if (block == null) return "Invalid_Block";
|
||||
if (room == -1) return "Invalid_Room";
|
||||
if (phone == -1) return "Invalid_Phone_Number";
|
||||
User user = TableUser.getUserById(sid);
|
||||
if (user == null) return lang("Invalid_Student_Id");
|
||||
if (!user.getName().equals(name)) return lang("Invalid_Name");
|
||||
if (user.getWechatId() != null) return lang("User_Already_Registered");
|
||||
if (user == null) return "Invalid_Student_Id";
|
||||
if (!user.getName().equals(name)) return "Invalid_Name";
|
||||
if (user.getWechatId() != null) return "User_Already_Registered";
|
||||
user.setIsp(isp);
|
||||
user.setNetAccount(netAccount);
|
||||
user.setBlock(block);
|
||||
user.setRoom(room);
|
||||
user.setPhone(phone);
|
||||
user.setWechatId(wechat);
|
||||
return lang("Register_Success");
|
||||
TableUser.updateUser(user);
|
||||
return "Register_Success";
|
||||
}
|
||||
|
||||
|
||||
@@ -150,7 +157,7 @@ public class Register extends HttpServlet {
|
||||
private String checkWechat(String wechat) {
|
||||
if (wechat == null) return null;
|
||||
Long l = authorized.remove(wechat);
|
||||
return l == null ? null : l < System.currentTimeMillis() - Settings.I.User_Register_Timeout ? null : wechat;
|
||||
return l == null ? null : l < System.currentTimeMillis() - Settings.I.User_Register_Timeout * 1000 ? null : wechat;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ public class Response {
|
||||
ILLEGAL_PARAMETER(-2),
|
||||
AUTHORIZE_FAILED(-9),
|
||||
USER_NOT_FOUND(-11),
|
||||
REQUEST_EXPIRED(-21),
|
||||
;
|
||||
|
||||
private static final Map<Integer, ResponseCode> ID_MAP = new HashMap<>();
|
||||
|
||||
25
src/main/java/love/sola/netsupport/enums/BlockConverter.java
Normal file
25
src/main/java/love/sola/netsupport/enums/BlockConverter.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package love.sola.netsupport.enums;
|
||||
|
||||
import javax.persistence.AttributeConverter;
|
||||
import javax.persistence.Converter;
|
||||
|
||||
@Converter
|
||||
public class BlockConverter implements AttributeConverter<Block, Integer> {
|
||||
|
||||
@Override
|
||||
public Integer convertToDatabaseColumn(Block attribute) {
|
||||
if (attribute == null) {
|
||||
return null;
|
||||
}
|
||||
return attribute.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block convertToEntityAttribute(Integer dbData) {
|
||||
if (dbData == null) {
|
||||
return null;
|
||||
}
|
||||
return Block.fromId(dbData);
|
||||
}
|
||||
|
||||
}
|
||||
25
src/main/java/love/sola/netsupport/enums/ISPConverter.java
Normal file
25
src/main/java/love/sola/netsupport/enums/ISPConverter.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package love.sola.netsupport.enums;
|
||||
|
||||
import javax.persistence.AttributeConverter;
|
||||
import javax.persistence.Converter;
|
||||
|
||||
@Converter
|
||||
public class ISPConverter implements AttributeConverter<ISP, Integer> {
|
||||
|
||||
@Override
|
||||
public Integer convertToDatabaseColumn(ISP attribute) {
|
||||
if (attribute == null) {
|
||||
return null;
|
||||
}
|
||||
return attribute.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ISP convertToEntityAttribute(Integer dbData) {
|
||||
if (dbData == null) {
|
||||
return null;
|
||||
}
|
||||
return ISP.fromId(dbData);
|
||||
}
|
||||
|
||||
}
|
||||
38
src/main/java/love/sola/netsupport/pojo/Ticket.java
Normal file
38
src/main/java/love/sola/netsupport/pojo/Ticket.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package love.sola.netsupport.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* ***********************************************
|
||||
* Created by Sola on 2015/12/2.
|
||||
* Don't modify this source without my agreement
|
||||
* ***********************************************
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "tickets")
|
||||
public class Ticket {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private int id;
|
||||
@ManyToOne(optional = false)
|
||||
@JoinColumn(name = "sid")
|
||||
private User user;
|
||||
private String description;
|
||||
private Date submitTime;
|
||||
private String remark;
|
||||
private Date updateTime;
|
||||
@ManyToOne(optional = true)
|
||||
@JoinColumn(name = "opsid")
|
||||
private User operator;
|
||||
private int status;
|
||||
|
||||
}
|
||||
@@ -2,8 +2,13 @@ package love.sola.netsupport.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import love.sola.netsupport.enums.Block;
|
||||
import love.sola.netsupport.enums.BlockConverter;
|
||||
import love.sola.netsupport.enums.ISP;
|
||||
import love.sola.netsupport.enums.ISPConverter;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
/**
|
||||
* ***********************************************
|
||||
@@ -13,15 +18,25 @@ import love.sola.netsupport.enums.ISP;
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
|
||||
private final long id;
|
||||
private final String name;
|
||||
@Id
|
||||
@Column(name = "id", updatable = false, nullable = false)
|
||||
private long id;
|
||||
@Column(name = "name", updatable = false, nullable = false)
|
||||
private String name;
|
||||
@Convert(converter = ISPConverter.class)
|
||||
private ISP isp;
|
||||
@Column(name = "netaccount")
|
||||
private String netAccount;
|
||||
@Column(name = "wechat")
|
||||
private String wechatId;
|
||||
@Convert(converter = BlockConverter.class)
|
||||
private Block block;
|
||||
private int room;
|
||||
private long phone;
|
||||
private Integer room;
|
||||
private Long phone;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package love.sola.netsupport.sql;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
|
||||
import javax.naming.InitialContext;
|
||||
import javax.sql.DataSource;
|
||||
@@ -15,12 +19,17 @@ public class SQLCore {
|
||||
|
||||
public static DataSource ds;
|
||||
public static Gson gson = new Gson();
|
||||
public static SessionFactory sf;
|
||||
public static ServiceRegistry sr;
|
||||
|
||||
static {
|
||||
try {
|
||||
InitialContext ic = new InitialContext();
|
||||
ds = (DataSource) ic.lookup("java:comp/env/jdbc/netsupport");
|
||||
ds.setLoginTimeout(3);
|
||||
|
||||
sr = new StandardServiceRegistryBuilder().configure().build();
|
||||
sf = new MetadataSources(sr).buildMetadata().buildSessionFactory();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -1,19 +1,9 @@
|
||||
package love.sola.netsupport.sql;
|
||||
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import love.sola.netsupport.config.Settings;
|
||||
import love.sola.netsupport.enums.Block;
|
||||
import love.sola.netsupport.enums.ISP;
|
||||
import love.sola.netsupport.pojo.User;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
|
||||
/**
|
||||
* ***********************************************
|
||||
@@ -33,160 +23,44 @@ public class TableUser extends SQLCore {
|
||||
public static final String COLUMN_ROOM = "room";
|
||||
public static final String COLUMN_PHONE = "phone";
|
||||
|
||||
|
||||
public static User getUserById(long id) {
|
||||
if (id < 0) return null;
|
||||
try {
|
||||
return cache_id.get(id);
|
||||
} catch (ExecutionException e) {
|
||||
return null;
|
||||
try (Session s = sf.openSession()) {
|
||||
return s.get(User.class, id);
|
||||
} catch (HibernateException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static User getUserByWechat(String wechat) {
|
||||
if (wechat == null) return null;
|
||||
try {
|
||||
return cache_wechat.get(wechat);
|
||||
} catch (ExecutionException e) {
|
||||
return null;
|
||||
try (Session s = sf.openSession()) {
|
||||
return (User) s.createCriteria(User.class).add(Restrictions.eq(COLUMN_WECHAT, wechat)).uniqueResult();
|
||||
} catch (HibernateException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static User getUserByName(String name) {
|
||||
if (name == null) return null;
|
||||
User u = getUserByName0(name);
|
||||
if (u != null) {
|
||||
cache_id.put(u.getId(), u);
|
||||
if (u.getWechatId()!=null) cache_wechat.put(u.getWechatId(), u);
|
||||
try (Session s = sf.openSession()) {
|
||||
return (User) s.createCriteria(User.class).add(Restrictions.eq(COLUMN_NAME, name)).uniqueResult();
|
||||
} catch (HibernateException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return u;
|
||||
return null;
|
||||
}
|
||||
|
||||
public static int updateUser(User user) {
|
||||
int r = updateUser0(user);
|
||||
if (r > 0) {
|
||||
cache_id.put(user.getId(), user);
|
||||
if (user.getWechatId() != null) cache_wechat.put(user.getWechatId(), user);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
private static LoadingCache<Long, User> cache_id = CacheBuilder.newBuilder()
|
||||
.concurrencyLevel(4)
|
||||
.maximumSize(2048)
|
||||
.expireAfterAccess(Settings.I.User_Caching_Time, TimeUnit.SECONDS)
|
||||
.build(new IdLoader());
|
||||
|
||||
private static LoadingCache<String, User> cache_wechat = CacheBuilder.newBuilder()
|
||||
.concurrencyLevel(4)
|
||||
.maximumSize(2048)
|
||||
.expireAfterAccess(Settings.I.User_Caching_Time, TimeUnit.SECONDS)
|
||||
.build(new WechatLoader());
|
||||
|
||||
|
||||
private static class IdLoader extends CacheLoader<Long, User> {
|
||||
@Override
|
||||
public User load(Long key) throws Exception {
|
||||
User u = getUserById0(key);
|
||||
System.out.println("Queried user: " + u);
|
||||
if (u == null) throw new UserNotFoundException();
|
||||
if (u.getWechatId() != null) cache_wechat.put(u.getWechatId(), u);
|
||||
return u;
|
||||
}
|
||||
}
|
||||
|
||||
private static class WechatLoader extends CacheLoader<String, User> {
|
||||
@Override
|
||||
public User load(String key) throws Exception {
|
||||
User u = getUserByWechat0(key);
|
||||
System.out.println("Queried user: " + u);
|
||||
if (u == null) throw new UserNotFoundException();
|
||||
cache_id.put(u.getId(), u);
|
||||
return u;
|
||||
}
|
||||
}
|
||||
|
||||
public static class UserNotFoundException extends Exception { }
|
||||
|
||||
|
||||
private static User getUserById0(long id) {
|
||||
try (Connection conn = ds.getConnection()) {
|
||||
PreparedStatement ps = conn.prepareStatement("SELECT * FROM user_info WHERE id=?");
|
||||
ps.setLong(1, id);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
return constructUser(rs);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private static User getUserByWechat0(String wechat) {
|
||||
try (Connection conn = ds.getConnection()) {
|
||||
PreparedStatement ps = conn.prepareStatement("SELECT * FROM user_info WHERE wechat=?");
|
||||
ps.setString(1, wechat);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
return constructUser(rs);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static User getUserByName0(String name) {
|
||||
try (Connection conn = ds.getConnection()) {
|
||||
PreparedStatement ps = conn.prepareStatement("SELECT * FROM user_info WHERE name=?");
|
||||
ps.setString(1, name);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
return constructUser(rs);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static int updateUser0(User user) {
|
||||
try (Connection conn = ds.getConnection()) {
|
||||
PreparedStatement ps = conn.prepareStatement("UPDATE user_info SET " +
|
||||
COLUMN_WECHAT + "=?," +
|
||||
COLUMN_ISP + "=?," +
|
||||
COLUMN_NET_ACCOUNT + "=?," +
|
||||
COLUMN_BLOCK + "=?," +
|
||||
COLUMN_ROOM + "=?," +
|
||||
COLUMN_PHONE + "=? " +
|
||||
"WHERE id=?");
|
||||
ps.setString(1, user.getWechatId());
|
||||
ps.setInt(2, user.getIsp().id);
|
||||
ps.setString(3, user.getNetAccount());
|
||||
ps.setInt(4, user.getBlock().id);
|
||||
ps.setInt(5, user.getRoom());
|
||||
ps.setLong(6, user.getPhone());
|
||||
ps.setLong(7, user.getId());
|
||||
return ps.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
try (Session s = sf.openSession()) {
|
||||
s.beginTransaction();
|
||||
s.update(user);
|
||||
s.getTransaction().commit();
|
||||
return 1;
|
||||
} catch (HibernateException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private static User constructUser(ResultSet rs) throws SQLException {
|
||||
return new User(
|
||||
rs.getLong(COLUMN_ID),
|
||||
rs.getString(COLUMN_NAME),
|
||||
ISP.fromId(rs.getInt(COLUMN_ISP)),
|
||||
rs.getString(COLUMN_NET_ACCOUNT),
|
||||
rs.getString(COLUMN_WECHAT),
|
||||
Block.fromId(rs.getInt(COLUMN_BLOCK)),
|
||||
rs.getInt(COLUMN_ROOM),
|
||||
rs.getInt(COLUMN_PHONE)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
21
src/main/java/love/sola/netsupport/util/JsonP.java
Normal file
21
src/main/java/love/sola/netsupport/util/JsonP.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package love.sola.netsupport.util;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* ***********************************************
|
||||
* Created by Sola on 2015/12/3.
|
||||
* Don't modify this source without my agreement
|
||||
* ***********************************************
|
||||
*/
|
||||
public class JsonP {
|
||||
|
||||
public static String parse(HttpServletRequest request, String json) {
|
||||
String jsonp = request.getParameter("jsonp");
|
||||
if (jsonp == null || jsonp.isEmpty())
|
||||
return json;
|
||||
else
|
||||
return jsonp.replace("{0}", json);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package love.sola.netsupport.wechat;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static love.sola.netsupport.config.Lang.lang;
|
||||
|
||||
/**
|
||||
* ***********************************************
|
||||
* Created by Sola on 2015/11/26.
|
||||
@@ -11,7 +13,8 @@ import java.util.Map;
|
||||
*/
|
||||
public enum Command {
|
||||
|
||||
REGISTER(0, "Register", ".*"),
|
||||
REGISTER(0, ".*"),
|
||||
QUERY(1, "Query"),
|
||||
;
|
||||
|
||||
private static final Map<Integer, Command> ID_MAP = new HashMap<>();
|
||||
@@ -28,8 +31,8 @@ public enum Command {
|
||||
public final String regex;
|
||||
public final int id;
|
||||
|
||||
Command(int id, String name, String regex) {
|
||||
this.name = name;
|
||||
Command(int id, String regex) {
|
||||
this.name = lang("CMD_" + name());
|
||||
this.id = id;
|
||||
this.regex = regex;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user