|
| 1 | +package life.qbic.datamanager.views.projects.project.measurements; |
| 2 | + |
| 3 | +import com.vaadin.flow.router.BeforeEnterEvent; |
| 4 | +import com.vaadin.flow.router.BeforeEnterObserver; |
| 5 | +import com.vaadin.flow.router.Route; |
| 6 | +import com.vaadin.flow.spring.annotation.SpringComponent; |
| 7 | +import com.vaadin.flow.spring.annotation.UIScope; |
| 8 | +import jakarta.annotation.security.PermitAll; |
| 9 | +import java.io.Serial; |
| 10 | +import java.util.Objects; |
| 11 | +import life.qbic.application.commons.ApplicationException; |
| 12 | +import life.qbic.datamanager.views.Context; |
| 13 | +import life.qbic.datamanager.views.general.Main; |
| 14 | +import life.qbic.datamanager.views.general.download.MeasurementTemplateDownload; |
| 15 | +import life.qbic.datamanager.views.projects.project.experiments.ExperimentMainLayout; |
| 16 | +import life.qbic.datamanager.views.projects.project.measurements.MeasurementTemplateListComponent.DownloadMeasurementTemplateEvent; |
| 17 | +import life.qbic.datamanager.views.projects.project.samples.SampleInformationMain; |
| 18 | +import life.qbic.logging.api.Logger; |
| 19 | +import life.qbic.logging.service.LoggerFactory; |
| 20 | +import life.qbic.projectmanagement.domain.model.experiment.Experiment; |
| 21 | +import life.qbic.projectmanagement.domain.model.experiment.ExperimentId; |
| 22 | +import life.qbic.projectmanagement.domain.model.project.Project; |
| 23 | +import life.qbic.projectmanagement.domain.model.project.ProjectId; |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | + |
| 26 | +/** |
| 27 | + * Measurement Main Component |
| 28 | + * <p> |
| 29 | + * This component hosts the components necessary to show and update the Measurement information |
| 30 | + * associated with an {@link Experiment} within a {@link Project} via the provided |
| 31 | + * {@link ExperimentId} and {@link ProjectId} in the URL |
| 32 | + */ |
| 33 | + |
| 34 | +@SpringComponent |
| 35 | +@UIScope |
| 36 | +@Route(value = "projects/:projectId?/experiments/:experimentId?/measurements", layout = ExperimentMainLayout.class) |
| 37 | +@PermitAll |
| 38 | +public class MeasurementMain extends Main implements BeforeEnterObserver { |
| 39 | + |
| 40 | + @Serial |
| 41 | + private static final long serialVersionUID = 3778218989387044758L; |
| 42 | + private static final Logger log = LoggerFactory.logger(SampleInformationMain.class); |
| 43 | + public static final String PROJECT_ID_ROUTE_PARAMETER = "projectId"; |
| 44 | + public static final String EXPERIMENT_ID_ROUTE_PARAMETER = "experimentId"; |
| 45 | + private final MeasurementTemplateListComponent measurementTemplateListComponent; |
| 46 | + private final MeasurementTemplateDownload measurementTemplateDownload; |
| 47 | + private transient Context context; |
| 48 | + |
| 49 | + public MeasurementMain( |
| 50 | + @Autowired MeasurementTemplateListComponent measurementTemplateListComponent) { |
| 51 | + Objects.requireNonNull(measurementTemplateListComponent); |
| 52 | + this.measurementTemplateListComponent = measurementTemplateListComponent; |
| 53 | + measurementTemplateDownload = new MeasurementTemplateDownload(); |
| 54 | + measurementTemplateListComponent.addDownloadMeasurementTemplateClickListener( |
| 55 | + this::onDownloadMeasurementTemplateClicked); |
| 56 | + add(measurementTemplateListComponent); |
| 57 | + add(measurementTemplateDownload); |
| 58 | + addClassName("measurement"); |
| 59 | + log.debug(String.format( |
| 60 | + "New instance for %s(#%s) created with %s(#%s)", |
| 61 | + getClass().getSimpleName(), System.identityHashCode(this), |
| 62 | + measurementTemplateListComponent.getClass().getSimpleName(), |
| 63 | + System.identityHashCode(measurementTemplateListComponent))); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Callback executed before navigation to attaching Component chain is made. |
| 68 | + * |
| 69 | + * @param event before navigation event with event details |
| 70 | + */ |
| 71 | + @Override |
| 72 | + public void beforeEnter(BeforeEnterEvent event) { |
| 73 | + String projectID = event.getRouteParameters().get(PROJECT_ID_ROUTE_PARAMETER) |
| 74 | + .orElseThrow(); |
| 75 | + if (!ProjectId.isValid(projectID)) { |
| 76 | + throw new ApplicationException("invalid project id " + projectID); |
| 77 | + } |
| 78 | + ProjectId parsedProjectId = ProjectId.parse(projectID); |
| 79 | + context = new Context().with(parsedProjectId); |
| 80 | + String experimentId = event.getRouteParameters().get(EXPERIMENT_ID_ROUTE_PARAMETER) |
| 81 | + .orElseThrow(); |
| 82 | + if (!ExperimentId.isValid(experimentId)) { |
| 83 | + throw new ApplicationException("invalid experiment id " + experimentId); |
| 84 | + } |
| 85 | + ExperimentId parsedExperimentId = ExperimentId.parse(experimentId); |
| 86 | + this.context = context.with(parsedExperimentId); |
| 87 | + } |
| 88 | + |
| 89 | + private void onDownloadMeasurementTemplateClicked( |
| 90 | + DownloadMeasurementTemplateEvent downloadMeasurementTemplateEvent) { |
| 91 | + measurementTemplateDownload.trigger(downloadMeasurementTemplateEvent.measurementTemplate()); |
| 92 | + } |
| 93 | +} |
0 commit comments