/**
 * <p>
 * Title: RequestDetailsAction Class>
 * <p>
 * Description: This class initializes the fields of RequestDetails.jsp Page
 * </p>
 * Copyright: Copyright (c) year Company: Washington University, School of
 * Medicine, St. Louis.
 *
 * @author Ashish Gupta
 * @version 1.00 Created on Oct 05,2006
 */

package edu.wustl.catissuecore.action;

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.json.JSONObject;

import com.google.gson.Gson;

import edu.wustl.catissuecore.bizlogic.OrderBizLogic;
import edu.wustl.catissuecore.dto.OrderStatusDTO;
import edu.wustl.catissuecore.dto.OrderSubmissionDTO;
import edu.wustl.catissuecore.util.OrderingSystemUtil;
import edu.wustl.catissuecore.util.global.AppUtility;
import edu.wustl.catissuecore.util.global.Constants;
import edu.wustl.common.action.BaseAction;
import edu.wustl.common.beans.SessionDataBean;
import edu.wustl.dao.HibernateDAO;

/**
 * @author renuka_bajpai
 */
public class SubmitOrderAction extends BaseAction
{

	/**
	 * Overrides the executeSecureAction method of SecureAction class.
	 * @param mapping
	 *            object of ActionMapping
	 * @param form
	 *            object of ActionForm
	 * @param request
	 *            object of HttpServletRequest
	 * @param response
	 *            object of HttpServletResponse
	 * @throws Exception
	 *             generic exception
	 * @return ActionForward : ActionForward
	 */
	@Override
	public ActionForward executeAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception
			{
		
		String dataJSON = request.getParameter("dataJSON");
		Gson gson = new Gson();
		
		OrderSubmissionDTO orderSubmissionDTO=gson.fromJson(dataJSON, OrderSubmissionDTO.class);
	
		JSONObject jsonObject=new JSONObject(dataJSON);
	    orderSubmissionDTO.setOrderItemSubmissionDTOs(OrderingSystemUtil.getOrderItemDTOs(jsonObject.get("gridXML").toString()));
	    
		OrderBizLogic orderBizLogic=new OrderBizLogic();
		
		SessionDataBean sessionDataBean=(SessionDataBean)request.getSession().getAttribute(Constants.SESSION_DATA);
		Long userId=sessionDataBean.getUserId();
		HibernateDAO dao=null;
		StringBuilder errors=new StringBuilder();
		try
		{
			dao=(HibernateDAO)AppUtility.openDAOSession(sessionDataBean);
			OrderStatusDTO orderStatusDTO=orderBizLogic.updateOrder(orderSubmissionDTO,userId,dao);
			
			dao.commit();
			
			if(orderStatusDTO.getSpecimensWithError().isEmpty())
			{	
				dao.commit();
				orderBizLogic.sendOrderUpdateEmail(
						orderSubmissionDTO.getRequestorName(),
						orderSubmissionDTO.getRequestorEmail(),
						sessionDataBean.getUserName(),
						orderSubmissionDTO.getOrderName(),
						sessionDataBean.getLastName()+ sessionDataBean.getFirstName(),
						orderStatusDTO.getStatus(), request.getRequestURL()	+ "?id=" + orderStatusDTO.getOrderId());
			}
			else
			{	
				for (String error: orderStatusDTO.getSpecimensWithError()) {
					errors.append(error).append("\n");
				}
			}
			response.getWriter().write("Success");	
		}
		catch(Exception exception)
		{
			response.getWriter().write(errors.toString());	
		}
		finally
		{
			AppUtility.closeDAOSession(dao);
		}
		return null;
 }
}	
@@ -2669,31 +2669,8 @@
 %09%09%0D%0A
-%09%09%09dao.commit();%0D%0A%09%09%09%0D%0A
 %09%09%09i
@@ -2689,34 +2689,30 @@
 sDTO.get
-SpecimensWith
+Order
 Error
+DTOs
 ().isEmp
@@ -2994,16 +2994,20 @@
 tName()+
+%22,%22+
  session
@@ -3132,16 +3132,62 @@
 Id());%0D%0A
+%09%09%09%09response.getWriter().write(%22Success%22);%0D%0A%0D%0A
 %09%09%09%7D%0D%0A%09%09
@@ -3208,124 +3208,53 @@
 %09%09%09%09
-for (
 String 
-error: orderStatusDTO.getSpecimensWithError()) %7B%0D%0A%09%09%09%09%09errors.append(error).append(%22%5Cn%22);%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D%0D%0A
+json = gson.toJson(orderStatusDTO);%0D%0A%09
 %09%09%09r
@@ -3279,27 +3279,33 @@
 ).write(
-%22Success%22);
+json);%0D%0A%09%09%09%7D%0D%0A%09%09%09
 %09%0D%0A%09%09%7D%0D%0A

Order error related changes.