|
3 | 3 | import java.io.Serializable; |
4 | 4 | import java.util.ArrayList; |
5 | 5 | import java.util.List; |
| 6 | +import java.util.concurrent.atomic.AtomicBoolean; |
6 | 7 |
|
| 8 | +import org.apache.commons.lang.StringUtils; |
7 | 9 | import org.orcid.core.togglz.Features; |
8 | 10 | import org.orcid.jaxb.model.common.CitationType; |
9 | 11 | import org.orcid.jaxb.model.common.Relationship; |
@@ -1039,4 +1041,253 @@ public boolean equals(Object obj) { |
1039 | 1041 | return false; |
1040 | 1042 | return true; |
1041 | 1043 | } |
| 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 | + |
1042 | 1293 | } |
0 commit comments