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

📄 coursestoragemanager.java

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

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.course.Category;
import eols.bean.course.Course;
import eols.bean.course.Chapter;
import eols.bean.course.Content;
import eols.bean.course.Word;
import eols.tools.MessageUtils;
import sim.xml.jdom.Element;
import sim.xml.jdom.Document;

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

    /** The data file */
    private String courseFile = dataPath + "course.xml";

    /** Different constant types */
    private static int ERROR = 0;
    private static int CATEGORIES = -1;
    private static int CATEGORY = 1;
    private static int COURSE = 2;
    private static int CHAPTER = 3;
    private static int CONTENT = 4;
    private static int WORD = 5;

    /**
     * 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 Category && ( (Category) obj).getId() == -1)
            return CATEGORIES;
        if (obj instanceof Category)
            return CATEGORY;
        if (obj instanceof Course)
            return COURSE;
        if (obj instanceof Chapter)
            return CHAPTER;
        if (obj instanceof Content)
            return CONTENT;
        if (obj instanceof Word)
            return WORD;
        return ERROR;
    }

    /**
     * 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) {
        throw new RuntimeException("This method is not supported here.");
    }

    /**
     * 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) {
        throw new RuntimeException("This method is not supported here.");
    }

    /**
     * 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) {
        throw new RuntimeException("This method is not supported here.");
    }

    /**
     * Get the specified 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 retrieved.
     * 					It can an integer, such as "id"; Or a String
     * 					, such as "name"
     * @return Whether the operation is successful
     */
    public Object getRecord(Object indicator) {
        int type = determineType(indicator);
        if (type == CATEGORY) {
            /** Get the category object */
            // Get the document root
            Element root = null;
            try {
                root = XMLUtils.loadFromXML(courseFile);
            }
            catch (Exception e) {
                MessageUtils.debug(e);
                return null;
            }
            // Category object
            Category cat = (Category) 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()) {
                    cat.setName(c.getAttributeValue("name"));
                    cat.setDesc(c.getAttributeValue("desc"));
                    break;
                }
            }
            return cat;
        }
        if (type == COURSE) {
            /** Get the course object */
            // Get the document root
            Element root = null;
            try {
                root = XMLUtils.loadFromXML(courseFile);
            }
            catch (Exception e) {
                MessageUtils.debug(e);
                return null;
            }
            // Course object
            Course course = (Course) 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")) ==
                    course.getCategoryID()) {
                    List children2 = c.getChildren();
                    for (int j = 0; j < children2.size(); j++) {
                        Element cc = (Element) children2.get(j);
                        if (Long.parseLong(cc.getAttributeValue("id")) ==
                            course.getId()) {
                            course.setName(cc.getAttributeValue("name"));
                            course.setDesc(cc.getAttributeValue("desc"));
                            course.setImagePath(cc.getAttributeValue(
                                "imagePath"));
                            break;
                        }
                    }
                }
            }
            return course;
        }
        if (type == CHAPTER) {
            /** Get the chapter object */
            // Get the document root
            Element root = null;
            try {
                root = XMLUtils.loadFromXML(courseFile);
            }
            catch (Exception e) {
                MessageUtils.debug(e);
                return null;
            }
            // Chapter object
            Chapter chapter = (Chapter) 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")) ==
                    chapter.getCategoryID()) {
                    List children2 = c.getChildren();
                    for (int j = 0; j < children2.size(); j++) {
                        Element cc = (Element) children2.get(j);
                        if (Long.parseLong(cc.getAttributeValue("id")) ==
                            chapter.getCourseID()) {
                            List children3 = cc.getChildren();
                            for (int k = 0; k < children3.size(); k++) {
                                Element ccc = (Element) children3.get(k);
                                if (Long.parseLong(ccc.getAttributeValue("id")) ==
                                    chapter.getId()) {
                                    chapter.setName(ccc.getAttributeValue(
                                        "name"));
                                    chapter.setDesc(ccc.getAttributeValue(
                                        "desc"));
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            return chapter;
        }
        if (type == CONTENT) {
            /** Get the content object */
            // Get the document root
            Element root = null;
            try {
                root = XMLUtils.loadFromXML(courseFile);
            }
            catch (Exception e) {
                MessageUtils.debug(e);
                return null;
            }
            // Content object
            Content content = (Content) 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")) ==
                    content.getCategoryID()) {
                    List children2 = c.getChildren();
                    for (int j = 0; j < children2.size(); j++) {
                        Element cc = (Element) children2.get(j);
                        if (Long.parseLong(cc.getAttributeValue("id")) ==
                            content.getCourseID()) {
                            List children3 = cc.getChildren();
                            for (int k = 0; k < children3.size(); k++) {
                                Element ccc = (Element) children3.get(k);
                                if (Long.parseLong(ccc.getAttributeValue("id")) ==
                                    content.getChapterID()) {
                                    List children4 = ccc.getChildren();
                                    for (int l = 0; l < children4.size(); l++) {
                                        Element t = (Element) children4.get(l);
                                        if (Long.parseLong(t.getAttributeValue(
                                            "id")) == content.getId()) {
                                            content.setName(t.getAttributeValue(
                                                "name"));
                                            content.setImagePath(t.
                                                getAttributeValue("imagePath"));
                                            content.setAudioPath(t.
                                                getAttributeValue("audioPath"));
                                            content.setVedioPath(t.
                                                getAttributeValue("vedioPath"));
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return content;
        }
        if (type == WORD) {
            /** Get the word object */
            // Get the document root
            Element root = null;
            try {
                root = XMLUtils.loadFromXML(courseFile);
            }
            catch (Exception e) {
                MessageUtils.debug(e);
                return null;
            }
            // Word object
            Word word = (Word) 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")) ==
                    word.getCategoryID()) {
                    List children2 = c.getChildren();
                    for (int j = 0; j < children2.size(); j++) {
                        Element cc = (Element) children2.get(j);
                        if (Long.parseLong(cc.getAttributeValue("id")) ==
                            word.getCourseID()) {
                            List children3 = cc.getChildren();
                            for (int k = 0; k < children3.size(); k++) {
                                Element ccc = (Element) children3.get(k);
                                if (Long.parseLong(ccc.getAttributeValue("id")) ==
                                    word.getChapterID()) {
                                    List children4 = ccc.getChildren();
                                    for (int l = 0; l < children4.size(); l++) {
                                        Element t = (Element) children4.get(l);
                                        if (Long.parseLong(t.getAttributeValue(
                                            "id")) == word.getContentID()) {
                                            List children5 = t.getChildren();
                                            for (int h = 0; h < children5.size();
                                                 h++) {

⌨️ 快捷键说明

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