Search in sources :

Example 1 with AJAX

use of com.flowlogix.web.services.annotations.AJAX in project flowlogix by flowlogix.

the class AjaxAnnotationWorker method transform.

@Override
public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model) {
    boolean hasTrackerMixin = model.getMixinClassNames().contains(SessionTracker.class.getName());
    for (PlasticMethod method : plasticClass.getMethodsWithAnnotation(AJAX.class)) {
        final AJAX annotation = method.getAnnotation(AJAX.class);
        final boolean isVoid = method.isVoid();
        if (annotation.requireSession() && (!hasTrackerMixin)) {
            model.addMixinClassName(SessionTracker.class.getName());
            hasTrackerMixin = true;
        }
        method.addAdvice(new MethodAdvice() {

            @Override
            @SneakyThrows(IOException.class)
            public void advise(MethodInvocation invocation) {
                if (!request.isXHR() || annotation.requireSession() == false) {
                    invocation.proceed();
                } else {
                    // do not invoke on bad sessions
                    if (SessionTrackerUtil.isValidSession(rg.getActivePageName(), rg.getRequest().getSession(false))) {
                        invocation.proceed();
                    } else {
                        showSessionExpiredMessage = true;
                        SessionTrackerUtil.redirectToSelf(rg, linkSource);
                        if (!isVoid) {
                            invocation.setReturnValue(null);
                        }
                        return;
                    }
                }
                Object result = null;
                if (!isVoid) {
                    result = invocation.getReturnValue();
                }
                if (!request.isXHR()) {
                    if (result != null) {
                        result = defaultForReturnType(result.getClass());
                    }
                } else {
                    if (annotation.discardAfter()) {
                        cs.getActivePage().getComponentResources().discardPersistentFieldChanges();
                    }
                }
                if (!isVoid) {
                    invocation.setReturnValue(result);
                }
            }
        });
    }
}
Also used : SneakyThrows(lombok.SneakyThrows) MethodInvocation(org.apache.tapestry5.plastic.MethodInvocation) SessionTracker(com.flowlogix.web.mixins.SessionTracker) PlasticMethod(org.apache.tapestry5.plastic.PlasticMethod) MethodAdvice(org.apache.tapestry5.plastic.MethodAdvice) IOException(java.io.IOException) AJAX(com.flowlogix.web.services.annotations.AJAX)

Aggregations

SessionTracker (com.flowlogix.web.mixins.SessionTracker)1 AJAX (com.flowlogix.web.services.annotations.AJAX)1 IOException (java.io.IOException)1 SneakyThrows (lombok.SneakyThrows)1 MethodAdvice (org.apache.tapestry5.plastic.MethodAdvice)1 MethodInvocation (org.apache.tapestry5.plastic.MethodInvocation)1 PlasticMethod (org.apache.tapestry5.plastic.PlasticMethod)1