Skip to content

Commit a83cd37

Browse files
committed
Improve code behind MCC animal rename
1 parent dba9a65 commit a83cd37

1 file changed

Lines changed: 97 additions & 32 deletions

File tree

mcc/src/org/labkey/mcc/MccController.java

Lines changed: 97 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.labkey.mcc;
1818

19+
import jakarta.mail.Address;
20+
import jakarta.mail.Message;
1921
import org.apache.commons.lang3.StringUtils;
2022
import org.apache.logging.log4j.LogManager;
2123
import org.apache.logging.log4j.Logger;
@@ -87,8 +89,6 @@
8789
import org.springframework.validation.Errors;
8890
import org.springframework.web.servlet.ModelAndView;
8991

90-
import jakarta.mail.Address;
91-
import jakarta.mail.Message;
9292
import java.sql.SQLException;
9393
import java.util.ArrayList;
9494
import java.util.Arrays;
@@ -836,10 +836,50 @@ public Object execute(RenameIdsForm form, BindException errors) throws Exception
836836
// NOTE: include this value so it will get added to the audit trail. This is a loose way to connect changes made in this transaction
837837
final String batchId = "MCC.Rename." + new GUID();
838838
Set<String> messages = new HashSet<>();
839+
840+
// Perform MCC ID updates first, to ensure the dataset updates dont auto-create them:
841+
for (String oldId : oldToNew.keySet())
842+
{
843+
// Find the MCC ID of the new ID:
844+
String mccIdForOldId = new TableSelector(mccAliases, PageFlowUtil.set("externalAlias"), new SimpleFilter(FieldKey.fromString("subjectname"), oldId), null).getObject(String.class);
845+
String mccIdForNewId = new TableSelector(mccAliases, PageFlowUtil.set("externalAlias"), new SimpleFilter(FieldKey.fromString("subjectname"), oldToNew.get(oldId)), null).getObject(String.class);
846+
847+
if (mccIdForOldId == null)
848+
{
849+
// This should not really happen...
850+
_log.error("An MCC rename was performed where the original ID lacked an MCC alias: " + oldId);
851+
messages.add("Missing MCC alias: " + oldId);
852+
}
853+
854+
if (mccIdForNewId == null)
855+
{
856+
if (mccIdForOldId != null)
857+
{
858+
// Create record for the new ID pointing to the MCC ID of the original
859+
List<Map<String, Object>> toInsert = Arrays.asList(Map.of(
860+
"subjectname", mccIdForOldId,
861+
"_batchId_", batchId
862+
));
863+
BatchValidationException bve = new BatchValidationException();
864+
mccAliases.getUpdateService().insertRows(getUser(), getContainer(), toInsert, bve, null, null);
865+
if (bve.hasErrors())
866+
{
867+
throw bve;
868+
}
869+
}
870+
}
871+
else if (mccIdForOldId != null)
872+
{
873+
messages.add("Both IDs have existing MCC aliases, no changes were made: " + oldId + " / " + oldToNew.get(oldId));
874+
}
875+
}
876+
877+
// Update ID field of each dataset:
839878
for (Dataset ds : s.getDatasets())
840879
{
880+
Set<String> fieldList = "demographics".equalsIgnoreCase(ds.getName()) ? PageFlowUtil.set("Id", "lsid", "alternateIds") : PageFlowUtil.set("Id", "lsid");
841881
TableInfo ti = ds.getTableInfo(getUser());
842-
TableSelector ts = new TableSelector(ti, PageFlowUtil.set("Id", "lsid"), new SimpleFilter(FieldKey.fromString("Id"), oldToNew.keySet(), CompareType.IN), null);
882+
TableSelector ts = new TableSelector(ti, fieldList, new SimpleFilter(FieldKey.fromString("Id"), oldToNew.keySet(), CompareType.IN), null);
843883
if (!ts.exists())
844884
{
845885
continue;
@@ -858,7 +898,22 @@ public Object execute(RenameIdsForm form, BindException errors) throws Exception
858898
}
859899
}
860900

861-
toUpdate.add(Map.of("lsid", rs.getString(FieldKey.fromString("lsid")), "Id", oldToNew.get(rs.getString(FieldKey.fromString("Id"))), "_batchId_", batchId));
901+
Map<String, Object> rowMap = Map.of(
902+
"lsid", rs.getString(FieldKey.fromString("lsid")),
903+
"Id", oldToNew.get(rs.getString(FieldKey.fromString("Id"))),
904+
"_batchId_", batchId
905+
);
906+
907+
if ("demographics".equalsIgnoreCase(ds.getName()))
908+
{
909+
String alternateIds = StringUtils.trimToNull(rs.getString(FieldKey.fromString("alternateIds")));
910+
alternateIds = alternateIds == null ? rs.getString(FieldKey.fromString("Id")) : alternateIds + "," + rs.getString(FieldKey.fromString("Id"));
911+
912+
rowMap = new HashMap<>(rowMap);
913+
rowMap.put("alternateIds", alternateIds);
914+
}
915+
916+
toUpdate.add(rowMap);
862917
oldKeys.add(Map.of("lsid", rs.getString(FieldKey.fromString("lsid"))));
863918
idsUpdated.add(rs.getString(FieldKey.fromString("Id")));
864919
totalRecordsUpdated.getAndIncrement();
@@ -879,41 +934,51 @@ public Object execute(RenameIdsForm form, BindException errors) throws Exception
879934
}
880935
}
881936

882-
for (String oldId : oldToNew.keySet())
937+
// Now dam/sire:
938+
TableInfo ti = s.getDatasetByName("demographics").getTableInfo(getUser());
939+
TableSelector ts = new TableSelector(ti, PageFlowUtil.set("lsid", "Id", "dam", "sire"), new SimpleFilter(new SimpleFilter.OrClause(
940+
new SimpleFilter.InClause(FieldKey.fromString("dam"), oldToNew.keySet()),
941+
new SimpleFilter.InClause(FieldKey.fromString("sire"), oldToNew.keySet())
942+
)), null);
943+
944+
if (!ts.exists())
883945
{
884-
// Find the MCC ID of the new ID:
885-
String mccIdForOldId = new TableSelector(mccAliases, PageFlowUtil.set("externalAlias"), new SimpleFilter(FieldKey.fromString("subjectname"), oldId), null).getObject(String.class);
886-
String mccIdForNewId = new TableSelector(mccAliases, PageFlowUtil.set("externalAlias"), new SimpleFilter(FieldKey.fromString("subjectname"), oldToNew.get(oldId)), null).getObject(String.class);
946+
throw new IllegalStateException("Demographics table not found");
947+
}
887948

888-
if (mccIdForOldId == null)
949+
List<Map<String, Object>> toUpdate = new ArrayList<>();
950+
List<Map<String, Object>> oldKeys = new ArrayList<>();
951+
ts.forEachResults(rs -> {
952+
for (String oldId : oldToNew.keySet())
889953
{
890-
// This should not really happen...
891-
_log.error("An MCC rename was performed where the original ID lacked an MCC alias: " + oldId);
892-
messages.add("Missing MCC alias: " + oldId);
893-
}
954+
Map<String, Object> rowMap = new HashMap<>(Map.of(
955+
"lsid", rs.getString(FieldKey.fromString("lsid")),
956+
"Id", rs.getString(FieldKey.fromString("Id")),
957+
"_batchId_", batchId
958+
));
959+
960+
boolean hasChanges = false;
961+
if (oldId.equalsIgnoreCase(rs.getString(FieldKey.fromString("dam"))))
962+
{
963+
rowMap.put("dam", oldToNew.get(oldId));
964+
hasChanges = true;
965+
}
894966

895-
if (mccIdForNewId == null)
896-
{
897-
if (mccIdForOldId != null)
967+
if (oldId.equalsIgnoreCase(rs.getString(FieldKey.fromString("sire"))))
898968
{
899-
// Create record for the new ID pointing to the MCC ID of the original
900-
List<Map<String, Object>> toInsert = Arrays.asList(Map.of(
901-
"subjectname", mccIdForOldId,
902-
"_batchId_", batchId
903-
));
904-
BatchValidationException bve = new BatchValidationException();
905-
mccAliases.getUpdateService().insertRows(getUser(), getContainer(), toInsert, bve, null, null);
906-
if (bve.hasErrors())
907-
{
908-
throw bve;
909-
}
969+
rowMap.put("sire", oldToNew.get(oldId));
970+
hasChanges = true;
971+
}
972+
973+
if (hasChanges)
974+
{
975+
toUpdate.add(rowMap);
976+
oldKeys.add(Map.of("lsid", rs.getString(FieldKey.fromString("lsid"))));
977+
idsUpdated.add(rs.getString(FieldKey.fromString("Id")));
978+
totalRecordsUpdated.getAndIncrement();
910979
}
911980
}
912-
else if (mccIdForOldId != null)
913-
{
914-
messages.add("Both IDs have existing MCC aliases, no changes were made: " + oldId + " / " + oldToNew.get(oldId));
915-
}
916-
}
981+
});
917982

918983
transaction.commit();
919984

0 commit comments

Comments
 (0)