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

com.terapico.caf.SimpleInvocationResult Maven / Gradle / Ivy

package com.terapico.caf;

import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.terapico.uccaf.BaseUserContext;

public class SimpleInvocationResult implements InvocationResult {
	
	private InvocationContext invocationContext;
	private Object actualResult;
	public void setActualResult(Object actualResult) {
		this.actualResult=actualResult;
	}
	public void setInvocationContext(InvocationContext context) {
		this.invocationContext=context;
	}
	public InvocationContext getInvocationContext() {
		return invocationContext;
	}
	public Object getActualResult() {
		
		if(actualResult==null){
			return Boolean.TRUE;
		}
		
		return actualResult;
	}
	
	public Object getResultExpr() {
		return getObjectExpr(actualResult);
	}
	
	protected String getObjectExpr(Object target) {
		try {
			ObjectMapper mapper = new ObjectMapper();
			return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(target);
		} catch (Throwable e) {
			return e.getMessage();
		}
	}
	@Override
	public boolean isAssignedRenderingWay() {
		return getAssignedRenderingWay() != null;
	}
	
	private String getAssignedRenderingWay() {
		InvocationContext invokeCtx = getInvocationContext();
		if (invokeCtx == null){
			return null;
		}
		Object[] params = invokeCtx.getParameters();
		if (params == null || params.length < 1){
			return null;
		}
		if (!(params[0] instanceof BaseUserContext)){
			return null;
		}
		BaseUserContext usrCtx = (BaseUserContext) params[0];
		return usrCtx.getAssignedRederingWay();
	}
	@Override
	public Map getResponseHeader() {
		InvocationContext invokeCtx = getInvocationContext();
		if (invokeCtx == null){
			return null;
		}
		Object[] params = invokeCtx.getParameters();
		if (params == null || params.length < 1){
			return null;
		}
		if (!(params[0] instanceof BaseUserContext)){
			return null;
		}
		BaseUserContext usrCtx = (BaseUserContext) params[0];
		return usrCtx.getResponseHeaders();
	}
	@Override
	public boolean isRenderAsHtml() {
		return InvocationResult.RENDER_AS_HTML.equals(getAssignedRenderingWay());
	}
	@Override
	public boolean isRenderAsJavaScript() {
		return InvocationResult.RENDER_AS_JAVASCRIPT.equals(getAssignedRenderingWay());
	}
	@Override
	public boolean isRenderAsJSON() {
		return InvocationResult.RENDER_AS_JSON.equals(getAssignedRenderingWay());
	}
	@Override
	public String getEnvType() {
		InvocationContext invokeCtx = getInvocationContext();
		if (invokeCtx == null){
			return "product";
		}
		Object[] params = invokeCtx.getParameters();
		if (params == null || params.length < 1){
			return "product";
		}
		if (!(params[0] instanceof BaseUserContext)){
			return "product";
		}
		BaseUserContext usrCtx = (BaseUserContext) params[0];
		return usrCtx.isProductEnvironment()?"product":"test";
	}
	@Override
	public String getEnvName() {
		InvocationContext invokeCtx = getInvocationContext();
		if (invokeCtx == null){
			return "product";
		}
		Object[] params = invokeCtx.getParameters();
		if (params == null || params.length < 1){
			return "product";
		}
		if (!(params[0] instanceof BaseUserContext)){
			return "product";
		}
		BaseUserContext usrCtx = (BaseUserContext) params[0];
		return usrCtx.getEnvironmentName();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy