📄 modelservice.java
字号:
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) { List missingMsg = FastList.newInstance(); Iterator rbni = requiredButNull.iterator(); while (rbni.hasNext()) { String missingKey = (String) rbni.next(); String message = this.getParam(missingKey).getPrimaryFailMessage(locale); if (message == null) { String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "ModelService.following_required_parameter_missing", locale); message = errMsg + " [" + this.name + "." + missingKey + "]"; } missingMsg.add(message); } throw new ServiceValidationException(missingMsg, 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, locale); validate(optionalInfo, optionalTest, false, this, mode, locale); } catch (ServiceValidationException e) { Debug.logError("[ModelService.validate] : {" + name + "} : (" + mode + ") Required test error: " + e.toString(), module); throw e; } } /** * 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, Locale locale) throws ServiceValidationException { if (info == null || test == null) { throw new ServiceValidationException("Cannot validate NULL maps", model); } // * 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); List missingMsgs = FastList.newInstance(); Iterator iter = missing.iterator(); while (iter.hasNext()) { String key = (String) iter.next(); String msg = model.getParam(key).getPrimaryFailMessage(locale); if (msg == null) { String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "ModelService.following_required_parameter_missing", locale) ; msg = errMsg + " [" + model.name + "." + key + "]"; } missingMsgs.add(msg); } List missingCopy = FastList.newInstance(); missingCopy.addAll(missing); throw new ServiceValidationException(missingMsgs, model, missingCopy, 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); List extraMsgs = FastList.newInstance(); Iterator iter = extra.iterator(); while (iter.hasNext()) { String key = (String) iter.next(); ModelParam param = model.getParam(key); String msg = null; if (param != null) { msg = param.getPrimaryFailMessage(locale); } if (msg == null) { msg = "Unknown parameter found: [" + model.name + "." + key + "]"; } extraMsgs.add(msg); } List extraCopy = FastList.newInstance(); extraCopy.addAll(extra); throw new ServiceValidationException(extraMsgs, model, null, extraCopy, mode); } // * Validate types next List typeFailMsgs = FastList.newInstance(); Iterator i = testSet.iterator(); while (i.hasNext()) { String key = (String) i.next(); ModelParam param = model.getParam(key); Object testObject = test.get(key); String infoType = (String) info.get(key); if (param.validators != null && param.validators.size() > 0) { Iterator vali = param.validators.iterator(); while (vali.hasNext()) { ModelParam.ModelParamValidator val = (ModelParam.ModelParamValidator) vali.next(); if (UtilValidate.isNotEmpty(val.getMethodName())) { try { if (!typeValidate(val, testObject)) { String msg = val.getFailMessage(locale); if (msg == null) { msg = "The following parameter failed validation: [" + model.name + "." + key + "]"; } typeFailMsgs.add(msg); } } catch (GeneralException e) { Debug.logError(e, module); String msg = param.getPrimaryFailMessage(locale); if (msg == null) { msg = "The following parameter failed validation: [" + model.name + "." + key + "]"; } typeFailMsgs.add(msg); } } else { if (!ObjectType.instanceOf(testObject, infoType, null)) { String msg = val.getFailMessage(locale); if (msg == null) { msg = "The following parameter failed validation: [" + model.name + "." + key + "]"; } typeFailMsgs.add(msg); } } } } else { if (!ObjectType.instanceOf(testObject, infoType, null)) { String testType = testObject == null ? "null" : testObject.getClass().getName(); String msg = "Type check failed for field [" + model.name + "." + key + "]; expected type is [" + infoType + "]; actual type is [" + testType + "]"; typeFailMsgs.add(msg); } } } if (typeFailMsgs.size() > 0) { throw new ServiceValidationException(typeFailMsgs, model, mode); } } public static boolean typeValidate(ModelParam.ModelParamValidator vali, Object testValue) throws GeneralException { // find the validator class Class validatorClass = null; try { validatorClass = ObjectType.loadClass(vali.getClassName()); } catch (ClassNotFoundException e) { } if (validatorClass == null) { throw new GeneralException("Unable to load validation class [" + vali.getClassName() + "]"); } boolean foundObjectParam = true; Class[] stringParam = new Class[] { String.class }; Class[] objectParam = new Class[] { Object.class }; Method validatorMethod = null; try { // try object type first validatorMethod = validatorClass.getMethod(vali.getMethodName(), objectParam); } catch (NoSuchMethodException e) { foundObjectParam = false; // next try string type try { validatorMethod = validatorClass.getMethod(vali.getMethodName(), stringParam); } catch (NoSuchMethodException e2) { } } if (validatorMethod == null) { throw new GeneralException("Unable to find validation method [" + vali.getMethodName() + "] in class [" + vali.getClassName() + "]"); } Object[] params; if (!foundObjectParam) { // convert to string String converted = null; try { converted = (String) ObjectType.simpleTypeConvert(testValue, "String", null, null); } catch (GeneralException e) { throw new GeneralException("Unable to convert parameter to String"); } params = new Object[] { converted }; } else { // use plain object params = new Object[] { testValue }; } // run the validator Boolean resultBool = Boolean.FALSE; try { resultBool = (Boolean) validatorMethod.invoke(null, params); } catch (ClassCastException e) { throw new GeneralException("Validation method [" + vali.getMethodName() + "] in class [" + vali.getClassName() + "] did not return expected Boolean"); } catch (Exception e) { throw new GeneralException("Unable to run validation method [" + vali.getMethodName() + "] in class [" + vali.getClassName() + "]"); } return resultBool.booleanValue(); } /** * 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 * @param internal True to include internal parameters * @return List of parameter names */ public List getParameterNames(String mode, boolean optional, boolean internal) { List names = FastList.newInstance(); 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)) { if (internal || (!internal && !param.internal)) { names.add(param.name); } } } } return names; } public List getParameterNames(String mode, boolean optional) { return this.getParameterNames(mode, optional, 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 */ public Map makeValid(Map source, String mode) { return makeValid(source, mode, true, null, null); } /** * 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, List errorMessages) { return makeValid(source, mode, includeInternal, errorMessages, null); } /** * 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 * @param locale locale to use to do some type conversion */ public Map makeValid(Map source, String mode, boolean includeInternal, List errorMessages, Locale locale) { 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)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -