View Javadoc

1   /*   Copyright (C) 2004 Finalist IT Group
2    *
3    *   This file is part of JAG - the Java J2EE Application Generator
4    *
5    *   JAG is free software; you can redistribute it and/or modify
6    *   it under the terms of the GNU General Public License as published by
7    *   the Free Software Foundation; either version 2 of the License, or
8    *   (at your option) any later version.
9    *   JAG is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *   GNU General Public License for more details.
13   *   You should have received a copy of the GNU General Public License
14   *   along with JAG; if not, write to the Free Software
15   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   */
17  
18  package com.finalist.util.struts;
19  
20  import com.finalist.util.ejb.validation.ModelError;
21  import com.finalist.util.ejb.validation.ModelErrors;
22  import org.apache.struts.action.ActionError;
23  import org.apache.struts.action.ActionErrors;
24  
25  import java.util.Iterator;
26  
27  /*** This class provides some static methodes that do model - struts data translations
28   *
29   * @author  Gerard van de Weerd
30   * @created 26 march 2002
31   * @version
32   */
33  public class StrutsUtil {
34  
35     /*** Creates an (equivalent) action error for each model error in the given model errors object, and adds them
36      * at the specified property value (in the model errors) to the action errors object.
37      */
38  
39     public static void addModelErrorsToActionErrors(ModelErrors modelErrors, ActionErrors actionErrors) {
40        Iterator properties = modelErrors.properties();
41        while (properties.hasNext()) {
42           String currentPropName = (String) properties.next();
43           Iterator modelErrorIter = modelErrors.get(currentPropName);
44           if (currentPropName.equals(ModelErrors.GLOBAL_ERROR)) {
45              currentPropName = ActionErrors.GLOBAL_ERROR;
46           }
47           // Copy the action errors
48           while (modelErrorIter.hasNext()) {
49              ModelError currentModelError = (ModelError) modelErrorIter.next();
50              actionErrors.add(currentPropName, createActionError(currentModelError));
51           }
52        }
53     }
54  
55  
56     /*** Creates an (equivalent) action errors object for the given model errors object.
57      */
58     public static ActionErrors createActionErrorsFromModelErrors(ModelErrors modelErrors) {
59        ActionErrors actionErrors = new ActionErrors();
60        addModelErrorsToActionErrors(modelErrors, actionErrors);
61        return actionErrors;
62     }
63  
64  
65     /*** Creates an (equivalent) action error object for the given model error object. */
66     public static ActionError createActionError(ModelError modelError) {
67        Object[] params = modelError.getValues();
68        if (params == null || params.length == 0)
69           return new ActionError(modelError.getKey());
70        switch (params.length) {
71           case 1:
72              return new ActionError(modelError.getKey(), params[0]);
73           case 2:
74              return new ActionError(modelError.getKey(), params[0], params[1]);
75           case 3:
76              return new ActionError(modelError.getKey(), params[0], params[1], params[2]);
77           case 4:
78              return new ActionError(modelError.getKey(), params[0], params[1], params[2], params[3]);
79           default:
80              throw new Error("Passed too many parameters: " + params.length + ". Maximum is 4.");
81        }
82     }
83  
84  
85  }