You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gdms/src/core/user/utils/AccountManagement.java

82 lines
2.6 KiB

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<String> columns = new ArrayList<>();
columns.add("*");
Map<String,String> 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<String, String> vMap = new HashMap<>();
ResultSetMetaData rsm = rs.getMetaData();
rs.next();
for(int i=0;i<rsm.getColumnCount();i++){
vMap.put(rsm.getCatalogName(i),rs.getString(i));
}
return createUser(userType,vMap);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
static User register(String userType, Map<String, String> 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<String, String> vMap) {
User user = getUser(userType);
user.setAttr(vMap);
return user;
}
}