package org.ofbiz.extended;

import java.util.HashMap;
import java.util.Map;

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

import org.ofbiz.entity.GenericValue;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ModelService;

public class OrderEvents {

	public static String cancelOrder(HttpServletRequest request, HttpServletResponse response) {
		LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
		GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
		String orderId = request.getParameter("orderId");
		if (orderId == null || orderId.length() == 0) {
			String errorMsg = "parameter orderId not valid";
			request.setAttribute("_ERROR_MESSAGE_", errorMsg);
			return "error";
		}

		Map cancelOrderContext = new HashMap();
		cancelOrderContext.put("orderId", orderId);
		cancelOrderContext.put("userLogin", userLogin);
		try {
			Map result = dispatcher.runSync("cancelOrder", cancelOrderContext);
			if (result.containsKey(ModelService.ERROR_MESSAGE)) {
				throw new Exception((String) result.get(ModelService.ERROR_MESSAGE));
			}
			
		} catch (Exception e) {
			request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
			return "error";
		}
		return "success";
	}
}
