⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testmanagerbean.java

📁 老外的在线考试
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * 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.*;/*** TestManagerBean is a session bean facade that provides* useful methods for managing tests.** @author Alexander Yap*/public class TestManagerBean implements SessionBean{	public TestManagerBean()	{		try		{			Context ctx = new InitialContext();			m_testHome = (LocalTestHome)ctx.lookup("java:comp/env/ejb/localTest");			m_quesHome = (LocalQuestionHome)ctx.lookup("java:comp/env/ejb/localQuestion");			m_userTestAssocHome = (LocalUserTestAssocHome)ctx.lookup("java:comp/env/ejb/localUserTestAssoc");			m_answerHistoryRecordHome =  (LocalAnswerHistoryRecordHome)ctx.lookup("java:comp/env/ejb/localAnswerHistoryRecord");		}		catch (NamingException nex)		{			throw SchoolUtils.convertToEJBException(nex);		}	}		/**	 * Adds a new test to the database.	 * @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 EJBException if there is a problem creating the test.	 */	public String addTest(String name, String ownerHierId, double passPercentage, 		int timeLimitSecs, boolean multiQuesMode, boolean suppressQuesFeedback,		boolean suppressTestEndFeedback)	{		s_logger.log( Level.INFO, "Creating new Test name="+name+", owner="+ownerHierId+", passPercentage="+passPercentage);		String id = null;		try		{			LocalTest test = m_testHome.create(name, ownerHierId, passPercentage, timeLimitSecs, multiQuesMode, suppressQuesFeedback, suppressTestEndFeedback);			id = test.getId();		}		catch (CreateException cex)		{			s_logger.log( Level.SEVERE, "Error creating Test "+name, cex);			throw SchoolUtils.convertToEJBException(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.	 * 	 * @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(String id)	{		boolean success = false;		try		{			LocalTest test = m_testHome.findByPrimaryKey(id);			if (canDeactivateTest(test))			{				success = test.deactivate();				test.setLastModifiedMillis(System.currentTimeMillis());				if (success)				{					// Remove all existing questions from this Test					test.removeAllQuestions();					Iterator relatedQuesIter = m_quesHome.findByTest(id).iterator();					while (relatedQuesIter.hasNext())					{						LocalQuestion relatedQues = (LocalQuestion)relatedQuesIter.next();						relatedQues.setTestId("");					}				}			}		}		catch (FinderException fex)		{			throw SchoolUtils.convertToEJBException(fex);		}		return success;	}		/**	 * Gets list of tests belonging to a specified owner hierarchy ID.	 * @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 ? m_testHome.findAllActive().iterator() : m_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 (FinderException fex)		{			throw SchoolUtils.convertToEJBException(fex);		}		return testDataArr;	}		/**	 * Gets active tests assigned to a specified user.	 * @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, String userId)	{		TestData[] testDataArr = null;		try		{			Iterator assocIter = m_userTestAssocHome.findByUser(userId).iterator();			ArrayList testDataList = new ArrayList();			while (assocIter.hasNext())			{				LocalUserTestAssoc assoc= (LocalUserTestAssoc)assocIter.next();				String testId = assoc.getTestId();				LocalTest test = m_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 (FinderException fex)		{			throw SchoolUtils.convertToEJBException(fex);		}		return testDataArr;	}		/**	 * Gets active tests that are <b>not</b> assigned to a specified user.	 * @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, String userId)	{		TestData[] testDataArr = null;		try		{			Iterator testIter = m_testHome.findAllActive().iterator();			Iterator assocIter = m_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 (FinderException fex)		{			throw SchoolUtils.convertToEJBException(fex);		}		return testDataArr;	}		/**	 * Adds a question to a test. Both question and test must exist and be active.	 * @param testId Test ID	 * @param quesId Question ID	 * @throws EJBException if either test or question is missing or inactive.	 */		public void addQuestionToTest( String testId, String quesId)	{		try		{			LocalTest test = m_testHome.findByPrimaryKey(testId);			LocalQuestion ques = m_quesHome.findByPrimaryKey(quesId);			// Client code should prevent this method from being called			// if either the test or question is inactive.			if (!test.isActive())			{				throw new EJBException("Cannot add Question "+quesId+" to inactive Test "+testId);			}			if (!ques.isActive())			{				throw new EJBException("Cannot add inactive Question "+quesId+" to Test "+testId);			}						ques.setTestId(testId);			test.addQuestion(ques);			test.setLastModifiedMillis(System.currentTimeMillis());		}		catch (FinderException fex)		{			throw SchoolUtils.convertToEJBException(fex);		}	}		/**	 * Removes a question from a test. Both question and test must exist.	 * @param testId Test ID	 * @param quesId Question ID	 * @throws EJBException if either test or question is missing.	 */		public void removeQuestionFromTest( String testId, String quesId)	{		try		{			LocalTest test = m_testHome.findByPrimaryKey(testId);			LocalQuestion ques = m_quesHome.findByPrimaryKey(quesId);			test.removeQuestion(ques);			ques.setTestId("");			test.setLastModifiedMillis(System.currentTimeMillis());		}		catch (FinderException fex)		{			throw SchoolUtils.convertToEJBException(fex);		}	}		/**	 * Gets data of resources (questions) for a test.	 * @param testId Test ID.	 * @param count Maximum number of resources to retrieve.	 * @return Array of ResourceListData instances.	 * @throws EJBException if test is missing.	 */	public ResourceListData[] getResourcesDataFromTest( String testId, int count)	{		ResourceListData[] resources = null;		try		{			LocalTest test = m_testHome.findByPrimaryKey(testId);			List resourceList = test.getQuestions(count);			resources = new ResourceListData[ resourceList.size() ];			Iterator resourceIter = resourceList.iterator();			for (int ridx=0; resourceIter.hasNext() && (ridx<resources.length); ridx++)			{				LocalQuestion ques = (LocalQuestion)resourceIter.next();				resources[ridx] = new ResourceListData( ques.getId(), ques.getName() );			}		}		catch (FinderException fex)		{			s_logger.log( Level.SEVERE, "Error finding Test ID="+testId, fex);			throw SchoolUtils.convertToEJBException(fex);		}		catch (RuntimeException ex)		{			s_logger.log( Level.SEVERE, "Error getting resources data for Test ID="+testId, ex);						throw ex;		}		return resources;	}					/**	 * Gets data of a test.	 * @param id Test ID.	 * @return TestData instance storing the test's data.	 * @throws EJBException if test is missing.	 */	public TestData getTestData(String id) 	{		TestData data = null;		try		{			LocalTest test = m_testHome.findByPrimaryKey(id);			data = getDataFromTest(test);		}		catch (FinderException fex)		{			s_logger.log( Level.SEVERE, "Error finding Test ID="+id, fex);			throw SchoolUtils.convertToEJBException(fex);		}		catch (RuntimeException ex)		{			s_logger.log( Level.SEVERE, "Error getting data for Test ID="+id, ex);						throw ex;		}		return data;	}	/**	 * Updates the data of an existing test.	 * @param id Test ID.	 * @param data TestData instance with the test's data.	 * @throws EJBException if test is missing	 * @throws RuntimeException if there is a problem updating the test.	 */	public void setTestData(String id, TestData data) 	{		try

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -