gt: 				Task taskTmp = new Task();
context: /*******************************************************************************
   * This file is part of SQLCoach.
   *
   * SQLCoach is free software: you can redistribute it and/or modify
   * it under the terms of the GNU Affero General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * SQLCoach 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 Affero General Public License for more details.
   *
   * You should have received a copy of the GNU Affero General Public License
   * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
   *******************************************************************************/
  /*
   * Created on 11.03.2007 at 18:37:57
   *
   * Authors:
   * Florian Moritz - http://www.flomedia.de
   * Christoph Gerstle - http://www.christophgerstle.de
   *
   * Project: SQLcoach
   * Subject: Project Digital Media
   * Insitution: University of Applied Sciences Kaiserslautern, Zweibruecken - http://www.hs-kl.de
   * License: LGPL - GNU Lesser General Public License - http://www.gnu.org/licenses/lgpl.html
   */
  
  package de.sqlcoach.action;
  
  import java.io.ByteArrayInputStream;
  import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.io.Serializable;
  import java.sql.SQLException;
  import java.util.Date;
  import java.util.HashMap;
  import java.util.List;
  
  import javax.ejb.EJB;
  import javax.servlet.ServletConfig;
  import javax.servlet.ServletException;
  import javax.servlet.http.Cookie;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  import javax.xml.bind.DatatypeConverter;
  
  import org.apache.log4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import de.sqlcoach.bean.Alert;
  import de.sqlcoach.bean.TrainingForm;
  import de.sqlcoach.beans.DBAppStatisticService;
  import de.sqlcoach.beans.DBAppUserService;
  import de.sqlcoach.beans.DBConnectionService;
  import de.sqlcoach.beans.DBScenarioService;
  import de.sqlcoach.beans.DBScenarioTableService;
  import de.sqlcoach.beans.DBTaskService;
  import de.sqlcoach.beans.DBTaskgroupService;
  import de.sqlcoach.db.entities.AppStatistic;
  import de.sqlcoach.db.entities.Scenario;
  import de.sqlcoach.db.entities.ScenarioTable;
  import de.sqlcoach.db.entities.Task;
  import de.sqlcoach.db.entities.Taskgroup;
  import de.sqlcoach.util.MetaTable;
  import de.sqlcoach.util.ParamUtil;
  import de.sqlcoach.util.ViewResultSet;
  
  /**
   * The Class TrainingController.
   * 
   * @author Florian Moritz
   * @version 0.1
   */
  public class TrainingController extends HttpServlet {
  
  	/** The Constant serialVersionUID. */
  	private static final long serialVersionUID = 1L;
  
  	/** The log. */
  	private static final Logger log = Logger.getLogger(TrainingController.class);
  	private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(TrainingController.class);
  	
  	private static final Integer DEFAULT_SAMPLE_SOLUTION_HINT_COUNT = 3;
  
  	@EJB
  	private DBAppUserService dbAppUserService;
  
  	@EJB
  	private DBAppStatisticService dbAppStatisticService;
  
  	@EJB
  	private DBScenarioService dbScenarioService;
  
  	@EJB
  	private DBScenarioTableService dbScenarioTableService;
  
  	@EJB
  	private DBTaskService dbTaskService;
  
  	@EJB
  	private DBTaskgroupService dbTaskgroupService;
  
  	@EJB
  	private DBConnectionService dbConnectionService;
  
  	/*
  	 * (non-Javadoc)
  	 * 
  	 * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
  	 */
  
  	public void init(ServletConfig poConfig) throws ServletException {
  		log.info("MPA init");
  		super.init(poConfig);
  	}
  
  	/*
  	 * (non-Javadoc)
  	 * 
  	 * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.
  	 * HttpServletRequest, javax.servlet.http.HttpServletResponse)
  	 */
  
  	@SuppressWarnings("unchecked")
  	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  		log.info("MPA service");
  
  		TrainingForm tf = (TrainingForm) request.getAttribute("TrainingForm");
  
  		if (tf == null) { // erster Aufruf von extern
  			tf = new TrainingForm(request);
  			request.setAttribute("TrainingForm", tf);
  			log.info("service : tf == null => Neu erzeugt! ");
  			// Falls Form null muss scenarioId woanders herkommen
  		}
  
  		final String scenarioId = (String) request.getSession().getAttribute("scenarioId");
  		final String oldScenarioId = (String) request.getSession().getAttribute("oldScenarioId");
  
  		if (log.isInfoEnabled())
  			log.info("service ENTER scenarioId=" + scenarioId + " tf=" + tf);
  
  		if (ParamUtil.isNull(scenarioId)) { // session timeout
  			log.warn("service LEAVE scenarioId=" + scenarioId + " tf=" + tf);
  			request.getRequestDispatcher("/index.jsp").forward(request, response);
  			return;
  		}
  
  		Scenario scenario = dbScenarioService.get(Long.valueOf(scenarioId));
  		request.setAttribute("scenario", scenario);
  		
  		//check value sample solution hint count is set, else get default value
  		Integer sampleSolutionHintCount = null;
  		if(null != scenario.getSampleSolutionHintCount()) {
  			sampleSolutionHintCount = scenario.getSampleSolutionHintCount();
  		} else {
  			sampleSolutionHintCount = DEFAULT_SAMPLE_SOLUTION_HINT_COUNT;
  		}
  		request.setAttribute("sampleSolutionHintCount", sampleSolutionHintCount);
  		
  		// Task
  		Task task = ParamUtil.isNull(tf.getTaskId()) ? null : dbTaskService.get(Long.valueOf(tf.getTaskId()));
  		request.setAttribute("task", task);
  
  		// Taskgroup
  		if (null != task) {
  			tf.setTaskgroupId(task.getTaskgroup().getId().toString());
  		}
  
  		Taskgroup taskgroup = ParamUtil.isNull(tf.getTaskgroupId()) ? null
  				: dbTaskgroupService.get(Long.valueOf(tf.getTaskgroupId()));
  
  		request.setAttribute("taskgroup", taskgroup);
  
  		// ScenarioTable Collection
  		List<ScenarioTable> scenarioTableCol = null;
  		List<MetaTable> metaTableCol = null;
  
  		if (log.isInfoEnabled())
  			log.info("service, scenarioId aus session scenarioId=" + scenarioId + " taskgroup=" + taskgroup);
  
  		if (scenarioId != null && scenarioId.equals(oldScenarioId)) {
  			scenarioTableCol = (List<ScenarioTable>) request.getSession().getAttribute("scenarioTableCol");
  			metaTableCol = (List<MetaTable>) request.getSession().getAttribute("metaTableCol");
  		} else { // Neues Szenario gewaehlt (bs)
  			request.getSession().setAttribute("oldScenarioId", scenarioId);
  			scenarioTableCol = dbScenarioTableService.getByScenarioId(Long.valueOf(scenarioId));
  			request.getSession().setAttribute("scenarioTableCol", scenarioTableCol);
  		}
  
  		// MetaTable Collection
  		// External Connection for scenario DataSource
  		if (log.isInfoEnabled()) {
  			log.info("external DataSource=" + scenario.getDatasource());
  		}
  
  		ViewResultSet viewResultSet = null;
  		if (metaTableCol == null) { // Metainfo in Session puffern (bs)
  			metaTableCol = dbConnectionService.readByScenarioTableCol(scenarioTableCol, scenario);
  			request.getSession().setAttribute("metaTableCol", metaTableCol);
  			final String db_prodname = dbConnectionService.getDatabaseProductName(scenario);
  			final String db_prodversion = dbConnectionService.getDatabaseProductVersion(scenario);
  			request.getSession().setAttribute("db_prodname", db_prodname);
  			request.getSession().setAttribute("db_prodversion", db_prodversion);
  		}
  
  		// Sample Solution as ViewResultSet (based on admin query)
  		if (null != task) { //if free training choosen then task == null
  			try {
  				log.info("task.getQuery(): " + task.getQuery());
  				viewResultSet = (task != null) ? dbConnectionService.get(task.getQuery(), scenario) : null;
  				request.getSession().setAttribute("sampleSolutionHint", task.getQuery());
  			} catch (SQLException e) {
  				log.error("viewResultSet: " + e);
  			}
  		}
  
  		int resultCnt = (viewResultSet != null && viewResultSet.getRows() != null) ? viewResultSet.getRows().size() : 0;
  		request.setAttribute("solutionViewResultSet", viewResultSet);
  		request.setAttribute("solutionViewResultSetCount", resultCnt);
  
  		// compare the two ViewResultSets of admin and trainee
  		if (tf.getStatus().equals("check")) {
  			ViewResultSet traineeSet = (ViewResultSet) request.getAttribute("traineeViewResultSet");
  			boolean equals = false;
  			if (log.isInfoEnabled())
  				log.info("traineeSet.size=" + (traineeSet == null ? -1 : traineeSet.size()) + " viewResultSet.size="
  						+ (viewResultSet == null ? -1 : viewResultSet.size()));
  
  			if (traineeSet != null && viewResultSet != null) {
  				if (traineeSet.equals(viewResultSet)) {
  					equals = true;
  					Alert.catchSuccess("alert.success.equal", request, response);
  				} else {
  					Alert.catchError("alert.error.notEqual", request);
  				}
  			}
  			log.info("2 ViewResultSets are equal? -> " + equals);
  
  			// add AppStatistic entry
  			if (tf.getTaskId() != null && !tf.getTaskId().equals("null")) {
  				final AppStatistic appStatistics = new AppStatistic();
  				appStatistics.setQuery(tf.getQuery());
  				appStatistics.setSessionID(request.getSession().getId());
  				Date dateCreate = new Date();
  				appStatistics.setDateCreate(dateCreate);
  