<commit_before>/*
    jBilling - The Enterprise Open Source Billing System
    Copyright (C) 2003-2009 Enterprise jBilling Software Ltd. and <NAME>

    This file is part of jbilling.

    jbilling is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    jbilling is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with jbilling.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * Created on 17-Mar-2003
 *
 * Copyright Sapienter Enterprise Software
 */
package com.sapienter.jbilling.client.order;

import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;

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

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
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.Resources;

import com.sapienter.jbilling.client.util.Constants;
import com.sapienter.jbilling.client.util.FormHelper;
import com.sapienter.jbilling.common.SessionInternalError;
import com.sapienter.jbilling.server.customer.ICustomerSessionBean;
import com.sapienter.jbilling.server.item.ItemDecimalsException;
import com.sapienter.jbilling.server.order.IOrderSessionBean;
import com.sapienter.jbilling.server.order.db.OrderDTO;
import com.sapienter.jbilling.server.order.db.OrderLineDTO;
import com.sapienter.jbilling.server.util.Context;
import java.math.BigDecimal;

/**
 * @author Emil
 */
public class ReviewOrderAction extends Action {
    static Logger log = null;
    private HttpSession session;
    
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

        log = Logger.getLogger(ReviewOrderAction.class);
        ActionErrors errors = new ActionErrors();
        ActionMessages messages = new ActionMessages();
        session = request.getSession(false);
        OrderDTO newOrder = (OrderDTO) session.getAttribute(
                Constants.SESSION_ORDER_SUMMARY);
        String forward = null;

        String action = request.getParameter("action");
        log.debug("Review. action = " + action);

        IOrderSessionBean remoteOrder = null;
        if (!action.equals("read")) {
            remoteOrder = (IOrderSessionBean) Context.getBean(
                    Context.Name.ORDER_SESSION);
        }

        try {
            if (action.equals("setup")) {
                // get the processing of this order, so the taxes, etc, show up
                newOrder = remoteOrder.recalculate(newOrder,
                        (Integer) session.getAttribute(
                            Constants.SESSION_ENTITY_ID_KEY));
                // initializing the wraping form to allow displaying and
                // updating the dto
                Hashtable hashlines = new Hashtable();
                ((NewOrderDTOForm) form).setOrderLines(hashlines);
                // the price has to be formated i18n
                for(OrderLineDTO line : newOrder.getLines()) {
                	if (line.getDeleted() == 0) {
        	            line.setPriceStr(FormHelper.float2string(
            	                line.getPrice().floatValue(), session));
                    	hashlines.put(line.getItemId(), line);
                    }
                }
                
                log.debug("The form has been set");
                forward = "show";
            } else if (action.equals("cancel")) {
                // garbage collect the stuff from the session
                FormHelper.cleanUpSession(session);

                forward ="list";
                messages.add(ActionMessages.GLOBAL_MESSAGE, 
                        new ActionMessage("order.canceled"));
                
            } else if (action.equals("process")) {
                // this means that the form has to be either recalculated
                // or the order is confirmed

                if (request.getParameter("recalc") != null) {
                    log.debug("recaclculate!");
                    // the price has to be formated i18n
                    for(OrderLineDTO line : newOrder.getLines()) {
                        if (!line.getEditable().booleanValue()) //probalby a tax
                            continue;
                        line.setPrice(new BigDecimal(FormHelper.string2float(
                                line.getPriceStr(), session)));
                        log.debug("line = " + line);
                        if (line.getPrice() == null) {
                            String field = Resources.getMessage(request, 
                                    "order.line.prompt.price");
                            errors.add(ActionErrors.GLOBAL_ERROR,
                                    new ActionError("errors.float", field));
                            saveErrors(request, errors);
                            forward = "show";
                            break;
                        }
                    }

                    if (errors.isEmpty()) {
                        newOrder = remoteOrder.recalculate(newOrder,
                                (Integer) session.getAttribute(
                                    Constants.SESSION_ENTITY_ID_KEY));
                        forward = "show";
                    }
                } else if (request.getParameter("commit") != null) {
                    log.debug("commit !");
                    // create or update the order record
                    Integer orderId = remoteOrder.createUpdate(
                            (Integer) session.getAttribute(
                            Constants.SESSION_ENTITY_ID_KEY),
                            (Integer) session.getAttribute(<commit_msg>Fixed an NPE in edit order when price is null<commit_after>/*
    jBilling - The Enterprise Open Source Billing System
    Copyright (C) 2003-2009 Enterprise jBilling Software Ltd. and <NAME>

    This file is part of jbilling.

    jbilling is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    jbilling is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with jbilling.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * Created on 17-Mar-2003
 *
 * Copyright Sapienter Enterprise Software
 */
package com.sapienter.jbilling.client.order;

import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;

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

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
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.Resources;

import com.sapienter.jbilling.client.util.Constants;
import com.sapienter.jbilling.client.util.FormHelper;
import com.sapienter.jbilling.common.SessionInternalError;
import com.sapienter.jbilling.server.customer.ICustomerSessionBean;
import com.sapienter.jbilling.server.item.ItemDecimalsException;
import com.sapienter.jbilling.server.order.IOrderSessionBean;
import com.sapienter.jbilling.server.order.db.OrderDTO;
import com.sapienter.jbilling.server.order.db.OrderLineDTO;
import com.sapienter.jbilling.server.util.Context;
import java.math.BigDecimal;

/**
 * @author Emil
 */
public class ReviewOrderAction extends Action {
    static Logger log = null;
    private HttpSession session;
    
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

        log = Logger.getLogger(ReviewOrderAction.class);
        ActionErrors errors = new ActionErrors();
        ActionMessages messages = new ActionMessages();
        session = request.getSession(false);
        OrderDTO newOrder = (OrderDTO) session.getAttribute(
                Constants.SESSION_ORDER_SUMMARY);
        String forward = null;

        String action = request.getParameter("action");
        log.debug("Review. action = " + action);

        IOrderSessionBean remoteOrder = null;
        if (!action.equals("read")) {
            remoteOrder = (IOrderSessionBean) Context.getBean(
                    Context.Name.ORDER_SESSION);
        }

        try {
            if (action.equals("setup")) {
                // get the processing of this order, so the taxes, etc, show up
                newOrder = remoteOrder.recalculate(newOrder,
                        (Integer) session.getAttribute(
                            Constants.SESSION_ENTITY_ID_KEY));
                // initializing the wraping form to allow displaying and
                // updating the dto
                Hashtable hashlines = new Hashtable();
                ((NewOrderDTOForm) form).setOrderLines(hashlines);
                // the price has to be formated i18n
                for(OrderLineDTO line : newOrder.getLines()) {
                	if (line.getDeleted() == 0) {
        	            line.setPriceStr(FormHelper.decimal2string(line.getPrice(), session));
                    	hashlines.put(line.getItemId(), line);
                    }
                }
                
                log.debug("The form has been set");
                forward = "show";
            } else if (action.equals("cancel")) {
                // garbage collect the stuff from the session
                FormHelper.cleanUpSession(session);

                forward ="list";
                messages.add(ActionMessages.GLOBAL_MESSAGE, 
                        new ActionMessage("order.canceled"));
                
            } else if (action.equals("process")) {
                // this means that the form has to be either recalculated
                // or the order is confirmed

                if (request.getParameter("recalc") != null) {
                    log.debug("recaclculate!");
                    // the price has to be formated i18n
                    for(OrderLineDTO line : newOrder.getLines()) {
                        if (!line.getEditable().booleanValue()) //probalby a tax
                            continue;
                        line.setPrice(FormHelper.string2decimal(line.getPriceStr(), session));
                        log.debug("line = " + line);
                        if (line.getPrice() == null) {
                            String field = Resources.getMessage(request, 
                                    "order.line.prompt.price");
                            errors.add(ActionErrors.GLOBAL_ERROR,
                                    new ActionError("errors.float", field));
                            saveErrors(request, errors);
                            forward = "show";
                            break;
                        }
                    }

                    if (errors.isEmpty()) {
                        newOrder = remoteOrder.recalculate(newOrder,
                                (Integer) session.getAttribute(
                                    Constants.SESSION_ENTITY_ID_KEY));
                        forward = "show";
                    }
                } else if (request.getParameter("commit") != null) {
                    log.debug("commit !");
                    // create or update the order record
                    Integer orderId = remoteOrder.createUpdate(
                            (Integer) session.getAttribute(
                            Constants.SESSION_ENTITY_ID_KEY),
                            (Integer) session.getAttribute(
{'fraction_non_alphanumeric': 0.08509827822642084, 'fraction_numerical': 0.003504494895627, 'mean_word_length': 1.9061323887535975, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '532740', 'n_tokens_mistral': 3307, 'n_tokens_neox': 3171, 'n_words': 831}