📄 authenticationmanager.java
字号:
/**
*
*/
package org.tshs.core;
import java.io.File;
import java.sql.SQLException;
import java.sql.Types;
import java.text.MessageFormat;
import org.apache.struts.action.ActionForm;
import org.tshs.core.Constant.AuthState;
import org.tshs.core.Constant.ObjectType;
import org.tshs.storage.rdbms.ColumnInfo;
import org.tshs.storage.rdbms.DbManager;
import org.tshs.storage.rdbms.TableRow;
import org.tshs.storage.rdbms.TableRowIterator;
import org.tshs.entity.Client;
import org.tshs.entity.SightSpot;
import org.tshs.entity.TravelCorp;
import org.tshs.entity.TravelDept;
import org.tshs.entity.TravelGroup;
import org.tshs.entity.TshsObject;
import org.tshs.exception.DBLockedException;
import org.tshs.exception.DBTableFullException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
/**
* @author Administrator
*
*/
public class AuthenticationManager {
/**
* Authenticate the login user
*
* @param email
* @param password
* @return
* @throws Exception
*/
public static AuthState authenticatePassword(String email, String password) throws Exception {
TshsObject obj = null;
try {
obj = CacheManager.getByEmail(email);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return AuthState.NO_SUCH_EMAIL;
}
if (obj == null) {
return AuthState.NO_SUCH_EMAIL;
} else if (obj.checkPassword(password) == false) {
return AuthState.WRONG_PASSWORD;
} else {
AuthState.SUCCESS.setId(obj.getId());
return AuthState.SUCCESS;
}
}
/**
* Registry the
*
* @param form
* @param type
* @return
*/
public static boolean registry(ActionForm form, ObjectType type){
boolean result = false;
if(type == ObjectType.CLIENT){
result = registryCient(form);
}else if(type == ObjectType.CORP){
result = registryCorp(form);
}else if(type == ObjectType.DEPT){
result = registryDept(form);
}
return result;
}
/**
* @param form
* @return
*/
private static boolean registryDept(ActionForm form) {
return false;
}
/**
* @param form
* @return
*/
private static boolean registryCorp(ActionForm form) {
// TODO Auto-generated method stub
return false;
}
/**
* @param form
* @return
*/
private static boolean registryCient(ActionForm form) {
// TODO Auto-generated method stub
return false;
}
private static Vector searchHelp(String table, String name) throws Exception{
Vector<TshsObject> objs = new Vector<TshsObject>();
String sql = "select * from " + table + " where name='" + name + "';";
//test
System.out.println(sql);
TableRowIterator iterator = DbManager.query(sql);
TableRow row = null;
while(iterator.hasNext()){
row = iterator.next();
objs.add(CacheManager.convert(row));
}
return objs;
}
/*
private static Vector getAllData(ObjectType type){
try{
Vector<String> v = new Vector<String>();
TableRowIterator iterator = CacheManager.getAllNames(type);
TableRow row = null;
while(iterator.hasNext()){
row = iterator.next();
v.add((String) row.getColumn("name"));
}
return v;
}catch(Exception e){
e.printStackTrace();
return null;
}
}*/
private static Vector getAllObj(ObjectType type){
try{
Vector<TshsObject> v = new Vector<TshsObject>();
TableRowIterator iterator = CacheManager.getAllObj(type);
TableRow row = null;
String table = null;
if(type == ObjectType.CLIENT){
table = "client";
}else if(type == ObjectType.CORP){
table = "travelcorp";
}else if(type == ObjectType.SIGHTSPOT){
table = "sightspot";
}else{
System.out.println("Error from getAllData in CacheManager: invalid input ObjectType");
return null;
}
while(iterator.hasNext()){
row = iterator.next() ;
DbManager.addRow(table, row);
v.add(CacheManager.convert(row));
}
return v;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
/**
* note:Only if you finish the following fuctions,
*then I could write the details of my code
*I hope you accomplish these methods as quickly as possible
*beacause my code relys on them hardly
*/
//th following part is about client
/**
*the following method needed by youqi's code
*used when the client registes
*return true if succeded
*else return false
*/
public static boolean insertClient(String email,String password, String name, int sex, Date birth, String address, String description){
List<String> names = new ArrayList<String>();
List<Integer> types = new ArrayList<Integer>();
Long id;
try {
id = IdGenerationFactory.generateId(ObjectType.CLIENT);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return false;
} catch (DBTableFullException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return false;
}
names.add("id");
types.add(Types.BIGINT);
names.add("email");
types.add(Types.VARCHAR);
names.add("password");
types.add(Types.VARCHAR);
names.add("name");
types.add(Types.VARCHAR);
names.add("sex");
types.add(Types.TINYINT);
names.add("birth");
types.add(Types.DATE);
names.add("address");
types.add(Types.VARCHAR);
names.add("description");
types.add(Types.VARCHAR);
ColumnInfo info = null;
try {
info = new ColumnInfo(names, types);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", id);
map.put("email", email);
map.put("password", password);
map.put("name", name);
map.put("sex", sex);
map.put("birth", new java.sql.Date(birth.getTime()));
map.put("address", address);
map.put("description", description);
TableRow row = new TableRow("client", map, info);
try {
DbManager.insert(row);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
/**
* the following method needed by youqi's code
*used when the client logins
*return true if succeded
*else return false
* @param email
* @param password
* @return
* @throws DBLockedException
*/
public static boolean validateClient(String email,String password) {
try{
AuthState state = authenticatePassword(email, password);
if(state == AuthState.SUCCESS){
return true;
}else{
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
*the following method needed by youqi's code
*return all the information of travCorp named name to the collcetion of Vector by
*querying the database
*return null if failure
* @throws SQLException
* @throws DBLockedException
*/
/* 考虑到公司的名称可能重复,所以这个查询可能返回多个TravelCorp 。
* 将所有Corp放在Vector里,每个Corp可以通过get×××()方法得到相应的属性。
*/
public static Vector searchTravCorp(String name){
try {
return searchHelp("travelcorp", name);
/*Vector corps = searchHelp("travelcorp", name);
Vector names = new Vector(corps.size());
for(int i = 0; i<corps.size(); i++){
names.add(((TravelCorp)corps.get(i)).getName());
}
return names;*/
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
*the following method needed by youqi's code
*return all the information of TravelGroup named name to the collection of Vector[]
*one Vector contains one TravelGroup's or Travlpath's informaion
*note:TravelGroup also means Travlpath
*return null if failure
*/
/* 有更改!我用一个vector向量来存储所有返回值,每个返回值(TravelGroup)可以通过get×××()方法得到属性的引用。
* 其中每个travelGroup 有个corpId,这是它所在的travelCorp的Id,如果需要的话,可以用CacheManager.getById()
* 方法得到相应的公司对象。如有什么疑问可以讨论。
*/
public static Vector showAllTravelpathList(){
try{
Vector<TshsObject> groups = new Vector<TshsObject>();
String sql = "select * from travelgroup";
TableRowIterator iterator = DbManager.query(sql);
while(iterator.hasNext()){
groups.add(CacheManager.convert(iterator.next()));
}
return groups;
} catch (Exception e){
e.printStackTrace();
return null;
}
}
/**
*the following method needed by youqi's code
*return all the information of Sightspot named name to the collcetion of Vector by
*querying the database
*return null if failure
*
* @param name
* @return
* @throws SQLException
* @throws DBLockedException
*/
public static Vector searchSightspot(String name) {
try {
return searchHelp("sightspot", name);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* the following method needed by youqi's code add the travlGroup to the
* client of email at the same time ,the capacity of travlGroup decreases by
* 1 return true if succeded return false if full
*/
/* 考虑到travelGroup可能重名的情况,不能用name属性来进行orderS0ubmit,
* 由于现在的返回值只能为true or false,所以此种方法得到id的可能性已经没有了,
* 但是,在你获得tracelCorp的时候能够得到一组groupId,可以从中获取。
* 要是觉得还是没什么办法,那我们就限制group不能重名,然后把下边的注释去掉就行了。
* 呵呵,只是这样,我们数据库里的id值变得就一点用都没有了。
*/
public static boolean orderSubmit(String email, Long groupId){
Client client = null;
TravelGroup group = null;
try {
client = (Client) CacheManager.getByEmail(email);
group = (TravelGroup) CacheManager.getById(groupId);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
client.addToGroup(group);
List<String> names = new ArrayList<String>();
List<Integer> types = new ArrayList<Integer>();
names.add("clientId");
types.add(Types.BIGINT);
names.add("groupId");
types.add(Types.BIGINT);
ColumnInfo info = null;
try {
info = new ColumnInfo(names, types);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
Map<String, Long> map = new HashMap<String, Long>();
map.put("clientId", client.getId());
map.put("groupId", group.getId());
TableRow row = new TableRow("group2client", map, info);
try {
DbManager.insert(row);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
/**
* the following method needed by youqi's code
*minus the travlGroup to the client of email
*at the same time ,the capacity of travlGroup increases by 1
*return true if succeded
*else return false
*
* @param email
* @param travlGroup
* @return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -