📄 testmanagerbean.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;import javax.ejb.*;import javax.naming.*;import java.util.*;import java.util.logging.*;import com.cyberdemia.ejb.BaseSessionBean;import com.cyberdemia.util.StringUtils;/** * TestManagerBean is a session bean facade that provides * useful methods for managing tests. * * @ejb.bean name="TestManagerBean" * type="Stateless" * view-type="remote" * jndi-name="ejb/TestManager" * * @ejb.util generate="physical" * @ejb.interface remote-class="com.cyberdemia.school.TestManager" extends="javax.ejb.EJBObject" * @ejb.home remote-class="com.cyberdemia.school.TestManagerHome" extends="javax.ejb.EJBHome" * @ejb.ejb-ref ejb-name="TestBean" ref-name="ejb/LocalTest" view-type="local" * @ejb.ejb-ref ejb-name="QuestionBean" ref-name="ejb/LocalQuestion" view-type="local" * @ejb.ejb-ref ejb-name="UserTestAssocBean" ref-name="ejb/LocalUserTestAssoc" view-type="local" * @ejb.ejb-ref ejb-name="AnswerHistoryRecordBean" ref-name="ejb/LocalAnswerHistoryRecord" view-type="local" * * @author Alexander Yap * @version $Revision: 1.12 $ at $Date: 2004/06/26 17:08:39 $ by $Author: alexycyap $ */public class TestManagerBean extends BaseSessionBean{ /** * Adds a new test to the database. * * @ejb.interface-method * @ejb.transaction type="Required" * * @param name Test name * @param ownerHierId Owner hierarchy ID. * @param passPercentage Passing percentage, between 0 and 100 * @param timeLimitSecs Time limit in seconds * @param multiQuesMode true to enable multi-questions mode, false to disable it * @param suppressQuesFeedback true to suppress question result feedback, false to allow it * @param suppressTestEndFeedback true to suppress test results feedback, false to allow it. * @return Unique ID of the new test. * @throws SchoolException if there is a problem creating the test. */ public Integer addTest(String name, String ownerHierId, double passPercentage, int timeLimitSecs, boolean multiQuesMode, boolean suppressQuesFeedback, boolean suppressTestEndFeedback) throws SchoolException { s_logger.log( Level.FINE, "Creating new Test name="+name+", owner="+ownerHierId+", passPercentage="+passPercentage); Integer id = null; try { LocalTest test = s_testHome.create(name, ownerHierId, passPercentage, timeLimitSecs, multiQuesMode, suppressQuesFeedback, suppressTestEndFeedback); id = test.getGeneratedPrimaryKey(); } catch (CreateException cex) { getSessionContext().setRollbackOnly(); throw new SchoolException("Error creating Test "+name, cex); } s_logger.log( Level.FINE, "Test with ID "+id+" successfully created."); return id; } /** * Deactives the specified test, after which it may be considered to * be logically removed from the system. * This method may perform some checks first to ensure that the test * can be safely deactivated. * * @ejb.interface-method * @ejb.transaction type="Required" * * @param id Test ID * @return true if the test is deactivated, false if it is not (either a problem or it is already inactive) * @throws EJBException if the test can't be found. */ public boolean deactivateTest(Integer id) throws SchoolException { LocalTest test = null; try { test = s_testHome.findByPrimaryKey(id); } catch (FinderException fex) { throw new SchoolException(SchoolException.TEST_NOT_FOUND, "Cannot find Test with ID "+id, fex); } boolean success = false; try { if (canDeactivateTest(test)) { success = test.deactivate(); test.setLastModifiedMillis(System.currentTimeMillis()); if (success) { // Remove all existing questions from this Test test.removeAllQuestions(); } } } catch (Exception ex) { getSessionContext().setRollbackOnly(); throw new SchoolException("Error deactivating Test with ID "+id, ex); } return success; } /** * Gets list of tests belonging to a specified owner hierarchy ID. * * @ejb.interface-method * @ejb.transaction type="Required" * * @param ownerHierId Owner hierarchy ID, or null to ignore the hierarchy ID. * @param activeOnly true to only return active tests, false to return all tests. * @return Array of TestData instances. */ public TestData[] getTests(String ownerHierId, boolean activeOnly) { TestData[] testDataArr = null; try { Iterator testIter = activeOnly ? s_testHome.findAllActive().iterator() : s_testHome.findAll().iterator(); ArrayList testDataList = new ArrayList(); while (testIter.hasNext()) { LocalTest test = (LocalTest)testIter.next(); if (ownerHierId!=null) { if ( ownerHierId.equals(test.getOwnerHierarchyId()) ) { testDataList.add( getDataFromTest(test) ); } } else { testDataList.add( getDataFromTest(test) ); } } testDataArr = (TestData[])testDataList.toArray(new TestData[0]); } catch (Exception ex) { // If there is a problem finding tests, just return empty array testDataArr = new TestData[0]; } return testDataArr; } /** * Gets active tests assigned to a specified user. * * @ejb.interface-method * @ejb.transaction type="Required" * * @param ownerHierId Owner hierarchy ID to filter tests, or null to ignore the hierarchy ID. * @param userId User ID to search for. * @return Array of TestData instances. */ public TestData[] getTestsForUser(String ownerHierId, Integer userId) { TestData[] testDataArr = null; try { Iterator assocIter = s_userTestAssocHome.findByUser(userId).iterator(); ArrayList testDataList = new ArrayList(); while (assocIter.hasNext()) { LocalUserTestAssoc assoc= (LocalUserTestAssoc)assocIter.next(); Integer testId = assoc.getTestId(); LocalTest test = s_testHome.findByPrimaryKey(testId); if (ownerHierId!=null) { if ( ownerHierId.equals(test.getOwnerHierarchyId()) ) { testDataList.add( getDataFromTest(test) ); } } else { testDataList.add( getDataFromTest(test) ); } } testDataArr = (TestData[])testDataList.toArray(new TestData[0]); } catch (Exception ex) { // If there is a problem finding tests, just return empty array testDataArr = new TestData[0]; } return testDataArr; } /** * Gets active tests that are <b>not</b> assigned to a specified user. * * @ejb.interface-method * @ejb.transaction type="Required" * * @param ownerHierId Owner hierarchy ID to filter tests, or null to ignore the hierarchy ID. * @param userId User ID to exclude. * @return Array of TestData instances. */ public TestData[] getTestsForNotUser(String ownerHierId, Integer userId) { TestData[] testDataArr = null; try { Iterator testIter = s_testHome.findAllActive().iterator(); Iterator assocIter = s_userTestAssocHome.findByUser(userId).iterator(); Set assocIdSet = new HashSet(); while(assocIter.hasNext()) { assocIdSet.add( ((LocalUserTestAssoc)assocIter.next()).getTestId() ); } ArrayList testDataList = new ArrayList(); while (testIter.hasNext()) { LocalTest test= (LocalTest)testIter.next(); if (!assocIdSet.contains(test.getId())) { testDataList.add( getDataFromTest(test) ); } } testDataArr = (TestData[])testDataList.toArray(new TestData[0]); } catch (Exception ex) { // If there is a problem finding tests, just return empty array testDataArr = new TestData[0]; } return testDataArr; } /** * Adds a question to a test. Both question and test must exist and be active. * * @ejb.interface-method * @ejb.transaction type="Required" * * @param testId Test ID * @param quesId Question ID * @throws SchoolException if either test or question is missing or inactive. */ public void addQuestionToTest( Integer testId, Integer quesId) throws SchoolException { LocalTest test = null; try { test = s_testHome.findByPrimaryKey(testId); } catch (FinderException fex) { throw new SchoolException(SchoolException.TEST_NOT_FOUND, "Cannot find Test with ID "+testId, fex); } LocalQuestion ques = null; try { ques = s_quesHome.findByPrimaryKey(quesId); } catch (FinderException fex) { throw new SchoolException(SchoolException.QUESTION_NOT_FOUND, "Cannot find Question with ID "+quesId, fex); } // Client code should prevent this method from being called // if either the test or question is inactive. if (!test.isActive()) { throw new SchoolException("Cannot add Question "+quesId+" to inactive Test "+testId); } if (!ques.isActive()) { throw new SchoolException("Cannot add inactive Question "+quesId+" to Test "+testId); } try { test.addQuestion(ques); test.setLastModifiedMillis(System.currentTimeMillis()); } catch (Exception ex) { getSessionContext().setRollbackOnly(); throw new SchoolException("Error adding Question with ID "+quesId+" to Test with ID "+testId, ex); } } /** * Removes a question from a test. Both question and test must exist. * * @ejb.interface-method * @ejb.transaction type="Required" * * @param testId Test ID * @param quesId Question ID * @throws SchoolException if either test or question is missing. */ public void removeQuestionFromTest( Integer testId, Integer quesId) throws SchoolException { LocalTest test = null; try { test = s_testHome.findByPrimaryKey(testId); } catch (FinderException fex) { throw new SchoolException(SchoolException.TEST_NOT_FOUND, "Cannot find Test with ID "+testId, fex); } LocalQuestion ques = null; try { ques = s_quesHome.findByPrimaryKey(quesId); } catch (FinderException fex) { throw new SchoolException(SchoolException.QUESTION_NOT_FOUND, "Cannot find Question with ID "+quesId, fex); } try
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -