Skip to content

Commit d403050

Browse files
committed
feature: Compare work to be updated with the one stored in the database
1 parent 6bda78d commit d403050

5 files changed

Lines changed: 569 additions & 20 deletions

File tree

orcid-api-web/src/main/java/org/orcid/api/memberV3/server/delegator/impl/MemberV3ApiServiceDelegatorImpl.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@
129129
import org.orcid.jaxb.model.v3.release.record.summary.Works;
130130
import org.orcid.jaxb.model.v3.release.search.Search;
131131
import org.orcid.jaxb.model.v3.release.search.expanded.ExpandedSearch;
132+
import org.orcid.pojo.ajaxForm.WorkForm;
133+
import org.springframework.beans.factory.annotation.Value;
132134
import org.springframework.context.MessageSource;
133135
import org.springframework.stereotype.Component;
134136

@@ -269,6 +271,9 @@ public class MemberV3ApiServiceDelegatorImpl implements
269271

270272
@Resource
271273
private OrcidUrlManager orcidUrlManager;
274+
275+
@Value("${org.orcid.core.work.contributors.ui.max:50}")
276+
private int maxContributorsForUI;
272277

273278
public Boolean getFilterVersionOfIdentifiers() {
274279
return filterVersionOfIdentifiers;
@@ -388,8 +393,20 @@ public Response updateWork(String orcid, Long putCode, Work work) {
388393
throw new MismatchedPutCodeException(addParmsMismatchedPutCode(putCode, work.getPutCode()));
389394
}
390395
clearSource(work);
391-
Work w = workManager.updateWork(orcid, work, true);
392-
sourceUtils.setSourceName(w);
396+
Work w = work;
397+
398+
WorkForm newWorkForm = WorkForm.valueOf(work, maxContributorsForUI);
399+
Work workSaved = workManager.getWork(orcid, putCode);
400+
WorkForm workFormSaved = WorkForm.valueOf(workSaved, maxContributorsForUI);
401+
402+
if (Features.STOP_SENDING_NOTIFICATION_WORK_NOT_UPDATED.isActive()) {
403+
if (!workFormSaved.compare(newWorkForm)) {
404+
w = updateWork(orcid, work);
405+
}
406+
} else {
407+
w = updateWork(orcid, work);
408+
}
409+
393410
return Response.ok(w).build();
394411
}
395412

@@ -1567,4 +1584,10 @@ private Map<String, String> addParmsMismatchedPutCode(Long urlPutCode, Long body
15671584
return params;
15681585
}
15691586

1587+
private Work updateWork(String orcid, Work work) {
1588+
Work w = workManager.updateWork(orcid, work, true);
1589+
sourceUtils.setSourceName(w);
1590+
return w;
1591+
}
1592+
15701593
}

orcid-core/src/main/java/org/orcid/core/togglz/Features.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
public enum Features implements Feature {
88

9+
@Label("Stop sending notification if work has not been updated")
10+
STOP_SENDING_NOTIFICATION_WORK_NOT_UPDATED,
11+
912
@Label("Move client from a member to another member")
1013
MOVE_CLIENT,
1114

orcid-core/src/main/java/org/orcid/pojo/ajaxForm/WorkForm.java

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import java.io.Serializable;
44
import java.util.ArrayList;
55
import java.util.List;
6+
import java.util.concurrent.atomic.AtomicBoolean;
67

8+
import org.apache.commons.lang.StringUtils;
79
import org.orcid.core.togglz.Features;
810
import org.orcid.jaxb.model.common.CitationType;
911
import org.orcid.jaxb.model.common.Relationship;
@@ -1039,4 +1041,253 @@ public boolean equals(Object obj) {
10391041
return false;
10401042
return true;
10411043
}
1044+
1045+
public boolean compare(Object obj) {
1046+
if (this == obj)
1047+
return true;
1048+
if (obj == null)
1049+
return false;
1050+
if (getClass() != obj.getClass())
1051+
return false;
1052+
WorkForm other = (WorkForm) obj;
1053+
1054+
if (!compareStrings(citationForDisplay, other.citationForDisplay))
1055+
return false;
1056+
if (!compareTexts(countryCode, other.countryCode, true))
1057+
return false;
1058+
if (!compareTexts(countryName, other.countryName, true))
1059+
return false;
1060+
if (!compareTexts(journalTitle, other.journalTitle, false))
1061+
return false;
1062+
if (!compareTexts(languageCode, other.languageCode, true))
1063+
return false;
1064+
if (!compareTexts(languageName, other.languageName, true))
1065+
return false;
1066+
if (!compareTexts(putCode, other.putCode, true))
1067+
return false;
1068+
if (!compareTexts(shortDescription, other.shortDescription, false))
1069+
return false;
1070+
if (!compareTexts(subtitle, other.subtitle, false))
1071+
return false;
1072+
if (!compareTexts(title, other.title, false))
1073+
return false;
1074+
if (!compareTexts(url, other.url, false))
1075+
return false;
1076+
if (!compareTexts(workType, other.workType, true))
1077+
return false;
1078+
1079+
if (!isEachObjectNull(citation, other.citation)) {
1080+
if (isAnyObjectNotNull(citation, other.citation)) {
1081+
return false;
1082+
} else if (citation.getCitation() != null && other.citation.getCitation() != null && !compareTexts(citation.getCitation(), other.citation.getCitation(), false))
1083+
return false;
1084+
}
1085+
if (!isEachObjectNull(translatedTitle, other.translatedTitle)) {
1086+
if (isAnyObjectNotNull(translatedTitle, other.translatedTitle)) {
1087+
return false;
1088+
} else if (other.translatedTitle.getContent() != null && !translatedTitle.getContent().equals(other.translatedTitle.getContent()))
1089+
return false;
1090+
}
1091+
if (!isEachObjectNull(publicationDate, other.publicationDate)) {
1092+
if (isAnyObjectNotNull(publicationDate, other.publicationDate)) {
1093+
if (publicationDate == null && StringUtils.isNotBlank(other.publicationDate.getYear()))
1094+
return false;
1095+
} else if (
1096+
!compareStrings(publicationDate.getYear(), other.publicationDate.getYear()) &&
1097+
!compareStrings(publicationDate.getMonth(), other.publicationDate.getMonth()) &&
1098+
!compareStrings(publicationDate.getDay(), other.publicationDate.getDay())
1099+
)
1100+
return false;
1101+
}
1102+
if (visibility != null && other.visibility != null && !visibility.getVisibility().value().equals(other.visibility.getVisibility().value()))
1103+
return false;
1104+
if (isAnyObjectNotNull(workExternalIdentifiers, other.workExternalIdentifiers)) {
1105+
return false;
1106+
} else if (workExternalIdentifiers != null && other.workExternalIdentifiers != null && workExternalIdentifiers.size() != other.workExternalIdentifiers.size()) {
1107+
return false;
1108+
} else if (compareExternalIdentifiers(workExternalIdentifiers, other.workExternalIdentifiers))
1109+
return false;
1110+
if (isAnyObjectNotNull(contributors, other.contributors)) {
1111+
if (contributors == null && other.contributors != null && other.contributors.size() > 0) {
1112+
return false;
1113+
}
1114+
} else if (contributors != null && other.contributors != null && contributors.size() != other.contributors.size()) {
1115+
return false;
1116+
} else if (compareContributors(contributors, other.contributors))
1117+
return false;
1118+
if (isAnyObjectNotNull(contributorsGroupedByOrcid, other.contributorsGroupedByOrcid)) {
1119+
if (contributorsGroupedByOrcid == null && other.contributorsGroupedByOrcid != null && other.contributorsGroupedByOrcid.size() > 0) {
1120+
return false;
1121+
}
1122+
} else if (contributorsGroupedByOrcid != null && other.contributorsGroupedByOrcid != null && contributorsGroupedByOrcid.size() != other.contributorsGroupedByOrcid.size()) {
1123+
return false;
1124+
} else if (compareContributorsGroupedByOrcid(contributorsGroupedByOrcid, other.contributorsGroupedByOrcid))
1125+
return false;
1126+
return true;
1127+
}
1128+
1129+
private boolean compareExternalIdentifiers(List<ActivityExternalIdentifier> a, List<ActivityExternalIdentifier> b) {
1130+
if (isEachObjectNull(a, b)) {
1131+
return false;
1132+
}
1133+
AtomicBoolean isDifferent = new AtomicBoolean(false);
1134+
a.forEach(activityA -> b.forEach(activityB -> {
1135+
if (!compareTexts(activityA.getExternalIdentifierType(), activityB.getExternalIdentifierType(), false)) {
1136+
isDifferent.set(true);
1137+
return;
1138+
}
1139+
if (!compareTexts(activityA.getExternalIdentifierId(), activityB.getExternalIdentifierId(), false)) {
1140+
isDifferent.set(true);
1141+
return;
1142+
}
1143+
if (!compareTexts(activityA.getUrl(), activityB.getUrl(), true)) {
1144+
isDifferent.set(true);
1145+
return;
1146+
}
1147+
if (!compareTexts(activityA.getRelationship(), activityB.getRelationship(), false)) {
1148+
isDifferent.set(true);
1149+
return;
1150+
}
1151+
}));
1152+
return isDifferent.get();
1153+
}
1154+
1155+
private boolean compareContributors(List<Contributor> a, List<Contributor> b) {
1156+
if (isEachObjectNull(a, b)) {
1157+
return false;
1158+
}
1159+
AtomicBoolean isDifferent = new AtomicBoolean(false);
1160+
a.forEach(contributorA -> b.forEach(contributorB -> {
1161+
if (!compareTexts(contributorA.getCreditName(), contributorB.getCreditName(), false)) {
1162+
isDifferent.set(true);
1163+
return;
1164+
}
1165+
if (!compareTexts(contributorA.getOrcid(), contributorB.getOrcid(), false)) {
1166+
isDifferent.set(true);
1167+
return;
1168+
}
1169+
if (!compareTexts(contributorA.getContributorRole(), contributorB.getContributorRole(), true)) {
1170+
isDifferent.set(true);
1171+
return;
1172+
}
1173+
if (!compareTexts(contributorA.getContributorSequence(), contributorB.getContributorSequence(), true)) {
1174+
isDifferent.set(true);
1175+
return;
1176+
}
1177+
}));
1178+
return isDifferent.get();
1179+
}
1180+
1181+
private boolean compareContributorsGroupedByOrcid(List<ContributorsRolesAndSequences> a, List<ContributorsRolesAndSequences> b) {
1182+
if (isEachObjectNull(a, b)) {
1183+
return false;
1184+
}
1185+
AtomicBoolean isDifferent = new AtomicBoolean(false);
1186+
a.forEach(contributorA -> b.forEach(contributorB -> {
1187+
if (contributorA.getCreditName() != null && contributorB.getCreditName() != null) {
1188+
if (!contributorA.getCreditName().getContent().equals(contributorB.getCreditName().getContent())) {
1189+
isDifferent.set(true);
1190+
return;
1191+
}
1192+
} else if (isAnyObjectNotNull(contributorA.getCreditName(), contributorB.getCreditName())) {
1193+
isDifferent.set(true);
1194+
return;
1195+
}
1196+
1197+
1198+
if (contributorA.getContributorOrcid() != null && contributorB.getContributorOrcid() != null) {
1199+
if (
1200+
!isEachObjectNull(contributorA.getContributorOrcid().getUri(), contributorB.getContributorOrcid().getUri()) &&
1201+
contributorA.getContributorOrcid().getUri().equalsIgnoreCase(contributorB.getContributorOrcid().getUri())
1202+
) {
1203+
isDifferent.set(true);
1204+
return;
1205+
}
1206+
} else if (isAnyObjectNotNull(contributorA.getContributorOrcid(), contributorB.getContributorOrcid())) {
1207+
isDifferent.set(true);
1208+
return;
1209+
}
1210+
1211+
if (contributorA.getRolesAndSequences() != null && contributorB.getRolesAndSequences() != null) {
1212+
if (contributorA.getRolesAndSequences().size() != contributorB.getRolesAndSequences().size()) {
1213+
isDifferent.set(true);
1214+
return;
1215+
}
1216+
contributorA.getRolesAndSequences().forEach(rolesA -> contributorB.getRolesAndSequences().forEach(rolesB -> {
1217+
if (rolesA.getContributorRole() != null && rolesB.getContributorRole() != null) {
1218+
if (compareStrings(rolesA.getContributorRole(), rolesB.getContributorRole())) {
1219+
isDifferent.set(true);
1220+
return;
1221+
}
1222+
}
1223+
if (isAnyObjectNotNull(rolesA.getContributorRole(), rolesB.getContributorRole())) {
1224+
isDifferent.set(true);
1225+
return;
1226+
}
1227+
if (rolesA.getContributorSequence() != null && rolesB.getContributorSequence() != null) {
1228+
if (rolesA.getContributorSequence().equals(rolesB.getContributorSequence())) {
1229+
isDifferent.set(true);
1230+
return;
1231+
}
1232+
}
1233+
if (isAnyObjectNotNull(rolesA.getContributorSequence(), rolesB.getContributorSequence())) {
1234+
isDifferent.set(true);
1235+
return;
1236+
}
1237+
}));
1238+
1239+
} else if (isAnyObjectNotNull(contributorA.getRolesAndSequences(), contributorB.getRolesAndSequences())) {
1240+
isDifferent.set(true);
1241+
return;
1242+
}
1243+
}));
1244+
return isDifferent.get();
1245+
}
1246+
1247+
private boolean compareTexts(Text a, Text b, boolean ignoreCase) {
1248+
if (isEachObjectNull(a, b)) {
1249+
return true;
1250+
} else if (isAnyObjectNotNull(a, b)) {
1251+
if (a == null && b.getValue() == null) {
1252+
return true;
1253+
} else if (a == null && StringUtils.isBlank(b.getValue())) {
1254+
return true;
1255+
}
1256+
return false;
1257+
} else if (a.getValue() != null && b.getValue() != null) {
1258+
if (ignoreCase) {
1259+
if (!a.getValue().equalsIgnoreCase(b.getValue())) {
1260+
return false;
1261+
}
1262+
} else {
1263+
if (!a.getValue().equals(b.getValue())) {
1264+
return false;
1265+
}
1266+
}
1267+
}
1268+
return true;
1269+
}
1270+
1271+
private boolean compareStrings(String a, String b) {
1272+
if (isEachObjectNull(a, b)) {
1273+
return true;
1274+
} else if (isAnyObjectNotNull(a, b)) {
1275+
if (a == null && b != null && StringUtils.isNotBlank(b)) {
1276+
return true;
1277+
}
1278+
return false;
1279+
} else return a.equalsIgnoreCase(b);
1280+
}
1281+
1282+
private boolean isEachObjectNull(Object a, Object b) {
1283+
return a == null && b == null;
1284+
}
1285+
1286+
private boolean isAnyObjectNotNull(Object a, Object b) {
1287+
if (a == null && b != null || a != null && b == null) {
1288+
return true;
1289+
}
1290+
return false;
1291+
}
1292+
10421293
}

0 commit comments

Comments
 (0)