📄 questionmanagerbean.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 javax.naming.*;import java.util.*;import com.cyberdemia.school.*;import java.util.logging.*;/*** QuestionManagerBean is a session bean implementation* of QuestionManager.** @author Alexander Yap*/public class QuestionManagerBean implements SessionBean{ /** * * Creates an instance of QuestionManagerBean. * */ public QuestionManagerBean() { try { Context ctx = new InitialContext(); m_quesHome = (LocalQuestionHome)ctx.lookup("java:comp/env/ejb/localQuestion"); m_answerChoiceHome = (LocalAnswerChoiceHome)ctx.lookup("java:comp/env/ejb/localAnswerChoice"); } catch (NamingException nex) { throw SchoolUtils.convertToEJBException(nex); } } /** * Adds a new question to the database. * @param data QuestionData instance with the data for the new question. * @return The unique ID of the new question. * @throws EJBException if there is a problem creating the question. */ public String addQuestion(QuestionData data) { String id = null; try { LocalQuestion ques = m_quesHome.create( data.name, data.question, data.answer, data.hint, data.difficulty, data.explanation, data.timeLimitSeconds ); for (int c=0; c<data.answerChoices.length; c++) { ques.addAnswerChoice( m_answerChoiceHome.create(ques.getId()+"_"+c, c, data.answerChoices[c]) ); } id = ques.getId(); } catch (CreateException cex) { s_logger.log( Level.SEVERE, "Error creating Question "+data.name, cex); throw SchoolUtils.convertToEJBException(cex); } return id; } /** * Deactives the specified question, after which it may be considered to * be logically removed from the system. * This method may perform some checks first to ensure that the question * can be safely deactivated. * * @param id Question ID * @return true if the question is deactivated, false if it is not (either a problem or it is already inactive) * @throws EJBException if the question can't be found. */ public boolean deactivateQuestion(String id) { boolean success = false; try { LocalQuestion ques = m_quesHome.findByPrimaryKey(id); ques.setLastModifiedMillis( System.currentTimeMillis() ); success = ques.deactivate(); } catch (FinderException fex) { throw SchoolUtils.convertToEJBException(fex); } return success; } /** * Gets list of questions from database. * @param activeOnly true to only return active questions, false to return all questions. * @return Array of QuestionData instances. */ public QuestionData[] getQuestions(boolean activeOnly) { QuestionData[] dataArr = null; try { Iterator quesIter = activeOnly ? m_quesHome.findAllActive().iterator() : m_quesHome.findAll().iterator(); ArrayList dataList = new ArrayList(); while (quesIter.hasNext()) { LocalQuestion ques = (LocalQuestion)quesIter.next(); dataList.add( getDataFromQuestion(ques) ); } dataArr = (QuestionData[])dataList.toArray(new QuestionData[0]); } catch (FinderException fex) { throw SchoolUtils.convertToEJBException(fex); } return dataArr; } /** * Gets Ids of questions that belong to specified test. * If testId is null or an empty String, this methods gets all questions that * are not assigned to any test. * @param testId Test Id * @return Array of QuestionData instances. */ public QuestionData[] getQuestionsByTest(String testId) { QuestionData[] dataArr = null; try { Collection quesCol = m_quesHome.findByTest( ((testId!=null) ? testId :"") ); s_logger.fine("Retrieved "+quesCol.size()+" LocalQuestions."); Iterator quesIter = quesCol.iterator(); ArrayList dataList = new ArrayList(); while (quesIter.hasNext()) { LocalQuestion ques = (LocalQuestion)quesIter.next(); dataList.add( getDataFromQuestion(ques) ); } dataArr = (QuestionData[])dataList.toArray(new QuestionData[0]); } catch (FinderException fex) { throw SchoolUtils.convertToEJBException(fex); } return dataArr; } /** * Gets the data of a specified question. * @param id Question ID. * @return QuestionData instance storing the question's data. * @throws EJBException if the question can't be found. */ public QuestionData getQuestionData(String id) { QuestionData data = null; try { LocalQuestion ques = m_quesHome.findByPrimaryKey(id); data = getDataFromQuestion(ques); } catch (FinderException fex) { s_logger.log( Level.SEVERE, "Error finding Question ID="+id, fex); throw SchoolUtils.convertToEJBException(fex); } return data; } /** * Sets the data of a specified question. * @param id Question ID. * @param data QuestionData instance with the question's data. * @throws EJBException if the question can't be found. */ public void setQuestionData(String id, QuestionData data) { try { LocalQuestion ques = m_quesHome.findByPrimaryKey(id); ques.setName(data.name); ques.setContent( data.question ); ques.setAnswer(data.answer); ques.setHint(data.hint); ques.setDifficulty(data.difficulty); ques.setExplanation(data.explanation); ques.setTimeLimitSeconds(data.timeLimitSeconds); ques.setLastModifiedMillis( System.currentTimeMillis() ); for (int c=0; c<data.answerChoices.length; c++) { ques.setAnswerChoiceText( c, data.answerChoices[c] ); } } catch (FinderException fex) { throw SchoolUtils.convertToEJBException(fex); } } private QuestionData getDataFromQuestion(LocalQuestion ques) { QuestionData data = new QuestionData(); data.id = ques.getId(); data.name = ques.getName(); data.question = ques.getContent(); data.answer = ques.getAnswer(); data.hint = ques.getHint(); data.difficulty = ques.getDifficulty(); data.explanation = ques.getExplanation(); data.canDeactivate = ques.isActive() && "".equals(ques.getTestId()); data.timeLimitSeconds = ques.getTimeLimitSeconds(); LocalAnswerChoice[] answerChoices = ques.getAnswerChoices(); data.answerChoices = new String[answerChoices.length]; for (int c=0; c<answerChoices.length; c++) { if (answerChoices[c]!=null) { data.answerChoices[c] = answerChoices[c].getAnswerText(); } else { data.answerChoices[c] = ""; } } return data; } public void ejbCreate() {} public void ejbPostCreate() { } public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sc) { m_context = sc; } private SessionContext m_context = null; private LocalQuestionHome m_quesHome = null; private LocalAnswerChoiceHome m_answerChoiceHome = null; private static Logger s_logger = Logger.getLogger( QuestionManagerBean.class.getName());}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -