@Override protected void doExecuteCommand() { List<InvalidValue> invalidExceptions = new ArrayList<InvalidValue>(5); MyBean myBean = new MyBean(); invalidExceptions.add(new InvalidValue("first invalid message", MyBean.class, "firstProperty", "first invalid value", myBean)); invalidExceptions.add(new InvalidValue("second invalid message", MyBean.class, "secondProperty", "second invalid value", myBean)); invalidExceptions.add(new InvalidValue("third invalid message", MyBean.class, "thirdProperty", "third invalid value", myBean)); invalidExceptions.add(new InvalidValue("fourth invalid message", MyBean.class, "fourthProperty", "fourth invalid value", myBean)); invalidExceptions.add(new InvalidValue("fifth invalid message", MyBean.class, "fifthProperty", "fifth invalid value", myBean)); throw new InvalidStateException(invalidExceptions.toArray(new InvalidValue[] {})); }
public Object createExceptionContent(Throwable throwable) { if (!(throwable instanceof InvalidStateException)) { String ILLEGAL_THROWABLE_ARGUMENT = "Could not handle exception: throwable is not an InvalidStateException:\n" + throwable.getClass().getName(); logger.error(ILLEGAL_THROWABLE_ARGUMENT); return ILLEGAL_THROWABLE_ARGUMENT; } InvalidStateException invalidStateException = (InvalidStateException) throwable; String explanation = messageSourceAccessor.getMessage(EXPLANATION_KEY, EXPLANATION_KEY); JPanel panel = new JPanel(new BorderLayout()); JLabel explanationLabel = new JLabel(explanation); panel.add(explanationLabel, BorderLayout.NORTH); List<String> invalidValueMessageList = new ArrayList<String>(); for (InvalidValue invalidValue : invalidStateException.getInvalidValues()) { StringBuffer messageBuffer = new StringBuffer(); String propertyName = invalidValue.getPropertyName(); messageBuffer.append(messageSourceAccessor.getMessage(propertyName + ".label", propertyName)); messageBuffer.append(" "); messageBuffer.append(invalidValue.getMessage()); invalidValueMessageList.add(messageBuffer.toString()); } JList invalidValuesJList = new JList(invalidValueMessageList.toArray()); JScrollPane invalidValuesScrollPane = new JScrollPane(invalidValuesJList, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); panel.add(invalidValuesScrollPane, BorderLayout.CENTER); return panel; }
/** * Add all {@link InvalidValue}s to the {@link ValidationResults}. */ protected void addInvalidValues(InvalidValue[] invalidValues) { if (invalidValues != null) { for (InvalidValue invalidValue : invalidValues) { results.addMessage(translateMessage(invalidValue)); } } }
/** * Validates the object through Hibernate Validator * * @param object The object that needs to be validated * @param property The properties that needs to be validated * @return An array of {@link InvalidValue}, containing all validation * errors */ protected InvalidValue[] doHibernateValidate(final Object object, final String property) { if (property == null) { final List<InvalidValue> ret = new ArrayList<InvalidValue>(); PropertyDescriptor[] propertyDescriptors; try { propertyDescriptors = Introspector.getBeanInfo(object.getClass()).getPropertyDescriptors(); } catch (IntrospectionException e) { throw new IllegalStateException("Could not retrieve property information"); } for (final PropertyDescriptor prop : propertyDescriptors) { String propertyName = prop.getName(); if (formModel.hasValueModel(propertyName) && !ignoredHibernateProperties.contains(propertyName)) { final InvalidValue[] result = hibernateValidator.getPotentialInvalidValues(propertyName, formModel .getValueModel(propertyName).getValue()); if (result != null) { for (final InvalidValue r : result) { ret.add(r); } } } } return ret.toArray(new InvalidValue[ret.size()]); } else if (!ignoredHibernateProperties.contains(property) && formModel.hasValueModel(property)) { return hibernateValidator.getPotentialInvalidValues(property, formModel.getValueModel(property).getValue()); } else { return null; } }
private ArrayDesign setupAndSaveDesign(File... designFiles) throws IllegalAccessException, InvalidDataFileException { this.hibernateHelper.getCurrentSession().save(DUMMY_ORGANIZATION); this.hibernateHelper.getCurrentSession().save(DUMMY_ORGANISM); this.hibernateHelper.getCurrentSession().save(DUMMY_TERM); this.hibernateHelper.getCurrentSession().save(DUMMY_ASSAY_TYPE); final ArrayDesign design = new ArrayDesign(); design.setName("DummyTestArrayDesign1"); design.setVersion("2.0"); design.setProvider(DUMMY_ORGANIZATION); design.setLsidForEntity("authority:namespace:" + designFiles[0].getName()); design.getAssayTypes().add(DUMMY_ASSAY_TYPE); final Set<AssayType> assayTypes = new TreeSet<AssayType>(); assayTypes.add(DUMMY_ASSAY_TYPE); for (final File designFile : designFiles) { design.addDesignFile(getCaArrayFile(designFile)); } design.setTechnologyType(DUMMY_TERM); design.setOrganism(DUMMY_ORGANISM); try { this.arrayDesignService.saveArrayDesign(design); } catch (final InvalidStateException e) { e.printStackTrace(); for (final InvalidValue iv : e.getInvalidValues()) { System.out.println("Invalid value: " + iv); } } return design; }
private void createBoxesForOrderLines() { for (BatchOrderLine line : orderLines) { // validate individual fields using Hibernate3 validator InvalidValue[] validationMessages = lineValidator.getInvalidValues(line); if (validationMessages.length > 0) { rejectOrderLine(line, Arrays.toString(validationMessages)); continue; } // validate the order references are unique for this merchant if (line.getOrderReference() != null && merchantExistingOrderReferences.contains(line.getOrderReference())) { rejectOrderLine(line, "This order reference was previously used for this merchant"); continue; } // create box for order line Box box = new Box(); if (line.getGoodsType() != null) { box.setGoodCategory(GoodCategory.valueOf(line.getGoodsType())); } if (line.getBoxType() != null) { box.setType(BoxType.valueOf(line.getBoxType())); } line.setBox(box); // validate box type is allowed for merchant Set<BoxType> allowedBoxTypes = uploadRequest.getMerchant().getAllowedBoxTypes(); if (box.getType() != null && !allowedBoxTypes.contains(box.getType())) { rejectOrderLine(line, "The Box type ('"+box.getType()+"') is not allowed for this merchant"); continue; } // validate box type matches goods category within it if (box.getType() != null ^ box.getGoodCategory() != null) { rejectOrderLine(line, "The box type and good category are either BOTH specified, either NONE"); continue; } if (box.getType()!=null && box.getGoodCategory()!=null) { Set<GoodCategory> allowedGoodCategories = GoodCategory.getGoodCategories(box.getType()); if (!allowedGoodCategories.contains(box.getGoodCategory())) { rejectOrderLine(line,"Invalid Good Type ('"+box.getGoodCategory()+"') for selected Box Type ('"+box.getType()+"'). Allowed values are : " + allowedGoodCategories); continue; } } } }
private void validateFieldConstraints(BatchOrderLine orderLine) { InvalidValue[] validationMessages = lineValidator.getInvalidValues(orderLine); if (validationMessages.length > 0) { throw new InvalidOrderLine(Arrays.toString(validationMessages)); } }
/** * Translate a single {@link InvalidValue} to a {@link ValidationMessage}. */ protected ValidationMessage translateMessage(InvalidValue invalidValue) { return new DefaultValidationMessage(invalidValue.getPropertyName(), Severity.ERROR, resolveObjectName(invalidValue.getPropertyName()) + " " + invalidValue.getMessage()); }