public static boolean basicValidateFiles( ActionErrors actionErrors, Number fieldId, ActionForm form, IGTEntity entity, String[] extensions) throws GTClientException { try { boolean newEntity = entity.isNewEntity(); if (!(form instanceof GTActionFormBase)) { throw new java.lang.UnsupportedOperationException( "Form must be a subclass of GTActionFormBase"); } IGTFieldMetaInfo fmi = entity.getFieldMetaInfo(fieldId); if (fmi == null) { throw new java.lang.NullPointerException( "No fieldMetaInfo for field:" + fieldId + " of entity " + entity); } if ((!fmi.isDisplayable(newEntity)) || (!fmi.isEditable(newEntity))) { return true; } String fieldName = fmi.getFieldName(); // 20030410AH FormFileElement[] elements = ((GTActionFormBase) form).getFormFileElements(fieldName); // 20030410AH if (fmi.isMandatory(newEntity)) { if ((elements == null) || (elements.length == 0)) { addFieldError(actionErrors, fieldName, entity.getType(), REQUIRED, null); // 20021220AH return false; } } if (extensions != null) { // 20030410AH - Check file extensions are of those allowed boolean allOk = true; if (elements != null) { for (int i = 0; i < elements.length; i++) { String filename = elements[i].getFileName(); if (filename == null) throw new NullPointerException("Internal assertion failure: filename is null"); String extension = StaticUtils.getFileExtension(filename); if (StaticUtils.findValueInArray(extensions, extension, false) == -1) { String[] params = new String[] {filename}; addFieldError(actionErrors, fieldName, entity.getType(), INVALID_EXTENSION, params); return false; } } } } return true; } catch (Throwable t) { throw new GTClientException("Error validating file field:" + fieldId, t); } }
public static boolean validateKeys( ActionErrors actionErrors, String field, String[] keys, Number entityField, IGTEntity entity) throws GTClientException { try { IGTFieldMetaInfo fmi = entity.getFieldMetaInfo(entityField); boolean newEntity = entity.isNewEntity(); if (fmi == null) { throw new java.lang.NullPointerException( "No fieldMetaInfo for field:" + entityField + "(" + field + ") of entity " + entity); } if ((!fmi.isDisplayable(newEntity)) || (!fmi.isEditable(newEntity))) { return true; } if (fmi.isMandatory(newEntity)) { if ((keys == null) || (keys.length == 0)) { // actionErrors.add(field,new ActionError(entity.getType() + ".error." + field + // ".required")); addFieldError(actionErrors, field, entity.getType(), REQUIRED, null); // 20021220AH return false; } else { // 20030515AH - Check each key to make sure it has a value as we could well // just have an array with a single empty string (or such like) boolean someOk = false; boolean someEmpty = false; for (int i = 0; i < keys.length; i++) { if (StaticUtils.stringEmpty(keys[i])) { someEmpty = true; } else { someOk = true; } } if (someEmpty) { addFieldError(actionErrors, field, entity.getType(), someOk ? INVALID : REQUIRED, null); return false; } } } return true; } catch (Throwable t) { throw new GTClientException("Error validating field " + entityField, t); } }
// 20030918 DDJ: Added method public static boolean basicValidateStringArray( ActionErrors actionErrors, String field, String[] value, Number entityField, IGTEntity entity) throws GTClientException { try { IGTFieldMetaInfo fmi = entity.getFieldMetaInfo(entityField); if (fmi == null) { throw new java.lang.NullPointerException( "No fieldMetaInfo for field:" + entityField + "(" + field + ") of entity " + entity); } boolean newEntity = entity.isNewEntity(); if (!fmi.isDisplayable(newEntity) || !fmi.isEditable(newEntity)) { return true; } if (fmi.isMandatory(newEntity)) { if (value == null || value.length == 0) { // actionErrors.add(field,new ActionError(entity.getType() + ".error." + field + // ".required")); addFieldError(actionErrors, field, entity.getType(), REQUIRED, null); return false; } } // validate based on value class String valueClass = fmi.getValueClass(); for (int i = 0; i < value.length; i++) { try { StaticUtils.convert(value[i], valueClass, true, false); } catch (UnsupportedOperationException valueClassUnsupported) { // swallow, ignore testing unsupported value class } catch (Throwable t) { addFieldError(actionErrors, field, entity.getType(), INVALID, null); return false; } } return true; } catch (Throwable t) { throw new GTClientException("Error validating field " + entityField, t); } }
/** * Performs rudimentary string field validation checking for the presence of data in fields that * are mandatory and the maximum length of the field. */ public static boolean basicValidateString( ActionErrors actionErrors, String field, String value, Number entityField, IGTEntity entity) throws GTClientException { try { IGTFieldMetaInfo fmi = entity.getFieldMetaInfo(entityField); boolean newEntity = entity.isNewEntity(); if (fmi == null) { throw new java.lang.NullPointerException( "No fieldMetaInfo for field:" + entityField + "(" + field + ") of entity " + entity); } if ((!fmi.isDisplayable(newEntity)) || (!fmi.isEditable(newEntity))) { return true; } if (fmi.isMandatory(newEntity)) { if ((value == null) || (value.equals(""))) { // actionErrors.add(field,new ActionError(entity.getType() + ".error." + field + // ".required")); addFieldError(actionErrors, field, entity.getType(), REQUIRED, null); // 20021220AH return false; } } if (fmi.getConstraintType() == IConstraint.TYPE_TEXT) { ITextConstraint textConstraint = (ITextConstraint) fmi.getConstraint(); /* 20030918 DDJ: Fixed checking of minLength when maxLength = -1 if(textConstraint.getMaxLength() > 0) { int valueLength = value==null ? 0 : value.length(); if( (valueLength > textConstraint.getMaxLength()) || (valueLength < textConstraint.getMinLength()) ) */ int valueLength = (value == null) ? 0 : value.length(); if ((textConstraint.getMaxLength() > 0 && valueLength > textConstraint.getMaxLength()) || (valueLength < textConstraint.getMinLength())) { // actionErrors.add(field,new ActionError(entity.getType() + ".error." + field + // ".invalid")); addFieldError(actionErrors, field, entity.getType(), INVALID, null); // 20021220AH return false; } } boolean valueEmpty = ((value == null) || ("".equals(value))); Number convertedValue = null; if ("java.lang.Integer".equals(fmi.getValueClass()) && !valueEmpty) { try { convertedValue = new Integer(value); } catch (Exception e) { // actionErrors.add(field,new ActionError(entity.getType() + ".error." + field + // ".invalid")); addFieldError(actionErrors, field, entity.getType(), INVALID, null); // 20021220AH return false; } } else if ("java.lang.Short".equals(fmi.getValueClass()) && !valueEmpty) { try { convertedValue = new Short(value); } catch (Exception e) { // actionErrors.add(field,new ActionError(entity.getType() + ".error." + field + // ".invalid")); addFieldError(actionErrors, field, entity.getType(), INVALID, null); // 20021220AH return false; } } else if ("java.lang.Long".equals(fmi.getValueClass()) && !valueEmpty) { try { convertedValue = new Long(value); } catch (Exception e) { // actionErrors.add(field,new ActionError(entity.getType() + ".error." + field + // ".invalid")); addFieldError(actionErrors, field, entity.getType(), INVALID, null); // 20021220AH return false; } } if (convertedValue != null && fmi.getConstraintType() == IConstraint.TYPE_RANGE) { // This is to apply range constraint try { IRangeConstraint rangeConstraint = (IRangeConstraint) fmi.getConstraint(); Number min = rangeConstraint.getMin(0); Number max = rangeConstraint.getMax(0); if (min == null) throw new NullPointerException("min is null"); if (max == null) throw new NullPointerException("min is null"); if ((convertedValue.doubleValue() < min.doubleValue()) || (convertedValue.doubleValue() > max.doubleValue())) throw new Exception("Value out of range, constraint min=" + min + ", max=" + max); } catch (Exception ex) { addFieldError(actionErrors, field, entity.getType(), INVALID, null); return false; } } return true; } catch (Throwable t) { throw new GTClientException("Error validating field " + entityField, t); } }