/* * This file was automatically generated by MD4J. * * Licensed under the GNU General Public License, Version 2.0 (the "License") or above; * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * *http://www.gnu.org/licenses/gpl.txt * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package gr.abiss.md4j.sampledomain.web; import java.io.Serializable; import java.util.HashMap; import java.util.Map; import java.util.Iterator; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.log4j.Logger; import org.apache.commons.lang.math.NumberUtils; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; import org.apache.struts.action.ActionMapping; import org.apache.commons.beanutils.BeanUtils; import javax.ejb.EJBException; import gr.abiss.md4j.dto.File; import gr.abiss.md4j.business.BrokenConstraintsException; import org.apache.struts.action.Action; import gr.abiss.md4j.util.StringUtil; import gr.abiss.md4j.sampledomain.web.CityCrudForm; import gr.abiss.md4j.sampledomain.business.CityManagerLocal; import gr.abiss.md4j.sampledomain.business.CityManagerUtil; /** * @struts.action path="/City/Create" * name="CityCrudForm" * input="/City/LoadCreate.do" * scope="request" * validate="true" * * * @struts.action-forward path="/City/View.do" * name="CityCreateAction" redirect="true" * */ public class CityCreateAction extends Action{ private static Logger log = Logger.getLogger(CityCreateAction.class); /** * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ public ActionForward execute(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) { ActionForward fwd = null; try{ CityCrudForm form = (CityCrudForm) actionForm; if(this.isTokenValid(request, true)) { CityManagerLocal manager = CityManagerUtil.getLocalHome().create(); Map params = form.toMap(); // md4j:oncreate-update-code-before-business form.setId(manager.save(params).toString()); ActionForward basefw = mapping.findForward("CityCreateAction"); fwd = new ActionForward(basefw.getPath()+"?id="+java.net.URLEncoder.encode(form.getId(), "UTF-8"), true); } // invalid token, try to redirect to view if identifier is available else if(form.getId() != null){ ActionForward basefw = mapping.findForward("CityCreateAction"); fwd = new ActionForward(basefw.getPath()+"?id="+java.net.URLEncoder.encode(form.getId(), "UTF-8"), true); } // if no identifier is available, save an error message and FW to error page else { ActionMessages messages = new ActionMessages(); messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("invalid input, probably taken value for unique field")); this.addMessages(request, messages); } } catch (EJBException e) { // see if this is a business validation caused exception, report as appropriate Exception cause = e.getCausedByException(); if(cause.getClass().equals(BrokenConstraintsException.class)){ ActionMessages messages = new ActionMessages(); for(Iterator iter = ((BrokenConstraintsException) cause).getViolated().iterator();iter.hasNext();){ String key = (String) iter.next(); // is this the right key? messages.add(key, new ActionMessage("errors.unique")); this.addMessages(request, messages); } fwd = mapping.getInputForward(); } } catch (Exception e) { throw new RuntimeException(e); } return fwd != null ? fwd : mapping.findForward("error"); } }