⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addtocartaction.java

📁 OPIAM stands for Open Identity and Access Management. This Suite will provide modules for user & rig
💻 JAVA
字号:
/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.struts.actions;

//DW/2622/BeginPatch
import opiam.admin.faare.MessageUtil;
//DW/2622/EndPatch

import org.apache.commons.beanutils.PropertyUtils;

import org.apache.log4j.Logger;

import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

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


/**
 *
 * This action allows to add in a list a selected subset of elements from another list.
 * Target list is created if it does not already exist.
 * Checks that elements are not already in the list.
 *<br>
 * HTTP PARAMETERS :<br>
 * <li>cartname : target list name</li>
 * <li>srcname  : source object name</li>
 * <li>srcproperty : attribute containing list in source object, or null if the source object is the list</li>
 * <li>eltindex : multivalued elements indexes</li>
 *
 */
public class AddToCartAction extends SecureAction
{
    /** Logger de log4j. */
    private static Logger _logger = Logger.getLogger(AddToCartAction.class);

    /**
     * Struts Action method.
     * See SecureAction
     *
     * @param mapping Struts mapping data.
     * @param actionForm Input form.
     * @param request HTTP request.
     * @param httpServletResponse HTTP response.
     *
     * @return forward to success or to error
     *
     * @throws java.io.IOException see Action.execute()
     * @throws javax.servlet.ServletException see Action.execute()
     */
    public ActionForward secureExecute(
        ActionMapping mapping,
        ActionForm actionForm,
        HttpServletRequest request,
        HttpServletResponse httpServletResponse)
        throws IOException, ServletException
    {
        String cartname = request.getParameter("cartname");
        String srcname = request.getParameter("srcname");
        String srcproperty = request.getParameter("srcproperty");
        String[] elements = request.getParameterValues("eltindex");

        if (elements == null)
        {
            // aucune selection
            request.setAttribute("cartname", cartname);

            return (mapping.findForward("success"));
        }

        _logger.debug("cartname = " + cartname);
        _logger.debug("srcname = " + srcname);
        _logger.debug("srcproperty = " + srcproperty);

        ActionMessages errors = new ActionMessages();

//DW/2622/BeginPatch
        if ((cartname == null) || cartname.trim().equals("") ||
            (srcname == null) || srcname.trim().equals(""))
        {
            _logger.error(MessageUtil.getMessageString("MSG_ARGUMENT_NULL"));
            errors.add(ActionMessages.GLOBAL_MESSAGE,
                new ActionMessage("error.service.unknown"));
            saveErrors(request, errors);

            return (mapping.findForward("service_error"));
        }
//DW/2622/EndPatch

        try
        {
            HttpSession session = request.getSession();
            List currentCart = (List) session.getAttribute(cartname);

            if (currentCart == null)
            {
                // nouveau panier
                _logger.debug("cart " + cartname + " is null, will be created");
                currentCart = new ArrayList();
                session.setAttribute(cartname, currentCart);
            }

            Object objSource = session.getAttribute(srcname);
            _logger.debug("objSource class = " + objSource.getClass());

            List currentSrc = null;

            if (srcproperty == null)
            {
                currentSrc = (List) objSource;
            }
            else
            {
                currentSrc = (List) PropertyUtils.getProperty(objSource,
                        srcproperty);
            }

            Object obj = null;

            for (int i = 0; i < elements.length; i++)
            {
                obj = currentSrc.get(Integer.parseInt(elements[i]));

                if (!currentCart.contains(obj))
                {
                    currentCart.add(obj);
                }
            }

            request.setAttribute("cartname", cartname);

            return (mapping.findForward("success"));
        }
        catch (Exception se)
        {
            _logger.debug("Trace", se);
            _logger.error(se.getMessage());
            errors.add(ActionMessages.GLOBAL_MESSAGE,
                new ActionMessage("error.service.unknown"));
            saveErrors(request, errors);

            return (mapping.findForward("error"));
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -