|
1 | 1 | package edu.harvard.iq.dataverse.workflow.internalspi; |
2 | 2 |
|
| 3 | +import edu.harvard.iq.dataverse.ControlledVocabularyValue; |
3 | 4 | import edu.harvard.iq.dataverse.Dataset; |
4 | 5 | import edu.harvard.iq.dataverse.DatasetField; |
| 6 | +import edu.harvard.iq.dataverse.DatasetFieldCompoundValue; |
5 | 7 | import edu.harvard.iq.dataverse.DatasetFieldType; |
| 8 | +import edu.harvard.iq.dataverse.DatasetFieldValue; |
6 | 9 | import edu.harvard.iq.dataverse.DatasetVersion; |
7 | 10 | import edu.harvard.iq.dataverse.GlobalId; |
8 | 11 | import edu.harvard.iq.dataverse.branding.BrandingUtil; |
|
26 | 29 | import java.net.URI; |
27 | 30 | import java.net.URISyntaxException; |
28 | 31 | import java.nio.charset.StandardCharsets; |
29 | | -import java.util.Arrays; |
| 32 | +import java.util.ArrayList; |
30 | 33 | import java.util.Collection; |
31 | 34 | import java.util.HashMap; |
32 | 35 | import java.util.Iterator; |
@@ -88,12 +91,34 @@ public WorkflowStepResult run(WorkflowContext context) { |
88 | 91 | // First check that we have what is required |
89 | 92 | Dataset d = context.getDataset(); |
90 | 93 | DatasetVersion dv = d.getReleasedVersion(); |
| 94 | + DatasetVersion priorVersion = d.getPriorReleasedVersion(); |
91 | 95 | List<DatasetField> dvf = dv.getDatasetFields(); |
92 | 96 | Map<String, DatasetField> fields = new HashMap<String, DatasetField>(); |
93 | 97 | List<String> reqFields = ListSplitUtil.split((String) context.getSettings().getOrDefault(COARNotifyRelationshipAnnouncementTriggerFields.toString(), "")); |
| 98 | + |
| 99 | + Map<String, DatasetField> priorFields = new HashMap<String, DatasetField>(); |
| 100 | + if (priorVersion != null) { |
| 101 | + for (DatasetField pdf : priorVersion.getDatasetFields()) { |
| 102 | + if (!pdf.isEmpty() && reqFields.contains(pdf.getDatasetFieldType().getName())) { |
| 103 | + priorFields.put(pdf.getDatasetFieldType().getName(), pdf); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
94 | 108 | for (DatasetField df : dvf) { |
95 | 109 | if (!df.isEmpty() && reqFields.contains(df.getDatasetFieldType().getName())) { |
96 | | - fields.put(df.getDatasetFieldType().getName(), df); |
| 110 | + DatasetField priorField = priorFields.get(df.getDatasetFieldType().getName()); |
| 111 | + |
| 112 | + if (priorVersion == null || priorField == null) { |
| 113 | + // No prior version, include all values |
| 114 | + fields.put(df.getDatasetFieldType().getName(), df); |
| 115 | + } else { |
| 116 | + // Create a filtered field with only new values |
| 117 | + DatasetField filteredField = filterNewValues(df, priorField); |
| 118 | + if (!filteredField.isEmpty()) { |
| 119 | + fields.put(df.getDatasetFieldType().getName(), filteredField); |
| 120 | + } |
| 121 | + } |
97 | 122 | } |
98 | 123 | } |
99 | 124 |
|
@@ -214,6 +239,14 @@ JsonArray getObjects(WorkflowContext ctxt, Map<String, DatasetField> fields) { |
214 | 239 |
|
215 | 240 | private JsonObject getRelationshipObject(DatasetFieldType dft, JsonValue jval, Dataset d, |
216 | 241 | Map<String, String> localContext) { |
| 242 | + if (logger.isLoggable(Level.FINE)) { |
| 243 | + if (jval.getValueType().equals(jakarta.json.JsonValue.ValueType.OBJECT)) { |
| 244 | + logger.fine("Parsing : " + JsonUtil.prettyPrint(jval.asJsonObject())); |
| 245 | + } |
| 246 | + else if (jval.getValueType().equals(jakarta.json.JsonValue.ValueType.STRING)) { |
| 247 | + logger.fine("Parsing : " + jval.toString()); |
| 248 | + } |
| 249 | + } |
217 | 250 | String[] answers = getBestIdAndType(dft, jval); |
218 | 251 | String id = answers[0]; |
219 | 252 | String type = answers[1]; |
@@ -335,7 +368,8 @@ private String[] getBestIdAndType(DatasetFieldType dft, JsonValue jv) { |
335 | 368 | break; |
336 | 369 | } |
337 | 370 | } |
338 | | - } else if (jo.containsKey(publicationURL.getLabel())) { |
| 371 | + } |
| 372 | + if (id == null && jo.containsKey(publicationURL.getLabel())) { |
339 | 373 |
|
340 | 374 | String value = jo.getString(publicationURL.getLabel()); |
341 | 375 | if (isURI(value)) { |
@@ -412,4 +446,90 @@ private boolean isURI(String number) { |
412 | 446 | return false; |
413 | 447 | } |
414 | 448 |
|
| 449 | + /** |
| 450 | + * Create a new DatasetField containing only values that are new compared to the |
| 451 | + * prior field. This creates a detached copy to avoid modifying the managed |
| 452 | + * entity. |
| 453 | + * |
| 454 | + * @param currentField The field from the current version |
| 455 | + * @param priorField The field from the prior version |
| 456 | + * @return A new DatasetField with only new values |
| 457 | + */ |
| 458 | + private DatasetField filterNewValues(DatasetField currentField, DatasetField priorField) { |
| 459 | + DatasetField filtered = new DatasetField(); |
| 460 | + DatasetFieldType fieldType = currentField.getDatasetFieldType(); |
| 461 | + filtered.setDatasetFieldType(fieldType); |
| 462 | + |
| 463 | + // Handle primitive fields |
| 464 | + if (fieldType.isPrimitive()) { |
| 465 | + if (fieldType.isControlledVocabulary()) { |
| 466 | + // Handle controlled vocabulary fields |
| 467 | + List<ControlledVocabularyValue> currentCVs = currentField.getControlledVocabularyValues(); |
| 468 | + List<ControlledVocabularyValue> priorCVs = priorField != null ? priorField.getControlledVocabularyValues() : new ArrayList<>(); |
| 469 | + |
| 470 | + List<ControlledVocabularyValue> newCVs = new ArrayList<>(); |
| 471 | + for (ControlledVocabularyValue currentCV : currentCVs) { |
| 472 | + boolean isNew = true; |
| 473 | + for (ControlledVocabularyValue priorCV : priorCVs) { |
| 474 | + if (currentCV.getStrValue().equals(priorCV.getStrValue())) { |
| 475 | + isNew = false; |
| 476 | + break; |
| 477 | + } |
| 478 | + } |
| 479 | + if (isNew) { |
| 480 | + newCVs.add(currentCV); |
| 481 | + } |
| 482 | + } |
| 483 | + filtered.setControlledVocabularyValues(newCVs); |
| 484 | + } else { |
| 485 | + // Handle regular fields |
| 486 | + List<DatasetFieldValue> currentDFVs = currentField.getDatasetFieldValues(); |
| 487 | + List<DatasetFieldValue> priorDFVs = priorField != null ? priorField.getDatasetFieldValues() : new ArrayList<>(); |
| 488 | + |
| 489 | + List<DatasetFieldValue> newDFVs = new ArrayList<>(); |
| 490 | + for (DatasetFieldValue currentDFV : currentDFVs) { |
| 491 | + boolean isNew = true; |
| 492 | + for (DatasetFieldValue priorDFV : priorDFVs) { |
| 493 | + if (currentDFV.valuesEqual(priorDFV)) { |
| 494 | + isNew = false; |
| 495 | + break; |
| 496 | + } |
| 497 | + } |
| 498 | + if (isNew) { |
| 499 | + newDFVs.add(currentDFV); |
| 500 | + } |
| 501 | + } |
| 502 | + filtered.setDatasetFieldValues(newDFVs); |
| 503 | + } |
| 504 | + } else { |
| 505 | + // Handle compound fields |
| 506 | + List<DatasetFieldCompoundValue> currentCompounds = currentField.getDatasetFieldCompoundValues(); |
| 507 | + List<DatasetFieldCompoundValue> priorCompounds = priorField != null ? priorField.getDatasetFieldCompoundValues() : new ArrayList<>(); |
| 508 | + |
| 509 | + List<DatasetFieldCompoundValue> newCompounds = new ArrayList<>(); |
| 510 | + |
| 511 | + for (DatasetFieldCompoundValue currentCompound : currentCompounds) { |
| 512 | + boolean isNew = true; |
| 513 | + |
| 514 | + for (DatasetFieldCompoundValue priorCompound : priorCompounds) { |
| 515 | + |
| 516 | + if (currentCompound.valuesEqual(priorCompound)) { |
| 517 | + isNew = false; |
| 518 | + break; |
| 519 | + } |
| 520 | + } |
| 521 | + |
| 522 | + if (isNew) { |
| 523 | + // Create a copy of the compound value with all its children |
| 524 | + DatasetFieldCompoundValue newCompound = currentCompound.copy(filtered); |
| 525 | + newCompound.setParentDatasetField(filtered); |
| 526 | + newCompounds.add(newCompound); |
| 527 | + } |
| 528 | + } |
| 529 | + |
| 530 | + filtered.setDatasetFieldCompoundValues(newCompounds); |
| 531 | + } |
| 532 | + |
| 533 | + return filtered; |
| 534 | + } |
415 | 535 | } |
0 commit comments