📄 onetomanycrudcontroller.java
字号:
/** * Adds a new row to the collection. Note that no call to the service layer * is made. * @param request The request * @param response The response * @param command The command object * @param errors The bind Exception * @return model and view * @throws Exception */ protected ModelAndView addRow(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { if (logger.isInfoEnabled()) logger.info("addRow() invoked."); List list = (List) PropertyUtils.getSimpleProperty(command, collectionPropertyName); Object element = collectionElementClass.newInstance(); if (logger.isInfoEnabled()) logger.info("element of type " + element.getClass() + " added to collection " + collectionPropertyName); PropertyUtils.setSimpleProperty(element, "processType", "insert"); list.add(element); // skip any errors request.setAttribute(SKIP_ERRORS, "skip"); return showForm(request, response, errors); } /** * Removes the ignored row from the collection. Note that no call to the * service layer is made. * @param request The request * @param response The response * @param command The command object * @param errors The bind Exception * @return model and view * @throws Exception */ protected ModelAndView ignoreRow(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { if (logger.isInfoEnabled()) logger.info("ignoreRow() invoked."); // remove the ignored row from the collection. List list = (List) PropertyUtils.getSimpleProperty(command, collectionPropertyName); Iterator it = list.iterator(); while (it.hasNext()) { CollectionElement row = (CollectionElement) it.next(); if (IGNORE.equals(row.getProcessType())) { it.remove(); } } // skip any errors request.setAttribute(SKIP_ERRORS, "skip"); return showForm(request, response, errors); } /** * Save object. Processes exceptions OptimisticLockingException, * UniqueConstraintException and NoParentRowFkException, IPropertyException. * Also stores a transaction token in session and saves a success message in * message queue for jsps. * @param request The request * @param response The response * @param command The command object * @param errors The bind exception * @return ModelAndView the model and view * @throws Exception */ protected ModelAndView save(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { if (logger.isInfoEnabled()) logger.info("save() invoked."); try { saveMethod.invoke(service, new Object[] { command }); saveMessage(request, "save.success"); // to prevent duplicate submissions create a new token saveToken(request); return showForm(request, response, errors); } catch (InvocationTargetException ite) { Throwable t = ite.getTargetException(); if (t instanceof OptimisticLockingException) { errors.reject(((OptimisticLockingException) t).getErrorCode()); return showForm(request, response, errors); } else if (t instanceof UniqueConstraintException) { populatePropertyErrors(errors, (IPropertyException) t); return showFormWhenException(request, response, command, errors); } else if (t instanceof NoParentRowFkException) { populatePropertyErrors(errors, (IPropertyException) t); return showFormWhenException(request, response, command, errors); } else if (t instanceof IPropertyException) { populatePropertyErrors(errors, (IPropertyException) t); return showFormWhenException(request, response, command, errors); } else throw (Exception) t; } } /** * Gets the object from the service layer. Also saves a ViewHelper for the * jsps to help render the view appropriately and transaction token to * prevent multiple submits. * @param request the http request * @param pk The primary key * @return The object */ protected Object getObject(HttpServletRequest request, Object pk) { if (logger.isInfoEnabled()) logger.info("getObject invoked."); try { Object result = getObjectMethod .invoke(service, new Object[] { pk }); saveViewHelper(request, ViewHelper.SUBMIT_MODE); // initialize the synchronizer token. Is used in submit // processing to prevent duplicate submits saveToken(request); return result; } catch (Exception e) { throw new RuntimeException(e); } } /** * Starts the flow which will set the value for a field. Stores a * <code>FlowSetValueHelper</code> in users session. Expects to find the * following parameters in the request: COLLECTION_IDX, * TARGET_PROPERTY_NAME_FOR_VALUE, SUB_FLOW * @param request the request * @param response the response * @param command the command * @param errors the bind exception * @return @throws Exception */ protected ModelAndView startFlowSetValue(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { if (logger.isInfoEnabled()) logger.info("startFlowSetValue() invoked."); String collectionIdx = request.getParameter(COLLECTION_IDX); Validate.notEmpty(collectionIdx, "http request parameter 'collectionIdx' cannot be null"); String targetPropertyNameForValue = request .getParameter(TARGET_PROPERTY_NAME_FOR_VALUE); Validate .notEmpty(targetPropertyNameForValue, "http request parameter 'targetPropertyNameForValue' cannot be null"); String subFlow = request.getParameter(SUB_FLOW); Validate.notEmpty(collectionIdx, "http request parameter 'subFlow' cannot be null"); if (logger.isInfoEnabled()) { logger.info(COLLECTION_IDX + "=" + collectionIdx); logger.info(TARGET_PROPERTY_NAME_FOR_VALUE + "=" + targetPropertyNameForValue); logger.info(SUB_FLOW + "=" + subFlow); } FlowSetValueHelper flowHelper = new FlowSetValueHelper(collectionIdx, targetPropertyNameForValue, (String) request.getSession() .getAttribute(TOKEN_ATTRIBUTE_NAME)); request.getSession().setAttribute(getFlowSetValueHelperAttributeName(), flowHelper); // store the command object in the session and redirect to the subflow // controller request.getSession().setAttribute(getFormSessionAttributeName(), command); return new ModelAndView(getView(request, subFlow)); } /** * Cancels the flow which has started to set a value of a field. * @param request the request * @param response the response * @param command the command * @param errors the bind exception * @return @throws Exception */ protected ModelAndView cancelFlowSetValue(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { if (logger.isInfoEnabled()) logger.info("cancelFlowSetValue() invoked."); FlowSetValueHelper flowHelper = (FlowSetValueHelper) request .getSession() .getAttribute(getFlowSetValueHelperAttributeName()); // Make sure the current token is same as token in helper. String token = request.getParameter(TOKEN_PARAMETER_NAME); Validate.notEmpty(token, "The transaction token not available in the http request"); // The token could have changed since the flow helper was last stored in // session. Make sure the token is valid if (!token.equals(flowHelper.getToken())) throw new InvalidSynchronizerTokenException( "transaction token mismatch with flow helper"); request.getSession().removeAttribute( getFlowSetValueHelperAttributeName()); // create new BindException so that no bind errors are shown return showForm(request, response, createNewBindException(request, command, getCommandName())); } /** * Assigns the selected value to the target collection element. Expects the * parameter SELECTED_VALUE to be in the request. Also makes sure the * transaction token is valid * @param request The request * @param response The response * @param command The command * @param errors The bind exception * @return @throws Exception */ protected ModelAndView assignSelectedValue(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { if (logger.isInfoEnabled()) logger.info("assignSelectedValue() invoked."); FlowSetValueHelper flowHelper = (FlowSetValueHelper) request .getSession() .getAttribute(getFlowSetValueHelperAttributeName()); Validate.notNull(flowHelper, "Could not find flow helper in session:" + getFlowSetValueHelperAttributeName()); // Make sure the current token is same as token in helper. String token = request.getParameter(TOKEN_PARAMETER_NAME); Validate.notEmpty(token, "The transaction token not available in the http request"); // The token could have changed since the flow helper was last stored in // session. Make sure the token is valid if (!token.equals(flowHelper.getToken())) throw new InvalidSynchronizerTokenException( "transaction token mismatch with flow helper"); List list = (List) PropertyUtils.getSimpleProperty(command, collectionPropertyName); Object element = list.get(new Integer(flowHelper.getCollectionIdx()) .intValue()); String selectedValue = request.getParameter(SELECTED_VALUE); Validate.notEmpty(selectedValue, "'selectedValue' parameter not available in the http request"); if (String.class.equals(PropertyUtils.getPropertyType(element, flowHelper.getTargetPropertyNameForValue()))) { PropertyUtils.setSimpleProperty(element, flowHelper .getTargetPropertyNameForValue(), selectedValue); } else { //the assumption is if property being set is not a String it is a // Long. //TODO fix this PropertyUtils.setSimpleProperty(element, flowHelper .getTargetPropertyNameForValue(), new Long(selectedValue)); } // create a new BindException to override any bind exceptions created // during binding return showForm(request, response, createNewBindException(request, command, getCommandName())); } /** * get the view for the page which will be used to select a value * @param request The request * @param subFlow The sub flow url. * @return the view */ private String getView(HttpServletRequest request, String subFlow) { if (subFlow.indexOf('?') != -1) return "redirect:" + subFlow + "&parentFlow=" + getControllerPath(request); else return "redirect:" + subFlow + "?parentFlow=" + getControllerPath(request); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -