package core.user.utils; import core.user.User; import dao.DBManagement; import error.GExcptAccount; import error.GExcptSQL; import utils.Utils; import java.lang.reflect.InvocationTargetException; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import static dao.DBManagement.userTables; public interface AccountManagement { static User login(String id, String password) throws GExcptSQL, GExcptAccount, SQLException { ResultSet rs = null; String userType = null; for(String userTable:userTables){ List columns = new ArrayList<>(); columns.add("*"); Map limits = new HashMap<>(); limits.put("id",id); try { rs = DBManagement.select(columns,userTable,limits,0,1); } catch (Exception e) { throw new GExcptSQL("QUERY\n\t"+id+"\nfailure"); } if(rs!=null){ userType = userTable; break; } } if(rs==null){ throw new GExcptAccount("id "+id+"don't exists"); } rs.next(); try { if(!rs.getString(2).equals(password)) throw new GExcptAccount("password wrong"); Map vMap = new HashMap<>(); ResultSetMetaData rsm = rs.getMetaData(); rs.next(); for(int i=0;i vMap) throws GExcptSQL { DBManagement.insert(userType,vMap); return createUser(userType, vMap); } static void logout(User user){ //todo } static void destroy(User user){ //todo } static User getUser(String userType){ try { return (User) Class.forName("core.user."+ Utils.toUpperFirstChar(userType)).getDeclaredConstructor().newInstance(); } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException | ClassNotFoundException e) { e.printStackTrace(); } return null; } static User createUser(String userType, Map vMap) { User user = getUser(userType); user.setAttr(vMap); return user; } }