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

net.officefloor.web.value.retrieve.PropertyValueRetrieverImpl Maven / Gradle / Ivy

/*
 * OfficeFloor - http://www.officefloor.net
 * Copyright (C) 2005-2018 Daniel Sagenschneider
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 */
package net.officefloor.web.value.retrieve;

import java.lang.reflect.Method;
import java.util.Map;

import net.officefloor.server.http.HttpException;

/**
 * {@link ValueRetriever} implementation.
 * 
 * @author Daniel Sagenschneider
 */
public class PropertyValueRetrieverImpl implements ValueRetriever {

	/**
	 * {@link PropertyMetaData}.
	 */
	private final PropertyMetaData metaData;

	/**
	 * Delegate {@link ValueRetriever}.
	 */
	private final ValueRetriever delegate;

	/**
	 * Initiate.
	 * 
	 * @param metaData
	 *            {@link PropertyMetaData}.
	 * @param isCaseInsensitive
	 *            Indicates if case insensitive.
	 * @param valueRetrieverByMetaData
	 *            {@link ValueRetriever} by its {@link PropertyMetaData}.
	 */
	PropertyValueRetrieverImpl(PropertyMetaData metaData, boolean isCaseInsensitive,
			Map> valueRetrieverByMetaData) {
		this.metaData = metaData;

		// Register this property before continuing further.
		// Must be registered before recursively creating further retrievers.
		valueRetrieverByMetaData.put(metaData, this);

		// Create further retrievers in the property meta-data tree
		this.delegate = new RootValueRetrieverImpl(this.metaData.getProperties(), isCaseInsensitive,
				valueRetrieverByMetaData);
	}

	/*
	 * ===================== ValueRetreiver ======================
	 */

	@Override
	public Class getValueType(String name) throws HttpException {
		// Delegate to obtain value type
		return this.delegate.getValueType(name);
	}

	@Override
	public  A getValueAnnotation(String name, Class annotationType) throws HttpException {
		// Delegate to obtain value annotation
		return this.delegate.getValueAnnotation(name, annotationType);
	}

	@Override
	public Object retrieveValue(T object, String name) throws HttpException {

		// Ensure have value
		if (object == null) {
			return null; // no value
		}

		try {
			// Obtain the method
			Method method = this.metaData.getMethod(object.getClass());

			// Retrieve the property value
			Object value = ValueRetrieverSource.retrieveValue(object, method);

			// Determine if further property navigation
			if (name.length() > 0) {
				// Further navigation
				return this.delegate.retrieveValue(value, name);
			} else {
				// Navigated to the value, so return the value
				return value;
			}

		} catch (Exception ex) {
			throw new RetrieveValueException(ex);
		}
	}

}