public boolean isPositionValid(String position) { if (getArrayModel() == null) { return false; } Matcher m = positionRegex.matcher(position); if (!m.matches()) { return false; } int y = Integer.parseInt(m.group(1)); int x = Integer.parseInt(m.group(2)); return isInBounds(x, y); }
private void validatePosition(String position) { if (getArrayModel() == null) { throw new IllegalStateException("ArrayModel not set. Cannot verify size of Array."); } Matcher m = positionRegex.matcher(position); if (!m.matches()) { throw new IllegalArgumentException("Invalid Array position String: " + position); } int y = Integer.parseInt(m.group(1)); int x = Integer.parseInt(m.group(2)); if (!isInBounds(x, y)) { throw new IndexOutOfBoundsException("Position " + position + " is outside of the Array bounds"); } }
private boolean isInBounds(int x, int y) { return x > 0 && x <= getArrayModel().getColumns() && y > 0 && y <= getArrayModel().getRows(); }
private void loadChildEntities(Array array) throws IOException { if (array.getArrayModel() != null) { array.setArrayModel(getArrayModel(array.getArrayModel().getId())); } Map<String, Sample> samples = new HashMap<>(); for (Entry<String, Sample> entry : array.getSamples().entrySet()) { // samples not found will end up as null mapped to a position, which will cause validation error samples.put(entry.getKey(), sampleService.get(entry.getValue().getId())); } array.setSamples(samples); }
/** * Checks submitted data for validity, throwing a ValidationException containing all of the errors if invalid * * @param array submitted Array to validate * @param beforeChange the already-persisted Array before changes * @throws IOException */ private void validateChange(Array array, Array beforeChange) throws IOException { List<ValidationError> errors = new ArrayList<>(); if (array.getId() != Array.UNSAVED_ID && beforeChange == null) { errors.add(new ValidationError("Array not found")); } if (isStringEmptyOrNull(array.getAlias())) { errors.add(new ValidationError("alias", "Alias cannot be blank")); } if (isStringEmptyOrNull(array.getSerialNumber())) { errors.add(new ValidationError("serialNumber", "Serial number cannot be blank")); } if (array.getArrayModel() == null) { // was either blank or not found during #loadChildEntities errors.add(new ValidationError("arrayModel", "Choose a valid model")); } validateUniqueFields(array, beforeChange, errors); validateSamples(array, errors); if (!errors.isEmpty()) { throw new ValidationException(errors); } }
public static ArrayDto asDto(@Nonnull Array from) { ArrayDto dto = new ArrayDto(); dto.setId(from.getId()); dto.setAlias(from.getAlias()); if (from.getArrayModel() != null) { dto.setArrayModelId(from.getArrayModel().getId()); dto.setArrayModelAlias(from.getArrayModel().getAlias()); dto.setRows(from.getArrayModel().getRows()); dto.setColumns(from.getArrayModel().getColumns()); } dto.setSerialNumber(from.getSerialNumber()); dto.setDescription(from.getDescription()); if (from.getSamples() != null) { dto.setSamples(asArraySampleDtos(from.getSamples())); } if (from.getLastModified() != null) { dto.setLastModified(formatDate(from.getLastModified())); } return dto; }
public static Array to(@Nonnull ArrayDto from) { Array array = new Array(); if (from.getId() != null) { array.setId(from.getId()); } array.setAlias(from.getAlias()); array.setArrayModel(new ArrayModel()); if (from.getArrayModelId() != null) { array.getArrayModel().setId(from.getArrayModelId()); } if (from.getArrayModelAlias() != null) { array.getArrayModel().setAlias(from.getArrayModelAlias()); } array.setSerialNumber(from.getSerialNumber()); array.setDescription(nullifyStringIfBlank(from.getDescription())); array.setSamples(toArraySamples(from.getSamples())); return array; }