forked from IQSS/dataverse
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #2554: Generalize Suggestion box to support controlled vocabul…
…ary values (#2555) Co-authored-by: Filipe Dias Lewandowski Signed-off-by: Daniel Korbel <[email protected]> Co-authored-by: Sylwester Niewczas <[email protected]> Co-authored-by: Krzysztof Mądry <[email protected]>
- Loading branch information
1 parent
2e81850
commit 2eb0ad6
Showing
19 changed files
with
402 additions
and
20 deletions.
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.