<commit_before>package gov.nih.nci.calab.ui.particle;

/**
 * This class sets up input form for size characterization. 
 *  
 * @author pansu
 */

/* CVS $Id: NanoparticleFunctionAction.java,v 1.10 2007-12-13 16:30:36 pansu Exp $ */

import gov.nih.nci.calab.dto.common.UserBean;
import gov.nih.nci.calab.dto.function.AgentBean;
import gov.nih.nci.calab.dto.function.AgentTargetBean;
import gov.nih.nci.calab.dto.function.FunctionBean;
import gov.nih.nci.calab.dto.function.LinkageBean;
import gov.nih.nci.calab.dto.particle.ParticleBean;
import gov.nih.nci.calab.exception.CaNanoLabSecurityException;
import gov.nih.nci.calab.exception.ParticleFunctionException;
import gov.nih.nci.calab.service.particle.NanoparticleFunctionService;
import gov.nih.nci.calab.service.particle.NanoparticleService;
import gov.nih.nci.calab.service.util.CaNanoLabConstants;
import gov.nih.nci.calab.ui.core.AbstractDispatchAction;
import gov.nih.nci.calab.ui.core.InitSessionSetup;
import gov.nih.nci.calab.ui.security.InitSecuritySetup;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.DynaValidatorForm;

