📄 addattributefromlistaction.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.Iterator;
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 selected elements from one list to a given object.
*<br>
* HTTP PARAMETERS :<br>
* <li>objname : name of the object which will be augmented</li>
* <li>attname : name of the attribute which will be augmented</li>
* <li>srcname : name of source object</li>
* <li>srcprop : name of source attribute, which must be a list</li>
* <li>eltindex : multivalued element containing indexes of elements to get from the source list</li>
* <br><br>Example :<br>
* <li>srcname will be an object containing a opiam.admin.faare.SearchResult</li>
* <li>srcprop will be "lResults" (list of results)</li>
* <li>objname will be an object kept in session</li>
* <li>attname will be a list of entries</li>
* <li>eltindex will be set as HTTP request parameters (for example: checkboxes in a form)</li>
*
*/
public class AddAttributeFromListAction extends SecureAction
{
/** Logger de log4j. */
private static Logger _logger = Logger.getLogger(AddAttributeFromListAction.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 service_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 srcname = request.getParameter("srcname");
String srcprop = request.getParameter("srcprop");
String objname = request.getParameter("objname");
String attname = request.getParameter("attname");
String[] elements = request.getParameterValues("eltindex");
_logger.debug("srcname = " + srcname);
_logger.debug("srcprop = " + srcprop);
_logger.debug("objname = " + objname);
_logger.debug("attname = " + attname);
ActionMessages errors = new ActionMessages();
//DW/2622/BeginPatch
if ((srcname == null) || srcname.trim().equals("") ||
(srcprop == null) || srcprop.trim().equals("") ||
(objname == null) || objname.trim().equals("") ||
(attname == null) || attname.trim().equals("") ||
(elements == null))
{
_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
{
//R閏up閞ation de la session
HttpSession session = request.getSession();
Object obj = session.getAttribute(objname);
Object att = PropertyUtils.getProperty(obj, attname);
Object src = session.getAttribute(srcname);
List currentList = (List) PropertyUtils.getProperty(src, srcprop);
if (att instanceof List)
{
List attList = (List) att;
Iterator it = currentList.iterator();
for (int i = 0; i < elements.length; i++)
{
obj = currentList.get(Integer.parseInt(elements[i]));
if (!attList.contains(obj))
{
attList.add(obj);
}
}
}
return (mapping.findForward("success"));
}
catch (Exception se)
{
_logger.error(se.getMessage());
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("error.service.unknown"));
saveErrors(request, errors);
return (mapping.findForward("service_error"));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -