Search in sources :

Example 1 with RenderWith

use of jodd.madvoc.meta.RenderWith in project jodd by oblac.

the class ResultsManager method lookup.

/**
	 * Lookups for {@link jodd.madvoc.result.ActionResult action result handler}
	 * based on current {@link jodd.madvoc.ActionRequest action request} and action method
	 * result object. Lookup performs the following in given order:
	 * <ul>
	 *     <li>if result object is <code>null</code>, check if {@link jodd.madvoc.result.Result} is used</li>
	 *     <li>check result definition in <code>@Action</code> annotation</li>
	 *     <li>if result is not a <code>String</code>, check if it is annotated with {@link jodd.madvoc.meta.RenderWith} annotation</li>
	 *     <li>if result is not a <code>String</code>, find ActionResult for matching result object type</li>
	 *     <li>if action result still not found, call <code>toString</code> on result object and parse it</li>
	 * </ul>
	 */
public ActionResult lookup(ActionRequest actionRequest, Object resultObject) {
    ActionResult actionResult = null;
    // + special class: result
    if (resultObject == null) {
        Result result = actionRequest.getResult();
        if (result != null) {
            // read Result, if used; if not, values will be null
            Class<? extends ActionResult> actionResultClass = result.getActionResult();
            resultObject = result.getResultValue();
            if (resultObject == null) {
                resultObject = result.value();
            }
            if (actionResultClass != null) {
                actionResult = lookupAndRegisterIfMissing(actionResultClass);
            }
        }
    }
    if (actionResult == null) {
        // + still not found, read @Action value
        ActionConfig actionConfig = actionRequest.getActionConfig();
        Class<? extends ActionResult> actionResultClass = actionConfig.getActionResult();
        if (actionResultClass != null) {
            actionResult = lookupAndRegisterIfMissing(actionResultClass);
        }
    }
    if (actionResult == null && resultObject != null) {
        Class resultType = resultObject.getClass();
        if (resultType != String.class) {
            // + still not found, read @RenderWith value if exist
            RenderWith renderWith = resultObject.getClass().getAnnotation(RenderWith.class);
            if (renderWith != null) {
                actionResult = lookupAndRegisterIfMissing(renderWith.value());
            }
            if (actionResult == null) {
                // + still not found, lookup for type
                actionResult = typeResults.get(resultObject.getClass());
            }
        }
    }
    if (actionResult == null) {
        // + still not found, toString()
        ActionResult defaultActionResult = lookupAndRegisterIfMissing(madvocConfig.getDefaultActionResult());
        if (stringResults.isEmpty()) {
            // no string results registered, carry on with the defaults.
            actionResult = defaultActionResult;
        } else {
            String resultValue = resultObject != null ? resultObject.toString() : null;
            String resultName = null;
            // first check result value
            if (resultValue != null) {
                int columnIndex = resultValue.indexOf(':');
                if (columnIndex != -1) {
                    resultName = resultValue.substring(0, columnIndex);
                    resultValue = resultValue.substring(columnIndex + 1);
                }
            }
            if (resultName != null) {
                actionResult = stringResults.get(resultName);
            } else {
                actionResult = defaultActionResult;
            }
            if (actionResult.getResultName() != null) {
                // only when action result is string result
                try {
                    Class targetClass = actionResult.getResultValueType();
                    if (targetClass == null || targetClass == String.class) {
                        resultObject = resultValue;
                    } else {
                        resultObject = TypeConverterManager.convertType(resultValue, targetClass);
                    }
                } catch (Exception ex) {
                    resultObject = resultValue;
                }
            }
        }
    }
    // set action result object into action request!
    actionRequest.setActionResult(resultObject);
    return actionResult;
}
Also used : ActionConfig(jodd.madvoc.ActionConfig) RenderWith(jodd.madvoc.meta.RenderWith) ActionResult(jodd.madvoc.result.ActionResult) MadvocException(jodd.madvoc.MadvocException) ActionResult(jodd.madvoc.result.ActionResult) Result(jodd.madvoc.result.Result)

Aggregations

ActionConfig (jodd.madvoc.ActionConfig)1 MadvocException (jodd.madvoc.MadvocException)1 RenderWith (jodd.madvoc.meta.RenderWith)1 ActionResult (jodd.madvoc.result.ActionResult)1 Result (jodd.madvoc.result.Result)1