/*
 * 
 * Created on Jun 11, 2005
 * 
 * Gy�rgy S�veges
 * Etixpert Kft.
 * Copyright(c) 2005
 * 
 */
package com.etixpert.evolution.gui.servlets.pageactions;

import java.util.*;

import javax.servlet.http.*;

import com.etixpert.evolution.gui.servlets.*;
import org.hibernate.*;
import com.etixpert.evolution.*;
import java.sql.*;

/**
 * @author Suveges Gyorgy
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class orderpaymentstatuschange extends PageActionHandler {

    /**
     * 
     */
    public orderpaymentstatuschange() {
        super();
        // TODO Auto-generated constructor stub
    }

    /* (non-Javadoc)
     * @see com.etixpert.evolution.gui.servlets.pageactions.PageActionHandler#handle(javax.servlet.http.HttpServletRequest)
     */
    protected void handle(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        Map m = (Map) session.getAttribute("list_orders.jsp"+request.getParameter("status"));
        String[] ids = OrderListHelper.getVal(m, "payment_status_cb");
        
        if (ids == null) return;
        logger.info("size of orders: "+ids.length);
        Set orderids = new TreeSet();
        for (int i=0; i<ids.length; i++)
           orderids.add(new Long(ids[i]));
        
        Session hsession = HibernateUtil.currentSession();
        Transaction tx = hsession.beginTransaction();
        Connection conn = hsession.connection();
        Statement stat = conn.createStatement();
        String sql = "update order_header set payment_status=true where id in "+Tools.idsToSql(orderids, false);
        logger.info("Changing payment status: "+sql);
        int affected = stat.executeUpdate(sql);
        logger.info("affected rows: "+affected);
        tx.commit();
        HibernateUtil.closeSession();
        m.remove("payment_status_cb");
        
  
    }

}
