1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.orcid.user;
20
21 import java.io.IOException;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28 import org.jdom2.Element;
29 import org.jdom2.JDOMException;
30 import org.mycore.common.config.MCRConfiguration2;
31 import org.mycore.datamodel.metadata.MCRMetadataManager;
32 import org.mycore.datamodel.metadata.MCRObject;
33 import org.mycore.datamodel.metadata.MCRObjectID;
34 import org.mycore.mods.MCRMODSWrapper;
35 import org.mycore.orcid.MCRORCIDProfile;
36 import org.mycore.orcid.oauth.MCRTokenResponse;
37 import org.mycore.user2.MCRUser;
38 import org.mycore.user2.MCRUserAttribute;
39 import org.mycore.user2.MCRUserManager;
40 import org.xml.sax.SAXException;
41
42
43
44
45
46
47
48 public class MCRORCIDUser {
49
50 private static final Logger LOGGER = LogManager.getLogger(MCRORCIDUser.class);
51
52 public static final String ATTR_ID_PREFIX = "id_";
53
54 private static final String ATTR_ORCID_ID = ATTR_ID_PREFIX + "orcid";
55
56 private static final String ATTR_ORCID_TOKEN = "token_orcid";
57
58 private static final String MATCH_ONLY_NAME_IDENTIFIER = MCRConfiguration2
59 .getString("MCR.ORCID.User.NameIdentifier").orElse("");
60
61 private MCRUser user;
62
63 private MCRORCIDProfile profile;
64
65 public MCRORCIDUser(MCRUser user) {
66 this.user = user;
67 }
68
69 public MCRUser getUser() {
70 return user;
71 }
72
73 public MCRUserStatus getStatus() {
74 return new MCRUserStatus(this);
75 }
76
77
78 public void store(MCRTokenResponse token) {
79 user.setUserAttribute(ATTR_ORCID_ID, token.getORCID());
80 user.setUserAttribute(ATTR_ORCID_TOKEN, token.getAccessToken());
81 MCRUserManager.updateUser(user);
82 }
83
84 public String getORCID() {
85 return user.getUserAttribute(ATTR_ORCID_ID);
86 }
87
88 public String getAccessToken() {
89 return user.getUserAttribute(ATTR_ORCID_TOKEN);
90 }
91
92 public MCRORCIDProfile getProfile() {
93 if ((profile == null) && (getORCID() != null)) {
94 String orcid = getORCID();
95 String token = getAccessToken();
96
97 profile = new MCRORCIDProfile(orcid);
98 if (token != null) {
99 profile.setAccessToken(token);
100 }
101 }
102 return profile;
103 }
104
105 public MCRPublicationStatus getPublicationStatus(MCRObjectID oid) throws JDOMException, IOException, SAXException {
106 return new MCRPublicationStatus(this, oid);
107 }
108
109 public boolean isMyPublication(MCRObjectID oid) {
110 MCRObject obj = MCRMetadataManager.retrieveMCRObject(oid);
111 MCRMODSWrapper wrapper = new MCRMODSWrapper(obj);
112
113 Set<String> nameIdentifierKeys = getNameIdentifierKeys(wrapper);
114 Set<String> userIdentifierKeys = getUserIdentifierKeys();
115 nameIdentifierKeys.retainAll(userIdentifierKeys);
116
117 if (!nameIdentifierKeys.isEmpty()) {
118 for (String key : nameIdentifierKeys) {
119 LOGGER.info("user's identifier occurs in publication: " + key);
120 }
121 }
122 return !nameIdentifierKeys.isEmpty();
123 }
124
125 private Set<String> getUserIdentifierKeys() {
126 Set<String> identifierKeys = new HashSet<>();
127 for (MCRUserAttribute attribute : user.getAttributes()) {
128 if (attribute.getName().startsWith("id_")) {
129 String idType = attribute.getName().substring(3);
130 String key = buildNameIdentifierKey(idType, attribute.getValue());
131 LOGGER.info("user has name identifier: " + key);
132 identifierKeys.add(key);
133 }
134 }
135 return identifierKeys;
136 }
137
138 public static Set<String> getNameIdentifierKeys(MCRMODSWrapper wrapper) {
139 Set<String> identifierKeys = new HashSet<>();
140
141 String xPath = "mods:name/mods:nameIdentifier";
142 if (!MATCH_ONLY_NAME_IDENTIFIER.isEmpty()) {
143 xPath += "[@type = '" + MATCH_ONLY_NAME_IDENTIFIER + "']";
144 }
145
146 List<Element> nameIdentifiers = wrapper.getElements(xPath);
147 for (Element nameIdentifier : nameIdentifiers) {
148 String key = buildNameIdentifierKey(nameIdentifier.getAttributeValue("type"), nameIdentifier.getText());
149 LOGGER.info("found name identifier in publication: " + key);
150 identifierKeys.add(key);
151 }
152 return identifierKeys;
153 }
154
155 private static String buildNameIdentifierKey(String type, String id) {
156 return type + ":" + id;
157 }
158
159 }