Search in sources :

Example 6 with Action

use of org.webpieces.router.api.controller.actions.Action in project webpieces by deanhiller.

the class CrudUserController method userAddEdit.

public Action userAddEdit(Integer id) {
    if (id == null) {
        return Actions.renderThis("entity", new UserDbo(), "levels", EducationEnum.values(), "roles", RoleEnum.values(), "selectedRoles", null, "password", null);
    }
    UserDbo user = UserDbo.findWithJoin(Em.get(), id);
    List<UserRole> roles = user.getRoles();
    List<RoleEnum> selectedRoles = roles.stream().map(r -> r.getRole()).collect(Collectors.toList());
    return Actions.renderThis("entity", user, "levels", EducationEnum.values(), "roles", RoleEnum.values(), "selectedRoles", selectedRoles, "password", null);
}
Also used : Action(org.webpieces.router.api.controller.actions.Action) UserDbo(webpiecesxxxxxpackage.db.UserDbo) Size(javax.validation.constraints.Size) NotBlank(javax.validation.constraints.NotBlank) Logger(org.slf4j.Logger) UseQuery(org.webpieces.plugin.hibernate.UseQuery) Render(org.webpieces.router.api.controller.actions.Render) FlashAndRedirect(org.webpieces.router.api.controller.actions.FlashAndRedirect) GET_EDIT_USER_FORM(webpiecesxxxxxpackage.web.secure.crud.CrudUserRouteId.GET_EDIT_USER_FORM) LoggerFactory(org.slf4j.LoggerFactory) EntityManager(javax.persistence.EntityManager) Singleton(javax.inject.Singleton) UserRole(webpiecesxxxxxpackage.db.UserRole) Collectors(java.util.stream.Collectors) EducationEnum(webpiecesxxxxxpackage.db.EducationEnum) List(java.util.List) Query(javax.persistence.Query) GET_ADD_USER_FORM(webpiecesxxxxxpackage.web.secure.crud.CrudUserRouteId.GET_ADD_USER_FORM) Em(org.webpieces.plugin.hibernate.Em) Redirect(org.webpieces.router.api.controller.actions.Redirect) Current(org.webpieces.ctx.api.Current) Actions(org.webpieces.router.api.controller.actions.Actions) RoleEnum(webpiecesxxxxxpackage.db.RoleEnum) UserDbo(webpiecesxxxxxpackage.db.UserDbo) UserRole(webpiecesxxxxxpackage.db.UserRole) RoleEnum(webpiecesxxxxxpackage.db.RoleEnum)

Example 7 with Action

use of org.webpieces.router.api.controller.actions.Action in project webpieces by deanhiller.

the class ControllerLoader method htmlPreconditionCheck.

private void htmlPreconditionCheck(Object meta, Method controllerMethod, boolean isPostOnly) {
    if (isPostOnly) {
        Class<?> clazz = controllerMethod.getReturnType();
        if (CompletableFuture.class.isAssignableFrom(clazz)) {
            Type genericReturnType = controllerMethod.getGenericReturnType();
            ParameterizedType t = (ParameterizedType) genericReturnType;
            Type type2 = t.getActualTypeArguments()[0];
            if (!(type2 instanceof Class))
                throw new IllegalArgumentException("Since this route=" + meta + " is for POST, the method MUST return a type 'Redirect' or 'CompletableFuture<Redirect>' for this method=" + controllerMethod);
            @SuppressWarnings("rawtypes") Class<?> type = (Class) type2;
            if (!Redirect.class.isAssignableFrom(type))
                throw new IllegalArgumentException("Since this route=" + meta + " is for POST, the method MUST return a type 'Redirect' or 'XFuture<Redirect>' not 'XFuture<" + type.getSimpleName() + ">'for this method=" + controllerMethod);
        } else if (!Redirect.class.isAssignableFrom(clazz))
            throw new IllegalArgumentException("Since this route=" + meta + " is for POST, the method MUST return a type 'Redirect' or 'XFuture<Redirect>' not '" + clazz.getSimpleName() + "' for this method=" + controllerMethod);
    } else {
        Class<?> clazz = controllerMethod.getReturnType();
        if (CompletableFuture.class.isAssignableFrom(clazz)) {
            Type genericReturnType = controllerMethod.getGenericReturnType();
            ParameterizedType t = (ParameterizedType) genericReturnType;
            Type type2 = t.getActualTypeArguments()[0];
            if (!(type2 instanceof Class))
                throw new IllegalArgumentException("This route=" + meta + " has a method that MUST return a type 'Action' or 'XFuture<Action>' for this method(and did not)=" + controllerMethod);
            @SuppressWarnings("rawtypes") Class<?> type = (Class) type2;
            if (!Action.class.isAssignableFrom(type))
                throw new IllegalArgumentException("This route=" + meta + " has a method that MUST return a type 'Action' or 'XFuture<Action>' not 'XFuture<" + type.getSimpleName() + ">'for this method=" + controllerMethod);
        } else if (!Action.class.isAssignableFrom(clazz))
            throw new IllegalArgumentException("This route=" + meta + " has a method that MUST return a type 'Action' or 'XFuture<Action>' not '" + clazz.getSimpleName() + "' for this method=" + controllerMethod);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Action(org.webpieces.router.api.controller.actions.Action) Redirect(org.webpieces.router.api.controller.actions.Redirect)

Aggregations

Action (org.webpieces.router.api.controller.actions.Action)7 MethodMeta (org.webpieces.router.api.routes.MethodMeta)3 RequestContext (org.webpieces.ctx.api.RequestContext)2 Redirect (org.webpieces.router.api.controller.actions.Redirect)2 AbstractRouter (org.webpieces.router.impl.routers.AbstractRouter)2 Injector (com.google.inject.Injector)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Singleton (javax.inject.Singleton)1 EntityManager (javax.persistence.EntityManager)1 Query (javax.persistence.Query)1 NotBlank (javax.validation.constraints.NotBlank)1 Size (javax.validation.constraints.Size)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Current (org.webpieces.ctx.api.Current)1 Messages (org.webpieces.ctx.api.Messages)1