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

org.oddjob.arooa.standard.MapItemRuntime Maven / Gradle / Ivy

There is a newer version: 1.6.0
Show newest version
/**
 * 
 */
package org.oddjob.arooa.standard;

import java.util.Map;
import java.util.Set;

import org.oddjob.arooa.ArooaException;
import org.oddjob.arooa.convert.ArooaConversionException;
import org.oddjob.arooa.convert.ArooaConverter;
import org.oddjob.arooa.parsing.ArooaContext;
import org.oddjob.arooa.reflect.ArooaPropertyException;
import org.oddjob.arooa.runtime.RuntimeConfiguration;

class MapItemRuntime extends InstanceRuntime {

	private final String key;
	
	MapItemRuntime(
			String key, 
			InstanceConfiguration item,
			ArooaContext context) {
		super(item, context);
		this.key = key;
	}
	
	ParentPropertySetter getParentPropertySetter() {
		return new ParentPropertySetter() {
			public void parentSetProperty(Object value) 
			throws ArooaPropertyException {
				RuntimeConfiguration parentRuntime = getParentContext().getRuntime();

					if (key == null) {
						if (value == null) {
							// probably destroy... this needs reworking.
							return;
						}
						
						// property must be map
						
						ArooaConverter converter = getContext().getSession(
								).getTools().getArooaConverter();
						
						try {
							value = converter.convert(value, Map.class);
						} catch (ArooaConversionException e) {
							throw new ArooaException("Key must be provided for mapped property.");
						}
						
						@SuppressWarnings("unchecked")
						Map map = (Map) value;
						for (Map.Entry mapEntry : (Set>) map.entrySet() ) {
							parentRuntime.setMappedProperty(
									null, 
									(String) mapEntry.getKey(), 
									mapEntry.getValue());
						}
					}
					else {
						parentRuntime.setMappedProperty(
								null, 
								key, 
								value);
					}		
			
			}
		};
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy