📄 questionbean.java
字号:
/* * SchoolEJB - CyberDemia's library of EJBs for educational related services. * Copyright (C) 2003 CyberDemia Research and Services * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * * See the COPYING file located in the top-level-directory of * the archive of this library for complete text of license. */package com.cyberdemia.school.impl;import javax.ejb.*;import com.cyberdemia.util.UniqueIdGenerator;import com.cyberdemia.school.*;import java.util.*;/*** QuestionBean is the EJB implementation of a multiple-choice question. * It is a AbstractTextResource with additional properties* such as answer, hint, etc. to model a multiple-choice * question. It can be persisted to database.** @author Alexander Yap*/public abstract class QuestionBean extends AbstractTextResource{ /** * Gets the answer for this Question. * The answer is an Integer index that refers to one of the * answer choices. * This is a CMP field. * @return Answer for this Question. */ public abstract Integer getAnswer(); /** * Sets the answer for this Question. * The answer is an Integer index that refers to one of the * answer choices. * This is a CMP field. * @param ans Answer for this Question. */ public abstract void setAnswer( Integer answer); /** * Sets the Set of answer choices. * It is a CMR field. * @param choices Set of answer choices. */ public abstract void setAnswerChoiceSet( Set choices); /** * Gets the Set of answer choices. * It is a CMR field. * @return Set of answer choices. */ public abstract Set getAnswerChoiceSet(); /** * Gets the hint for this question. * This is a CMP field. * @return Hint for this question. */ public abstract String getHint(); /** * Sets the hint for this question. * This is a CMP field. * @param hint Hint for this question. */ public abstract void setHint( String hint); /** * Gets the explanation for this question. * This is a CMP field. * @return Explanation for this question. */ public abstract String getExplanation(); /** * Sets the explanation for this question. * This is a CMP field. * @param explain Explanation for this question. */ public abstract void setExplanation( String explain); /** * Sets the difficulty code of this question. * This method is equivalent to setDifficulty(DifficultyEnum), * except that difficulty is specified using its code instead * of DifficultyEnum. * This is a CMP field. * @param diffCode Difficulty code of this question. * @see #setDifficulty(DifficultyEnum) */ public abstract void setDifficultyCode( Integer diffCode); /** * Gets the difficulty code of this question. * This method is equivalent to getDifficulty(), * except that difficulty is identified by its code instead * of DifficultyEnum. * This is a CMP field. * @return Difficulty code of this question. * @see #getDifficulty() */ public abstract Integer getDifficultyCode(); /** * Gets the score for this question. * This score is awarded to the user for a correct answer. * This is a CMP field. * @return Score for this question. */ public abstract int getScore(); /** * Sets the score for this question. * This score is awarded to the user for a correct answer. * This is a CMP field. * @param score Score for this question. */ public abstract void setScore(int score); /** * Gets the ID of the test that this question is currently assigned to. * This is a CMP field. * @return Test ID, or empty String if this question not assigned to any test. */ public abstract String getTestId(); /** * Sets the test ID for this question. * This is a CMP field. * @param testId Test ID. */ public abstract void setTestId(String testId); /** * Sets the allocated time limit (in seconds) for this question. * This is a CMP field. * @param limit Time limit in seconds, or 0 (or negative) for no time limit. */ public abstract void setTimeLimitSeconds(int limit); /** * Gets the allocated time limit (in seconds) for this question. * This is a CMP field. * @return Time limit in seconds, or 0 (or negative) for no time limit. */ public abstract int getTimeLimitSeconds(); /** * Checks if this question is active. An active question * may be allocated to a test, editted and used. * An inactive question may be considered to be logically removed * from the system, but is kept in the database to maintain * consistency in reporting and historical logging. * This is a CMP field. * @return true if this question is active, false if inactive. */ public abstract boolean getActive(); /** * Sets whether this question is active. An active question * may be allocated to a test, editted and used. * An inactive question may be considered to be logically removed * from the system, but is kept in the database to maintain * consistency in reporting and historical logging. * This is a CMP field. * @param active true to make this question active, false to make it inactive. */ public abstract void setActive(boolean active); /** * Checks if this question is active. An active question * may be allocated to a test, editted and used. * An inactive question may be considered to be logically removed * from the system, but is kept in the database to maintain * consistency in reporting and historical logging. * @return true if this question is active, false if inactive. */ public boolean isActive() { return getActive(); } /** * Actives this question, so that it may be editted and used. * This method may perform some checks first to ensure that this question * can be safely activated. * @return true if this question is activated, false if it is not (either a problem or it is already active) */ public boolean activate() { // Can always activate as long as it is currently inactive. if (!isActive()) { setActive(true); return true; } return false; } /** * Deactives this question, after which it may be considered to be logically removed * from the system. * This method may perform some checks first to ensure that this question * can be safely deactivated. * @return true if this question is deactivated, false if it is not (either a problem or it is already inactive) */ public boolean deactivate() { // Can only deactivate if it is currently active and not assigned to any Test. if (isActive() && "".equals(getTestId()) ) { setActive(false); return true; } return false; } /** * Gets the answer choices as array of LocalAnswerChoice instances. * The answer refers to the index to this array. * @return Array of answer choices. */ public LocalAnswerChoice[] getAnswerChoices() { Set answerChoiceSet = getAnswerChoiceSet(); LocalAnswerChoice[] answerChoiceArr = new LocalAnswerChoice[ answerChoiceSet.size()]; for (int c=0; c<answerChoiceArr.length; c++) { // initalize all elements to null for safety. answerChoiceArr[c] = null; } Iterator answerChoiceIter = answerChoiceSet.iterator(); while (answerChoiceIter.hasNext()) { LocalAnswerChoice choice = (LocalAnswerChoice)answerChoiceIter.next(); int index = choice.getAnswerIndex(); if (index < answerChoiceArr.length) { answerChoiceArr[ index ] = choice; } } return answerChoiceArr; } /** * Adds an answer choice. * @param choice Answer choice to add. */ public void addAnswerChoice(LocalAnswerChoice choice) { if (isActive()) { Set answerChoiceSet = getAnswerChoiceSet(); answerChoiceSet.add(choice); } } /** * Sets the text of an existing answer choice. * @param index Index of answer choice to set. * @param text Text to set. */ public void setAnswerChoiceText(int index, String text) { if (isActive()) { Set answerChoiceSet = getAnswerChoiceSet(); // Find the answer choice with the specified index. Iterator choiceIter = answerChoiceSet.iterator(); while (choiceIter.hasNext()) { LocalAnswerChoice choice = (LocalAnswerChoice)choiceIter.next(); if (choice.getAnswerIndex()==index) { choice.setAnswerText(text); break; } } } } /** * Checks the specified response to determine if it * matches the answer for this Question. * @param response The response to check. * @return true if response matches the answer, otherwise false. */ public boolean checkResponse( Integer response) { return response.equals(getAnswer()); } /** * Sets the difficulty of this Question. * @param diff Difficulty of this Question. */ public void setDifficulty( DifficultyEnum diff) { setDifficultyCode(diff.getCode()); } /** * Gets the difficulty of this Question. * @return Difficulty of this Question. */ public DifficultyEnum getDifficulty() { return DifficultyEnum.getEnumFromCode(getDifficultyCode()); } //----------------------------- // Create methods. //----------------------------- public Object ejbCreate( String name, String ques, Integer answer, String hint, DifficultyEnum difficulty, String explain, int timeLimitSecs ) throws CreateException { setId( UniqueIdGenerator.getInstance().createId(name) ); setName(name); setContent(ques); setAnswer(answer); setHint(hint); setDifficultyCode(difficulty.getCode()); // Just use the diffilcuty code as the score for this Question. setScore(difficulty.getCode().intValue()); setExplanation( explain); setTestId(""); setTimeLimitSeconds(timeLimitSecs); setActive(true); long nowMillis = System.currentTimeMillis(); setCreatedMillis( nowMillis ); setLastModifiedMillis(nowMillis); return null; } public void ejbPostCreate( String name, String ques, Integer answer, String hint, DifficultyEnum difficulty, String explain, int timeLimitSecs ) { } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -