Source code: com/RuntimeCollective/webapps/form/ValidatedUserGroupForm.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/form/ValidatedUserGroupForm.java,v 1.4 2003/09/30 15:13:14 joe Exp $
2 * $Revision: 1.4 $
3 * $Date: 2003/09/30 15:13:14 $
4 *
5 * ====================================================================
6 *
7 * Josephine : http://www.runtime-collective.com/josephine/index.html
8 *
9 * Copyright (C) 2003 Runtime Collective
10 *
11 * This product includes software developed by the
12 * Apache Software Foundation (http://www.apache.org/).
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30 package com.RuntimeCollective.webapps.form;
31
32 import com.RuntimeCollective.webapps.RuntimeParameters;
33 import com.RuntimeCollective.webapps.bean.UserGroup;
34 import com.RuntimeCollective.webapps.form.UserGroupForm;
35 import com.RuntimeCollective.webapps.action.BeanActionMapping;
36 import com.RuntimeCollective.webapps.bean.EntityBean;
37
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpSession;
40
41 import org.apache.struts.action.ActionError;
42 import org.apache.struts.action.ActionErrors;
43 import org.apache.struts.action.ActionForm;
44 import org.apache.struts.action.ActionMapping;
45
46 /**
47 * A form for editing a group of users.
48 * This subclass adds a validate method that check for empty names
49 * and already existing names.
50 *
51 * @version $Id: ValidatedUserGroupForm.java,v 1.4 2003/09/30 15:13:14 joe Exp $
52 * @see com.RuntimeCollective.webapps.bean.UserGroup
53 */
54
55 public class ValidatedUserGroupForm extends UserGroupForm {
56
57 /** Maximum name length */
58 public static final int NAME_LENGTH = 100;
59
60 /**
61 * If the name field is empty then return error. We also check if this
62 * if the new name is different from existing names.
63 *
64 */
65 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
66 ActionErrors errors = new ActionErrors();
67
68 BeanActionMapping beanMapping = (BeanActionMapping) mapping;
69 HttpSession session = request.getSession();
70 String action = getFormAction();
71 String id = request.getParameter("id");
72
73
74 //check to see if this is a name that already exists
75 if(beanMapping != null && session != null){
76 String beanName = (String) beanMapping.getBeanName();
77 EntityBean bean = (EntityBean) request.getSession().getAttribute( beanName );
78 UserGroup group = UserGroup.getForName(getName());
79
80 if(bean != null && action.equals(EDIT_ACTION)){
81 if( group != null && group.getId() != bean.getId()){
82 RuntimeParameters.logDebug(this, "error:" + getName() + " does already exist with id:" +
83 group.getId());
84 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.alreadyExistingName"));
85 return errors;
86 }
87 }
88 }
89
90 // this is a new group
91 if ((id != null) && (id.equals("new"))) {
92 return errors;
93 }
94
95 // this is an existing group
96 if (id != null){
97 RuntimeParameters.logDebug(this, "groupid:" + id);
98 return errors;
99 }
100
101 if ((getName() == null) || getName().equals("")) {
102 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.groupNameNull"));
103
104 } else if (getName().length() > NAME_LENGTH) {
105 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.usergroup.nameTooLong", ""+NAME_LENGTH));
106 }
107
108 return errors;
109 }
110
111 }