|
3 | 3 | import static org.apache.commons.collections4.CollectionUtils.isEmpty;
|
4 | 4 | import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
|
5 | 5 | import static org.folio.entlinks.domain.entity.AuthorityConstants.CORPORATE_NAME_HEADING;
|
| 6 | +import static org.folio.entlinks.domain.entity.AuthorityConstants.CORPORATE_NAME_HEADING_TRUNC; |
6 | 7 | import static org.folio.entlinks.domain.entity.AuthorityConstants.CORPORATE_NAME_TITLE_HEADING;
|
| 8 | +import static org.folio.entlinks.domain.entity.AuthorityConstants.CORPORATE_NAME_TITLE_HEADING_TRUNC; |
7 | 9 | import static org.folio.entlinks.domain.entity.AuthorityConstants.GENRE_TERM_HEADING;
|
| 10 | +import static org.folio.entlinks.domain.entity.AuthorityConstants.GENRE_TERM_HEADING_TRUNC; |
8 | 11 | import static org.folio.entlinks.domain.entity.AuthorityConstants.GEOGRAPHIC_NAME_HEADING;
|
| 12 | +import static org.folio.entlinks.domain.entity.AuthorityConstants.GEOGRAPHIC_NAME_HEADING_TRUNC; |
9 | 13 | import static org.folio.entlinks.domain.entity.AuthorityConstants.MEETING_NAME_HEADING;
|
| 14 | +import static org.folio.entlinks.domain.entity.AuthorityConstants.MEETING_NAME_HEADING_TRUNC; |
10 | 15 | import static org.folio.entlinks.domain.entity.AuthorityConstants.MEETING_NAME_TITLE_HEADING;
|
| 16 | +import static org.folio.entlinks.domain.entity.AuthorityConstants.MEETING_NAME_TITLE_HEADING_TRUNC; |
11 | 17 | import static org.folio.entlinks.domain.entity.AuthorityConstants.PERSONAL_NAME_HEADING;
|
| 18 | +import static org.folio.entlinks.domain.entity.AuthorityConstants.PERSONAL_NAME_HEADING_TRUNC; |
12 | 19 | import static org.folio.entlinks.domain.entity.AuthorityConstants.PERSONAL_NAME_TITLE_HEADING;
|
| 20 | +import static org.folio.entlinks.domain.entity.AuthorityConstants.PERSONAL_NAME_TITLE_HEADING_TRUNC; |
13 | 21 | import static org.folio.entlinks.domain.entity.AuthorityConstants.TOPICAL_TERM_HEADING;
|
| 22 | +import static org.folio.entlinks.domain.entity.AuthorityConstants.TOPICAL_TERM_HEADING_TRUNC; |
14 | 23 | import static org.folio.entlinks.domain.entity.AuthorityConstants.UNIFORM_TITLE_HEADING;
|
| 24 | +import static org.folio.entlinks.domain.entity.AuthorityConstants.UNIFORM_TITLE_HEADING_TRUNC; |
15 | 25 |
|
16 | 26 | import java.util.ArrayList;
|
| 27 | +import java.util.HashSet; |
17 | 28 | import java.util.List;
|
18 | 29 | import java.util.Objects;
|
| 30 | +import java.util.Set; |
19 | 31 | import lombok.experimental.UtilityClass;
|
20 | 32 | import lombok.extern.log4j.Log4j2;
|
| 33 | +import org.apache.commons.collections4.CollectionUtils; |
21 | 34 | import org.folio.entlinks.domain.dto.AuthorityDto;
|
| 35 | +import org.folio.entlinks.domain.dto.RelatedHeading; |
22 | 36 | import org.folio.entlinks.domain.entity.AuthorityBase;
|
23 | 37 | import org.folio.entlinks.domain.entity.HeadingRef;
|
| 38 | +import org.folio.entlinks.domain.entity.RelationshipType; |
24 | 39 |
|
25 | 40 | @UtilityClass
|
26 | 41 | @Log4j2
|
@@ -145,9 +160,66 @@ public static void extractAuthoritySaftHeadings(AuthorityDto source, AuthorityBa
|
145 | 160 | if (isNotEmpty(source.getSaftGenreTerm())) {
|
146 | 161 | saftHeadings.addAll(asSftHeadings(source.getSaftGenreTerm(), GENRE_TERM_HEADING));
|
147 | 162 | }
|
| 163 | + extractAuthoritySaftHeadingsTruncated(source, saftHeadings); |
| 164 | + addRelationshipsToSaftHeadings(source, saftHeadings); |
148 | 165 | target.setSaftHeadings(saftHeadings);
|
149 | 166 | }
|
150 | 167 |
|
| 168 | + private void extractAuthoritySaftHeadingsTruncated(AuthorityDto source, List<HeadingRef> saftHeadings) { |
| 169 | + if (isNotEmpty(source.getSaftPersonalNameTrunc())) { |
| 170 | + saftHeadings.addAll(asSftHeadings(source.getSaftPersonalNameTrunc(), PERSONAL_NAME_HEADING_TRUNC)); |
| 171 | + } |
| 172 | + if (isNotEmpty(source.getSaftPersonalNameTitleTrunc())) { |
| 173 | + saftHeadings.addAll(asSftHeadings(source.getSaftPersonalNameTitleTrunc(), PERSONAL_NAME_TITLE_HEADING_TRUNC)); |
| 174 | + } |
| 175 | + if (isNotEmpty(source.getSaftCorporateNameTrunc())) { |
| 176 | + saftHeadings.addAll(asSftHeadings(source.getSaftCorporateNameTrunc(), CORPORATE_NAME_HEADING_TRUNC)); |
| 177 | + } |
| 178 | + if (isNotEmpty(source.getSaftCorporateNameTitleTrunc())) { |
| 179 | + saftHeadings.addAll(asSftHeadings(source.getSaftCorporateNameTitleTrunc(), CORPORATE_NAME_TITLE_HEADING_TRUNC)); |
| 180 | + } |
| 181 | + if (isNotEmpty(source.getSaftMeetingNameTrunc())) { |
| 182 | + saftHeadings.addAll(asSftHeadings(source.getSaftMeetingNameTrunc(), MEETING_NAME_HEADING_TRUNC)); |
| 183 | + } |
| 184 | + if (isNotEmpty(source.getSaftMeetingNameTitleTrunc())) { |
| 185 | + saftHeadings.addAll(asSftHeadings(source.getSaftMeetingNameTitleTrunc(), MEETING_NAME_TITLE_HEADING_TRUNC)); |
| 186 | + } |
| 187 | + if (isNotEmpty(source.getSaftUniformTitleTrunc())) { |
| 188 | + saftHeadings.addAll(asSftHeadings(source.getSaftUniformTitleTrunc(), UNIFORM_TITLE_HEADING_TRUNC)); |
| 189 | + } |
| 190 | + if (isNotEmpty(source.getSaftTopicalTermTrunc())) { |
| 191 | + saftHeadings.addAll(asSftHeadings(source.getSaftTopicalTermTrunc(), TOPICAL_TERM_HEADING_TRUNC)); |
| 192 | + } |
| 193 | + if (isNotEmpty(source.getSaftGeographicNameTrunc())) { |
| 194 | + saftHeadings.addAll(asSftHeadings(source.getSaftGeographicNameTrunc(), GEOGRAPHIC_NAME_HEADING_TRUNC)); |
| 195 | + } |
| 196 | + if (isNotEmpty(source.getSaftGenreTermTrunc())) { |
| 197 | + saftHeadings.addAll(asSftHeadings(source.getSaftGenreTermTrunc(), GENRE_TERM_HEADING_TRUNC)); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + private void addRelationshipsToSaftHeadings(final AuthorityDto source, final List<HeadingRef> headingRefs) { |
| 202 | + processRelationshipHeadings(source.getSaftBroaderTerm(), headingRefs, RelationshipType.BROADER_TERM); |
| 203 | + processRelationshipHeadings(source.getSaftNarrowerTerm(), headingRefs, RelationshipType.NARROWER_TERM); |
| 204 | + processRelationshipHeadings(source.getSaftEarlierHeading(), headingRefs, RelationshipType.EARLIER_HEADING); |
| 205 | + processRelationshipHeadings(source.getSaftLaterHeading(), headingRefs, RelationshipType.LATER_HEADING); |
| 206 | + } |
| 207 | + |
| 208 | + private void processRelationshipHeadings(List<RelatedHeading> relationshipHeadings, |
| 209 | + final List<HeadingRef> headingRefs, final RelationshipType relationshipType) { |
| 210 | + if (isNotEmpty(relationshipHeadings)) { |
| 211 | + headingRefs.forEach(headingRef -> |
| 212 | + relationshipHeadings.forEach(relationshipHeading -> { |
| 213 | + if (relationshipHeading.getHeadingType().equals(headingRef.getHeadingType()) |
| 214 | + && relationshipHeading.getHeadingRef().equals(headingRef.getHeading())) { |
| 215 | + Set<RelationshipType> relationshipTypeSet = getOrCreateRelationshipTypeSet(headingRef); |
| 216 | + relationshipTypeSet.add(relationshipType); |
| 217 | + } |
| 218 | + }) |
| 219 | + ); |
| 220 | + } |
| 221 | + } |
| 222 | + |
151 | 223 | public static void extractAuthorityDtoHeadingValue(AuthorityBase source, AuthorityDto target) {
|
152 | 224 | if (source.getHeadingType() == null || source.getHeading() == null) {
|
153 | 225 | return;
|
@@ -215,13 +287,65 @@ private void extractAuthorityDtoSaftHeading(HeadingRef headingRef, AuthorityDto
|
215 | 287 | case TOPICAL_TERM_HEADING -> target.addSaftTopicalTermItem(headingRef.getHeading());
|
216 | 288 | case GEOGRAPHIC_NAME_HEADING -> target.addSaftGeographicNameItem(headingRef.getHeading());
|
217 | 289 | case GENRE_TERM_HEADING -> target.addSaftGenreTermItem(headingRef.getHeading());
|
| 290 | + case PERSONAL_NAME_HEADING_TRUNC -> target.addSaftPersonalNameTruncItem(headingRef.getHeading()); |
| 291 | + case PERSONAL_NAME_TITLE_HEADING_TRUNC -> target.addSaftPersonalNameTitleTruncItem(headingRef.getHeading()); |
| 292 | + case CORPORATE_NAME_HEADING_TRUNC -> target.addSaftCorporateNameTruncItem(headingRef.getHeading()); |
| 293 | + case CORPORATE_NAME_TITLE_HEADING_TRUNC -> target.addSaftCorporateNameTitleTruncItem(headingRef.getHeading()); |
| 294 | + case MEETING_NAME_HEADING_TRUNC -> target.addSaftMeetingNameTruncItem(headingRef.getHeading()); |
| 295 | + case MEETING_NAME_TITLE_HEADING_TRUNC -> target.addSaftMeetingNameTitleTruncItem(headingRef.getHeading()); |
| 296 | + case UNIFORM_TITLE_HEADING_TRUNC -> target.addSaftUniformTitleTruncItem(headingRef.getHeading()); |
| 297 | + case TOPICAL_TERM_HEADING_TRUNC -> target.addSaftTopicalTermTruncItem(headingRef.getHeading()); |
| 298 | + case GEOGRAPHIC_NAME_HEADING_TRUNC -> target.addSaftGeographicNameTruncItem(headingRef.getHeading()); |
| 299 | + case GENRE_TERM_HEADING_TRUNC -> target.addSaftGenreTermTruncItem(headingRef.getHeading()); |
218 | 300 | default -> log.warn("Invalid saft heading type - {} cannot be mapped", headingRef.getHeadingType());
|
219 | 301 | }
|
| 302 | + extractSaftHeadingsRelationships(headingRef, target); |
| 303 | + } |
| 304 | + |
| 305 | + private void extractSaftHeadingsRelationships(HeadingRef headingRef, AuthorityDto target) { |
| 306 | + if (CollectionUtils.isEmpty(target.getSaftNarrowerTerm())) { |
| 307 | + target.setSaftNarrowerTerm(new ArrayList<>()); |
| 308 | + } |
| 309 | + if (CollectionUtils.isEmpty(target.getSaftBroaderTerm())) { |
| 310 | + target.setSaftBroaderTerm(new ArrayList<>()); |
| 311 | + } |
| 312 | + if (CollectionUtils.isEmpty(target.getSaftEarlierHeading())) { |
| 313 | + target.setSaftEarlierHeading(new ArrayList<>()); |
| 314 | + } |
| 315 | + if (CollectionUtils.isEmpty(target.getSaftLaterHeading())) { |
| 316 | + target.setSaftLaterHeading(new ArrayList<>()); |
| 317 | + } |
| 318 | + if (isNotEmpty(headingRef.getRelationshipType())) { |
| 319 | + headingRef.getRelationshipType().forEach( |
| 320 | + relationshipType -> { |
| 321 | + switch (relationshipType) { |
| 322 | + case BROADER_TERM -> target.getSaftBroaderTerm() |
| 323 | + .add(new RelatedHeading(headingRef.getHeading(), headingRef.getHeadingType())); |
| 324 | + case NARROWER_TERM -> target.getSaftNarrowerTerm() |
| 325 | + .add(new RelatedHeading(headingRef.getHeading(), headingRef.getHeadingType())); |
| 326 | + case EARLIER_HEADING -> target.getSaftEarlierHeading() |
| 327 | + .add(new RelatedHeading(headingRef.getHeading(), headingRef.getHeadingType())); |
| 328 | + case LATER_HEADING -> target.getSaftLaterHeading() |
| 329 | + .add(new RelatedHeading(headingRef.getHeading(), headingRef.getHeadingType())); |
| 330 | + default -> log.warn("Invalid saft relationship type - {} cannot be mapped", relationshipType); |
| 331 | + } |
| 332 | + } |
| 333 | + ); |
| 334 | + } |
220 | 335 | }
|
221 | 336 |
|
222 | 337 | private static List<HeadingRef> asSftHeadings(List<String> headingValues, String headingType) {
|
223 | 338 | return headingValues.stream()
|
224 | 339 | .map(headingValue -> new HeadingRef(headingType, headingValue))
|
225 | 340 | .toList();
|
226 | 341 | }
|
| 342 | + |
| 343 | + private static Set<RelationshipType> getOrCreateRelationshipTypeSet(HeadingRef heading) { |
| 344 | + Set<RelationshipType> relationshipTypeSet = heading.getRelationshipType(); |
| 345 | + if (relationshipTypeSet == null) { |
| 346 | + relationshipTypeSet = new HashSet<>(); |
| 347 | + heading.setRelationshipType(relationshipTypeSet); |
| 348 | + } |
| 349 | + return relationshipTypeSet; |
| 350 | + } |
227 | 351 | }
|
0 commit comments