📄 userstoragemanager.java
字号:
package eols.storage.user;
import java.util.*;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import eols.storage.base.StorageAdapter;
import eols.tools.XMLUtils;
import eols.bean.test.KnowledgePoint;
import eols.bean.test.Question;
import eols.bean.test.QType;
import eols.bean.test.TestResult;
import eols.bean.user.UserData;
import eols.tools.MessageUtils;
import sim.xml.jdom.Element;
import sim.xml.jdom.Document;
/**
* The storage manager used in the user management subsystem
*
* @author Fasheng Qiu
* @since 11/01/2007
*
*/
public abstract class UserStorageManager
extends StorageAdapter {
/** The data file */
private static String testFile = dataPath + "user.xml";
public static boolean validate(String loginID, String pass1) {
// Get the document root
Element root = null;
try {
root = XMLUtils.loadFromXML(testFile);
}
catch (Exception e) {
MessageUtils.debug(e);
return false;
}
boolean pass = false;
// Visit children the root
List children = root.getChildren();
for (int i = 0; i < children.size(); i++) {
Element c = (Element) children.get(i);
String id = c.getAttributeValue("Id").trim();
String pass2 = c.getAttributeValue("Passwd").trim();
if (id.equals(loginID) && pass2.equals(pass1))
pass = true;
}
return pass;
}
public static boolean addUser(Object record) {
// TODO Get the document root
Element root = null;
try {
root = XMLUtils.loadFromXML(testFile);
}
catch (Exception e) {
MessageUtils.debug(e);
root = new Element("user");
}
// user
UserData r = (UserData) record;
Element user = new Element("user");
user.addAttribute("Fname", r.Fname);
user.addAttribute("Lname", r.Lname);
user.addAttribute("Id", String.valueOf(r.Id).trim());
user.addAttribute("Passwd", r.Passwd);
user.addAttribute("Accesstype", r.AccessType);
user.addAttribute("Dbirth", r.Dbirth);
user.addAttribute("Address", r.Address);
user.addAttribute("City", r.City);
user.addAttribute("State", r.State);
user.addAttribute("Zipcode", r.Zipcode);
user.addAttribute("Phone", r.Phone);
user.addAttribute("Email", r.Email);
user.addAttribute("Booklist", r.bookList);
root.addContent(user);
// write the user data to the file
try {
XMLUtils.writeToXML(root, testFile);
}
catch (Exception e) {
MessageUtils.debug(e);
throw new RuntimeException("Can not save the user.");
}
return true;
}
public static boolean addToBookShelf(String loginID, String courseID) {
Element root = null;
try {
root = XMLUtils.loadFromXML(testFile);
}
catch (Exception e) {
MessageUtils.debug(e);
return false;
}
// Visit children the root
List children = root.getChildren();
for (int i = 0; i < children.size(); i++) {
Element c = (Element) children.get(i);
if (c.getAttributeValue("Id").trim().equals(loginID.trim())) {
// find the user whose id is user.id
String booklist = c.getAttributeValue("Booklist");
StringTokenizer st = new StringTokenizer(booklist, ",");
Set all = new HashSet();
while(st.hasMoreTokens()) {
all.add(st.nextToken());
}
all.add(courseID);
StringBuffer sb = new StringBuffer();
Iterator iter = all.iterator();
while(iter.hasNext()) {
sb.append(iter.next()).append(",");
}
booklist = sb.toString();
if (booklist != null && !booklist.trim().equals("")) {
booklist = booklist.substring(0, booklist.lastIndexOf(','));
}
c.setAttribute("Booklist", booklist);
break;
}
}
// write the user data to the file
try {
XMLUtils.writeToXML(root, testFile);
}
catch (Exception e) {
MessageUtils.debug(e);
throw new RuntimeException("Can not save the user.");
}
return true;
}
public static boolean updatePass(String loginID, String oldPass, String newPass) {
Element root = null;
try {
root = XMLUtils.loadFromXML(testFile);
}
catch (Exception e) {
MessageUtils.debug(e);
return false;
}
// Visit children the root
List children = root.getChildren();
for (int i = 0; i < children.size(); i++) {
Element c = (Element) children.get(i);
if (c.getAttributeValue("Id").trim().equals(loginID.trim()) &&
c.getAttributeValue("Passwd").trim().equals(oldPass.trim())) {
c.setAttribute("Passwd", newPass);
break;
}
}
// write the user data to the file
try {
XMLUtils.writeToXML(root, testFile);
}
catch (Exception e) {
MessageUtils.debug(e);
throw new RuntimeException("Can not save the password.");
}
return true;
}
public static boolean removeFromBookShelf(String loginID, String courseID) {
Element root = null;
try {
root = XMLUtils.loadFromXML(testFile);
}
catch (Exception e) {
MessageUtils.debug(e);
return false;
}
// Visit children the root
List children = root.getChildren();
for (int i = 0; i < children.size(); i++) {
Element c = (Element) children.get(i);
if (c.getAttributeValue("Id").trim().equals(loginID.trim())) {
// find the user whose id is user.id
String booklist = c.getAttributeValue("Booklist");
StringTokenizer st = new StringTokenizer(booklist, ",");
Set all = new HashSet();
while(st.hasMoreTokens()) {
all.add(st.nextToken());
}
all.remove(courseID);
StringBuffer sb = new StringBuffer();
Iterator iter = all.iterator();
while(iter.hasNext()) {
sb.append(iter.next()).append(",");
}
booklist = sb.toString();
if (booklist != null && !booklist.trim().equals("")) {
booklist = booklist.substring(0, booklist.lastIndexOf(','));
}
c.setAttribute("Booklist", booklist);
break;
}
}
// write the user data to the file
try {
XMLUtils.writeToXML(root, testFile);
}
catch (Exception e) {
MessageUtils.debug(e);
throw new RuntimeException("Can not save the user.");
}
return true;
}
public static String getBookShelfList(String loginID) {
Element root = null;
try {
root = XMLUtils.loadFromXML(testFile);
}
catch (Exception e) {
MessageUtils.debug(e);
return null;
}
// Visit children the root
List children = root.getChildren();
for (int i = 0; i < children.size(); i++) {
Element c = (Element) children.get(i);
if (c.getAttributeValue("Id").trim().equals(loginID.trim())) {
// find the user whose id is user.id
String booklist = c.getAttributeValue("Booklist");
return booklist;
}
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -