-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactored dashboard ExportSearchResultsPage #2611
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 20 additions & 77 deletions
97
...pp/src/main/java/edu/harvard/iq/dataverse/dashboard/DashboardExportSearchResultsPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Metadata> metadataTypes; | ||
private List<DatasetFieldType> 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<Metadata> 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<DatasetFieldType> 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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You still need a
NavigationWrapper
mock.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont need it since I don't test DashboardExportSearchResultsPage.verifyAccess() method, and I don't intend to for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But you're purposefully breaking the constructor interface by providing null and you're still creating a
SystemConfig
mock even though it doesn't seem necessary. I mean, it's not very consistent. Tests are in a way much like production code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed