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

com.github.exerrk.engine.SimpleJasperReportsContext Maven / Gradle / Ivy

The newest version!
/*
 * JasperReports - Free Java Reporting Library.
 * Copyright (C) 2001 - 2016 TIBCO Software Inc. All rights reserved.
 * http://www.jaspersoft.com
 *
 * Unless you have purchased a commercial license agreement from Jaspersoft,
 * the following license terms apply:
 *
 * This program is part of JasperReports.
 *
 * JasperReports is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JasperReports is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with JasperReports. If not, see .
 */
package com.github.exerrk.engine;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @author Teodor Danciu ([email protected])
 */
public class SimpleJasperReportsContext implements JasperReportsContext
{
	/**
	 *
	 */
	private JasperReportsContext parent;
	
	private Map values = new ConcurrentHashMap(16, .75f, 1);// assume low update concurrency
	private Map properties;
	private Map, List> extensionsMap;

	/**
	 * Constructs a SimpleJasperReportsContext instance that has the DefaultJasperReportsContext.getInstance() as parent.
	 */
	public SimpleJasperReportsContext()
	{
		this(DefaultJasperReportsContext.getInstance());
	}

	/**
	 *
	 */
	public SimpleJasperReportsContext(JasperReportsContext parent)
	{
		this.parent = parent;
	}

	/**
	 *
	 */
	public void setParent(JasperReportsContext parent)
	{
		this.parent = parent;
	}

	@Override
	public Object getValue(String key)
	{
		if (values.containsKey(key))
		{
			return values.get(key);
		}
		if (parent != null)
		{
			return parent.getValue(key);
		}
		return null;
	}

	@Override
	public Object getOwnValue(String key)
	{
		return values.get(key);
	}

	@Override
	public void setValue(String key, Object value)
	{
		values.put(key, value);
	}

	/**
	 *
	 */
	public void removeValue(String key)
	{
		values.remove(key);
	}
	
	/**
	 * Returns a list of extension objects for a specific extension type.
	 * 
	 * @param extensionType the extension type
	 * @param  generic extension type
	 * @return a list of extension objects
	 */
	@Override
	public  List getExtensions(Class extensionType)
	{
		if (extensionsMap == null || !extensionsMap.containsKey(extensionType))
		{
			if (parent == null)
			{
				return null;
			}
			else
			{
				return parent.getExtensions(extensionType);
			}
		}
		else
		{
			@SuppressWarnings("unchecked")
			List extensionsList = (List)extensionsMap.get(extensionType);
			if (parent == null)
			{
				return extensionsList;
			}
			else
			{
				List parentExtensions = parent.getExtensions(extensionType);
				if (extensionsList == null || extensionsList.isEmpty())
				{
					if (parentExtensions == null || parentExtensions.isEmpty())
					{
						return null;
					}
					else
					{
						return parentExtensions;
					}
				}
				else
				{
					if (parentExtensions == null || parentExtensions.isEmpty())
					{
						return extensionsList;
					}
					else
					{
						List returnedList = new ArrayList();
						returnedList.addAll(extensionsList);
						returnedList.addAll(parentExtensions);
						return returnedList;
					}
				}
			}
		}
	}
	
	/**
	 *
	 */
	public  void setExtensions(Class extensionType, List extensions)
	{
		if (extensionsMap == null)
		{
			extensionsMap = new HashMap, List>();
		}
		extensionsMap.put(extensionType, extensions);
	}
	
	/**
	 *
	 */
	public void setExtensions(Map, List> extensions)
	{
		extensionsMap = extensions;
	}
	
	/**
	 * Returns the value of the property.
	 * 
	 * @param key the key
	 * @return the property value
	 */
	@Override
	public String getProperty(String key)
	{
		if (properties != null && properties.containsKey(key))
		{
			return properties.get(key);
		}
		else
		{
			if (parent == null)
			{
				return null;
			}
			else
			{
				return parent.getProperty(key);
			}
		}
	}
	
	@Override
	public void setProperty(String key, String value)
	{
		if (properties == null)
		{
			properties = new HashMap();
		}
		
		properties.put(key, value);
	}
	
	@Override
	public void removeProperty(String key)
	{
		if (properties != null)
		{
			properties.remove(key);
		}
	}
	
	@Override
	public Map getProperties()
	{
		if (properties == null)
		{
			if (parent == null)
			{
				return null;
			}
			else
			{
				return parent.getProperties();
			}
		}
		else
		{
			if (parent == null)
			{
				return properties;
			}
			else
			{
				Map parentProperties = parent.getProperties();
				if (properties == null || properties.isEmpty())
				{
					if (parentProperties == null || parentProperties.isEmpty())
					{
						return null;
					}
					else
					{
						return parentProperties;
					}
				}
				else
				{
					if (parentProperties == null || parentProperties.isEmpty())
					{
						return properties;
					}
					else
					{
						Map returnedMap = new HashMap();
						returnedMap.putAll(parentProperties);
						returnedMap.putAll(properties);
						return returnedMap;
					}
				}
			}
		}
	}

	public Map getPropertiesMap()
	{
		return properties;
	}

	public void setPropertiesMap(Map propertiesMap)
	{
		this.properties = propertiesMap;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy