KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > CalloutEngine


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.model;
15
16 import java.lang.reflect.*;
17 import java.util.*;
18
19 import org.compiere.util.*;
20
21 /**
22  * Callout Emgine.
23  *
24  * @author Jorg Janke
25  * @version $Id: CalloutEngine.java,v 1.2 2003/10/17 06:15:20 jjanke Exp $
26  */

27 public class CalloutEngine implements Callout
28 {
29     /**
30      * Constructor
31      */

32     public CalloutEngine()
33     {
34         super();
35     }
36
37     protected Logger log = Logger.getCLogger(getClass());
38
39     /**
40      * Start Callout.
41      * <p>
42      * Callout's are used for cross field validation and setting values in other fields
43      * when returning a non empty (error message) string, an exception is raised
44      * <p>
45      * When invoked, the Tab model has the new value!
46      *
47      * @param ctx Context
48      * @param methodName Method name
49      * @param WindowNo current Window No
50      * @param mTab Model Tab
51      * @param mField Model Field
52      * @param value The new value
53      * @param oldValue The old value
54      * @return Error message or ""
55      */

56     public String JavaDoc start (Properties ctx, String JavaDoc methodName, int WindowNo,
57         MTab mTab, MField mField, Object JavaDoc value, Object JavaDoc oldValue)
58     {
59         if (methodName == null || methodName.length() == 0)
60             throw new IllegalArgumentException JavaDoc ("No Method Name");
61         //
62
String JavaDoc retValue = "";
63         StringBuffer JavaDoc msg = new StringBuffer JavaDoc(methodName).append(" - ")
64             .append(mField.getColumnName())
65             .append("=").append(value)
66             .append(" (old=").append(oldValue)
67             .append(") {active=").append(isCalloutActive()).append("}");
68         log.info (msg);
69         
70         // Find Method
71
Method method = getMethod(methodName);
72         if (method == null)
73             throw new IllegalArgumentException JavaDoc ("Method not found: " + methodName);
74         int argLength = method.getParameterTypes().length;
75         if (!(argLength == 5 || argLength == 6))
76             throw new IllegalArgumentException JavaDoc ("Method " + methodName + " has invalid no of arguments: " + argLength);
77
78         // Call Method
79
try
80         {
81             Object JavaDoc[] args = null;
82             if (argLength == 6)
83                 args = new Object JavaDoc[] {ctx, new Integer JavaDoc(WindowNo), mTab, mField, value, oldValue};
84             else
85                 args = new Object JavaDoc[] {ctx, new Integer JavaDoc(WindowNo), mTab, mField, value};
86             retValue = (String JavaDoc)method.invoke(this, args);
87         }
88         catch (Exception JavaDoc e)
89         {
90             setCalloutActive(false);
91             Throwable JavaDoc ex = e.getCause(); // InvocationTargetException
92
if (ex == null)
93                 ex = e;
94             log.error ("start: " + methodName, ex);
95             ex.printStackTrace(System.err);
96             retValue = ex.getLocalizedMessage();
97         }
98         return retValue;
99     } // start
100

101     /**
102      * Conversion Rules.
103      * Convert a String
104      *
105      * @param methodName method name
106      * @param value the value
107      * @return converted String or Null if no method found
108      */

109     public String JavaDoc convert (String JavaDoc methodName, String JavaDoc value)
110     {
111         if (methodName == null || methodName.length() == 0)
112             throw new IllegalArgumentException JavaDoc ("No Method Name");
113         //
114
String JavaDoc retValue = null;
115         StringBuffer JavaDoc msg = new StringBuffer JavaDoc(methodName).append(" - ").append(value);
116         log.info (msg);
117         //
118
// Find Method
119
Method method = getMethod(methodName);
120         if (method == null)
121             throw new IllegalArgumentException JavaDoc ("Method not found: " + methodName);
122         int argLength = method.getParameterTypes().length;
123         if (argLength != 1)
124             throw new IllegalArgumentException JavaDoc ("Method " + methodName + " has invalid no of arguments: " + argLength);
125
126         // Call Method
127
try
128         {
129             Object JavaDoc[] args = new Object JavaDoc[] {value};
130             retValue = (String JavaDoc)method.invoke(this, args);
131         }
132         catch (Exception JavaDoc e)
133         {
134             setCalloutActive(false);
135             log.error ("convert: " + methodName, e);
136             e.printStackTrace(System.err);
137         }
138         return retValue;
139     } // convert
140

141     /**
142      * Get Method
143      * @param methodName method name
144      * @return method or null
145      */

146     private Method getMethod (String JavaDoc methodName)
147     {
148         Method[] allMethods = getClass().getMethods();
149         for (int i = 0; i < allMethods.length; i++)
150         {
151             if (methodName.equals(allMethods[i].getName()))
152                 return allMethods[i];
153         }
154         return null;
155     } // getMethod
156

157     /*************************************************************************/
158     
159     private static boolean s_calloutActive = false;
160     
161     /**
162      * Is Callout Active
163      * @return true if active
164      */

165     protected static boolean isCalloutActive()
166     {
167         return s_calloutActive;
168     } // isCalloutActive
169

170     /**
171      * Set Callout (in)active
172      * @param active active
173      */

174     protected static void setCalloutActive (boolean active)
175     {
176         s_calloutActive = active;
177     } // setCalloutActive
178

179 } // CalloutAbstract
180
Popular Tags