?? userebo.java
字號(hào):
package cn.javass.bookmgr.user.business.ebo;
import cn.javass.bookmgr.user.business.ebi.UserEbi;
import cn.javass.bookmgr.user.valueobject.*;
import java.util.*;
import cn.javass.bookmgr.user.dao.factory.UserDBFactory;
/**
* 用戶(hù)模塊邏輯層具體實(shí)現(xiàn)類(lèi),提供外部模塊和表現(xiàn)層所需要的功能
*
* <p>Title: Java私塾第一個(gè)Java項(xiàng)目——圖書(shū)進(jìn)銷(xiāo)存系統(tǒng)(單機(jī)版)</p>
* <p>Description: 網(wǎng)址:<a href="http://www.javass.cn">http://www.javass.cn</a>
* 新電話:010-86835215 新地址:北京市海淀區(qū)廠洼路5號(hào)院深博達(dá)商務(wù)樓5層</p>
* <p>Copyright: Copyright (c) 2008</p>
* <p>Company: Java私塾</p>
* @author Java私塾
* @version 1.0
*/
public class UserEbo implements UserEbi {
/**
* 當(dāng)前用于操作的用戶(hù)數(shù)據(jù),格式是:key-用戶(hù)編號(hào),value-用戶(hù)model
*/
private static Map userMap = new HashMap();
/**
* 靜態(tài)塊,獲取用戶(hù)模塊對(duì)應(yīng)的數(shù)據(jù)文件中的所有數(shù)據(jù)
*/
static {
userMap = UserDBFactory.getInstance().createUserDAO().getData();
}
public boolean registUser(UserModel um) {
//1:把新數(shù)據(jù)添加到當(dāng)前的map中
userMap.put(um.getId(), um);
//2:調(diào)用數(shù)據(jù)層把數(shù)據(jù)保存起來(lái)
UserDBFactory.getInstance().createUserDAO().saveData(userMap);
return true;
}
public boolean updateUser(UserModel newUm) {
//1:修改當(dāng)前map中的對(duì)應(yīng)數(shù)據(jù)
userMap.put(newUm.getId(), newUm);
//2:調(diào)用數(shù)據(jù)層把數(shù)據(jù)保存起來(lái)
UserDBFactory.getInstance().createUserDAO().saveData(userMap);
return true;
}
public boolean deleteUser(UserModel um) {
//1:刪除當(dāng)前map中的對(duì)應(yīng)數(shù)據(jù)
userMap.remove(um.getId());
//2:調(diào)用數(shù)據(jù)層把數(shù)據(jù)保存起來(lái)
UserDBFactory.getInstance().createUserDAO().saveData(userMap);
return true;
}
public Collection getAll() {
//就是把當(dāng)前Map中的value值取出來(lái),放到集合中并返回
Collection col = new ArrayList();
Iterator it = userMap.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
UserModel um = (UserModel) userMap.get(key);
col.add(um);
}
return col;
}
public Collection getByCondition(QueryUserModel qm) {
//直接在當(dāng)前的Map中查找
Collection col = new ArrayList();
Iterator it = userMap.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
UserModel um = (UserModel) userMap.get(key);
//判斷是否滿足條件
if (this.isMatch(um, qm, false)) {
col.add(um);
}
}
return col;
}
/**
* 判斷一個(gè)用戶(hù)Model的數(shù)據(jù)是否能滿足查詢(xún)條件的要求
* @param um 用戶(hù)Model
* @param qm 查詢(xún)Model
* @param isById 表示用戶(hù)編號(hào)的查詢(xún)方式,
* true表示對(duì)用戶(hù)編號(hào)完全匹配,false表示對(duì)用戶(hù)編號(hào)模糊匹配
* @return true表示滿足條件,false表示不滿足條件
*/
private boolean isMatch(UserModel um, QueryUserModel qm, boolean isById) {
boolean flag = false;
boolean f1 = true;
boolean f2 = true;
boolean f3 = true;
if (!isById) {
//對(duì)用戶(hù)編號(hào)模糊匹配
if (qm.getId() != null && qm.getId().trim().length() > 0) {
if (um.getId() != null && um.getId().trim().length() > 0 &&
um.getId().indexOf(qm.getId()) >= 0) {
f1 = true;
}
else {
f1 = false;
}
}
}
else {
//對(duì)用戶(hù)編號(hào)完全匹配
if (qm.getId() != null && qm.getId().trim().length() > 0) {
if (um.getId() != null && um.getId().trim().length() > 0 &&
um.getId().equals(qm.getId())) {
f1 = true;
}
else {
f1 = false;
}
}
}
if (qm.getName() != null && qm.getName().trim().length() > 0) {
//對(duì)用戶(hù)名稱(chēng)進(jìn)行模糊匹配
if (um.getName() != null && um.getName().trim().length() > 0 &&
um.getName().indexOf(qm.getName()) >= 0) {
f2 = true;
}
else {
f2 = false;
}
}
if (qm.getType() > 0) {
//對(duì)用戶(hù)類(lèi)型進(jìn)行絕對(duì)匹配
if (um.getType() == qm.getType()) {
f3 = true;
}
else {
f3 = false;
}
}
//如果滿足所有的條件,那么這條記錄就完全符合條件
flag = f1 && f2 && f3;
return flag;
}
public UserModel getUserModelById(String userId) {
UserModel retUm = new UserModel();
//1:組織查詢(xún)的model
QueryUserModel qm = new QueryUserModel();
qm.setId(userId);
//2:到當(dāng)前的map中進(jìn)行數(shù)據(jù)匹配
Iterator it = userMap.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
UserModel um = (UserModel) userMap.get(key);
//3:判斷是否符合條件
if (this.isMatch(um, qm, true)) {
retUm = um;
//4:找到一條就停止
break;
}
}
//5:返回查詢(xún)到的數(shù)據(jù)
return retUm;
}
public boolean login(String userId, String pwd) {
boolean flag = false;
//1:先檢查是否是默認(rèn)的admin,如果是,那么就直接登錄
if(userId!=null && userId.equals("admin")){
//1.1檢查admin的記錄是否存在,如果存在就要檢查密碼是否正確
UserModel um = this.getUserModelById(userId);
//1.2判斷用戶(hù)是否存在
if(um!=null && um.getPwd()!=null && um.getPwd().trim().length()>0){
//1.3:如果用戶(hù)存在,還要判斷密碼是否正確
if(um.getPwd().equals(pwd)){
flag = true;
}else{
flag = false;
}
}else{
//1.4否則說(shuō)明admin的記錄不存在,那么是admin就直接登錄
flag = true;
//1.5如果是admin,而且沒(méi)有找到對(duì)應(yīng)的記錄,那么就生成一條,并加入到map中
UserModel tempUm = new UserModel();
tempUm.setId("admin");
tempUm.setName("系統(tǒng)管理員");
tempUm.setPwd("javass");
tempUm.setType(UserModel.TYPE_INT_1);
//直接調(diào)用用戶(hù)注冊(cè)就好了
this.registUser(tempUm);
}
}
//2:按照用戶(hù)編號(hào)去獲取對(duì)應(yīng)的用戶(hù)model
UserModel um = this.getUserModelById(userId);
//3:判斷用戶(hù)是否存在,還有密碼是否正確
if(um!=null && um.getPwd()!=null && um.getPwd().trim().length()>0
&& um.getPwd().equals(pwd)){
//3:如果用戶(hù)存在,還有密碼也正確,那么就能夠登錄,返回true
flag = true;
}
return flag;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -