Skip to content

Commit

Permalink
Merge pull request #63 from OsiriX-Foundation/update_grid_project
Browse files Browse the repository at this point in the history
Update grid project
  • Loading branch information
nicolasvandooren authored Oct 30, 2020
2 parents 6de9edc + da913a9 commit 702a683
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 14 deletions.
19 changes: 14 additions & 5 deletions mvc/src/main/java/org/karnak/ui/data/ProjectDataProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ public void save(Project project) {
boolean isNewProject = project.isNewData();
if (isNewProject) {
getItems().add(project);
refreshAll();
} else {
refreshItem(project);
}
projectPersistence.saveAndFlush(project);
refreshAll();
}

public void update(Project project) {
if (!project.isNewData()) {
refreshItem(project);
projectPersistence.saveAndFlush(project);
updateDestinations(project);
refreshAll();
}
}

Expand All @@ -56,12 +56,14 @@ private void updateDestinations(Project project) {
public void remove(Project project) {
projectPersistence.deleteById(project.getId());
projectPersistence.flush();
getItems().remove(project);
refreshAll();
}

public Project getProjectById(Long dataId) {
return projectPersistence.findById(dataId).orElse(null);
public Project getProjectById(Long projectID) {
refreshAll();
return getItems().stream()
.filter(project -> project.getId().equals(projectID))
.findAny().orElse(null);
}

public List<Project> getAllProjects() {
Expand All @@ -75,4 +77,11 @@ public void setApplicationEventPublisher(ApplicationEventPublisher applicationEv
public ApplicationEventPublisher getApplicationEventPublisher() {
return applicationEventPublisher;
}

@Override
public void refreshAll() {
getItems().clear();
getItems().addAll(getAllProjects());
super.refreshAll();
}
}
5 changes: 2 additions & 3 deletions mvc/src/main/java/org/karnak/ui/project/EditProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ public EditProject(ProjectDataProvider projectDataProvider) {
}

public void setProject(Project project) {
this.project = project;
if (project != null) {
this.project = projectDataProvider.getProjectById(project.getId());
binder.setBean(this.project);
binder.setBean(project);
setEnabled(true);
} else {
this.project = null;
binder.removeBean();
clear();
setEnabled(false);
Expand Down
6 changes: 5 additions & 1 deletion mvc/src/main/java/org/karnak/ui/project/GridProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public GridProject(ProjectDataProvider projectDataProvider) {
}

public void selectRow(Project row) {
getSelectionModel().select(row);
if (row != null) {
getSelectionModel().select(row);
} else {
getSelectionModel().deselectAll();
}
}
}
20 changes: 15 additions & 5 deletions mvc/src/main/java/org/karnak/ui/project/MainViewProjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.*;
import org.karnak.data.gateway.Project;
import org.karnak.profilepipe.utils.HMAC;
import org.karnak.ui.MainLayout;
Expand All @@ -14,7 +13,7 @@

@Route(value = "project", layout = MainLayout.class)
@PageTitle("KARNAK - Project")
public class MainViewProjects extends HorizontalLayout {
public class MainViewProjects extends HorizontalLayout implements HasUrlParameter<String> {
public static final String VIEW_NAME = "Project";

private ProjectDataProvider projectDataProvider;
Expand Down Expand Up @@ -46,17 +45,28 @@ private void setEventButtonNewProject() {
newProject.setSecret(HMAC.generateRandomKey());
projectDataProvider.save(newProject);
newProjectForm.clear();
gridProject.selectRow(newProject);
ProjectViewLogic.navigateProject(newProject);
}
});
}

private void setEventGridSelection() {
gridProject.asSingleSelect().addValueChangeListener(event -> {
editProject.setProject(event.getValue());
ProjectViewLogic.navigateProject(event.getValue());
});
}

@Override
public void setParameter(BeforeEvent event, @OptionalParameter String parameter) {
Long idProject = ProjectViewLogic.enter(parameter);
Project currentProject = null;
if (idProject != null) {
currentProject = projectDataProvider.getProjectById(idProject);
}
editProject.setProject(currentProject);
gridProject.selectRow(currentProject);
}

@Autowired
private void addEventManager(ApplicationEventPublisher publisher) {
projectDataProvider.setApplicationEventPublisher(publisher);
Expand Down
24 changes: 24 additions & 0 deletions mvc/src/main/java/org/karnak/ui/project/ProjectViewLogic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.karnak.ui.project;

import com.vaadin.flow.component.UI;
import org.karnak.data.gateway.Project;

public class ProjectViewLogic {
public static Long enter(String dataIdStr) {
try {
Long dataId = Long.valueOf(dataIdStr);
return dataId;
} catch (NumberFormatException e) {
}
return null;
}

public static void navigateProject(Project project) {
if (project == null) {
UI.getCurrent().navigate(MainViewProjects.class, "");
} else {
String projectID = String.valueOf(project.getId());
UI.getCurrent().navigate(MainViewProjects.class, projectID);
}
}
}

0 comments on commit 702a683

Please sign in to comment.