@Override public Long getId() { return array.getId(); }
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); }
private void applyChanges(Array from, Array to) { to.setAlias(from.getAlias()); to.setSerialNumber(from.getSerialNumber()); to.setDescription(from.getDescription()); // have to add/remove samples individually to avoid unneccessary "sample removed; sample added" changelogs for non-changes Map<String, Sample> toSamples = to.getSamples(); toSamples.entrySet().removeIf(entry -> !from.getSamples().containsKey(entry.getKey())); from.getSamples().forEach((key, val) -> { Sample current = toSamples.get(key); if (toSamples.get(key) == null || current.getId() != val.getId()) { toSamples.put(key, val); } }); }
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); }
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; }
/** * 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); } }
private void loadChildEntities(ArrayRun arrayRun) throws IOException { if (arrayRun.getInstrument() != null) { arrayRun.setInstrument(instrumentService.get(arrayRun.getInstrument().getId())); } if (arrayRun.getArray() != null) { Array array = arrayService.get(arrayRun.getArray().getId()); if (array == null) { // providing a way to detect "array not found" in validation arrayRun.getArray().setAlias(null); } else { arrayRun.setArray(array); } } }
private long update(Array array) throws IOException { Array managed = get(array.getId()); validateChange(array, managed); applyChanges(array, managed); managed.setChangeDetails(authorizationManager.getCurrentUser()); return arrayStore.save(managed); }
private void validateUniqueFields(Array array, Array beforeChange, List<ValidationError> errors) throws IOException { if (beforeChange == null || !array.getAlias().equals(beforeChange.getAlias())) { Array duplicateAlias = arrayStore.getByAlias(array.getAlias()); if (duplicateAlias != null) { errors.add(new ValidationError("alias", "There is already an array with this alias")); } } if (beforeChange == null || !array.getSerialNumber().equals(beforeChange.getSerialNumber())) { Array duplicateSerialNumber = arrayStore.getBySerialNumber(array.getSerialNumber()); if (duplicateSerialNumber != null) { errors.add(new ValidationError("serialNumber", "There is already an array with this serial number")); } } }
private boolean isInBounds(int x, int y) { return x > 0 && x <= getArrayModel().getColumns() && y > 0 && y <= getArrayModel().getRows(); }
@Override public void setId(Long id) { array.setId(id); }
private void validateSamples(Array array, List<ValidationError> errors) { array.getSamples().forEach((pos, sam) -> { if (sam == null) { // wasn't found during #loadChildEntities errors.add(new ValidationError("Invalid Sample in position " + pos)); } else if (isDetailedSample(sam) && !isAliquotSample(sam)) { errors.add(new ValidationError("Sample in position " + pos + " must be an aliquot")); } }); }
if (arrayRun.getArray() != null && arrayRun.getArray().getAlias() == null) { errors.add(new ValidationError("array", "Array not found"));
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"); } }
@Override public boolean isSaved() { return getId() != UNSAVED_ID; }
@Override public long save(Array array) throws IOException { loadChildEntities(array); if (array.getId() == Array.UNSAVED_ID) { return create(array); } else { return update(array); } }