001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.service;
019:
020: import java.io.Serializable;
021: import java.util.List;
022: import java.util.Locale;
023:
024: import javax.wsdl.Definition;
025: import javax.wsdl.Part;
026: import javax.wsdl.WSDLException;
027: import javax.xml.namespace.QName;
028:
029: import org.ofbiz.base.util.Debug;
030: import org.ofbiz.base.util.ObjectType;
031: import org.ofbiz.base.util.UtilProperties;
032:
033: /**
034: * Generic Service Model Parameter
035: */
036: public class ModelParam implements Serializable {
037:
038: public static final String module = ModelParam.class.getName();
039:
040: /** Parameter name */
041: public String name;
042:
043: /** Paramater type */
044: public String type;
045:
046: /** Parameter mode (IN/OUT/INOUT) */
047: public String mode;
048:
049: /** The form label */
050: public String formLabel;
051:
052: /** The entity name */
053: public String entityName;
054:
055: /** The entity field name */
056: public String fieldName;
057:
058: /** Parameter prefix for creating an attribute Map */
059: public String stringMapPrefix;
060:
061: /** Parameter suffix for creating an attribute List */
062: public String stringListSuffix;
063:
064: /** Validation methods */
065: public List validators;
066:
067: /** Default value */
068: private String defaultValue = null;
069:
070: /** Is this Parameter required or optional? Default to false, or required */
071: public boolean optional = false;
072: public boolean overrideOptional = false;
073:
074: /** Is this parameter to be displayed via the form tool? */
075: public boolean formDisplay = true;
076: public boolean overrideFormDisplay = false;
077:
078: /** Is this Parameter set internally? */
079: public boolean internal = false;
080:
081: public ModelParam() {
082: }
083:
084: public ModelParam(ModelParam param) {
085: this .name = param.name;
086: this .type = param.type;
087: this .mode = param.mode;
088: this .formLabel = param.formLabel;
089: this .entityName = param.entityName;
090: this .fieldName = param.fieldName;
091: this .stringMapPrefix = param.stringMapPrefix;
092: this .stringListSuffix = param.stringListSuffix;
093: this .validators = param.validators;
094: if (param.defaultValue != null)
095: this .setDefaultValue(param.defaultValue);
096: this .optional = param.optional;
097: this .overrideOptional = param.overrideOptional;
098: this .formDisplay = param.formDisplay;
099: this .overrideFormDisplay = param.overrideFormDisplay;
100: this .internal = param.internal;
101: }
102:
103: public void addValidator(String className, String methodName,
104: String failMessage) {
105: validators.add(new ModelParamValidator(className, methodName,
106: failMessage, null, null));
107: }
108:
109: public void addValidator(String className, String methodName,
110: String failResource, String failProperty) {
111: validators.add(new ModelParamValidator(className, methodName,
112: null, failResource, failProperty));
113: }
114:
115: public String getPrimaryFailMessage(Locale locale) {
116: if (validators != null && validators.size() > 0) {
117: return ((ModelParamValidator) validators.get(0))
118: .getFailMessage(locale);
119: } else {
120: return null;
121: }
122: }
123:
124: public Object getDefaultValue() {
125: Object defaultValueObj = null;
126: if (this .type != null) {
127: try {
128: defaultValueObj = ObjectType
129: .simpleTypeConvert(this .defaultValue,
130: this .type, null, null, false);
131: } catch (Exception e) {
132: Debug
133: .logWarning(
134: e,
135: "Service attribute ["
136: + name
137: + "] default value could not be converted to type ["
138: + type + "]: " + e.toString(),
139: module);
140: }
141: if (defaultValueObj == null) {
142: // uh-oh, conversion failed, set the String and see what happens
143: defaultValueObj = this .defaultValue;
144: }
145: } else {
146: defaultValueObj = this .defaultValue;
147: }
148: return defaultValueObj;
149: }
150:
151: public void setDefaultValue(String defaultValue) {
152: this .defaultValue = defaultValue;
153: if (this .defaultValue != null) {
154: this .optional = true;
155: }
156: if (Debug.verboseOn())
157: Debug.logVerbose("Default value for attribute ["
158: + this .name + "] set to [" + this .defaultValue
159: + "]", module);
160: }
161:
162: public void copyDefaultValue(ModelParam param) {
163: this .setDefaultValue(param.defaultValue);
164: }
165:
166: public boolean equals(ModelParam model) {
167: return model.name.equals(this .name);
168: }
169:
170: public String toString() {
171: StringBuffer buf = new StringBuffer();
172: buf.append(name).append("::");
173: buf.append(type).append("::");
174: buf.append(mode).append("::");
175: buf.append(formLabel).append("::");
176: buf.append(entityName).append("::");
177: buf.append(fieldName).append("::");
178: buf.append(stringMapPrefix).append("::");
179: buf.append(stringListSuffix).append("::");
180: buf.append(optional).append("::");
181: buf.append(overrideOptional).append("::");
182: buf.append(formDisplay).append("::");
183: buf.append(overrideFormDisplay).append("::");
184: buf.append(defaultValue).append("::");
185: buf.append(internal);
186: if (validators != null)
187: buf.append(validators.toString()).append("::");
188: return buf.toString();
189: }
190:
191: public Part getWSDLPart(Definition def) throws WSDLException {
192: Part part = def.createPart();
193: part.setName(this .name);
194: part.setTypeName(new QName(ModelService.XSD, this
195: .java2wsdlType()));
196: return part;
197: }
198:
199: protected String java2wsdlType() throws WSDLException {
200: if (ObjectType.instanceOf(java.lang.Character.class, this .type)) {
201: return "string";
202: } else if (ObjectType.instanceOf(java.lang.String.class,
203: this .type)) {
204: return "string";
205: } else if (ObjectType.instanceOf(java.lang.Byte.class,
206: this .type)) {
207: return "byte";
208: } else if (ObjectType.instanceOf(java.lang.Boolean.class,
209: this .type)) {
210: return "boolean";
211: } else if (ObjectType.instanceOf(java.lang.Integer.class,
212: this .type)) {
213: return "int";
214: } else if (ObjectType.instanceOf(java.lang.Double.class,
215: this .type)) {
216: return "double";
217: } else if (ObjectType.instanceOf(java.lang.Float.class,
218: this .type)) {
219: return "float";
220: } else if (ObjectType.instanceOf(java.lang.Short.class,
221: this .type)) {
222: return "short";
223: } else if (ObjectType.instanceOf(java.math.BigDecimal.class,
224: this .type)) {
225: return "decimal";
226: } else if (ObjectType.instanceOf(java.math.BigInteger.class,
227: this .type)) {
228: return "integer";
229: } else if (ObjectType.instanceOf(java.util.Calendar.class,
230: this .type)) {
231: return "dateTime";
232: } else if (ObjectType.instanceOf(java.util.Date.class,
233: this .type)) {
234: return "dateTime";
235: } else if (ObjectType.instanceOf(java.lang.Long.class,
236: this .type)) {
237: return "unsignedInt";
238: } else if (ObjectType.instanceOf(java.sql.Timestamp.class,
239: this .type)) {
240: return "string";
241: }
242:
243: // TODO add array support (maybe even convert List objects); add GenericValue/Map support
244: throw new WSDLException(WSDLException.OTHER_ERROR,
245: "Service cannot be described with WSDL (" + this .name
246: + " / " + this .type + ")");
247: }
248:
249: static class ModelParamValidator implements Serializable {
250: protected String className;
251: protected String methodName;
252: protected String failMessage;
253: protected String failResource;
254: protected String failProperty;
255:
256: public ModelParamValidator(String className, String methodName,
257: String failMessage, String failResource,
258: String failProperty) {
259: this .className = className;
260: this .methodName = methodName;
261: this .failMessage = failMessage;
262: this .failResource = failResource;
263: this .failProperty = failProperty;
264: }
265:
266: public String getClassName() {
267: return className;
268: }
269:
270: public String getMethodName() {
271: return methodName;
272: }
273:
274: public String getFailMessage(Locale locale) {
275: if (failMessage != null) {
276: return this .failMessage;
277: } else {
278: if (failResource != null && failProperty != null) {
279: return UtilProperties.getMessage(failResource,
280: failProperty, locale);
281: }
282: }
283: return null;
284: }
285:
286: public String toString() {
287: return className + "::" + methodName + "::" + failMessage
288: + "::" + failResource + "::" + failProperty;
289: }
290: }
291: }
|