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

com.talk2object.plum.interaction.generic.GenericSingleMethodInteraction Maven / Gradle / Ivy

There is a newer version: 0.1.22
Show newest version
package com.talk2object.plum.interaction.generic;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;

import com.talk2object.plum.interaction.AbstractSingleMethodInteraction;
import com.talk2object.plum.interaction.Form;
import com.talk2object.plum.interaction.InteractionManager;
import com.talk2object.plum.interaction.rich.field.FieldType;
import com.talk2object.plum.interaction.rich.field.InteractiveField;
import com.talk2object.plum.interaction.view.annotation.parameter.helper.ParameterWrapperFactory;
import com.talk2object.plum.view._abstract.components.containers.window.ModalResult;
import com.talk2object.util.ReflectionUtils;

/**
 * 
 * @author jack
 *
 */
public class GenericSingleMethodInteraction extends
		AbstractSingleMethodInteraction {

	private static Logger logger = Logger
			.getLogger(GenericSingleMethodInteraction.class);

	public GenericSingleMethodInteraction(Object model) {
		super(model);

	}
	public GenericSingleMethodInteraction(Object model, String methodName) {
		super(model);

		method = ReflectionUtils.findMethod(model.getClass(), methodName);		
	}

	private List createInputs() {

		List inputs = new ArrayList();

		ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();

		String[] names = parameterNameDiscoverer.getParameterNames(method);

		Annotation[][] allAnnotations = method.getParameterAnnotations();
		int i = 0;
		for (Class cls : method.getParameterTypes()) {

			FieldType type = FieldType.valueOf(cls);

			// annotations for this parameter
			Annotation[] parameterAnnotations = allAnnotations[i];

			String name = names[i];
			InteractiveField input = type.createRichField(name, cls);

			ParameterWrapperFactory.getInstance().processAnnotations(input,
					parameterAnnotations);
			
			input.setValue(input.getInitValue());

			inputs.add(input);

			i++;
		}

		return inputs;
	}

	@Override
	public void start() {
		InteractionManager interactionManager = getInteractionManager();
		Form form = getInteractionManager().getCurrentForm();

		List inputs = createInputs();
		if (inputs.size() > 0) {

			if (form == null || !form.contains(inputs)) {
				form = interactionManager.scan(inputs);

				if (form.getModalResult() == ModalResult.CANCEL) {
					return;
				}
			}
		}
		// obtain input values
		Object[] parameters = new Object[inputs.size()];
		for (int i = 0; i < inputs.size(); i++) {
			parameters[i] = inputs.get(i).getValue();
		}

		Object result;
		try {
			result = method.invoke(model, parameters);

			if (method.getReturnType() != Void.class) {
				// String s = result == null ? "" : result.toString();
				if (result != null)
					interactionManager.inform(result);

			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);

			interactionManager.inform("error:" + e.getMessage());
		}

	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy