forked from IQSS/dataverse
-
Notifications
You must be signed in to change notification settings - Fork 4
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
Closes #2554: Generalize Suggestion box to support controlled vocabulary #2555
Merged
diasf
merged 1 commit into
develop
from
dkorbel-2554-suggestion-box-support-for-controlled-vocabulary-values
Sep 25, 2024
Merged
Changes from all commits
Commits
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
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
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
74 changes: 74 additions & 0 deletions
74
...erse/dataset/metadata/inputRenderer/suggestion/ControlledVocabularySuggestionHandler.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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package edu.harvard.iq.dataverse.dataset.metadata.inputRenderer.suggestion; | ||
|
||
import com.google.common.collect.Lists; | ||
import edu.harvard.iq.dataverse.ControlledVocabularyValueServiceBean; | ||
import edu.harvard.iq.dataverse.dataset.metadata.inputRenderer.Suggestion; | ||
|
||
import javax.ejb.Stateless; | ||
import javax.inject.Inject; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
/** | ||
* This suggestion handler provides suggestions based on controlled vocabulary values. | ||
*/ | ||
@Stateless | ||
public class ControlledVocabularySuggestionHandler implements SuggestionHandler { | ||
|
||
public static final String CONTROLLED_VOCABULARY_NAME_COLUMN = "controlledVocabularyName"; | ||
private ControlledVocabularyValueServiceBean controlledVocabularyValueServiceBean; | ||
|
||
// -------------------- CONSTRUCTORS -------------------- | ||
|
||
@Deprecated | ||
public ControlledVocabularySuggestionHandler() { | ||
} | ||
|
||
@Inject | ||
public ControlledVocabularySuggestionHandler(ControlledVocabularyValueServiceBean controlledVocabularyValueServiceBean) { | ||
this.controlledVocabularyValueServiceBean = controlledVocabularyValueServiceBean; | ||
} | ||
|
||
// -------------------- LOGIC -------------------- | ||
|
||
/** | ||
* {@inheritDoc} | ||
* <p> | ||
* This implementation always returns class name. | ||
*/ | ||
@Override | ||
public String getName() { | ||
return getClass().getSimpleName(); | ||
} | ||
|
||
|
||
/** | ||
* This is not dependent on siblings input values. All values will be taken | ||
* to build matching suggestions list. | ||
*/ | ||
@Override | ||
public boolean isDependentOnSiblings() { | ||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* <p> | ||
* This implementation filers out base on controlled vocabulary (input) name, | ||
* a.k.a. dictionary name | ||
*/ | ||
@Override | ||
public List<String> getAllowedFilters() { | ||
return Lists.newArrayList(CONTROLLED_VOCABULARY_NAME_COLUMN); | ||
} | ||
|
||
@Override | ||
public List<Suggestion> generateSuggestions(Map<String, String> filters, String suggestionSourceFieldValue) { | ||
return controlledVocabularyValueServiceBean | ||
.findByDatasetFieldTypeNameAndValueLike(filters.get(CONTROLLED_VOCABULARY_NAME_COLUMN), suggestionSourceFieldValue, 10) | ||
.stream().map(vocabulary -> new Suggestion(vocabulary.getStrValue(), vocabulary.getIdentifier())) | ||
.collect(toList()); | ||
} | ||
} |
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
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
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
53 changes: 53 additions & 0 deletions
53
...a/edu/harvard/iq/dataverse/validation/field/validators/ControlledVocabularyValidator.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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package edu.harvard.iq.dataverse.validation.field.validators; | ||
|
||
import edu.harvard.iq.dataverse.ControlledVocabularyValueServiceBean; | ||
import edu.harvard.iq.dataverse.common.BundleUtil; | ||
import edu.harvard.iq.dataverse.persistence.dataset.ControlledVocabularyValue; | ||
import edu.harvard.iq.dataverse.persistence.dataset.ValidatableField; | ||
import edu.harvard.iq.dataverse.validation.field.FieldValidationResult; | ||
import org.omnifaces.cdi.Eager; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Validator for fields that should only contain values from a controlled vocabulary. | ||
* The vocabulary is passed in through the parameters. | ||
*/ | ||
@Eager | ||
@ApplicationScoped | ||
public class ControlledVocabularyValidator extends MultiValueValidatorBase { | ||
|
||
@Inject | ||
private ControlledVocabularyValueServiceBean vocabularyDao; | ||
|
||
// -------------------- LOGIC -------------------- | ||
|
||
@Override | ||
public String getName() { | ||
return "controlled_vocabulary_validator"; | ||
} | ||
|
||
@Override | ||
public FieldValidationResult validateValue | ||
(String value, | ||
ValidatableField field, | ||
Map<String, Object> params, | ||
Map<String, ? extends List<? extends ValidatableField>> fieldIndex) { | ||
|
||
List<ControlledVocabularyValue> matchesFromDb = | ||
vocabularyDao.findByDatasetFieldTypeNameAndValueLike( | ||
field.getDatasetFieldType().getName(), value, 1 | ||
); | ||
if(matchesFromDb.size() == 1 && matchesFromDb.get(0).getStrValue().equalsIgnoreCase(value)) { | ||
return FieldValidationResult.ok(); | ||
} else { | ||
return FieldValidationResult.invalid( | ||
field, | ||
BundleUtil.getStringFromBundle("validation.value.not.allowed.in.controlled.vocabulary", value) | ||
); | ||
} | ||
} | ||
} |
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.
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.
To me the name
getFilterValue
seems a bit misleading, but tbh not sure what would be a better one.The main issue is that the contract between
isDependentOnSiblings
,getAllowedFilters
and this method is not very obvious.For instance, I wonder if it wouldn't be clearer to have
getAllowedFilters
return a list ofFilterConfig
objects. Those filter objects would tell how a value for that filter can be retrieved, either from dataset fields or from db filter configuration.But I feel that would require a bigger refactoring
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've added improved comment.