📄 teststoragemanager.java
字号:
}
// Write to external file
}
if (type == QUESTION) {
/** Get the question object */
Question question = (Question) 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")) ==
question.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")) ==
question.getId()) {
children2.remove(j);
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;
}
/**
* 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) {
if (indicator != null && indicator instanceof Map) {
return _getQuestionsOfTest(indicator);
}
int type = determineType(indicator);
if (type == KNOWLEDGEPOINT) {
/** Get the knowledge point object */
// Get the document root
Element root = null;
try {
root = XMLUtils.loadFromXML(testFile);
}
catch (Exception e) {
MessageUtils.debug(e);
return null;
}
// KnowledgePoint 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()) {
cat.setName(c.getAttributeValue("name"));
cat.setDescription(c.getAttributeValue("desc"));
break;
}
}
return cat;
}
if (type == QUESTION) {
/** Get the question object */
// Get the document root
Element root = null;
try {
root = XMLUtils.loadFromXML(testFile);
}
catch (Exception e) {
MessageUtils.debug(e);
return null;
}
// Question object
Question c = (Question) indicator;
// Visit children the root
List children = root.getChildren();
for (int i = 0; i < children.size(); i++) {
Element cc = (Element) children.get(i);
if (Long.parseLong(cc.getAttributeValue("id")) == c.getKpID()) {
List children2 = cc.getChildren();
for (int j = 0; j < children2.size(); j++) {
Element question = (Element) children2.get(j);
if (Long.parseLong(question.getAttributeValue("id")) ==
c.getId()) {
c.setAnswer(question.getAttributeValue("answer"));
c.setChoice(question.getAttributeValue("choice"));
c.setHardNo(Integer.parseInt(question.
getAttributeValue("hardNo")));
c.setPhoto(question.getAttributeValue("photo"));
c.setPublishDate(question.getAttributeValue(
"publichDate"));
c.setQuestion(question.getAttributeValue("question"));
c.setTip(question.getAttributeValue("tip"));
c.setQuestionType(Integer.parseInt(question.
getAttributeValue("questionType")));
break;
}
}
}
}
return c;
}
return null;
}
/**
* Get all specified tests of the external storage file.
* For each sub-class, the parameter should be casted
* into the specific domain objects, such as Question,
* KnowledgePoint, etc.
*
* <p>
* The returned list will have the following structure:
* knowledgepoint1, knowledgepoint2, ..., childrenMap
* |
* question1, question2, ..., question-n
*
* </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) {
if (indicator != null && indicator instanceof Map) {
return _getTestsOfUser(indicator);
}
if (indicator != null && indicator instanceof List) {
return _getQuestionsOfTestResult(indicator);
}
// The returned list
List ret = new ArrayList();
// Get the document root
Element root = null;
try {
root = XMLUtils.loadFromXML(testFile);
}
catch (Exception e) {
MessageUtils.debug(e);
throw new RuntimeException("Can not open the test data file.");
}
// Handle the document
List kps = root.getChildren();
Map childrenOfKP = new HashMap();
int questionIndex = 0;
for (int i = 0; i < kps.size(); i++) {
Element element = (Element) kps.get(i);
String name = element.getName().trim();
if (!name.equals("knowledgepoint"))
continue;
// Read the knowledge point information and add it into the list
KnowledgePoint kp = new KnowledgePoint();
kp.setId(Long.parseLong(element.getAttributeValue("id")));
kp.setName(element.getAttributeValue("name"));
kp.setDescription(element.getAttributeValue("desc"));
ret.add(kp);
// Read the children of the knowledge point
List questions = element.getChildren();
for (int j = 0; j < questions.size(); j++) {
Element question = (Element) questions.get(j);
String cName = question.getName().trim();
if (!cName.equals("question"))
continue;
// Read the question information and put it into the map
Question c = new Question();
c.setKpID(kp.getId());
c.setId(Long.parseLong(question.getAttributeValue("id")));
c.setAnswer(question.getAttributeValue("answer"));
c.setChoice(question.getAttributeValue("choice"));
c.setHardNo(Integer.parseInt(question.getAttributeValue(
"hardNo")));
c.setPhoto(question.getAttributeValue("photo"));
c.setPublishDate(question.getAttributeValue("publichDate"));
c.setQuestion(question.getAttributeValue("question"));
c.setTip(question.getAttributeValue("tip"));
c.setQuestionType(Integer.parseInt(question.getAttributeValue(
"questionType")));
childrenOfKP.put("question" + (questionIndex++), c);
}
}
ret.add(childrenOfKP);
return ret;
}
private List _getTestsOfUser(Object indicator) {
String loginID = (String) ( (Map) indicator).get("loginID");
// Result
List results = new ArrayList();
// 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;
List children2 = userElement.getChildren();
for (int j = 0; j < children2.size(); j++) {
Element test = (Element) children2.get(j);
TestResult r = new TestResult();
r.setId(Long.parseLong(test.getAttributeValue("id")));
r.setTestDate(test.getAttributeValue("date"));
results.add(r);
}
}
}
}
catch (Exception e) {
MessageUtils.debug(e);
return null;
}
return results;
}
private List _getQuestionsOfTest(Object indicator) {
String loginID = (String) ( (Map) indicator).get("loginID");
String testID = (String) ( (Map) indicator).get("testID");
// Result
List results = new ArrayList();
// 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;
List children2 = userElement.getChildren();
for (int j = 0; j < children2.size(); j++) {
Element test = (Element) children2.get(j);
if (test.getAttributeValue("id").trim().equals(testID.
trim())) {
List children3 = test.getChildren();
for (int k = 0; k < children3.size(); k++) {
Element question = (Element) children3.get(k);
TestResult r = new TestResult();
r.setQuestionID(Long.parseLong(question.
getAttributeValue("id")));
r.setCorrect(new Boolean(question.
getAttributeValue(
"result")).booleanValue());
r.setYourA(question.getText());
results.add(r);
}
}
}
}
}
}
catch (Exception e) {
MessageUtils.debug(e);
return null;
}
return results;
}
private List _getQuestionsOfTestResult(Object indicator) {
List testResults = (List) indicator;
// Get the document root
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 k = 0; k < testResults.size(); k++) {
for (int i = 0; i < children.size(); i++) {
Element cc = (Element) children.get(i);
List children2 = cc.getChildren();
for (int j = 0; j < children2.size(); j++) {
Element question = (Element) children2.get(j);
if (Long.parseLong(question.getAttributeValue("id")) ==
( (TestResult) testResults.get(k)).getQuestionID()) {
Question c = new Question();
c.setAnswer(question.getAttributeValue("answer"));
c.setChoice(question.getAttributeValue("choice"));
c.setHardNo(Integer.parseInt(question.
getAttributeValue("hardNo")));
c.setPhoto(question.getAttributeValue("photo"));
c.setPublishDate(question.getAttributeValue(
"publichDate"));
c.setQuestion(question.getAttributeValue("question"));
c.setTip(question.getAttributeValue("tip"));
c.setQuestionType(Integer.parseInt(question.
getAttributeValue("questionType")));
( (TestResult) testResults.get(k)).setQuestion(c);
break;
}
}
}
}
return testResults;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -