From 528ae84e71d6eb73495842bfe611f4e7e71cda71 Mon Sep 17 00:00:00 2001 From: Russ Poetker Date: Thu, 5 Oct 2023 08:38:30 -0400 Subject: [PATCH] Clean difference logging --- .../support/grant/data/DifferenceLogger.java | 90 +++++++++++++++++++ .../grant/data/JhuPassInitUpdater.java | 67 ++------------ .../src/main/resources/logback.xml | 2 +- 3 files changed, 100 insertions(+), 59 deletions(-) create mode 100644 pass-grant-loader/pass-grant-data/src/main/java/org/eclipse/pass/support/grant/data/DifferenceLogger.java diff --git a/pass-grant-loader/pass-grant-data/src/main/java/org/eclipse/pass/support/grant/data/DifferenceLogger.java b/pass-grant-loader/pass-grant-data/src/main/java/org/eclipse/pass/support/grant/data/DifferenceLogger.java new file mode 100644 index 00000000..4e9f368e --- /dev/null +++ b/pass-grant-loader/pass-grant-data/src/main/java/org/eclipse/pass/support/grant/data/DifferenceLogger.java @@ -0,0 +1,90 @@ +/* + * Copyright 2023 Johns Hopkins University + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.eclipse.pass.support.grant.data; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; + +import org.eclipse.pass.support.client.model.PassEntity; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Russ Poetker (rpoetke1@jh.edu) + */ +public class DifferenceLogger { + + private static final Logger LOG = LoggerFactory.getLogger(DifferenceLogger.class); + + /** + * Logs the difference in attribute values between the source and target PassEntities. + * + * @param source the existing PassEntity state + * @param target the updated PassEntity state + */ + public void log(PassEntity source, PassEntity target) { + LOG.info("Updated " + source.getClass().getSimpleName() + " with ID: " + source.getId()); + if (LOG.isInfoEnabled()) { + List diffs = getDifference(source, target, source.getId()); + diffs.forEach(LOG::info); + } + } + + @SuppressWarnings("unchecked") + private List getDifference(Object s1, Object s2, String grantId) { + List values = new ArrayList<>(); + try { + for (Field field : s1.getClass().getDeclaredFields()) { + field.setAccessible(true); + Object value1 = field.get(s1); + Object value2 = field.get(s2); + if (value1 instanceof PassEntity || value2 instanceof PassEntity) { + getPassEntityDiffs((PassEntity) value1, (PassEntity) value2, values, field); + } else if (value1 instanceof List || value2 instanceof List) { + getListDiffs((List) value1, (List) value2, values, field); + } else if (!Objects.equals(value1, value2)) { + values.add(field.getName() + ": " + value1 + " -> " + value2); + } + } + } catch (IllegalAccessException e) { + LOG.error("Error printing diffs Grant ID: " + grantId, e); + } + return values; + } + + private void getPassEntityDiffs(PassEntity value1, PassEntity value2, List values, Field field) { + String funder1Id = Objects.nonNull(value1) ? value1.getId() : null; + String funder2Id = Objects.nonNull(value2) ? value2.getId() : null; + if (!Objects.equals(funder1Id, funder2Id)) { + values.add(field.getName() + " IDs: " + funder1Id + " -> " + funder2Id); + } + } + + private void getListDiffs(List value1, List value2, List values, Field field) { + Set coPiIds1 = Objects.nonNull(value1) ? + value1.stream().map(PassEntity::getId).collect(Collectors.toSet()) : null; + Set coPiIds2 = Objects.nonNull(value2) ? + value2.stream().map(PassEntity::getId).collect(Collectors.toSet()) : null; + if (!Objects.equals(coPiIds1, coPiIds2)) { + values.add(field.getName() + " IDs: " + coPiIds1 + " -> " + coPiIds2); + } + } + +} diff --git a/pass-grant-loader/pass-grant-data/src/main/java/org/eclipse/pass/support/grant/data/JhuPassInitUpdater.java b/pass-grant-loader/pass-grant-data/src/main/java/org/eclipse/pass/support/grant/data/JhuPassInitUpdater.java index 3954ee05..6cefe8ca 100644 --- a/pass-grant-loader/pass-grant-data/src/main/java/org/eclipse/pass/support/grant/data/JhuPassInitUpdater.java +++ b/pass-grant-loader/pass-grant-data/src/main/java/org/eclipse/pass/support/grant/data/JhuPassInitUpdater.java @@ -15,26 +15,24 @@ */ package org.eclipse.pass.support.grant.data; -import java.lang.reflect.Field; -import java.util.ArrayList; import java.util.HashSet; -import java.util.List; -import java.util.Objects; -import java.util.Set; -import java.util.stream.Collectors; -import org.eclipse.pass.support.client.model.Funder; import org.eclipse.pass.support.client.model.Grant; import org.eclipse.pass.support.client.model.User; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * The Init Grant Pass Updater for data sourced from Jhu Coeus. */ public class JhuPassInitUpdater extends JhuPassUpdater { - private static final Logger LOG = LoggerFactory.getLogger(JhuPassInitUpdater.class); + private final DifferenceLogger differenceLogger; + + /** + * Default constructor. + */ + public JhuPassInitUpdater() { + differenceLogger = new DifferenceLogger(); + } @Override public Grant updateGrantIfNeeded(Grant system, Grant stored) { @@ -134,7 +132,7 @@ private boolean grantNeedsUpdate(Grant system, Grant stored) { * @return the Grant object which represents the Pass object, with any new information from COEUS merged in */ private Grant updateGrant(Grant system, Grant stored) { - logDifferences(system, stored); + differenceLogger.log(system, stored); stored.setAwardNumber(system.getAwardNumber()); stored.setAwardStatus(system.getAwardStatus()); stored.setLocalKey(system.getLocalKey()); @@ -151,51 +149,4 @@ private Grant updateGrant(Grant system, Grant stored) { return stored; } - private void logDifferences(Grant system, Grant stored) { - LOG.info("Updated Grant with ID: " + stored.getId()); - if (LOG.isInfoEnabled()) { - List diffs = getDifference(stored, system, stored.getId()); - diffs.forEach(LOG::info); - } - } - - private List getDifference(Object s1, Object s2, String grantId) { - List values = new ArrayList<>(); - try { - for (Field field : s1.getClass().getDeclaredFields()) { - field.setAccessible(true); - Object value1 = field.get(s1); - Object value2 = field.get(s2); - if (value1 instanceof Funder || value2 instanceof Funder) { - getFunderDiffs((Funder) value1, (Funder) value2, values, field); - } else if (field.getName().equals("coPis")) { - getCoPisDiffs((List) value1, (List) value2, values); - } else if (!Objects.equals(value1, value2)) { - values.add(field.getName() + ": " + value1 + " -> " + value2); - } - } - } catch (IllegalAccessException e) { - LOG.error("Error printing diffs Grant ID: " + grantId, e); - } - return values; - } - - private void getFunderDiffs(Funder value1, Funder value2, List values, Field field) { - String funder1Id = Objects.nonNull(value1) ? value1.getId() : null; - String funder2Id = Objects.nonNull(value2) ? value2.getId() : null; - if (!Objects.equals(funder1Id, funder2Id)) { - values.add(field.getName() + " Funder IDs: " + funder1Id + " -> " + funder2Id); - } - } - - private void getCoPisDiffs(List value1, List value2, List values) { - Set coPiIds1 = Objects.nonNull(value1) ? - value1.stream().map(User::getId).collect(Collectors.toSet()) : null; - Set coPiIds2 = Objects.nonNull(value2) ? - value2.stream().map(User::getId).collect(Collectors.toSet()) : null; - if (!Objects.equals(coPiIds1, coPiIds2)) { - values.add("coPis User IDs: " + coPiIds1 + " -> " + coPiIds2); - } - } - } diff --git a/pass-grant-loader/pass-grant-data/src/main/resources/logback.xml b/pass-grant-loader/pass-grant-data/src/main/resources/logback.xml index cd95a418..34d04098 100644 --- a/pass-grant-loader/pass-grant-data/src/main/resources/logback.xml +++ b/pass-grant-loader/pass-grant-data/src/main/resources/logback.xml @@ -26,7 +26,7 @@ - + \ No newline at end of file