📄 modelservice.java
字号:
/*
* $Id: ModelService.java,v 1.9 2004/03/12 23:45:01 ajzeneski Exp $
*
* Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.ofbiz.service;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.ofbiz.service.group.GroupModel;
import org.ofbiz.service.group.GroupServiceModel;
import org.ofbiz.service.group.ServiceGroupReader;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.GeneralException;
import org.ofbiz.base.util.ObjectType;
import org.ofbiz.base.util.OrderedSet;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.security.Security;
/**
* Generic Service Model Class
*
* @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
* @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
* @version $Revision: 1.9 $
* @since 2.0
*/
public class ModelService {
public static final String module = ModelService.class.getName();
public static final String OUT_PARAM = "OUT";
public static final String IN_PARAM = "IN";
public static final String RESPONSE_MESSAGE = "responseMessage";
public static final String RESPOND_SUCCESS = "success";
public static final String RESPOND_ERROR = "error";
public static final String ERROR_MESSAGE = "errorMessage";
public static final String ERROR_MESSAGE_LIST = "errorMessageList";
public static final String ERROR_MESSAGE_MAP = "errorMessageMap";
public static final String SUCCESS_MESSAGE = "successMessage";
public static final String SUCCESS_MESSAGE_LIST = "successMessageList";
/** The name of this service */
public String name;
/** The description of this service */
public String description;
/** The name of the service engine */
public String engineName;
/** The namespace of this service */
public String nameSpace;
/** The package name or location of this service */
public String location;
/** The method or function to invoke for this service */
public String invoke;
/** The default Entity to use for auto-attributes */
public String defaultEntityName;
/** Does this service require authorization */
public boolean auth;
/** Can this service be exported via RPC, RMI, SOAP, etc */
public boolean export;
/** Enable verbose debugging when calling this service */
public boolean debug;
/** Validate the context info for this service */
public boolean validate;
/** Create a transaction for this service (if one is not already in place...)? */
public boolean useTransaction;
/** Require a new transaction for this service */
public boolean requireNewTransaction;
/** Override the default transaction timeout, only works if we start the transaction */
public int transactionTimeout;
/** Set of services this service implements */
public Set implServices = new OrderedSet();
/** Set of override parameters */
public Set overrideParameters = new OrderedSet();
/** List of permission groups for service invocation */
public List permissionGroups = new LinkedList();
/** Context Information, a list of parameters used by the service, contains ModelParam objects */
protected Map contextInfo = new HashMap();
/** Context Information, a list of parameters used by the service, contains ModelParam objects */
protected List contextParamList = new LinkedList();
/** Flag to say if we have pulled in our addition parameters from our implemented service(s) */
protected boolean inheritedParameters = false;
public ModelService() {}
public ModelService(ModelService model) {
this.name = model.name;
this.description = model.description;
this.engineName = model.engineName;
this.nameSpace = model.nameSpace;
this.location = model.location;
this.invoke = model.invoke;
this.defaultEntityName = model.defaultEntityName;
this.auth = model.auth;
this.export = model.export;
this.validate = model.validate;
this.useTransaction = model.useTransaction || true;
this.requireNewTransaction = model.requireNewTransaction || false;
this.transactionTimeout = model.transactionTimeout;
this.implServices = model.implServices;
this.overrideParameters = model.overrideParameters;
this.inheritedParameters = model.inheritedParameters();
List modelParamList = model.getModelParamList();
Iterator i = modelParamList.iterator();
while (i.hasNext()) {
this.addParamClone((ModelParam) i.next());
}
}
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append(name + "::");
buf.append(description + "::");
buf.append(engineName + "::");
buf.append(nameSpace + "::");
buf.append(location + "::");
buf.append(invoke + "::");
buf.append(defaultEntityName + "::");
buf.append(auth + "::");
buf.append(export + "::");
buf.append(validate + "::");
buf.append(useTransaction + "::");
buf.append(requireNewTransaction + "::");
buf.append(transactionTimeout + "::");
buf.append(implServices + "::");
buf.append(overrideParameters + "::");
buf.append(contextInfo + "::");
buf.append(contextParamList + "::");
buf.append(inheritedParameters + "::");
return buf.toString();
}
public String debugInfo() {
if (debug || Debug.verboseOn()) {
return " [" + this.toString() + "]";
}
return "";
}
/**
* Test if we have already inherited our interface parameters
* @return boolean
*/
public boolean inheritedParameters() {
return this.inheritedParameters;
}
/**
* Gets the ModelParam by name
* @param name The name of the parameter to get
* @return ModelParam object with the specified name
*/
public ModelParam getParam(String name) {
return (ModelParam) contextInfo.get(name);
}
/**
* Adds a parameter definition to this service; puts on list in order added
* then sorts by order if specified.
*/
public void addParam(ModelParam param) {
if (param != null) {
contextInfo.put(param.name, param);
contextParamList.add(param);
}
}
private void copyParams(Collection params) {
if (params != null) {
Iterator i = params.iterator();
while (i.hasNext()) {
ModelParam param = (ModelParam) i.next();
addParam(param);
}
}
}
/**
* Adds a clone of a parameter definition to this service
*/
public void addParamClone(ModelParam param) {
if (param != null) {
ModelParam newParam = new ModelParam(param);
addParam(newParam);
}
}
private void copyParamsAndClone(Collection params) {
if (params != null) {
Iterator i = params.iterator();
while (i.hasNext()) {
ModelParam param = (ModelParam) i.next();
addParamClone(param);
}
}
}
public Set getAllParamNames() {
Set nameList = new TreeSet();
Iterator i = this.contextParamList.iterator();
while (i.hasNext()) {
ModelParam p = (ModelParam) i.next();
nameList.add(p.name);
}
return nameList;
}
public Set getInParamNames() {
Set nameList = new TreeSet();
Iterator i = this.contextParamList.iterator();
while (i.hasNext()) {
ModelParam p = (ModelParam) i.next();
// don't include OUT parameters in this list, only IN and INOUT
if ("OUT".equals(p.mode)) continue;
nameList.add(p.name);
}
return nameList;
}
public Set getOutParamNames() {
Set nameList = new TreeSet();
Iterator i = this.contextParamList.iterator();
while (i.hasNext()) {
ModelParam p = (ModelParam) i.next();
// don't include IN parameters in this list, only OUT and INOUT
if ("IN".equals(p.mode)) continue;
nameList.add(p.name);
}
return nameList;
}
/**
* Validates a Map against the IN or OUT parameter information
* @param test The Map object to test
* @param mode Test either mode IN or mode OUT
*/
public void validate(Map test, String mode) throws ServiceValidationException {
Map requiredInfo = new HashMap();
Map optionalInfo = new HashMap();
boolean verboseOn = Debug.verboseOn();
if (verboseOn) Debug.logVerbose("[ModelService.validate] : {" + name + "} : Validating context - " + test, module);
// do not validate results with errors
if (mode.equals(OUT_PARAM) && test != null && test.containsKey(RESPONSE_MESSAGE) &&
test.get(RESPONSE_MESSAGE).equals(RESPOND_ERROR)) {
if (verboseOn) Debug.logVerbose("[ModelService.validate] : {" + name + "} : response was an error, not validating.", module);
return;
}
// get the info values
Collection values = contextInfo.values();
Iterator i = values.iterator();
while (i.hasNext()) {
ModelParam p = (ModelParam) i.next();
if (p.mode.equals("INOUT") || p.mode.equals(mode)) {
if (!p.optional)
requiredInfo.put(p.name, p.type);
else
optionalInfo.put(p.name, p.type);
}
}
// get the test values
Map requiredTest = new HashMap();
Map optionalTest = new HashMap();
if (test == null) test = new HashMap();
requiredTest.putAll(test);
List requiredButNull = new ArrayList();
if (requiredTest != null) {
List keyList = new ArrayList(requiredTest.keySet());
Iterator t = keyList.iterator();
while (t.hasNext()) {
Object key = t.next();
Object value = requiredTest.get(key);
if (!requiredInfo.containsKey(key)) {
requiredTest.remove(key);
optionalTest.put(key, value);
} else if (value == null) {
requiredButNull.add(key);
}
}
}
// check for requiredButNull fields and return an error since null values are not allowed for required fields
if (requiredButNull.size() > 0) {
String missing = "";
Iterator rbni = requiredButNull.iterator();
while (rbni.hasNext()) {
String missingKey = (String) rbni.next();
missing = missing + missingKey;
if (rbni.hasNext()) {
missing = missing + ", ";
}
}
throw new ServiceValidationException("The following required parameters are missing: " + missing, this, requiredButNull, null, mode);
}
if (verboseOn) {
String requiredNames = "";
Iterator requiredIter = requiredInfo.keySet().iterator();
while (requiredIter.hasNext()) {
requiredNames = requiredNames + requiredIter.next();
if (requiredIter.hasNext()) {
requiredNames = requiredNames + ", ";
}
}
Debug.logVerbose("[ModelService.validate] : required fields - " + requiredNames, module);
Debug.logVerbose("[ModelService.validate] : {" + name + "} : (" + mode + ") Required - " +
requiredTest.size() + " / " + requiredInfo.size(), module);
Debug.logVerbose("[ModelService.validate] : {" + name + "} : (" + mode + ") Optional - " +
optionalTest.size() + " / " + optionalInfo.size(), module);
}
try {
validate(requiredInfo, requiredTest, true, this, mode);
validate(optionalInfo, optionalTest, false, this, mode);
} catch (ServiceValidationException e) {
Debug.logError("[ModelService.validate] : {" + name + "} : (" + mode + ") Required test error: " + e.toString(), module);
throw e;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -