📄 coursestoragemanager.java
字号:
Element g = (Element) children5.
get(h);
if (Long.parseLong(g.
getAttributeValue("id")) ==
word.getId()) {
word.setName(g.
getAttributeValue(
"name"));
word.setFile(g.
getAttributeValue(
"file"));
word.setMeaning(g.
getAttributeValue(
"meaning"));
word.setType(g.
getAttributeValue(
"type"));
word.setDesc(g.
getAttributeValue(
"desc"));
break;
}
}
}
}
}
}
}
}
}
}
return word;
}
return null;
}
/**
* Get all specified courses of the external storage file.
* For each sub-class, the parameter should be casted
* into the specific domain objects, such as Category,
* Course, etc.
*
* <p>
* The returned list will have the following structure:
* category1, category2, ..., childrenMap
* |
* course1, course2, ..., childrenMap
* |
* ......
* </p>
*
* <p>
* The parameter is not used in this method.
* </p>
*
* @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 List getRecords(Object indicator) {
// The returned list
List ret = new ArrayList();
// Get the document root
Element root = null;
try {
root = XMLUtils.loadFromXML(courseFile);
}
catch (Exception e) {
MessageUtils.debug(e);
throw new RuntimeException("Can not open course data file.");
}
// Handle the document
int type = this.determineType(indicator);
if (type == this.CATEGORIES) {
Category all = (Category)indicator;
if (all.getKeyword() != null &&
!all.getKeyword().trim().equals("")) {
return _getCategoriesAndCourses(root, all.getKeyword());
}
List categories = root.getChildren();
Map childrenOfCategories = new HashMap();
int courseIndex = 0;
for (int i = 0; i < categories.size(); i++) {
Element element = (Element) categories.get(i);
String name = element.getName().trim();
if (!name.equals("category"))
continue;
// Read the category information and add it into the list
Category category = new Category();
category.setId(Long.parseLong(element.getAttributeValue("id")));
category.setName(element.getAttributeValue("name"));
category.setDesc(element.getAttributeValue("desc"));
ret.add(category);
// Read the children of the category
List courses = element.getChildren();
for (int j = 0; j < courses.size(); j++) {
Element course = (Element) courses.get(j);
String cName = course.getName().trim();
if (!cName.equals("course"))
continue;
// Read the course information and put it into the map
Course c = new Course();
c.setCategoryID(category.getId());
c.setId(Long.parseLong(course.getAttributeValue("id")));
c.setName(course.getAttributeValue("name"));
c.setDesc(course.getAttributeValue("desc"));
c.setImagePath(course.getAttributeValue("imagePath"));
childrenOfCategories.put("course" + (courseIndex++), c);
}
}
ret.add(childrenOfCategories);
}
if (type == this.CHAPTER) {
Chapter c = (Chapter) indicator;
List categories = root.getChildren();
for (int i = 0; i < categories.size(); i++) {
Element element = (Element) categories.get(i);
String name = element.getName().trim();
if (!name.equals("category"))
continue;
if (Long.parseLong(element.getAttributeValue("id")) !=
c.getCategoryID())
continue;
// Read the children of the category
List courses = element.getChildren();
for (int j = 0; j < courses.size(); j++) {
Element course = (Element) courses.get(j);
String cName = course.getName().trim();
if (!cName.equals("course"))
continue;
if (Long.parseLong(course.getAttributeValue("id")) !=
c.getCourseID())
continue;
List chapters = course.getChildren();
for (int k = 0; k < chapters.size(); k++) {
Element e = (Element) chapters.get(k);
Chapter chapter = new Chapter();
chapter.setCategoryID(c.getCategoryID());
chapter.setCourseID(c.getCourseID());
chapter.setId(Long.parseLong(e.getAttributeValue("id")));
chapter.setDesc(e.getAttributeValue("desc"));
chapter.setName(e.getAttributeValue("name"));
ret.add(chapter);
}
}
}
}
if (type == this.CONTENT) {
Content c = (Content) indicator;
List categories = root.getChildren();
for (int i = 0; i < categories.size(); i++) {
Element element = (Element) categories.get(i);
String name = element.getName().trim();
if (!name.equals("category"))
continue;
if (Long.parseLong(element.getAttributeValue("id")) !=
c.getCategoryID())
continue;
// Read the children of the category
List courses = element.getChildren();
for (int j = 0; j < courses.size(); j++) {
Element course = (Element) courses.get(j);
String cName = course.getName().trim();
if (!cName.equals("course"))
continue;
if (Long.parseLong(course.getAttributeValue("id")) !=
c.getCourseID())
continue;
List chapters = course.getChildren();
for (int k = 0; k < chapters.size(); k++) {
Element e = (Element) chapters.get(k);
cName = e.getName().trim();
if (!cName.equals("chapter"))
continue;
if (Long.parseLong(e.getAttributeValue("id")) !=
c.getChapterID())
continue;
List contents = e.getChildren();
for (int l = 0; l < contents.size(); l++) {
Element ee = (Element) contents.get(l);
cName = ee.getName().trim();
if (!cName.equals("content"))
continue;
Content content = new Content();
content.setCategoryID(c.getCategoryID());
content.setCourseID(c.getCourseID());
content.setChapterID(c.getChapterID());
content.setId(Long.parseLong(ee.getAttributeValue(
"id")));
content.setAudioPath(ee.getAttributeValue(
"audioPath"));
content.setImagePath(ee.getAttributeValue(
"imagePath"));
content.setVedioPath(ee.getAttributeValue(
"vedioPath"));
content.setName(ee.getAttributeValue("name"));
ret.add(content);
}
}
}
}
}
if (type == this.WORD) {
Word word = (Word) indicator;
List categories = root.getChildren();
for (int i = 0; i < categories.size(); i++) {
Element element = (Element) categories.get(i);
String name = element.getName().trim();
if (!name.equals("category"))
continue;
if (Long.parseLong(element.getAttributeValue("id")) !=
word.getCategoryID())
continue;
// Read the children of the category
List courses = element.getChildren();
for (int j = 0; j < courses.size(); j++) {
Element course = (Element) courses.get(j);
String cName = course.getName().trim();
if (!cName.equals("course"))
continue;
if (Long.parseLong(course.getAttributeValue("id")) !=
word.getCourseID())
continue;
List chapters = course.getChildren();
for (int k = 0; k < chapters.size(); k++) {
Element e = (Element) chapters.get(k);
cName = e.getName().trim();
if (!cName.equals("chapter"))
continue;
if (Long.parseLong(e.getAttributeValue("id")) !=
word.getChapterID())
continue;
List contents = e.getChildren();
for (int l = 0; l < contents.size(); l++) {
Element ee = (Element) contents.get(l);
cName = ee.getName().trim();
if (!cName.equals("content"))
continue;
if (Long.parseLong(ee.getAttributeValue("id")) !=
word.getContentID())
continue;
List ws = ee.getChildren();
for (int m = 0; m < ws.size(); m++) {
Element eee = (Element) ws.get(m);
cName = eee.getName().trim();
if (!cName.equals("word"))
continue;
Word w = new Word();
w.setDesc(eee.getAttributeValue("desc"));
w.setFile(eee.getAttributeValue("file"));
w.setMeaning(eee.getAttributeValue("meaning"));
w.setName(eee.getAttributeValue("name"));
w.setType(eee.getAttributeValue("type"));
ret.add(w);
}
}
}
}
}
}
return ret;
}
private List _getCategoriesAndCourses(Element root, String keyword) {
keyword = keyword.trim();
List ret = new ArrayList();
List categories = root.getChildren();
Map childrenOfCategories = new HashMap();
int courseIndex = 0;
for (int i = 0; i < categories.size(); i++) {
Element element = (Element) categories.get(i);
String name = element.getName().trim();
if (!name.equals("category"))
continue;
boolean exist = false;
// Read the children of the category
List courses = element.getChildren();
for (int j = 0; j < courses.size(); j++) {
Element course = (Element) courses.get(j);
String cName = course.getName().trim();
if (!cName.equals("course"))
continue;
String nameC = course.getAttributeValue("name");
String descC = course.getAttributeValue("desc");
if (nameC.toLowerCase().indexOf(keyword.toLowerCase()) != -1 ||
descC.toLowerCase().indexOf(keyword.toLowerCase()) != -1) {
// Read the course information and put it into the map
Course c = new Course();
c.setCategoryID(Long.parseLong(element.getAttributeValue("id")));
c.setId(Long.parseLong(course.getAttributeValue("id")));
c.setName(course.getAttributeValue("name"));
c.setDesc(course.getAttributeValue("desc"));
c.setImagePath(course.getAttributeValue("imagePath"));
childrenOfCategories.put("course" + (courseIndex++), c);
exist = true;
}
}
if (exist) {
// Read the category information and add it into the list
Category category = new Category();
category.setId(Long.parseLong(element.getAttributeValue("id")));
category.setName(element.getAttributeValue("name"));
category.setDesc(element.getAttributeValue("desc"));
ret.add(category);
}
}
ret.add(childrenOfCategories);
return ret;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -