All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gov.nasa.pds.registry.common.meta.MetadataNormalizer Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package gov.nasa.pds.registry.common.meta;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import gov.nasa.pds.registry.common.util.FieldMapList;
import gov.nasa.pds.registry.common.util.date.PdsDateConverter;


/**
 * Normalizes (converts) date and boolean values into formats acceptable 
 * by Elasticsearch (registry). 
 * @author karpenko
 */
public class MetadataNormalizer
{
    private PdsDateConverter dateConverter;
    private FieldNameCache fieldNameCache;
    
    /**
     * Constructor
     */
    public MetadataNormalizer(FieldNameCache cache)
    {
        dateConverter = new PdsDateConverter(true);
        this.fieldNameCache = cache;
    }

    
    /**
     * Normalize field values
     * @param fields Metadata extracted from PDS4 label
     */
    public void normalizeValues(FieldMapList fields) throws Exception
    {
        for(String key: fields.getNames())
        {
            // Convert date fields
            if(fieldNameCache.isDateField(key))
            {
                Collection oldValues = fields.getValues(key);
                if(oldValues == null) continue; 
                
                List newValues = convertDateValues(key, oldValues);
                fields.setValues(key, newValues);
            }
            // Convert boolean fields
            else if(fieldNameCache.isBooleanField(key))
            {
                Collection oldValues = fields.getValues(key);
                if(oldValues == null) continue; 
                
                List newValues = convertBooleanValues(key, oldValues);
                fields.setValues(key, newValues);
            }
        }
    }

    
    private List convertDateValues(String key, Collection oldValues) throws Exception
    {
        List newValues = new ArrayList<>();
        
        for(String oldValue: oldValues)
        {
            try
            {
                String newValue = dateConverter.toIsoInstantString(key, oldValue);
                newValues.add(newValue);
            }
            catch(Exception ex)
            {
                throw new Exception("Could not convert date value. Field = " + key + ", value = " + oldValue); 
            }
        }
        
        return newValues;
    }


    private List convertBooleanValues(String key, Collection oldValues) throws Exception
    {
        List newValues = new ArrayList<>();
        
        for(String oldValue: oldValues)
        {
            String tmp = oldValue.toLowerCase();
            
            if("true".equals(tmp) || "1".equals(tmp))
            {
                newValues.add("true");
            }
            else if("false".equals(tmp) || "0".equals(tmp))
            {
                newValues.add("false");
            }
            else
            {
                throw new Exception("Could not convert boolean value. Field = " + key + ", value = " + oldValue); 
            }
        }
        
        return newValues;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy