Skip to content

Commit

Permalink
Allows editing of experimental variables (#322)
Browse files Browse the repository at this point in the history
Introduces editing functionality for experimental variables.

Co-authored-by: Tobias Koch <[email protected]>
  • Loading branch information
sven1103 and KochTobi authored Jul 19, 2023
1 parent 3a6b1ff commit d979c61
Show file tree
Hide file tree
Showing 13 changed files with 410 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,87 +16,19 @@
*/
public class ApplicationException extends RuntimeException {

/**
* The error code of an ApplicationException. This code can be used to determine what kind of
* error occurred. This allows for the message of the ApplicationException to hold debug
* information and avoids string matching to search for the error message.
*/
public enum ErrorCode {
GENERAL,
INVALID_EXPERIMENTAL_DESIGN,
INVALID_PROJECT_OBJECTIVE,
INVALID_PROJECT_TITLE,
INVALID_PROJECT_CODE,
DUPLICATE_PROJECT_CODE,
UNDEFINED_VARIABLE_LEVEL,
NO_SPECIES_DEFINED,
NO_SPECIMEN_DEFINED,
NO_ANALYTE_DEFINED,
;

@Override
public String toString() {
return this.getClass().getSimpleName() + "." + this.name();
}

}

/**
* Error parameters to be used in error messages.
*
* @param value an ordered array of parameters
*/
public record ErrorParameters(Object[] value) {

public static ErrorParameters create() {
return new ErrorParameters(new Object[]{});
}

public static ErrorParameters of(Object... parameters) {
return new ErrorParameters(parameters);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

ErrorParameters that = (ErrorParameters) o;

return Arrays.equals(value, that.value);
}

@Override
public int hashCode() {
return Arrays.hashCode(value);
}

@Override
public String toString() {
return new StringJoiner(", ", ErrorParameters.class.getSimpleName() + "[", "]")
.add("value=" + Arrays.toString(value))
.toString();
}
}

private final ErrorCode errorCode;

private final ErrorParameters errorParameters;

public ApplicationException() {
this(ErrorCode.GENERAL, ErrorParameters.create());
this(ErrorCode.GENERAL, ErrorParameters.empty());
}

public ApplicationException(String message) {
this(message, ErrorCode.GENERAL, ErrorParameters.create());
this(message, ErrorCode.GENERAL, ErrorParameters.empty());
}

public ApplicationException(String message, Throwable cause) {
this(message, cause, ErrorCode.GENERAL, ErrorParameters.create());
this(message, cause, ErrorCode.GENERAL, ErrorParameters.empty());
}

public ApplicationException(ErrorCode errorCode, ErrorParameters errorParameters) {
Expand Down Expand Up @@ -132,14 +64,6 @@ public ApplicationException(String message, Throwable cause, boolean enableSuppr
this.errorParameters = errorParameters;
}

public ErrorCode errorCode() {
return errorCode;
}

public ErrorParameters errorParameters() {
return errorParameters;
}

/**
* Wraps the provided throwable into an ApplicationException and sets the provided error message.
* <p>
Expand Down Expand Up @@ -185,6 +109,14 @@ public static ApplicationException wrapping(Throwable e) {
}
}

public ErrorCode errorCode() {
return errorCode;
}

public ErrorParameters errorParameters() {
return errorParameters;
}

@Override
public String toString() {
return new StringJoiner(", ", ApplicationException.class.getSimpleName() + "[", "]")
Expand All @@ -193,4 +125,84 @@ public String toString() {
.add("errorParameters=" + errorParameters)
.toString();
}

/**
* The error code of an ApplicationException. This code can be used to determine what kind of
* error occurred. This allows for the message of the ApplicationException to hold debug
* information and avoids string matching to search for the error message.
*/
public enum ErrorCode {
GENERAL,
INVALID_EXPERIMENTAL_DESIGN,
INVALID_PROJECT_OBJECTIVE,
INVALID_PROJECT_TITLE,
INVALID_PROJECT_CODE,
DUPLICATE_PROJECT_CODE,
UNDEFINED_VARIABLE_LEVEL,
NO_SPECIES_DEFINED,
NO_SPECIMEN_DEFINED,
NO_ANALYTE_DEFINED,
;

@Override
public String toString() {
return this.getClass().getSimpleName() + "." + this.name();
}

}

/**
* Error parameters to be used in error messages.
*
* @param value an ordered array of parameters
*/
public record ErrorParameters(Object[] value) {

/**
* Creates a new instance of an empty error parameter array
*
* @return
* @since 1.0.0
*/
public static ErrorParameters empty() {
return new ErrorParameters(new Object[]{});
}

/**
* Creates a new instance with the provided error parameters
*
* @param parameters the error parameters
* @return
* @since 1.0.0
*/
public static ErrorParameters of(Object... parameters) {
return new ErrorParameters(parameters);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

ErrorParameters that = (ErrorParameters) o;

return Arrays.equals(value, that.value);
}

@Override
public int hashCode() {
return Arrays.hashCode(value);
}

@Override
public String toString() {
return new StringJoiner(", ", ErrorParameters.class.getSimpleName() + "[", "]")
.add("value=" + Arrays.toString(value))
.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Result<Collection<Sample>, SampleInformationService.ResponseCode> findSam
} catch (Exception e) {
log.error(
"Retrieving Samples for experiment with id " + experimentId.value() + " failed: " + e);
return Result.fromError(SampleInformationService.ResponseCode.SAMPLES_NOT_FOUND);
return Result.fromError(SampleInformationService.ResponseCode.QUERY_FAILED);
}
return Result.fromValue(samples);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package life.qbic.projectmanagement.application;

import static java.util.Objects.*;

import java.util.Objects;
import life.qbic.application.commons.Result;
import life.qbic.projectmanagement.application.sample.SampleInformationService;
import life.qbic.projectmanagement.domain.project.experiment.ExperimentId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* <b>Deletion Service</b>
*
* <p>Service that orchestrates deletion use cases in the application</p>
*
* @since 1.0.0
*/
@Service
public class DeletionService {

private final ExperimentInformationService experimentInformationService;

private final SampleInformationService sampleInformationService;

@Autowired
public DeletionService(ExperimentInformationService experimentInformationService,
SampleInformationService sampleInformationService) {
this.experimentInformationService = requireNonNull(experimentInformationService,
"experimentInformationService must not be null");
this.sampleInformationService = requireNonNull(sampleInformationService,
"sampleInformationService must not be null");
}

/**
* Deletes all experiment variables and groups in a given experiment.
* <p>
* Will contain an error, if samples are available and attached to the experiment. In this case,
* no variables and groups will be deleted.
*
* @param id the experiment id
* @return a result containing the experiment id on success or an error {@link ResponseCode}
* @since 1.0.0
*/
public Result<ExperimentId, ResponseCode> deleteAllExperimentalVariables(ExperimentId id) {
var queryResult = sampleInformationService.retrieveSamplesForExperiment(id);
if (queryResult.isError()) {
return Result.fromError(ResponseCode.QUERY_FAILED);
}
if (queryResult.isValue() && queryResult.getValue().size() > 0) {
return Result.fromError(ResponseCode.SAMPLES_STILL_ATTACHED_TO_EXPERIMENT);
}
experimentInformationService.deleteAllExperimentalVariables(id);
return Result.fromValue(id);
}

public enum ResponseCode {
SAMPLES_STILL_ATTACHED_TO_EXPERIMENT, QUERY_FAILED
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,18 @@ public void deleteExperimentGroup(ExperimentId experimentId, long groupId) {
experimentRepository.update(experiment);
}


public record ExperimentalGroupDTO(Set<VariableLevel> levels, int sampleSize) {

/**
* <b>ATTENTION!</b> This will remove all existing experimental variables and all defined experimental
* groups in a give experiment!
*
* @param experimentId the experiment reference to delete the experimental variables from
* @since 1.0.0
*/
public void deleteAllExperimentalVariables(ExperimentId experimentId) {
Experiment experiment = loadExperimentById(experimentId);
experiment.removeAllExperimentalGroups();
experiment.removeAllExperimentalVariables();
experimentRepository.update(experiment);
}

/**
Expand Down Expand Up @@ -196,7 +205,6 @@ public Collection<Analyte> getAnalytesOfExperiment(ExperimentId experimentId) {
return experiment.getAnalytes();
}


/**
* Retrieve all species of an experiment.
*
Expand Down Expand Up @@ -244,4 +252,8 @@ public boolean hasExperimentalGroup(ExperimentId experimentId) {
return !experiment.getExperimentalGroups().isEmpty();
}

public record ExperimentalGroupDTO(Set<VariableLevel> levels, int sampleSize) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ public int countPreviews(ExperimentId experimentId, String filter) {
}

public enum ResponseCode {
SAMPLES_NOT_FOUND
QUERY_FAILED
}
}
Loading

0 comments on commit d979c61

Please sign in to comment.