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

com.talk2object.plum.interaction.generic.GenericQueryInteraction 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;

public class GenericQueryInteraction extends
		AbstractSingleMethodInteraction {

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

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

	}
	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);

			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();
				
				
				//interactionManager.inform(s);
				
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);

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

	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy