Removing empty fields

Start of implementation of code that can remove empty fields from
objects before they are written.
This commit is contained in:
Douglas Gillespie 2024-01-19 17:29:12 +00:00
parent 65300d719a
commit e0392de9c7
2 changed files with 98 additions and 1 deletions

View File

@ -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());
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");

View File

@ -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<? extends Object> 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;
}
}