001: /**
002: * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: * USA
018: *
019: * Component of: Red Hat Application Server
020: *
021: * Initial Developers: Aizaz Ahmed
022: * Vivek Lakshmanan
023: * Andrew Overholt
024: * Matthew Wringe
025: *
026: */package olstore.action;
027:
028: import javax.ejb.TransactionRolledbackLocalException;
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031:
032: import olstore.dto.AddressValue;
033: import olstore.dto.CreateUserValue;
034: import olstore.form.DemoDynaBaseForm;
035: import olstore.framework.EJBHomeFactory;
036:
037: import org.apache.commons.beanutils.BeanUtils;
038: import org.apache.struts.action.ActionError;
039: import org.apache.struts.action.ActionErrors;
040: import org.apache.struts.action.ActionForm;
041: import org.apache.struts.action.ActionForward;
042: import org.apache.struts.action.ActionMapping;
043:
044: import olstore.session.helper.UserHelperLocal;
045: import olstore.session.helper.UserHelperLocalHome;
046:
047: public class UserSaveAction extends DemoBaseAction {
048:
049: /**
050: * Action to create a new user profile.
051: */
052: public ActionForward execute(ActionMapping mapping,
053: ActionForm form, HttpServletRequest request,
054: HttpServletResponse response) throws Exception {
055:
056: try {
057:
058: //get information from the form
059: DemoDynaBaseForm createUserForm = (DemoDynaBaseForm) form;
060: CreateUserValue createUserValue = new CreateUserValue();
061: AddressValue addressValue = new AddressValue();
062:
063: //copy values from the form to the dto
064: BeanUtils.copyProperties(addressValue, createUserForm);
065: BeanUtils.copyProperties(createUserValue, createUserForm);
066:
067: EJBHomeFactory factory = EJBHomeFactory.getInstance();
068: UserHelperLocalHome userHelperHome = (UserHelperLocalHome) factory
069: .getLocalHome(EJBHomeFactory.USER_HELPER);
070: UserHelperLocal userHelper = userHelperHome.create();
071:
072: //create a new user entity bean
073: userHelper.CreateUser(createUserValue, addressValue);
074:
075: //forward to a confirmation page
076: return mapping.findForward("CreateConfirm");
077:
078: }
079:
080: // if the username has already been choosen, this exception will be thrown
081: catch (TransactionRolledbackLocalException n) {
082: ActionErrors errors = new ActionErrors();
083: errors.add("error", new ActionError(
084: "errors.duplicate.username", n.getMessage()));
085: saveErrors(request, errors);
086: return (new ActionForward(mapping.getInput()));
087: }
088:
089: catch (Exception e) {
090: //Logging code here
091: ActionErrors errors = new ActionErrors();
092: errors.add("error", new ActionError("errors.type.save", e
093: .getMessage()));
094: saveErrors(request, errors);
095: // Return to same page
096: return (new ActionForward(mapping.getInput()));
097: }
098:
099: }
100:
101: }
|