📄 cybertesterutils.java
字号:
/* * CyberTester - J2EE Web application for creating, delivering and managing tests/exams/surveys. * Copyright (C) 2004 CyberDemia Research and Services Pty Ltd * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program (see the file COPYING); 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 program for complete text of license. */package com.cyberdemia.cybertester;import com.cyberdemia.school.*;import com.cyberdemia.user.*;import javax.servlet.*;import javax.servlet.http.*;import javax.naming.*;import javax.rmi.PortableRemoteObject;import org.apache.struts.action.*;import org.jfree.chart.ChartRenderingInfo;import org.jfree.chart.entity.ChartEntity;import org.jfree.chart.imagemap.StandardToolTipTagFragmentGenerator;import org.jfree.chart.imagemap.StandardURLTagFragmentGenerator;import com.cyberdemia.ejb.BaseSessionBean;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.logging.*;/** * * CyberTesterUtils is a utility class that defines useful static utility methods. * * @version $Revision: 1.6 $ at $Date: 2004/06/24 17:18:15 $ by $Author: alexycyap $ * @author Alexander Yap */public abstract class CyberTesterUtils{ private static Logger s_logger = Logger.getLogger("com.cyberdemia.cybertester"); private static QuestionManagerHome s_quesMgrHome = null; private static TestManagerHome s_testMgrHome = null; private static UserManagerHome s_userMgrHome = null; private static GroupManagerHome s_groupMgrHome = null; private static ReportGeneratorHome s_reportGeneratorHome = null; static { initManagers(); } /** * Gets logger for CyberTester server-side code. */ public static Logger getLogger() { return s_logger; } /** * Gets home interface for QuestionManager. * @return Home interface for QuestionManager. */ public static QuestionManagerHome getQuestionManagerHome() { return s_quesMgrHome; } /** * Gets home interface for TestManager. * @return Home interface for TestManager. */ public static TestManagerHome getTestManagerHome() { return s_testMgrHome; } /** * Gets home interface for UserManager. * @return Home interface for UserManager. */ public static UserManagerHome getUserManagerHome() { return s_userMgrHome; } /** * Gets home interface for GroupManager. * @return Home interface for GroupManager. */ public static GroupManagerHome getGroupManagerHome() { return s_groupMgrHome; } /** * Gets home interface for ReportGenerator. * @return Home interface for ReportGenerator. */ public static ReportGeneratorHome getReportGeneratorHome() { return s_reportGeneratorHome; } /** * Gets the session if it is valid for currently logged in user. * If the session is not found, timed-out, or missing login data, * it is considered invalid. This method returns null if session is invalid. * * @param request Request to extract session from. * @return Valid user session, or null if session is invalid. */ public static HttpSession getUserSession( HttpServletRequest request ) throws ServletException { HttpSession session = request.getSession(false); if (session==null) return null; Object userLogin = session.getAttribute(Constants.USER_LOGIN_KEY); Object userId = session.getAttribute(Constants.USER_ID_KEY); if ((userLogin==null) || (userId==null) ) { return null; } if (!userLogin.equals(request.getRemoteUser())) { throw new ServletException("User mismatch, expecting "+userLogin+", but got "+request.getRemoteUser()); } return session; } private static void initManagers() { Context ctx = null; Object homeObj = null; try { ctx = new InitialContext(); } catch (Exception ex) { throw new RuntimeException("Error creating InitialContext.",ex); } try { homeObj = ctx.lookup(BaseSessionBean.JNDI_PREFIX + QuestionManagerHome.JNDI_NAME); s_quesMgrHome = (QuestionManagerHome)PortableRemoteObject.narrow(homeObj, QuestionManagerHome.class); } catch (Exception ex) { throw new RuntimeException("Error initializing QuestionManagerHome.",ex); } try { homeObj = ctx.lookup(BaseSessionBean.JNDI_PREFIX + TestManagerHome.JNDI_NAME); s_testMgrHome = (TestManagerHome)PortableRemoteObject.narrow(homeObj, TestManagerHome.class); } catch (Exception ex) { throw new RuntimeException("Error initializing TestManagerHome.",ex); } try { homeObj = ctx.lookup(BaseSessionBean.JNDI_PREFIX + UserManagerHome.JNDI_NAME); s_userMgrHome = (UserManagerHome)PortableRemoteObject.narrow(homeObj, UserManagerHome.class); } catch (Exception ex) { throw new RuntimeException("Error initializing UserManagerHome.",ex); } try { homeObj = ctx.lookup(BaseSessionBean.JNDI_PREFIX + GroupManagerHome.JNDI_NAME); s_groupMgrHome = (GroupManagerHome)PortableRemoteObject.narrow(homeObj, GroupManagerHome.class); } catch (Exception ex) { throw new RuntimeException("Error initializing GroupManagerHome.",ex); } try { homeObj = ctx.lookup(BaseSessionBean.JNDI_PREFIX + ReportGeneratorHome.JNDI_NAME); s_reportGeneratorHome = (ReportGeneratorHome)PortableRemoteObject.narrow(homeObj, ReportGeneratorHome.class); } catch (Exception ex) { throw new RuntimeException("Error initializing ReportGeneratorHome.",ex); } } /** * Gets the data of tests under a owner hierarcy node. * @param ownerHierId Owner hierarchy ID, or null to return all tests. * @param activeOnly true to return only active tests, false to consider all tests. * @return Array of TestData instances, or empty array if there is an error. */ public static TestData[] getTestsData(String ownerHierId, boolean activeOnly) { TestData[] testDataArr = null; try { TestManager testMgr = (TestManager)s_testMgrHome.create(); testDataArr = testMgr.getTests(ownerHierId, activeOnly); } catch (Exception ex) { s_logger.log( Level.SEVERE, "Error getting tests for ownerHeirId "+ownerHierId, ex); testDataArr = new TestData[0]; } return testDataArr; } /** * Gets the data of tests, under a owner hierarchy node, that have been assigned to a user. * @param ownerHierId Owner hierarchy ID * @param userId User ID * @return Array of TestData instances, or empty array if there is an error. */ public static TestData[] getTestsDataForUser(String ownerHierId, Integer userId) { TestData[] testDataArr = null; try { TestManager testMgr = (TestManager)s_testMgrHome.create(); testDataArr = testMgr.getTestsForUser( ownerHierId, userId); } catch (Exception ex) { s_logger.log( Level.SEVERE, "Error getting tests for ownerHeirId "+ownerHierId+", user "+userId, ex); testDataArr = new TestData[0]; } return testDataArr; } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -