Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/RuntimeCollective/webapps/form/BeanLinksForm.java


1   /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/form/BeanLinksForm.java,v 1.4 2003/09/30 15:13:13 joe Exp $
2    * $Revision: 1.4 $
3    * $Date: 2003/09/30 15:13:13 $
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.EntityLinkTable;
33  import com.RuntimeCollective.webapps.RuntimeParameters;
34  import com.RuntimeCollective.webapps.bean.EntityBean;
35  import com.RuntimeCollective.webapps.form.EntityBeanForm;
36  
37  import java.net.URL;
38  import java.net.MalformedURLException;
39  import javax.servlet.http.HttpServletRequest;
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 to edit the links from a given EntityBean; links
48   * as in EntityLinkTable.
49   * <p>
50   * Works hands in hands with BeanLinksAction.
51   *
52   * @version $Id: BeanLinksForm.java,v 1.4 2003/09/30 15:13:13 joe Exp $
53   */
54  public class BeanLinksForm extends EntityBeanForm {
55  
56      // == Instance variables ===================================================
57  
58      /** The bean id. */
59      public int beanId = EntityBean.NULL_ID;
60  
61      /** The to ids. */
62      public int[] linkedId = new int[] {};
63  
64      /** The role. */
65      public String role = "";
66  
67      /** The bean position. */
68      public String beanPosition = "";
69  
70      /** The return url. */
71      public String returnPage = "";
72  
73      // == Bean property methods ===================================================
74  
75      public int getBeanId() {
76          return beanId;
77      }
78      public void setBeanId(int beanId) {
79          this.beanId = beanId;
80      }
81  
82      public int[] getLinkedId() {
83          return linkedId;
84      }
85      public void setLinkedId(int[] linkedId) {
86          this.linkedId = linkedId;
87      }
88  
89      public String getRole() {
90          return role;
91      }
92      public void setRole(String role) {
93          this.role = role;
94      }
95  
96      public String getBeanPosition() {
97          return beanPosition;
98      }
99      public void setBeanPosition(String beanPosition) {
100         this.beanPosition = beanPosition;
101     }
102 
103     public String getReturnPage() {
104         return returnPage;
105     }
106     public void setReturnPage(String returnPage) {
107         this.returnPage = returnPage;
108     }
109 
110     // == BeanForm Methods ===================================================
111 
112     /** Reset all properties to default values.
113      * This also sets the form action to REGISTER ;
114      * @param mapping The mapping used to select this instance
115      * @param request The servlet request we are processing
116      */
117     public void reset(ActionMapping mapping, HttpServletRequest request) {
118         beanId = EntityBean.NULL_ID;
119         linkedId = new int[] {};
120         role = "";
121         beanPosition = "";
122         returnPage = "";
123         super.reset(mapping, request);
124     }
125 
126     /** Validate entry field entries.
127      * <p>Error codes returned:
128      * <ul>
129      * <li><code>error.links.invalidBeanId</code> - invalid bean id
130      * <li><code>error.links.invalidLinkedId</code> - invalid linked id(s)
131      * </ul>
132      */
133     public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
134         ActionErrors errors = super.validate(mapping, request);
135                 
136         // Check the bean id is what it should be
137         validateInt(beanId, errors, "error.links.invalidBeanId");
138 
139         // Check the to ids is what it should be
140         validateIntArray(linkedId, errors, "error.links.invalidLinkedId");
141 
142         // Check the beanPosition
143         if ((!EntityLinkTable.TO.equals(beanPosition)) && (!EntityLinkTable.FROM.equals(beanPosition))) {
144             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.links.invalidBeanPosition"));
145         }
146 
147         // Check the returnPage
148         if ((returnPage == null) || returnPage.equals("")) {
149             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.links.invalidReturnPage"));
150         }
151 
152         return errors;
153     }
154 
155     /** Validate one int array. */
156     protected void validateIntArray(int[] array, ActionErrors errors, String message) {
157 
158         // that's ok actually
159 //         if (array.length == 0) {
160 //             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(message));
161 //         } else {
162 
163         if (array != null)
164             for (int i=0; i<array.length; i++) {
165                 validateInt(array[i], errors, message);
166             }
167     }
168 
169     /** Validate one int. */
170     protected void validateInt(int id, ActionErrors errors, String message) {
171 
172         try {
173             EntityBean bean = (EntityBean) RuntimeParameters.getStore().get(EntityBean.class.getName(), id);
174             if (bean == null) {
175                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(message));
176             }
177         } catch (Exception e) {
178             RuntimeParameters.logError(this, "Validation could not load id "+id+", as it threw an Exception.", e);
179             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(message));
180         }
181     }
182 }