diff --git a/dataverse-persistence/src/main/java/edu/harvard/iq/dataverse/persistence/JpaRepository.java b/dataverse-persistence/src/main/java/edu/harvard/iq/dataverse/persistence/JpaRepository.java index 51c9aa3966..4a6953888d 100644 --- a/dataverse-persistence/src/main/java/edu/harvard/iq/dataverse/persistence/JpaRepository.java +++ b/dataverse-persistence/src/main/java/edu/harvard/iq/dataverse/persistence/JpaRepository.java @@ -70,6 +70,10 @@ public T getById(ID id) { public T save(T entity) { return save(entity, false, false); } + + public void saveAll(final Iterable entities) { + entities.forEach(this::save); + } public T saveAndFlush(T entity) { return save(entity, true, false); diff --git a/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/DataverseSession.java b/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/DataverseSession.java index c1f0be39a1..8f7cfe2ddb 100644 --- a/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/DataverseSession.java +++ b/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/DataverseSession.java @@ -116,6 +116,10 @@ public void updateLocaleInViewRoot() { FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(localeCode)); } } + + public boolean canEditDashboard() { + return !this.systemConfig.isReadonlyMode() && getUser().isSuperuser(); + } // -------------------- PRIVATE -------------------- diff --git a/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/dashboard/DashboardExportSearchResultsPage.java b/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/dashboard/DashboardExportSearchResultsPage.java index 91193f2741..04bbd1bbe0 100644 --- a/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/dashboard/DashboardExportSearchResultsPage.java +++ b/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/dashboard/DashboardExportSearchResultsPage.java @@ -1,123 +1,66 @@ package edu.harvard.iq.dataverse.dashboard; -import static java.lang.Boolean.FALSE; import static java.util.Comparator.comparing; -import static java.util.stream.Collectors.toList; import static org.apache.commons.lang.StringUtils.EMPTY; import java.io.Serializable; import java.util.List; +import javax.annotation.PostConstruct; +import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.inject.Named; -import org.omnifaces.cdi.ViewScoped; - import edu.harvard.iq.dataverse.DataverseDao; import edu.harvard.iq.dataverse.DataverseSession; -import edu.harvard.iq.dataverse.PermissionsWrapper; +import edu.harvard.iq.dataverse.NavigationWrapper; import edu.harvard.iq.dataverse.persistence.dataset.DatasetFieldType; import edu.harvard.iq.dataverse.persistence.dataset.DatasetFieldTypeRepository; -import edu.harvard.iq.dataverse.util.SystemConfig; -@ViewScoped +@SuppressWarnings("serial") +@RequestScoped @Named("ExportSearchResultsPage") public class DashboardExportSearchResultsPage implements Serializable { private final DataverseSession session; - private final PermissionsWrapper permissionsWrapper; + private final NavigationWrapper navigation; private final DataverseDao dataverseDao; - private final SystemConfig systemConfig; private final DatasetFieldTypeRepository datasetFiledTypeRepo; - private List metadataTypes; + private List fieldTypes; @Inject public DashboardExportSearchResultsPage(final DataverseSession session, - final PermissionsWrapper permissionsWrapper, - final DataverseDao dataverseDao, final SystemConfig systemConfig, + final NavigationWrapper navigation, + final DataverseDao dataverseDao, final DatasetFieldTypeRepository datasetFiledTypeRepo) { this.session = session; - this.permissionsWrapper = permissionsWrapper; + this.navigation = navigation; this.dataverseDao = dataverseDao; - this.systemConfig = systemConfig; this.datasetFiledTypeRepo = datasetFiledTypeRepo; } - - public List getMetadataTypes() { - return this.metadataTypes; - } - - public String init() { - if (canEdit()) { - initMetadataTypes(); - return EMPTY; - } else { - return this.permissionsWrapper.notAuthorized(); - } + + @PostConstruct + public void init() { + this.fieldTypes = this.datasetFiledTypeRepo.findAll(); + this.fieldTypes.sort(comparing(DatasetFieldType::getTitle)); } - private void initMetadataTypes() { - this.metadataTypes = this.datasetFiledTypeRepo.findAll().stream() - .map(Metadata::new).sorted(comparing(Metadata::getTitle)) - .collect(toList()); + public List getFieldTypes() { + return this.fieldTypes; } - private boolean canEdit() { - return !this.systemConfig.isReadonlyMode() - && this.session.getUser().isSuperuser(); + public String verifyAccess() { + return this.session.canEditDashboard() ? EMPTY : this.navigation.notAuthorized(); } public String save() { - for (final DatasetFieldType fieldType : this.datasetFiledTypeRepo.findAll()) { - fieldType.setExportToFile(isExportedToFile(fieldType.getId())); - this.datasetFiledTypeRepo.save(fieldType); - } + this.datasetFiledTypeRepo.saveAll(this.fieldTypes); return EMPTY; } - private boolean isExportedToFile(final Long id) { - return this.metadataTypes.stream().filter(mt -> mt.getId().equals(id)) - .findFirst().map(Metadata::isExportable).orElse(FALSE); - } - public String cancel() { return "/dashboard.xhtml?faces-redirect=true&dataverseId=" + this.dataverseDao.findRootDataverse().getId(); } - - public static class Metadata { - - private final Long id; - private final String title; - private final String description; - private boolean exportable; - - private Metadata(final DatasetFieldType fieldType) { - this.id = fieldType.getId(); - this.title = fieldType.getTitle(); - this.description = fieldType.getDescription(); - this.exportable = fieldType.isExportToFile(); - } - - public boolean isExportable() { - return this.exportable; - } - - public void setExportable(final boolean exportable) { - this.exportable = exportable; - } - - public Long getId() { - return this.id; - } - - public String getTitle() { - return this.title; - } - - public String getDescription() { - return this.description; - } - } } diff --git a/dataverse-webapp/src/main/webapp/dashboard-exportsearchresults.xhtml b/dataverse-webapp/src/main/webapp/dashboard-exportsearchresults.xhtml index a058ea08b7..17bc3fe7b5 100644 --- a/dataverse-webapp/src/main/webapp/dashboard-exportsearchresults.xhtml +++ b/dataverse-webapp/src/main/webapp/dashboard-exportsearchresults.xhtml @@ -19,7 +19,7 @@ - +
- + @@ -51,7 +51,7 @@ - +
diff --git a/dataverse-webapp/src/test/java/edu/harvard/iq/dataverse/dashboard/DashboardExportSearchResultsPageTest.java b/dataverse-webapp/src/test/java/edu/harvard/iq/dataverse/dashboard/DashboardExportSearchResultsPageTest.java index 88d9222653..147178eeb2 100644 --- a/dataverse-webapp/src/test/java/edu/harvard/iq/dataverse/dashboard/DashboardExportSearchResultsPageTest.java +++ b/dataverse-webapp/src/test/java/edu/harvard/iq/dataverse/dashboard/DashboardExportSearchResultsPageTest.java @@ -6,6 +6,8 @@ import static org.mockito.Mockito.when; import static org.mockito.quality.Strictness.LENIENT; +import java.util.List; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -25,7 +27,6 @@ @ExtendWith(MockitoExtension.class) @MockitoSettings(strictness = LENIENT) public class DashboardExportSearchResultsPageTest { - @InjectMocks private DashboardExportSearchResultsPage page; @@ -39,10 +40,10 @@ public class DashboardExportSearchResultsPageTest { private PermissionsWrapper permissionsWrapper; @Mock private DataverseDao dataverseDao; - private DatasetFieldType type1 = new DatasetFieldType(); private DatasetFieldType type2 = new DatasetFieldType(); + private List types = asList(this.type1, this.type2); @BeforeEach public void setUp() { @@ -55,28 +56,28 @@ public void setUp() { this.type2.setId(2L); this.type2.setTitle("abc"); - when(this.datasetFiledTypeRepo.findAll()) - .thenReturn(asList(this.type1, this.type2)); + when(this.datasetFiledTypeRepo.findAll()).thenReturn(types); + + this.page.init(); } @Test public void selectionAndSavingWorks() throws Exception { - this.page.init(); - assertThat(this.page.getMetadataTypes().size()).isEqualTo(2); + assertThat(this.page.getFieldTypes().size()).isEqualTo(2); - assertThat(this.page.getMetadataTypes().get(0).getId()).isEqualTo(2L); - assertThat(this.page.getMetadataTypes().get(0).getTitle()).isEqualTo("abc"); - assertThat(this.page.getMetadataTypes().get(0).isExportable()).isFalse(); + assertThat(this.page.getFieldTypes().get(0).getId()).isEqualTo(2L); + assertThat(this.page.getFieldTypes().get(0).getTitle()).isEqualTo("abc"); + assertThat(this.page.getFieldTypes().get(0).isExportToFile()).isFalse(); - assertThat(this.page.getMetadataTypes().get(1).getId()).isEqualTo(1L); - assertThat(this.page.getMetadataTypes().get(1).getTitle()).isEqualTo("def"); - assertThat(this.page.getMetadataTypes().get(1).isExportable()).isFalse(); + assertThat(this.page.getFieldTypes().get(1).getId()).isEqualTo(1L); + assertThat(this.page.getFieldTypes().get(1).getTitle()).isEqualTo("def"); + assertThat(this.page.getFieldTypes().get(1).isExportToFile()).isFalse(); - this.page.getMetadataTypes().get(0).setExportable(true); + this.page.getFieldTypes().get(0).setExportToFile(true); this.page.save(); - verify(this.datasetFiledTypeRepo).save(this.type2); + verify(this.datasetFiledTypeRepo).saveAll(this.types); assertThat(this.type1.isExportToFile()).isFalse(); assertThat(this.type2.isExportToFile()).isTrue(); }