⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 teststoragemanager.java

📁 一个在线学习系统的服务端SERVLET程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package eols.storage.test;

import java.util.List;
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.tools.MessageUtils;
import sim.xml.jdom.Element;
import sim.xml.jdom.Document;

/**
 * The storage manager used in the test management subsystem
 *
 * @author Fasheng Qiu
 * @since 11/01/2007
 *
 */
public class TestStorageManager
    extends StorageAdapter {

    /** The data file */
    private String testFile = dataPath + "test.xml";
    private String testResultFile = dataPath + "test-rs.xml";

    /** Different constant types */
    private static int ERROR = 0;
    private static int KNOWLEDGEPOINTS = -1;
    private static int KNOWLEDGEPOINT = 1;
    private static int QUESTION = 2;

    /**
     * Determine the type of the given object
     *
     * @param obj The specified object
     * @return The type of the object
     */
    private static int determineType(Object obj) {
        if (obj instanceof KnowledgePoint &&
            ( (KnowledgePoint) obj).getId() == -1)
            return KNOWLEDGEPOINTS;
        if (obj instanceof KnowledgePoint)
            return KNOWLEDGEPOINT;
        if (obj instanceof Question)
            return QUESTION;
        return ERROR;
    }

    /**
     * Add a list of new records into the external storage file.
     * NOTE that, in the default implementation, whether the operation
     * on a specific record is successful is not checked.
     *
     * @param records The records to add into the external storage
     * @return Whether the operation is successful. True will be returned
     *         if the record list is empty or null.
     */
    public boolean addRecords(List records) {

        if (records == null || records.isEmpty())
            return true;
        // Get the user id
        String loginID = ( (TestResult) records.get(0)).getUserID();
        // Get the document root and the user element
        Element root = null;
        Element userElement = null;
        try {
            root = XMLUtils.loadFromXML(testResultFile);
            List children = root.getChildren();
            for (int i = 0; i < children.size(); i++) {
                Element temp = (Element) children.get(i);
                if (temp.getAttributeValue("id").trim().equals(loginID.trim())) {
                    userElement = temp;
                }
            }
            if (userElement == null) {
                userElement = new Element("user");
                userElement.addAttribute("id", String.valueOf(loginID));
                root.addContent(userElement);
            }
            Element test = new Element("test");
            test.addAttribute("id", ( (TestResult) records.get(0)).getId() + "");
            test.addAttribute("date",
                              ( (TestResult) records.get(0)).getTestDate());
            for (int i = 0; i < records.size(); i++) {
                if (records.get(i) == null)
                    continue;
                TestResult record = (TestResult) records.get(i);
                Element question = new Element("question");
                question.addAttribute("id", record.getQuestionID() + "");
                question.addAttribute("result", record.isCorrect() + "");
                if (!record.isCorrect()) {
                    question.addContent(record.getYourA());
                }
                test.addContent(question);
            }
            userElement.addContent(test);
            XMLUtils.writeToXML(root, testResultFile);
        }
        catch (Exception e) {
            MessageUtils.debug(e);
            return false;
        }

        return true;

    }

    /**
     * Add a new record into the external storage file.
     * For each sub-class, the parameter should be casted
     * into the specific domain objects, such as Category,
     * Course, etc.
     *
     * @param record The record to add into the external storage
     * @return Whether the operation is successful
     */
    public boolean addRecord(Object record) {
        int type = determineType(record);
        if (type == KNOWLEDGEPOINT) {
            /** Add a new knowledge point */
            // Get the document root
            Element root = null;
            try {
                root = XMLUtils.loadFromXML(testFile);
            }
            catch (Exception e) {
                MessageUtils.debug(e);
                root = new Element("questions");
            }
            // Knowledge point
            KnowledgePoint r = (KnowledgePoint) record;
            Element rr = new Element("knowledgepoint");
            rr.addAttribute("id", String.valueOf(r.getId()));
            rr.addAttribute("name", r.getName());
            rr.addAttribute("desc", r.getDescription());
            root.addContent(rr);
            // Output the knowledge point
            try {
                XMLUtils.writeToXML(root, testFile);
            }
            catch (Exception e) {
                MessageUtils.debug(e);
                throw new RuntimeException("Can not save the knowledge point.");
            }
            return true;
        }
        if (type == QUESTION) {
            /** Add a new question */
            // Get the document root
            Element root = null;
            try {
                root = XMLUtils.loadFromXML(testFile);
            }
            catch (Exception e) {
                MessageUtils.debug(e);
                root = new Element("questions");
            }
            // Question
            Question r = (Question) record;
            // Find the actual root
            List children = root.getChildren();
            root = null;
            try {
                for (int i = 0; i < children.size(); i++) {
                    long catID = Long.parseLong( ( (Element) children.get(i))
                                                .getAttributeValue("id"));
                    if (catID == r.getKpID()) {
                        root = (Element) children.get(i);
                        break;
                    }
                }
            }
            catch (Exception e) {
                MessageUtils.debug(e);
                return false;
            }
            if (root == null)
                return false;
            // Create the question element
            Element rr = new Element("question");
            rr.addAttribute("id", String.valueOf(r.getId()));
            rr.addAttribute("answer", r.getAnswer());
            rr.addAttribute("choice", r.getChoice());
            rr.addAttribute("photo", r.getPhoto());
            rr.addAttribute("publishDate", r.getPublishDate());
            rr.addAttribute("question", r.getQuestion());
            rr.addAttribute("tip", r.getTip());
            rr.addAttribute("hardNo", r.getHardNo() + "");
            rr.addAttribute("questionType", r.getQuestionType() + "");
            root.addContent(rr);
            // Output the course
            try {
                XMLUtils.writeToXML(root, testFile);
            }
            catch (Exception e) {
                MessageUtils.debug(e);
                throw new RuntimeException("Can not save the question.");
            }
            return true;
        }
        return false;
    }

    /**
     * Update the existing record of the external storage file.
     * For each sub-class, the parameter should be casted
     * into the specific domain objects, such as Category,
     * Course, etc.
     *
     * @param indicator The identifier of record to be updated.
     * 					It can an integer, such as "id"; Or a String
     * 					, such as "name"
     * @param newRecord The record which contains new information
     * @return Whether the operation is successful
     */
    public boolean updateRecord(Object indicator, Object newRecord) {
        // Since the indicator can use ids, which are included in newRecord,
        // indicator is not used here.
        // Get the document root
        Element root = null;
        try {
            root = XMLUtils.loadFromXML(testFile);
        }
        catch (Exception e) {
            MessageUtils.debug(e);
            return false;
        }
        int type = determineType(newRecord);
        if (type == KNOWLEDGEPOINT) {
            /** Update the knowledge point object */
            // KnowledgePoint object
            KnowledgePoint cat = (KnowledgePoint) newRecord;
            // Visit children the root
            List children = root.getChildren();
            for (int i = 0; i < children.size(); i++) {
                Element c = (Element) children.get(i);
                if (Long.parseLong(c.getAttributeValue("id")) == cat.getId()) {
                    c.setAttribute("name", cat.getName());
                    c.setAttribute("desc", cat.getDescription());
                    break;
                }
            }
            // Write to external file
        }
        if (type == QUESTION) {
            /** Get the question object */
            // Question object
            Question r = (Question) newRecord;
            // Visit children the root
            List children = root.getChildren();
            for (int i = 0; i < children.size(); i++) {
                Element c = (Element) children.get(i);
                if (Long.parseLong(c.getAttributeValue("id")) == r.getKpID()) {
                    List children2 = c.getChildren();
                    for (int j = 0; j < children2.size(); j++) {
                        Element cc = (Element) children2.get(j);
                        if (Long.parseLong(cc.getAttributeValue("id")) ==
                            r.getId()) {
                            cc.setAttribute("answer", r.getAnswer());
                            cc.setAttribute("choice", r.getChoice());
                            cc.setAttribute("photo", r.getPhoto());
                            cc.setAttribute("question", r.getQuestion());
                            cc.setAttribute("tip", r.getTip());
                            cc.setAttribute("hardNo", r.getHardNo() + "");
                            cc.setAttribute("questionType",
                                            r.getQuestionType() + "");
                            break;
                        }
                    }
                }
            }
        }
        // Output the content
        try {
            XMLUtils.writeToXML(root, testFile);
        }
        catch (Exception e) {
            MessageUtils.debug(e);
            throw new RuntimeException("Can not save the modification.");
        }
        return true;
    }

    /**
     * Delete the existing record of the external storage file.
     * For each sub-class, the parameter should be casted
     * into the specific domain objects, such as Category,
     * Course, etc.
     *
     * @param indicator The identifier of the record to be deleted.
     * 					It can an integer, such as "id"; Or a String
     * 					, such as "name"
     * @return Whether the operation is successful
     */
    public boolean deleteRecord(Object indicator) {
        Element root = null;
        try {
            root = XMLUtils.loadFromXML(testFile);
        }
        catch (Exception e) {
            MessageUtils.debug(e);
            return false;
        }
        int type = determineType(indicator);
        if (type == KNOWLEDGEPOINTS) {
            root.removeChildren();
        }
        if (type == KNOWLEDGEPOINT) {
            /** Remove the knowledge point object */
            KnowledgePoint cat = (KnowledgePoint) indicator;
            // Visit children the root
            List children = root.getChildren();
            for (int i = 0; i < children.size(); i++) {
                Element c = (Element) children.get(i);
                if (Long.parseLong(c.getAttributeValue("id")) == cat.getId()) {
                    children.remove(i);
                    break;
                }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -