diff --git a/src/tethys/calibration/CalibrationHandler.java b/src/tethys/calibration/CalibrationHandler.java index f3ce8bfe..f5f1acb0 100644 --- a/src/tethys/calibration/CalibrationHandler.java +++ b/src/tethys/calibration/CalibrationHandler.java @@ -45,6 +45,7 @@ import tethys.TethysTimeFuncs; import tethys.calibration.swing.CalibrationsExportWizard; import tethys.dbxml.DBXMLConnect; import tethys.dbxml.TethysException; +import tethys.niluswraps.NilusChecker; import tethys.niluswraps.NilusSettingsWrapper; import tethys.niluswraps.NilusUnpacker; import tethys.pamdata.AutoTethysProvider; @@ -197,9 +198,20 @@ public class CalibrationHandler implements TethysStateObserver { calDoc.setMetadataInfo(sampleCal.getMetadataInfo()); calDoc.setProcess(sampleCal.getProcess()); calDoc.setQualityAssurance(sampleCal.getQualityAssurance()); - calDoc.setResponsibleParty(sampleCal.getResponsibleParty()); + if (NilusChecker.isEmpty(sampleCal.getResponsibleParty()) == false) { + calDoc.setResponsibleParty(sampleCal.getResponsibleParty()); + } calDoc.setTimeStamp(sampleCal.getTimeStamp()); } + // check the contact info in the metadata. + // can't so because it's required. +// MetadataInfo metaData = calDoc.getMetadataInfo(); +// if (metaData != null) { +// if (NilusChecker.isEmpty(metaData.getContact())) { +// metaData.setContact(null); +// } +// } + addParameterDetails(calDoc, i); @@ -408,6 +420,10 @@ public class CalibrationHandler implements TethysStateObserver { hz.add(Double.valueOf(0)); db.add(Double.valueOf(hSens+preampGain)); + if (NilusChecker.isEmpty(calibration.getResponsibleParty())) { + calibration.setResponsibleParty(null); + } + MetadataInfo metaInf = calibration.getMetadataInfo(); if (metaInf == null) { metaInf = new MetadataInfo(); @@ -420,6 +436,12 @@ public class CalibrationHandler implements TethysStateObserver { contact = new ResponsibleParty(); metaInf.setContact(contact); } + if (NilusChecker.isEmpty(metaInf.getContact())) { + metaInf.setContact(null); + } + if (NilusChecker.isEmpty(metaInf)) { + calibration.setMetadataInfo(null); + } contact.setIndividualName("Unknown"); contact.setOrganizationName("unknown"); diff --git a/src/tethys/niluswraps/NilusChecker.java b/src/tethys/niluswraps/NilusChecker.java new file mode 100644 index 00000000..cb2c6cab --- /dev/null +++ b/src/tethys/niluswraps/NilusChecker.java @@ -0,0 +1,75 @@ +package tethys.niluswraps; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.List; + +import nilus.ResponsibleParty; + +/** + * A few static checks of some nilus classes to see if it's + * worth writing them or not. + * @author dg50 + * + */ +public class NilusChecker { + + public static boolean isEmpty(Object nilusObject) { + boolean empty = true; + if (nilusObject == null) { + return true; + } + // and check all getters + Class nilusClass = nilusObject.getClass(); + Method[] methods = nilusClass.getDeclaredMethods(); + // searching for getters. + int nGet = 0; + for (int i = 0; i < methods.length; i++) { + Method method = methods[i]; + if (method.getName().startsWith("get") && method.getParameterCount() == 0) { + nGet ++; + try { + Object got = method.invoke(nilusObject, null); + if (got != null) { + if (got instanceof String) { + if (isEmptyString((String) got) == false) { + empty = false; + } + } + else if (got instanceof List) { + if (isEmptyList((List) got) == false) { + empty = false; + } + } + else if (isEmpty(got) == false) {// it's some other class type, so recurecively ask back here. + empty = false; + } + + } + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + return false; // better save than sorry if we don't understand. + } + } + } + if (nGet == 0) { + // there weren't any understandable getters, so assume not empty. May be some other primitive type. + empty = false; + } + + return empty; + } + + private static boolean isEmptyList(List got) { + if (got == null) { + return true; + } + return got.size() == 0; + } + + public static boolean isEmptyString(String string) { + if (string == null || string.length() == 0) { + return true; + } + return false; + } +}