📄 modelservice.java
字号:
}
}
/**
* Validates a map of name, object types to a map of name, objects
* @param info The map of name, object types
* @param test The map to test its value types.
* @param reverse Test the maps in reverse.
*/
public static void validate(Map info, Map test, boolean reverse, ModelService model, String mode) throws ServiceValidationException {
if (info == null || test == null) {
throw new ServiceValidationException("Cannot validate NULL maps");
}
String serviceNameMessage = "";
if (model!= null && model.name != null) {
serviceNameMessage = "For service [" + model.name + "] ";
}
// * Validate keys first
Set testSet = test.keySet();
Set keySet = info.keySet();
// Quick check for sizes
if (info.size() == 0 && test.size() == 0) return;
// This is to see if the test set contains all from the info set (reverse)
if (reverse && !testSet.containsAll(keySet)) {
Set missing = new TreeSet(keySet);
missing.removeAll(testSet);
String missingStr = "";
Iterator iter = missing.iterator();
while (iter.hasNext()) {
missingStr += (String) iter.next();
if (iter.hasNext()) {
missingStr += ", ";
}
}
throw new ServiceValidationException(serviceNameMessage + "the following required parameters are missing: " + missingStr, model, new ArrayList(missing), null, mode);
}
// This is to see if the info set contains all from the test set
if (!keySet.containsAll(testSet)) {
Set extra = new TreeSet(testSet);
extra.removeAll(keySet);
String extraStr = "";
Iterator iter = extra.iterator();
while (iter.hasNext()) {
extraStr += (String) iter.next();
if (iter.hasNext()) {
extraStr += ", ";
}
}
throw new ServiceValidationException(serviceNameMessage + "unknown parameters found: " + extraStr, model, null, new ArrayList(extra), mode);
}
// * Validate types next
Iterator i = testSet.iterator();
while (i.hasNext()) {
Object key = i.next();
Object testObject = test.get(key);
String infoType = (String) info.get(key);
if (!ObjectType.instanceOf(testObject, infoType, null)) {
String testType = testObject == null ? "null" : testObject.getClass().getName();
throw new ServiceValidationException(serviceNameMessage + "type check failed for field [" + key + "]; expected type is [" +
infoType + "]; actual type is [" + testType + "]");
}
}
}
/**
* Gets the parameter names of the specified mode (IN/OUT/INOUT). The
* parameters will be returned in the order specified in the file.
* Note: IN and OUT will also contains INOUT parameters.
* @param mode The mode (IN/OUT/INOUT)
* @param optional True if to include optional parameters
* @return List of parameter names
*/
public List getParameterNames(String mode, boolean optional) {
List names = new ArrayList();
if (!"IN".equals(mode) && !"OUT".equals(mode) && !"INOUT".equals(mode)) {
return names;
}
if (contextInfo.size() == 0) {
return names;
}
Iterator i = contextParamList.iterator();
while (i.hasNext()) {
ModelParam param = (ModelParam) i.next();
if (param.mode.equals("INOUT") || param.mode.equals(mode)) {
if (optional || (!optional && !param.optional)) {
names.add(param.name);
}
}
}
return names;
}
/**
* Creates a new Map based from an existing map with just valid parameters.
* Tries to convert parameters to required type.
* @param source The source map
* @param mode The mode which to build the new map
*/
public Map makeValid(Map source, String mode) {
return makeValid(source, mode, true);
}
/**
* Creates a new Map based from an existing map with just valid parameters.
* Tries to convert parameters to required type.
* @param source The source map
* @param mode The mode which to build the new map
* @param includeInternal When false will exclude internal fields
*/
public Map makeValid(Map source, String mode, boolean includeInternal) {
Map target = new HashMap();
if (source == null) {
return target;
}
if (!"IN".equals(mode) && !"OUT".equals(mode) && !"INOUT".equals(mode)) {
return target;
}
if (contextInfo.size() == 0) {
return target;
}
Iterator i = contextParamList.iterator();
while (i.hasNext()) {
ModelParam param = (ModelParam) i.next();
boolean internalParam = param.internal;
if (param.mode.equals("INOUT") || param.mode.equals(mode)) {
Object key = param.name;
// internal map of strings
if (param.stringMapPrefix != null && param.stringMapPrefix.length() > 0 && !source.containsKey(key)) {
Map paramMap = this.makePrefixMap(source, param);
if (paramMap != null && paramMap.size() > 0) {
target.put(key, paramMap);
}
// internal list of strings
} else if (param.stringListSuffix != null && param.stringListSuffix.length() > 0 && !source.containsKey(key)) {
List paramList = this.makeSuffixList(source, param);
if (paramList != null && paramList.size() > 0) {
target.put(key, paramList);
}
// other attributes
} else {
if (source.containsKey(key)) {
if ((param.internal && includeInternal) || (!param.internal)) {
Object value = source.get(key);
try {
value = ObjectType.simpleTypeConvert(value, param.type, null, null);
} catch (GeneralException e) {
Debug.logWarning("[ModelService.makeValid] : Simple type conversion of param " +
key + " failed: " + e.toString(), module);
}
target.put(key, value);
}
}
}
}
}
return target;
}
private Map makePrefixMap(Map source, ModelParam param) {
Map paramMap = new HashMap();
Set sourceSet = source.keySet();
Iterator i = sourceSet.iterator();
while (i.hasNext()) {
String key = (String) i.next();
if (key.startsWith(param.stringMapPrefix)) {
paramMap.put(key, source.get(key));
}
}
return paramMap;
}
private List makeSuffixList(Map source, ModelParam param) {
List paramList = new ArrayList();
Set sourceSet = source.keySet();
Iterator i = sourceSet.iterator();
while (i.hasNext()) {
String key = (String) i.next();
if (key.endsWith(param.stringListSuffix)) {
paramList.add(source.get(key));
}
}
return paramList;
}
public boolean containsPermissions() {
if (this.permissionGroups != null && this.permissionGroups.size() > 0) {
return true;
}
return false;
}
/**
* Evaluates permissions for a service.
* @param security The security object to use for permission checking
* @param userLogin The logged in user's value object
* @return true if all permissions evaluate true.
*/
public boolean evalPermissions(Security security, GenericValue userLogin) {
if (this.containsPermissions()) {
Iterator i = this.permissionGroups.iterator();
while (i.hasNext()) {
ModelPermGroup group = (ModelPermGroup) i.next();
if (!group.evalPermissions(security, userLogin)) {
return false;
}
}
return true;
} else {
return true;
}
}
/**
* Gets a list of required IN parameters in sequence.
* @return A list of required IN parameters in the order which they were defined.
*/
public List getInParameterSequence(Map source) {
List target = new LinkedList();
if (source == null) {
return target;
}
if (contextInfo == null || contextInfo.size() == 0) {
return target;
}
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;
Object srcObject = source.get(p.name);
if (srcObject != null) {
target.add(srcObject);
}
}
return target;
}
/**
* Returns a list of ModelParam objects in the order they were defined when
* the service was created.
*/
public List getModelParamList() {
return new LinkedList(this.contextParamList);
}
/**
* Returns a list of ModelParam objects in the order they were defined when
* the service was created.
*/
public List getInModelParamList() {
List inList = new LinkedList();
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;
inList.add(p);
}
return inList;
}
/**
* Run the interface update and inherit all interface parameters
* @param dctx The DispatchContext to use for service lookups
*/
public synchronized void interfaceUpdate(DispatchContext dctx) throws GenericServiceException {
if (!inheritedParameters) {
// services w/ engine 'group' auto-implement the grouped services
if (this.engineName.equals("group") && implServices.size() == 0) {
GroupModel group = ServiceGroupReader.getGroupModel(this.location);
if (group != null) {
List groupedServices = group.getServices();
Iterator i = groupedServices.iterator();
while (i.hasNext()) {
GroupServiceModel sm = (GroupServiceModel) i.next();
implServices.add(sm.getName());
if (Debug.verboseOn()) Debug.logVerbose("Adding service [" + sm.getName() + "] as interface of: [" + this.name + "]", module);
}
}
}
// handle interfaces
if (implServices != null && implServices.size() > 0 && dctx != null) {
// backup the old info
List oldParams = this.contextParamList;
// reset the fields
this.contextInfo = new HashMap();
this.contextParamList = new LinkedList();
Iterator implIter = implServices.iterator();
while (implIter.hasNext()) {
String serviceName = (String) implIter.next();
ModelService model = dctx.getModelService(serviceName);
if (model != null) {
copyParamsAndClone(model.contextInfo.values());
} else {
Debug.logWarning("Inherited model [" + serviceName + "] not found for [" + this.name + "]", module);
}
}
// put the old values back on top
copyParams(oldParams);
}
// handle any override parameters
if (overrideParameters != null && overrideParameters.size() > 0) {
Iterator keySetIter = overrideParameters.iterator();
while (keySetIter.hasNext()) {
ModelParam overrideParam = (ModelParam) keySetIter.next();
ModelParam existingParam = (ModelParam) contextInfo.get(overrideParam.name);
// keep the list clean, remove it then add it back
contextParamList.remove(existingParam);
if (existingParam != null) {
// now re-write the parameters
if (overrideParam.type != null && overrideParam.type.length() > 0) {
existingParam.type = overrideParam.type;
}
if (overrideParam.mode != null && overrideParam.mode.length() > 0) {
existingParam.mode = overrideParam.mode;
}
if (overrideParam.entityName != null && overrideParam.entityName.length() > 0) {
existingParam.entityName = overrideParam.entityName;
}
if (overrideParam.fieldName != null && overrideParam.fieldName.length() > 0) {
existingParam.fieldName = overrideParam.fieldName;
}
if (overrideParam.formLabel != null && overrideParam.formLabel.length() > 0) {
existingParam.formLabel = overrideParam.formLabel;
}
if (overrideParam.overrideFormDisplay) {
existingParam.formDisplay = overrideParam.formDisplay;
}
if (overrideParam.overrideOptional) {
existingParam.optional = overrideParam.optional;
}
addParam(existingParam);
} else {
Debug.logWarning("Override param found but no parameter existing; ignoring: " + overrideParam.name, module);
}
}
}
// set the flag so we don't do this again
this.inheritedParameters = true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -