/**
 * 
 */
package gov.nih.nci.calab.ui.submit;

import gov.nih.nci.calab.dto.characterization.CharacterizationBean;
import gov.nih.nci.calab.service.submit.SubmitNanoparticleService;
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 java.util.List;

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;

/**
 * @author zengje
 * 
 */
public class DeleteMultiCharacterizationAction extends AbstractDispatchAction {

	public ActionForward setup(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionForward forward = null;

		DynaValidatorForm theForm = (DynaValidatorForm) form;
		String particleName = (String) theForm.get("particleName");
		String particleType = (String) theForm.get("particleType");

		// setCharacterizationTypeCharacterizations
		String deleteType = request.getParameter("charCategory");
		SubmitNanoparticleService service = new SubmitNanoparticleService();
		List<CharacterizationBean> charBeans = service
				.getAllCharacterizationByType(particleName, particleType,
						deleteType);

		// convert charBeans to array due to the struts-config
		theForm.set("charBeans", charBeans
				.toArray(new CharacterizationBean[charBeans.size()]));
		
		forward = mapping.findForward("success");

		return forward;
	}

	public ActionForward deleteConfirmed(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionForward forward = null;

		DynaValidatorForm theForm = (DynaValidatorForm) form;
		String[] charIds = (String[])theForm.get("charIds");
		String particleType = (String)theForm.get("particleType");
		String particleName = (String)theForm.get("particleName");
		
		// setCharacterizationTypeCharacterizations
		SubmitNanoparticleService service = new SubmitNanoparticleService();
		service.deleteCharacterizations(particleName, particleType, charIds);

		// signal the session that characterization has been changed
		request.getSession().setAttribute("newCharacterizationCreated", "true");
		
		InitSessionSetup.getInstance().setSideParticleMenu(request,
				particleName, particleType);
		
		ActionMessages msgs = new ActionMessages();
		ActionMessage msg = new ActionMessage("message.delete.characterization");
		msgs.add("message", msg);
		saveMessages(request, msgs);

		forward = mapping.findForward("success");

		return forward;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see gov.nih.nci.calab.ui.core.AbstractDispatchAction#loginRequired()
	 */
	@Override
	public boolean loginRequired() {
		// TODO Auto-generated method stub
		return true;
	}

}

@@ -360,16 +360,38 @@
 il.List;
+%0Aimport java.util.Map;
 %0A%0Aimport
@@ -1121,427 +1121,330 @@
 rm;%0A
-%09%09String particleName = (String) theForm.get(%22particleName%22);%0A%09%09String particleType = (String) theForm.get(%22particleType%22);%0A%0A%09%09// setCharacterizationTypeCharacterizations%0A%09%09String deleteType = request.getParameter(%22charCategory%22);%0A%09%09SubmitNanoparticleService service = new SubmitNanoparticleService();%0A%09%09List%3CCharacterizationBean%3E charBeans = service%0A%09%09%09%09.getAllCharacterizationByType(particleName, particleType,%0A%09%09%09%09%09%09
+%0A%09%09// setCharacterizationTypeCharacterizations%0A%09%09String deleteType = request.getParameter(%22charCategory%22);%0A%09%09Map%3CString, List%3CCharacterizationBean%3E%3E charsMap = (Map%3CString, List%3CCharacterizationBean%3E%3E) (request%0A%09%09%09%09.getSession().getAttribute(%22allCharacterizations%22));%0A%09%09List%3CCharacterizationBean%3E charBeans = charsMap.get(
 dele
@@ -1510,16 +1510,44 @@
 -config%0A
+%09%09if (charBeans != null) %7B%0A%09
 %09%09theFor
@@ -1575,16 +1575,17 @@
 arBeans%0A
+%09
 %09%09%09%09.toA
@@ -1637,16 +1637,69 @@
 )%5D));%0A%09%09
+%09request.setAttribute(%22charBeansValue%22, %22true%22);%0A%09%09%7D%0A
 %0A%09%09forwa
@@ -1810,33 +1810,36 @@
 Mapping mapping,
- 
+%0A%09%09%09
 ActionForm form,
@@ -1830,36 +1830,33 @@
 ActionForm form,
-%0A%09%09%09
+ 
 HttpServletReque
@@ -1858,33 +1858,36 @@
 Request request,
- 
+%0A%09%09%09
 HttpServletRespo
@@ -1891,36 +1891,33 @@
 sponse response)
-%0A%09%09%09
+ 
 throws Exception
@@ -2039,16 +2039,17 @@
 tring%5B%5D)
+ 
 theForm.
@@ -2088,32 +2088,33 @@
 eType = (String)
+ 
 theForm.get(%22par
@@ -2158,16 +2158,17 @@
 (String)
+ 
 theForm.
@@ -2188,18 +2188,16 @@
 Name%22);%0A
-%09%09
 %0A%09%09// se
@@ -2517,18 +2517,16 @@
 true%22);%0A
-%09%09
 %0A%09%09InitS
@@ -2613,18 +2613,16 @@
 eType);%0A
-%09%09
 %0A%09%09Actio

Use session variable for characterization category