KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > ModelParam


1 /*
2  * $Id: ModelParam.java 5720 2005-09-13 03:10:59Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.service;
25
26 import java.util.List JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.io.Serializable JavaDoc;
29
30 import javax.wsdl.WSDLException;
31 import javax.wsdl.Part;
32 import javax.wsdl.Definition;
33 import javax.xml.namespace.QName JavaDoc;
34
35 import org.ofbiz.base.util.UtilProperties;
36 import org.ofbiz.base.util.ObjectType;
37
38 /**
39  * Generic Service Model Parameter
40  *
41  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
42  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jon</a>
43  * @version $Rev: 5720 $
44  * @since 2.0
45  */

46 public class ModelParam implements Serializable JavaDoc {
47
48     /** Parameter name */
49     public String JavaDoc name;
50
51     /** Paramater type */
52     public String JavaDoc type;
53
54     /** Parameter mode (IN/OUT/INOUT) */
55     public String JavaDoc mode;
56     
57     /** The form label */
58     public String JavaDoc formLabel;
59     
60     /** The entity name */
61     public String JavaDoc entityName;
62     
63     /** The entity field name */
64     public String JavaDoc fieldName;
65
66     /** Parameter prefix for creating an attribute Map */
67     public String JavaDoc stringMapPrefix;
68
69     /** Parameter suffix for creating an attribute List */
70     public String JavaDoc stringListSuffix;
71
72     /** Validation methods */
73     public List JavaDoc validators;
74
75     /** Is this Parameter required or optional? Default to false, or required */
76     public boolean optional = false;
77     public boolean overrideOptional = false;
78
79     /** Is this parameter to be displayed via the form tool? */
80     public boolean formDisplay = true;
81     public boolean overrideFormDisplay = false;
82     
83     /** Is this Parameter set internally? */
84     public boolean internal = false;
85     
86     public ModelParam() {}
87     
88     public ModelParam(ModelParam param) {
89         this.name = param.name;
90         this.type = param.type;
91         this.mode = param.mode;
92         this.formLabel = param.formLabel;
93         this.entityName = param.entityName;
94         this.fieldName = param.fieldName;
95         this.stringMapPrefix = param.stringMapPrefix;
96         this.stringListSuffix = param.stringListSuffix;
97         this.validators = param.validators;
98         this.optional = param.optional;
99         this.overrideOptional = param.overrideOptional;
100         this.formDisplay = param.formDisplay;
101         this.overrideFormDisplay = param.overrideFormDisplay;
102         this.internal = param.internal;
103     }
104
105     public void addValidator(String JavaDoc className, String JavaDoc methodName, String JavaDoc failMessage) {
106         validators.add(new ModelParamValidator(className, methodName, failMessage, null, null));
107     }
108
109     public void addValidator(String JavaDoc className, String JavaDoc methodName, String JavaDoc failResource, String JavaDoc failProperty) {
110         validators.add(new ModelParamValidator(className, methodName, null, failResource, failProperty));
111     }
112
113     public String JavaDoc getPrimaryFailMessage(Locale JavaDoc locale) {
114         if (validators != null && validators.size() > 0) {
115             return ((ModelParamValidator) validators.get(0)).getFailMessage(locale);
116         } else {
117             return null;
118         }
119     }
120
121     public boolean equals(ModelParam model) {
122         if (model.name.equals(this.name))
123             return true;
124         return false;
125     }
126     
127     public String JavaDoc toString() {
128         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
129         buf.append(name + "::");
130         buf.append(type + "::");
131         buf.append(mode + "::");
132         buf.append(formLabel + "::");
133         buf.append(entityName + "::");
134         buf.append(fieldName + "::");
135         buf.append(stringMapPrefix + "::");
136         buf.append(stringListSuffix + "::");
137         buf.append(validators.toString() + "::");
138         buf.append(optional + "::");
139         buf.append(overrideOptional + "::");
140         buf.append(formDisplay + "::");
141         buf.append(overrideFormDisplay + "::");
142         buf.append(internal);
143         return buf.toString();
144     }
145
146     public Part getWSDLPart(Definition def) throws WSDLException {
147         Part part = def.createPart();
148         part.setName(this.name);
149         part.setTypeName(new QName JavaDoc(ModelService.XSD, this.java2wsdlType()));
150         return part;
151     }
152
153     protected String JavaDoc java2wsdlType() throws WSDLException {
154         if (ObjectType.instanceOf(java.lang.Character JavaDoc.class, this.type)) {
155             return "string";
156         } else if (ObjectType.instanceOf(java.lang.String JavaDoc.class, this.type)) {
157             return "string";
158         } else if (ObjectType.instanceOf(java.lang.Byte JavaDoc.class, this.type)) {
159             return "byte";
160         } else if (ObjectType.instanceOf(java.lang.Boolean JavaDoc.class, this.type)) {
161             return "boolean";
162         } else if (ObjectType.instanceOf(java.lang.Integer JavaDoc.class, this.type)) {
163             return "int";
164         } else if (ObjectType.instanceOf(java.lang.Double JavaDoc.class, this.type)) {
165             return "double";
166         } else if (ObjectType.instanceOf(java.lang.Float JavaDoc.class, this.type)) {
167             return "float";
168         } else if (ObjectType.instanceOf(java.lang.Short JavaDoc.class, this.type)) {
169             return "short";
170         } else if (ObjectType.instanceOf(java.math.BigDecimal JavaDoc.class, this.type)) {
171             return "decimal";
172         } else if (ObjectType.instanceOf(java.math.BigInteger JavaDoc.class, this.type)) {
173             return "integer";
174         } else if (ObjectType.instanceOf(java.util.Calendar JavaDoc.class, this.type)) {
175             return "dateTime";
176         } else if (ObjectType.instanceOf(java.util.Date JavaDoc.class, this.type)) {
177             return "dateTime";
178         }
179         // TODO add array support (maybe even convert List objects); add GenericValue/Map support
180
throw new WSDLException(WSDLException.OTHER_ERROR, "Service cannot be described with WSDL (" + this.name + " / " + this.type + ")");
181     }
182
183     static class ModelParamValidator implements Serializable JavaDoc {
184         protected String JavaDoc className;
185         protected String JavaDoc methodName;
186         protected String JavaDoc failMessage;
187         protected String JavaDoc failResource;
188         protected String JavaDoc failProperty;
189
190         public ModelParamValidator(String JavaDoc className, String JavaDoc methodName, String JavaDoc failMessage, String JavaDoc failResource, String JavaDoc failProperty) {
191             this.className = className;
192             this.methodName = methodName;
193             this.failMessage = failMessage;
194             this.failResource = failResource;
195             this.failProperty = failProperty;
196         }
197
198         public String JavaDoc getClassName() {
199             return className;
200         }
201
202         public String JavaDoc getMethodName() {
203             return methodName;
204         }
205
206         public String JavaDoc getFailMessage(Locale JavaDoc locale) {
207             if (failMessage != null) {
208                 return this.failMessage;
209             } else {
210                 if (failResource != null && failProperty != null) {
211                     return UtilProperties.getMessage(failResource, failProperty, locale);
212                 }
213             }
214             return null;
215         }
216
217         public String JavaDoc toString() {
218             return className + "::" + methodName + "::" + failMessage + "::" + failResource + "::" + failProperty;
219         }
220     }
221 }
222
Popular Tags