-
Notifications
You must be signed in to change notification settings - Fork 13
MCR-3578 Changes modsperson attribute handling #2773
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
Open
erodde
wants to merge
17
commits into
main
Choose a base branch
from
issues/MCR-3578-changes-modsperson-attribute-handling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1bd71fc
MCR-3441 base for new generic interface
erodde cbcfb05
MCR-3441 fix PMD
erodde fff7193
MCR-3441 using LegalEntityService in ORCID2 + unit tests
erodde 2df6d0e
MCR-3441 fix pom.xml
erodde b11cd3c
MCR-3441 checkstyle
erodde 3007f9c
MCR-3441 pmd
erodde 6bf8d8a
MCR-3441 missing dependency
erodde fb2ec01
MCR-3441 Added cases to MCRORCIDUserTest
erodde b73e199
Merge branch 'main' into issues/MCR-3441-changes-modsperson-attribute…
erodde a2b3298
MCR-3441 small improvements
erodde 4e5f633
MCR-3441 codestyle
erodde 94f1811
MCR-3578 code review + use mockito for mocking
erodde b8ba51c
Merge branch 'main' into issues/MCR-3578-changes-modsperson-attribute…
erodde 12415b3
MCR-3578 access MCRLegalEntityService implementation through interface
erodde e548f01
MCR-3578 remove method findTypedIdentifiers
erodde b621947
Merge branch 'main' into issues/MCR-3578-changes-modsperson-attribute…
erodde abf299f
MCR-3578 javadoc and fallback implementation MCRNoOpLegalEntityService
erodde 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
mycore-base/src/main/java/org/mycore/datamodel/legalentity/MCRIdentifier.java
This file contains hidden or 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,86 @@ | ||
| /* | ||
| * This file is part of *** M y C o R e *** | ||
| * See https://www.mycore.de/ for details. | ||
| * | ||
| * MyCoRe is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * MyCoRe is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with MyCoRe. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package org.mycore.datamodel.legalentity; | ||
|
|
||
| import java.util.Locale; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Class to store IDs. | ||
| */ | ||
| public class MCRIdentifier { | ||
|
|
||
| private final String type; | ||
|
|
||
| private final String value; | ||
|
|
||
| /** | ||
| * Constructs new MCRIdentifier object with type and value. | ||
| * | ||
| * @param type the id type | ||
| * @param value the id value | ||
| */ | ||
| public MCRIdentifier(String type, String value) { | ||
| this.type = type; | ||
| this.value = value; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the id type. | ||
| * | ||
| * @return id type | ||
| */ | ||
| public String getType() { | ||
| return type; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the id value. | ||
| * | ||
| * @return id value | ||
| */ | ||
| public String getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(type, value); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (this == obj) { | ||
| return true; | ||
| } | ||
| if (obj == null) { | ||
| return false; | ||
| } | ||
| if (getClass() != obj.getClass()) { | ||
| return false; | ||
| } | ||
| final MCRIdentifier identifier = (MCRIdentifier) obj; | ||
| return Objects.equals(type, identifier.type) && Objects.equals(value, identifier.value); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return String.format(Locale.ROOT, "%s:%s", type, value); | ||
| } | ||
| } |
59 changes: 59 additions & 0 deletions
59
mycore-base/src/main/java/org/mycore/datamodel/legalentity/MCRLegalEntityService.java
This file contains hidden or 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,59 @@ | ||
| /* | ||
| * This file is part of *** M y C o R e *** | ||
| * See https://www.mycore.de/ for details. | ||
| * | ||
| * MyCoRe is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * MyCoRe is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with MyCoRe. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package org.mycore.datamodel.legalentity; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| import org.mycore.common.config.MCRConfiguration2; | ||
|
|
||
| /** | ||
| * Services that implement this interface should search for all identifiers of a specific legal entity (e.g. a person) | ||
| * using a specific, identifying {@link MCRIdentifier}, or add an identifier to the legal entity. The identifier | ||
| * can be any key-value pair that can uniquely identify a legal entity. The interface is intentionally generic to allow | ||
| * different identifier schemes and lookup implementations. | ||
| */ | ||
| public interface MCRLegalEntityService { | ||
|
|
||
| /** | ||
| * Finds all identifiers of a legal entity determined by a specific identifier. | ||
| * @param identifier unique identifier of legal entity, not null | ||
| * @return a set of identifiers a legal entity owns | ||
| */ | ||
| Set<MCRIdentifier> findAllIdentifiers(MCRIdentifier identifier); | ||
|
|
||
| /** | ||
| * Adds an identifier to a legal entity. The entity is determined by a specific, given identifier | ||
| * @param primaryIdentifier unique identifier of legal entity, not null | ||
| * @param identifierToAdd the identifier to add, not null | ||
| */ | ||
| void addIdentifier(MCRIdentifier primaryIdentifier, MCRIdentifier identifierToAdd); | ||
|
|
||
| /** | ||
| * Get configured singleton service implementation. | ||
| */ | ||
| static MCRLegalEntityService obtainInstance() { | ||
| return InstanceHolder.SHARED_INSTANCE; | ||
| } | ||
|
|
||
| class InstanceHolder { | ||
| private static final MCRLegalEntityService SHARED_INSTANCE = MCRConfiguration2.getInstanceOfOrThrow( | ||
| MCRLegalEntityService.class, "MCR.LegalEntityService.Class"); | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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 hidden or 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
152 changes: 152 additions & 0 deletions
152
mycore-mods/src/main/java/org/mycore/mods/MCRMODSPersonIdentifierService.java
This file contains hidden or 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,152 @@ | ||
| /* | ||
| * This file is part of *** M y C o R e *** | ||
| * See https://www.mycore.de/ for details. | ||
| * | ||
| * MyCoRe is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * MyCoRe is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with MyCoRe. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package org.mycore.mods; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.jdom2.Element; | ||
| import org.mycore.access.MCRAccessException; | ||
| import org.mycore.common.MCRConstants; | ||
| import org.mycore.common.MCRPersistenceException; | ||
| import org.mycore.datamodel.legalentity.MCRIdentifier; | ||
| import org.mycore.datamodel.legalentity.MCRLegalEntityService; | ||
| import org.mycore.datamodel.metadata.MCRMetadataManager; | ||
| import org.mycore.datamodel.metadata.MCRObject; | ||
| import org.mycore.datamodel.metadata.MCRObjectID; | ||
| import org.mycore.user2.MCRUser; | ||
| import org.mycore.user2.MCRUserManager; | ||
|
|
||
| public class MCRMODSPersonIdentifierService implements MCRLegalEntityService { | ||
toKrause marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private static final Logger LOGGER = LogManager.getLogger(); | ||
|
|
||
| public static final String MODSPERSON_ATTR_NAME = "id_modsperson"; | ||
|
|
||
| public static final String USERID = "userid"; | ||
|
|
||
| public static final String MODS_NAME = "name"; | ||
|
|
||
| public static final String MODS_NAMEIDENTIFIER = "nameIdentifier"; | ||
|
|
||
| public static final String TYPE = "type"; | ||
|
|
||
|
|
||
| /** | ||
| * Gets all {@link MCRIdentifier MCRIdentifiers} of a modsperson by reference to a {@link org.mycore.user2.MCRUser} | ||
| * and its modsperson id. | ||
| * @param userId the user id connected to the modsperson | ||
| * @return all known identifiers or an empty set | ||
| */ | ||
| @Override | ||
| public Set<MCRIdentifier> findAllIdentifiers(MCRIdentifier userId) { | ||
| return getIdentifiers(userId, null); | ||
| } | ||
|
|
||
| /** | ||
| * Adds a {@link MCRIdentifier MCRIdentifiers} to a modsperson by reference to a {@link org.mycore.user2.MCRUser} | ||
| * and its modsperson id. | ||
| * @param userId the user id connected to the modsperson | ||
| * @param attributeToAdd the nameIdentifier to add to the modsperson | ||
| */ | ||
| @Override | ||
| public void addIdentifier(MCRIdentifier userId, MCRIdentifier attributeToAdd) { | ||
| Optional<MCRObject> modspersonOptional = findModspersonByUsername(userId); | ||
| if (modspersonOptional.isEmpty()) { | ||
| return; | ||
| } | ||
| MCRMODSWrapper wrapper = new MCRMODSWrapper(modspersonOptional.get()); | ||
| Element modsName = wrapper.getMODS().getChild(MODS_NAME, MCRConstants.MODS_NAMESPACE); | ||
| if (modsName == null) { | ||
| return; | ||
| } | ||
| Element nameIdentifier = new Element(MODS_NAMEIDENTIFIER, MCRConstants.MODS_NAMESPACE) | ||
| .setAttribute(TYPE, attributeToAdd.getType()) | ||
| .setText(attributeToAdd.getValue()); | ||
| modsName.addContent(nameIdentifier); | ||
| try { | ||
| MCRMetadataManager.update(modspersonOptional.get()); | ||
| } catch (MCRAccessException | MCRPersistenceException e) { | ||
| if (LOGGER.isWarnEnabled()) { | ||
| LOGGER.warn("Could not update modsperson object for user id {}", | ||
| userId.getValue(), e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * helper method to search for identifiers in a modsperson by a user-ID | ||
| * @param userId the user id connected to the modsperson | ||
| * @param identifierType optional type filter, leave null for no filter | ||
| * @return a set of all identifiers found | ||
| */ | ||
| private Set<MCRIdentifier> getIdentifiers(MCRIdentifier userId, String identifierType) { | ||
| Optional<MCRObject> modspersonOptional = findModspersonByUsername(userId); | ||
| if (modspersonOptional.isEmpty()) { | ||
| return Collections.emptySet(); | ||
| } | ||
| MCRMODSWrapper wrapper = new MCRMODSWrapper(modspersonOptional.get()); | ||
| Element modsName = wrapper.getMODS().getChild(MODS_NAME, MCRConstants.MODS_NAMESPACE); | ||
| if (modsName == null) { | ||
| return Collections.emptySet(); | ||
| } | ||
| if (identifierType != null) { | ||
| return modsName.getChildren(MODS_NAMEIDENTIFIER, MCRConstants.MODS_NAMESPACE) | ||
| .stream().filter(e -> identifierType.equals(e.getAttributeValue(TYPE))) | ||
| .map(e -> new MCRIdentifier(e.getAttributeValue(TYPE), e.getText())) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
| return modsName.getChildren(MODS_NAMEIDENTIFIER, MCRConstants.MODS_NAMESPACE) | ||
| .stream().map(e -> new MCRIdentifier(e.getAttributeValue(TYPE), e.getText())) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| /** | ||
| * Takes a username and returns an Optional with the referenced modsperson. | ||
| * @param userId the user id | ||
| * @return a nullable Optional that might contain a modsperson | ||
| */ | ||
| private Optional<MCRObject> findModspersonByUsername(MCRIdentifier userId) { | ||
| if (userId == null || !USERID.equals(userId.getType())) { | ||
| return Optional.empty(); | ||
| } | ||
| MCRUser user = MCRUserManager.getUser(userId.getValue()); | ||
| if (user == null) { | ||
| return Optional.empty(); | ||
| } | ||
| String modspersonId = user.getUserAttribute(MODSPERSON_ATTR_NAME); | ||
| if (modspersonId == null) { | ||
| return Optional.empty(); | ||
| } | ||
| try { | ||
| MCRObject modsperson = MCRMetadataManager.retrieveMCRObject(MCRObjectID.getInstance(modspersonId)); | ||
| return Optional.of(modsperson); | ||
| } catch (MCRPersistenceException e) { | ||
| if (LOGGER.isWarnEnabled()) { | ||
| LOGGER.warn("Could not retrieve modsperson object for user id {} (modspersonId={})", | ||
| userId.getValue(), modspersonId, e); | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.