public class NanoparticleFunctionAction extends AbstractDispatchAction {
	/**
	 * Add or update the data to database
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionForward forward = null;

		DynaValidatorForm theForm = (DynaValidatorForm) form;
		ParticleBean particle = (ParticleBean) theForm.get("particle");
		FunctionBean function = (FunctionBean) theForm.get("function");
		String functionId = request.getParameter("functionId");

		if (functionId != null && functionId.length() > 0) {
			function.setId((String) theForm.get("functionId"));
		}

		// validate agent target
		for (LinkageBean linkageBean : function.getLinkages()) {
			for (AgentTargetBean agentTargetBean : linkageBean.getAgent()
					.getAgentTargets()) {

				if (agentTargetBean.getType().length() == 0) {
					throw new ParticleFunctionException(
							"Agent target type can not be empty.");
				}
			}
		}
		NanoparticleFunctionService service = new NanoparticleFunctionService();
		service.addParticleFunction(particle.getSampleId(), function);

		ActionMessages msgs = new ActionMessages();
		ActionMessage msg = new ActionMessage("message.addFunction");
		msgs.add("message", msg);
		saveMessages(request, msgs);
		forward = mapping.findForward("success");

		InitParticleSetup.getInstance().setSideParticleMenu(request,
				particle.getSampleId());
		request.setAttribute("theParticle", particle);
		return forward;
	}

	/**
	 * Set up the input forms for adding data
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward setup(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaValidatorForm theForm = (DynaValidatorForm) form;

		HttpSession session = request.getSession();
		// clear session data from the input forms
		clearMap(session, theForm, mapping);
		initSetup(request, theForm);
		return mapping.getInputForward();
	}

	private void clearMap(HttpSession session, DynaValidatorForm theForm,
			ActionMapping mapping) throws Exception {
		// clear session data from the input forms
		theForm.set("particle", new ParticleBean());
		theForm.set("function", new FunctionBean());
	}

	private void initSetup(HttpServletRequest request, DynaValidatorForm theForm)
			throws Exception {
		HttpSession session = request.getSession();
		String particleId = request.getParameter("particleId");
		UserBean user = (UserBean) request.getSession().getAttribute("user");

		NanoparticleService searchNanoparticleService = new NanoparticleService();
		ParticleBean particle = searchNanoparticleService.getParticleInfo(
				particleId, user);
		theForm.set("particle", particle);
		InitParticleSetup.getInstance().setSideParticleMenu(request,
				particle.getSampleId());
		InitSessionSetup.getInstance().setStaticDropdowns(session);
		InitParticleSetup.getInstance().setAllFunctionDropdowns(session);
	}

	/**
	 * Set up the form for updating existing characterization
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward setupUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaValidatorForm theForm = (DynaValidatorForm) form;
		initSetup(request, theForm);
		String functionId = (String) theForm.get("functionId");
		NanoparticleFunctionService service = new NanoparticleFunctionService();
		FunctionBean functionBean = service.getFunctionBy(functionId);
		theForm.set("function", functionBean);
		return mapping.findForward("setup");
	}

	/**
	 * Prepare the form for viewing existing characterization
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward setupView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return setupUpdate(mapping, form, request, response);
	}

	public ActionForward addLinkage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaValidatorForm theForm = (DynaValidatorForm) form;
		FunctionBean function = (FunctionBean) theForm.get("function");<commit_msg>added logic to set displayType<commit_after>package gov.nih.nci.calab.ui.particle;

/**
 * This class sets up input form for size characterization. 
 *  
 * @author pansu
 */

/* CVS $Id: NanoparticleFunctionAction.java,v 1.11 2007-12-20 15:30:27 pansu Exp $ */

import gov.nih.nci.calab.dto.common.UserBean;
import gov.nih.nci.calab.dto.function.AgentBean;
import gov.nih.nci.calab.dto.function.AgentTargetBean;
import gov.nih.nci.calab.dto.function.FunctionBean;
import gov.nih.nci.calab.dto.function.LinkageBean;
import gov.nih.nci.calab.dto.particle.ParticleBean;
import gov.nih.nci.calab.exception.CaNanoLabSecurityException;
import gov.nih.nci.calab.exception.ParticleFunctionException;
import gov.nih.nci.calab.service.particle.NanoparticleFunctionService;
import gov.nih.nci.calab.service.particle.NanoparticleService;
import gov.nih.nci.calab.service.util.CaNanoLabConstants;
import gov.nih.nci.calab.service.util.StringUtils;
import gov.nih.nci.calab.ui.core.AbstractDispatchAction;
import gov.nih.nci.calab.ui.core.InitSessionSetup;
import gov.nih.nci.calab.ui.security.InitSecuritySetup;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.DynaValidatorForm;

public class NanoparticleFunctionAction extends AbstractDispatchAction {
	/**
	 * Add or update the data to database
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionForward forward = null;

		DynaValidatorForm theForm = (DynaValidatorForm) form;
		ParticleBean particle = (ParticleBean) theForm.get("particle");
		FunctionBean function = (FunctionBean) theForm.get("function");
		String functionId = request.getParameter("functionId");

		if (functionId != null && functionId.length() > 0) {
			function.setId((String) theForm.get("functionId"));
		}

		// validate agent target
		for (LinkageBean linkageBean : function.getLinkages()) {
			for (AgentTargetBean agentTargetBean : linkageBean.getAgent()
					.getAgentTargets()) {

				if (agentTargetBean.getType().length() == 0) {
					throw new ParticleFunctionException(
							"Agent target type can not be empty.");
				}
			}
		}
		NanoparticleFunctionService service = new NanoparticleFunctionService();
		service.addParticleFunction(particle.getSampleId(), function);

		ActionMessages msgs = new ActionMessages();
		ActionMessage msg = new ActionMessage("message.addFunction");
		msgs.add("message", msg);
		saveMessages(request, msgs);
		forward = mapping.findForward("success");

		InitParticleSetup.getInstance().setSideParticleMenu(request,
				particle.getSampleId());
		request.setAttribute("theParticle", particle);
		return forward;
	}

	/**
	 * Set up the input forms for adding data
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward setup(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaValidatorForm theForm = (DynaValidatorForm) form;

		HttpSession session = request.getSession();
		// clear session data from the input forms
		clearMap(session, theForm, mapping);
		initSetup(request, theForm);
		return mapping.getInputForward();
	}

	private void clearMap(HttpSession session, DynaValidatorForm theForm,
			ActionMapping mapping) throws Exception {
		// clear session data from the input forms
		theForm.set("particle", new ParticleBean());
		theForm.set("function", new FunctionBean());
	}

	private void initSetup(HttpServletRequest request, DynaValidatorForm theForm)
			throws Exception {
		HttpSession session = request.getSession();
		String particleId = request.getParameter("particleId");
		UserBean user = (UserBean) request.getSession().getAttribute("user");

		NanoparticleService searchNanoparticleService = new NanoparticleService();
		ParticleBean particle = searchNanoparticleService.getParticleInfo(
				particleId, user);
		String submitType = request.getParameter("submitType");
		String functionId = request.getParameter("functionId");
		FunctionBean functionBean = null;
		if (functionId != null) {
			NanoparticleFunctionService service = new NanoparticleFunctionService();
			functionBean = service.getFunctionBy(functionId);
			theForm.set("function", functionBean);
		} else {
			functionBean = (FunctionBean) theForm.get("function");
		}
		functionBean.setDisplayType(StringUtils
				.getOneWordLowerCaseFirstLetter(submitType));
		theForm.set("particle", particle);
		InitParticleSetup.getInstance().setSideParticleMenu(request,
				particle.getSampleId());
		InitSessionSetup.getInstance().setStaticDropdowns(session);
		InitParticleSetup.getInstance().setAllFunctionDropdowns(session);
	}

	/**
	 * Set up the form for updating existing characterization
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward setupUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaValidatorForm theForm = (DynaValidatorForm) form;
		initSetup(request, theForm);
		return mapping.findForward("setup");
	}

	/**
	 * Prepare the form for viewing existing characterization
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward setupView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return setupUpdate(mapping, form, request, response);
	}

	public ActionForward addLinkage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaValidatorForm theForm = (DynaValidatorForm) form;
		FunctionBean function = (FunctionBean) theForm.get("function");
{'fraction_non_alphanumeric': 0.10242846838182255, 'fraction_numerical': 0.0030456039111966017, 'mean_word_length': 5.140748031496063, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2014812', 'n_tokens_mistral': 3937, 'n_tokens_neox': 3524, 'n_words': 812}