Skip to content
This repository was archived by the owner on Aug 1, 2025. It is now read-only.

Commit 6ee3f2d

Browse files
authored
Merge pull request #35 from Breeding-Insight/feature/BI-2304
BI-2304 - Support Duplicate Germplasm in Lists Referencing Existing Germplasm
2 parents 4da5d94 + 76665ab commit 6ee3f2d

File tree

5 files changed

+100
-12
lines changed

5 files changed

+100
-12
lines changed

src/main/java/org/brapi/test/BrAPITestServer/model/entity/core/ListEntity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class ListEntity extends BrAPIPrimaryEntity {
3030
@ManyToOne(fetch = FetchType.LAZY)
3131
private PersonEntity listOwnerPerson;
3232
@OneToMany(mappedBy="list", cascade = CascadeType.ALL)
33+
@OrderColumn(name = "position")
3334
private List<ListItemEntity> data;
3435

3536
public PersonEntity getListOwnerPerson() {

src/main/java/org/brapi/test/BrAPITestServer/model/entity/core/ListItemEntity.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public class ListItemEntity extends BrAPIBaseEntity {
1111
private ListEntity list;
1212
@Column
1313
private String item;
14+
@Column
15+
private Integer position;
1416

1517
public ListEntity getList() {
1618
return list;
@@ -24,6 +26,10 @@ public String getItem() {
2426
public void setItem(String item) {
2527
this.item = item;
2628
}
27-
28-
29+
public Integer getPosition() {
30+
return position;
31+
}
32+
public void setPosition(Integer position) {
33+
this.position = position;
34+
}
2935
}

src/main/java/org/brapi/test/BrAPITestServer/service/core/ListService.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package org.brapi.test.BrAPITestServer.service.core;
22

3-
import java.util.ArrayList;
4-
import java.util.Date;
5-
import java.util.List;
6-
import java.util.Optional;
3+
import java.util.*;
74
import java.util.stream.Collectors;
85

96
import jakarta.validation.Valid;
@@ -237,12 +234,19 @@ private void updateEntity(ListEntity entity, @Valid ListNewRequest list) throws
237234
}
238235

239236
if (list.getData() != null) {
240-
entity.setData(list.getData().stream().map((item) -> {
241-
ListItemEntity itemEntity = new ListItemEntity();
242-
itemEntity.setItem(item);
243-
itemEntity.setList(entity);
244-
return itemEntity;
245-
}).collect(Collectors.toList()));
237+
List<ListItemEntity> items = new ArrayList<>();
238+
ListIterator<String> iter = list.getData().listIterator();
239+
while (iter.hasNext()) {
240+
String item = iter.next();
241+
if (item != null) {
242+
ListItemEntity itemEntity = new ListItemEntity();
243+
itemEntity.setPosition(iter.nextIndex());
244+
itemEntity.setItem(item);
245+
itemEntity.setList(entity);
246+
items.add(itemEntity);
247+
}
248+
}
249+
entity.setData(items);
246250
} else {
247251
entity.setData(new ArrayList<>());
248252
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
-- This migration updates existing list_item records based on DeltaBreed (Breeding Insight) specific fields.
3+
--
4+
-- These are the list types, the BJTS uses Java enums and stores ints in the database.
5+
-- 0: germplasm
6+
-- 1: markers
7+
-- 2: programs
8+
-- 3: trials
9+
-- 4: studies
10+
-- 5: observationUnits
11+
-- 6: observations
12+
-- 7: observationVariables
13+
-- 8: samples
14+
15+
DO
16+
$$
17+
BEGIN
18+
-- Update germplasm list items, the goal is to use the order defined by the listEntryNumbers.
19+
UPDATE
20+
list_item
21+
SET
22+
position = subquery.position
23+
FROM
24+
(
25+
SELECT
26+
-- Subtract 1 from row_number to get zero indexing.
27+
row_number() OVER (PARTITION BY li.list_id ORDER BY (g.additional_info->'listEntryNumbers'->>xr.external_reference_id)::int) - 1 AS position,
28+
li.id AS list_item_id
29+
FROM
30+
list_item li
31+
JOIN list l ON li.list_id = l.id
32+
JOIN list_external_references ler ON l.id = ler.list_entity_id
33+
JOIN external_reference xr ON xr.id = ler.external_references_id AND xr.external_reference_source = 'breedinginsight.org/lists'
34+
JOIN germplasm g ON li.item = g.germplasm_name
35+
WHERE
36+
l.list_type = 0 -- 0 is germplasm
37+
ORDER BY
38+
l.id
39+
) AS subquery
40+
WHERE
41+
list_item.id = subquery.list_item_id
42+
;
43+
44+
-- Update all non-germplasm list items. There is no existing order to preserve, assign sequential position values arbitrarily.
45+
UPDATE
46+
list_item
47+
SET
48+
position = subquery.position
49+
FROM
50+
(
51+
SELECT
52+
-- Subtract 1 from row_number to get zero indexing.
53+
row_number() OVER (PARTITION BY li.list_id) - 1 AS position,
54+
li.id AS list_item_id
55+
FROM
56+
list_item li
57+
JOIN list l ON li.list_id = l.id
58+
WHERE
59+
l.list_type != 0 -- 0 is germplasm, here we are addressing non-germplasm lists.
60+
ORDER BY
61+
l.id
62+
) AS subquery
63+
WHERE
64+
list_item.id = subquery.list_item_id
65+
;
66+
67+
END;
68+
$$;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- This migration adds a position field to list_item so that lists can be explicitly ordered.
2+
3+
-- Add position to list_item.
4+
ALTER TABLE list_item
5+
ADD COLUMN position INT NOT NULL DEFAULT 0;
6+
-- Add an index on the position column
7+
CREATE INDEX idx_list_item_position ON list_item(position);
8+
-- Add a composite index on list_id and position for better performance when querying items within a specific list
9+
CREATE INDEX idx_list_item_list_position ON list_item(list_id, position);

0 commit comments

Comments
 (0)