package eu.dnetlib.enabling.aas.utils;

import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.exolab.castor.util.LocalConfiguration;


/**
 * Overwrites castor properties.
 * @author mhorst
 *
 */
public class CastorPropertiesSetter {

	protected final Logger log = Logger.getLogger(this.getClass());
	
	/**
	 * Properties to be set at initialization phase.
	 */
	private Map<String, String> properties;

	/**
	 * Flag indicating null values should be omitted.
	 */
	private boolean omitNullValues = true;
	
	/**
	 * Spring init method.
	 */
	public void init() {
		if (properties!=null && properties.size()>0) {
			Properties props = LocalConfiguration.getInstance().getProperties();
			for (String currentKey : properties.keySet()) {
				if (properties.get(currentKey)==null && omitNullValues) {
					log.warn("omitting null value for property: " + currentKey);
				} else {
					Object prev = props.setProperty(currentKey, 
							properties.get(currentKey));
					log.warn("castor property value: " + 
							properties.get(currentKey) + ", set for key: " +
							currentKey + "; value being overwritten: " + prev);
				}
			}
		}
	}
	
	/**
	 * Sets properties to be set.
	 * @param properties
	 */
	public void setProperties(Map<String, String> properties) {
		this.properties = properties;
	}

	/**
	 * Sets omitNullValues flag.
	 * @param omitNullValues
	 */
	public void setOmitNullValues(boolean omitNullValues) {
		this.omitNullValues = omitNullValues;
	}
	
}
