<commit_before>package gov.nih.nci.cananolab.ui.sample;

/**
 * This class sets up the submit a new sample page and submits a new sample
 *
 * @author pansu
 */

/* CVS $Id: SubmitNanoparticleAction.java,v 1.37 2008-09-18 21:35:25 cais Exp $ */

import gov.nih.nci.cananolab.dto.common.PointOfContactBean;
import gov.nih.nci.cananolab.dto.common.UserBean;
import gov.nih.nci.cananolab.dto.particle.SampleBean;
import gov.nih.nci.cananolab.exception.DuplicateEntriesException;
import gov.nih.nci.cananolab.exception.NotExistException;
import gov.nih.nci.cananolab.exception.SampleException;
import gov.nih.nci.cananolab.exception.SecurityException;
import gov.nih.nci.cananolab.service.sample.SampleService;
import gov.nih.nci.cananolab.service.sample.impl.SampleServiceLocalImpl;
import gov.nih.nci.cananolab.ui.core.BaseAnnotationAction;
import gov.nih.nci.cananolab.ui.core.InitSetup;
import gov.nih.nci.cananolab.ui.security.InitSecuritySetup;
import gov.nih.nci.cananolab.util.StringUtils;

import java.util.List;
import java.util.SortedSet;

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

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 SampleAction extends BaseAnnotationAction {
	// logger
	// private static Logger logger = Logger.getLogger(ReviewDataAction.class);

	/**
	 * Save or update POC data.
	 *
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaValidatorForm theForm = (DynaValidatorForm) form;
		SampleBean sampleBean = (SampleBean) theForm.get("sampleBean");
		setServiceInSession(request);
		saveSample(request, sampleBean);
		request.getSession().setAttribute("updateSample", "true");
		request.setAttribute("theSample", sampleBean);

		return mapping.findForward("summaryEdit");
	}

	private void saveSample(HttpServletRequest request, SampleBean sampleBean)
			throws Exception {
		UserBean user = (UserBean) (request.getSession().getAttribute("user"));
		sampleBean.setupDomain(user.getLoginName());
		// persist in the database
		SampleService service = (SampleService) request.getSession()
				.getAttribute("sampleService");
		service.saveSample(sampleBean);

		ActionMessages messages = new ActionMessages();
		ActionMessage msg = null;
		String updateSample = (String) request.getSession().getAttribute(
				"updateSample");
		if (!StringUtils.isEmpty(updateSample)) {
			msg = new ActionMessage("message.updateSample");
			messages.add(ActionMessages.GLOBAL_MESSAGE, msg);
			saveMessages(request, messages);
		}
	}

	/**
	 * Handle view sample request on sample search result page (read-only view).
	 *
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward summaryView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaValidatorForm theForm = (DynaValidatorForm) form;
		this.setServiceInSession(request);

		// "setupSample()" will retrieve and return the SampleBean.
		SampleBean sampleBean = setupSample(theForm, request);
		theForm.set("sampleBean", sampleBean);
		return mapping.findForward("summaryView");
	}

	public ActionForward input(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String browserDispatch = getBrowserDispatch(request);
		// from cloning form
		if (browserDispatch.equals("clone")) {
			return mapping.findForward("cloneInput");
		} else {
			return mapping.findForward("createInput");
		}
	}

	public ActionForward setupView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaValidatorForm theForm = (DynaValidatorForm) form;
		this.setServiceInSession(request);
		// "setupSample()" will retrieve and return the SampleBean.
		SampleBean sampleBean = setupSample(theForm, request);
		theForm.set("sampleBean", sampleBean);
		return mapping.findForward("bareSummaryView");
	}

	/**
	 * Handle edit sample request on sample search result page (curator view).
	 *
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward summaryEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaValidatorForm theForm = (DynaValidatorForm) form;
		this.setServiceInSession(request);

		// "setupSample()" will retrieve and return the SampleBean.
		SampleBean sampleBean = setupSample(theForm, request);
		theForm.set("sampleBean", sampleBean);
		request.getSession().setAttribute("updateSample", "true");
		setupLookups(request, sampleBean.getPrimaryPOCBean().getDomain()
				.getOrganization().getName());

		// Feature request [26487] Deeper Edit Links.
		// String dispatch = request.getParameter("dispatch"); // as the
		// function
		// // is shared.
		// if ("summaryEdit".equals(dispatch)
		// || "removePointOfContact".equals(dispatch)) {
		// if (sampleBean.getPrimaryPOCBean() != null
		// && sampleBean.getOtherPOCBeans().isEmpty()) {
		// StringBuilder sb = new StringBuilder();
		// sb.append("openOnePointOfContact(");
		// sb.append(sampleBean.getPrimaryPOCBean().getDomain().getId());
		// sb.append(", true)");
		// request.setAttribute("onloadJavascript", sb.toString());
		// }
		// }

		InitSetup charsService = InitSetup.getInstance();
		ServletContext appContext = request.getSession().getServletContext();
		SortedSet<String> physicoChars = charsService
				.getDefaultTypesByReflection(
						appContext,
						"defaultCharTypeChars",
						"gov.nih.nci.cananolab.domain.characterization.physical.PhysicoChemicalCharacterization");
		SortedSet<String> invitroChars = charsService
				.getDefaultTypesByReflection(appContext,
						"defaultCharTypeChars",
						"gov.nih.nci.cananolab.domain.characterization.invitro.InvitroCharacterization");
		SortedSet<String> invivoChars = charsService
				.getDefaultTypesByReflection(appContext,
						"defaultCharTypeChars",
						"gov.nih.nci.cananolab.domain.characterization.invivo.InvivoCharacterization");
		SortedSet<String> chemicalAssocs = charsService
				.getDefaultTypesByReflection(appContext,
						"defaultCharTypeChars",
						"gov.nih.nci.cananolab.domain.particle.ChemicalAssociation");

		request.setAttribute("physicoChars", physicoChars);
		request.setAttribute("invitroChars", invitroChars);
		request.setAttribute("invivoChars", invivoChars);
		request.setAttribute("chemicalAssocs", chemicalAssocs);

		System.out.println("chemical associations size: "
				+ chemicalAssocs.size());
		return mapping.findForward("summaryEdit");
		// return mapping.findForward("summaryEdit");
	}

	/**
	 *
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception<commit_msg>modified for data availability feature<commit_after>package gov.nih.nci.cananolab.ui.sample;

/**
 * This class sets up the submit a new sample page and submits a new sample
 *
 * @author pansu
 */

/* CVS $Id: SubmitNanoparticleAction.java,v 1.37 2008-09-18 21:35:25 cais Exp $ */

import gov.nih.nci.cananolab.dto.common.PointOfContactBean;
import gov.nih.nci.cananolab.dto.common.UserBean;
import gov.nih.nci.cananolab.dto.particle.SampleBean;
import gov.nih.nci.cananolab.exception.DuplicateEntriesException;
import gov.nih.nci.cananolab.exception.NotExistException;
import gov.nih.nci.cananolab.exception.SampleException;
import gov.nih.nci.cananolab.exception.SecurityException;
import gov.nih.nci.cananolab.service.sample.SampleService;
import gov.nih.nci.cananolab.service.sample.impl.SampleServiceLocalImpl;
import gov.nih.nci.cananolab.ui.core.BaseAnnotationAction;
import gov.nih.nci.cananolab.ui.core.InitSetup;
import gov.nih.nci.cananolab.ui.security.InitSecuritySetup;
import gov.nih.nci.cananolab.util.StringUtils;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;

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

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 SampleAction extends BaseAnnotationAction {
	// logger
	// private static Logger logger = Logger.getLogger(ReviewDataAction.class);

	/**
	 * Save or update POC data.
	 *
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaValidatorForm theForm = (DynaValidatorForm) form;
		SampleBean sampleBean = (SampleBean) theForm.get("sampleBean");
		setServiceInSession(request);
		saveSample(request, sampleBean);
		request.getSession().setAttribute("updateSample", "true");
		request.setAttribute("theSample", sampleBean);

		return mapping.findForward("summaryEdit");
	}

	private void saveSample(HttpServletRequest request, SampleBean sampleBean)
			throws Exception {
		UserBean user = (UserBean) (request.getSession().getAttribute("user"));
		sampleBean.setupDomain(user.getLoginName());
		// persist in the database
		SampleService service = (SampleService) request.getSession()
				.getAttribute("sampleService");
		service.saveSample(sampleBean);

		ActionMessages messages = new ActionMessages();
		ActionMessage msg = null;
		String updateSample = (String) request.getSession().getAttribute(
				"updateSample");
		if (!StringUtils.isEmpty(updateSample)) {
			msg = new ActionMessage("message.updateSample");
			messages.add(ActionMessages.GLOBAL_MESSAGE, msg);
			saveMessages(request, messages);
		}
	}

	/**
	 * Handle view sample request on sample search result page (read-only view).
	 *
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward summaryView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaValidatorForm theForm = (DynaValidatorForm) form;
		this.setServiceInSession(request);

		// "setupSample()" will retrieve and return the SampleBean.
		SampleBean sampleBean = setupSample(theForm, request);
		theForm.set("sampleBean", sampleBean);
		return mapping.findForward("summaryView");
	}

	public ActionForward input(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String browserDispatch = getBrowserDispatch(request);
		// from cloning form
		if (browserDispatch.equals("clone")) {
			return mapping.findForward("cloneInput");
		} else {
			return mapping.findForward("createInput");
		}
	}

	public ActionForward setupView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaValidatorForm theForm = (DynaValidatorForm) form;
		this.setServiceInSession(request);
		// "setupSample()" will retrieve and return the SampleBean.
		SampleBean sampleBean = setupSample(theForm, request);
		theForm.set("sampleBean", sampleBean);
		return mapping.findForward("bareSummaryView");
	}

	/**
	 * Handle edit sample request on sample search result page (curator view).
	 *
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward summaryEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaValidatorForm theForm = (DynaValidatorForm) form;
		this.setServiceInSession(request);

		// "setupSample()" will retrieve and return the SampleBean.
		SampleBean sampleBean = setupSample(theForm, request);		
		theForm.set("sampleBean", sampleBean);
		request.getSession().setAttribute("updateSample", "true");
		setupLookups(request, sampleBean.getPrimaryPOCBean().getDomain()
				.getOrganization().getName());
		//

		// Feature request [26487] Deeper Edit Links.
		// String dispatch = request.getParameter("dispatch"); // as the
		// function
		// // is shared.
		// if ("summaryEdit".equals(dispatch)
		// || "removePointOfContact".equals(dispatch)) {
		// if (sampleBean.getPrimaryPOCBean() != null
		// && sampleBean.getOtherPOCBeans().isEmpty()) {
		// StringBuilder sb = new StringBuilder();
		// sb.append("openOnePointOfContact(");
		// sb.append(sampleBean.getPrimaryPOCBean().getDomain().getId());
		// sb.append(", true)");
		// request.setAttribute("onloadJavascript", sb.toString());
		// }
		// }

		/*InitSetup charsService = InitSetup.getInstance();
		ServletContext appContext = request.getSession().getServletContext();
		
		Map<String, Map<String, SortedSet<String>>> defaultLookup = (HashMap<String, Map<String, SortedSet<String>>>)appContext.getAttribute("defaultLookupTable");
		if(defaultLookup != null){
			Map<String, SortedSet<String>> map = defaultLookup.get("MINChar");
			Collection<Map<String, SortedSet<String>>> lookupValues  = defaultLookup.values();
			//lookupValues.
		}*/
		
/*
		request.setAttribute("physicoChars", physicoChars);
		request.setAttribute("invitroChars", invitroChars);
		request.setAttribute("invivoChars", invivoChars);
		request.setAttribute("chemicalAssocs", chemicalAssocs);

		System.out.println("chemical associations size: "
				+ chemicalAssocs.size());*/
		return mapping.findForward("summaryEdit");
		// return mapping.findForward("summaryEdit");
	}

	/**
	 *
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
{'fraction_non_alphanumeric': 0.1167802029751147, 'fraction_numerical': 0.0030585291255387182, 'mean_word_length': 5.037347880822493, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, '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': '142792', 'n_tokens_mistral': 4570, 'n_tokens_neox': 4100, 'n_words': 866}