Hi all,

I am using Wicket, and I manage to detach the session with this :

package com.netzep.common.frontend.application;

import org.apache.wicket.model.IModel;
import org.qi4j.api.entity.Identity;
import org.qi4j.api.unitofwork.UnitOfWork;
import org.qi4j.api.unitofwork.UnitOfWorkCallback;
import org.qi4j.api.unitofwork.UnitOfWorkCompletionException;

public class EntityIModel<T extends Identity> implements IModel<T>,
                UnitOfWorkCallback {

        @Override
        public boolean equals(Object obj) {
                if (obj != null && this.identity != null
                                && obj instanceof EntityIModel<?>) {
                        EntityIModel<?> eim = (EntityIModel<?>) obj;
                        return eim.getIdentity() != null
                                        && 
eim.entityClass.equals(this.entityClass)
                                        && this.identity.equals(eim.identity);
                }
                return false;
        }

        String identity;
        Class<T> entityClass;
        transient private T entity;

        public EntityIModel(T entity, Class<T> entityClass) {
                this(entity.identity().get(), entityClass);
        }

        public EntityIModel(String identity, Class<T> entityClass) {
                super();
                this.identity = identity;
                this.entityClass = entityClass;
        }

        private UnitOfWork getUnitOfWork() {
                final UnitOfWork currentUnitOfWork = ((Qi4jWebApplication)
Qi4jWebApplication
                                
.get()).module.unitOfWorkFactory().currentUnitOfWork();
                currentUnitOfWork.addUnitOfWorkCallback(this);
                return currentUnitOfWork;
        }

        @Override
        public T getObject() {
                if (this.entity == null)
                        this.entity = getUnitOfWork().get(entityClass, 
identity);
                return this.entity;
        }

        @Override
        public void setObject(T object) {
                assert false;
        }

        @Override
        public void detach() {
                this.entity = null;
        }

        public String getIdentity() {
                return this.identity;
        }

        @Override
        public void afterCompletion(UnitOfWorkStatus status) {
                this.detach();
        }

        @Override
        public void beforeCompletion() throws UnitOfWorkCompletionException {
        }
}

It is far from perfect especially since the entity identity string ends up
in the session (a security layer may be needed here but I don't rely on
anything coming from the web tier anyway, the security aspects are in my
business layer)

Regarding the unitofwork management :

package com.netzep.common.frontend.application;

import org.apache.wicket.Page;
import org.apache.wicket.Response;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.protocol.http.WebRequestCycle;
import org.qi4j.api.structure.Module;
import org.qi4j.api.unitofwork.ConcurrentEntityModificationException;
import org.qi4j.api.unitofwork.UnitOfWork;
import org.qi4j.api.unitofwork.UnitOfWorkCallback;
import org.qi4j.api.unitofwork.UnitOfWorkCompletionException;
import org.qi4j.api.unitofwork.UnitOfWorkFactory;
import org.qi4j.api.unitofwork.UnitOfWorkCallback.UnitOfWorkStatus;

public class UoWRequestCycle extends WebRequestCycle {

        UnitOfWorkFactory uowf;

        public UoWRequestCycle(WebApplication application, WebRequest request,
                        Response response, Module module) {
                super(application, request, response);
                this.uowf = module.unitOfWorkFactory();
        }

        @Override
        protected void onBeginRequest() {
                super.onBeginRequest();
                if (this.uowf.currentUnitOfWork() == null) {
                        this.uowf.newUnitOfWork();
                }
        }

        @Override
        protected void onEndRequest() {
                final UnitOfWork currentUnitOfWork = 
this.uowf.currentUnitOfWork();

                if (currentUnitOfWork != null) {
                        try {
                                currentUnitOfWork.complete();
                        } catch (ConcurrentEntityModificationException e) {
                                e.refreshEntities(currentUnitOfWork);

                                try {
                                        currentUnitOfWork.complete();

                                } catch (ConcurrentEntityModificationException 
e1) {
                                        e1.printStackTrace();

                                } catch (UnitOfWorkCompletionException e1) {
                                        e1.printStackTrace();
                                }
                        } catch (UnitOfWorkCompletionException e) {
                                e.printStackTrace();
                        }
                }

                super.onEndRequest();
        }

        @Override
        public Page onRuntimeException(Page page, RuntimeException e) {
                this.uowf.currentUnitOfWork().discard();
                return super.onRuntimeException(page, e);
        }

}


Any comment and improvement welcome !

Phil





Niclas Hedhman wrote:
> 
> Adding to Rickard's reply...
> 
> The mentioned web framework wanted to leverage Wicket, but there are
> problems around the serialization of sessions. Exactly how this should be
> resolved isn't clear.
> 
> There is also work done with Struts2 integration, which I think currently
> sits in qi4j-sandbox. I have not looked at that myself, and perhaps Edward
> has other Richard Wallace.
> 
> Lastly, I have been thinking a lot on how this *should* be done with the
> Qi4j toolkit, but have so far failed to formulate a structure that I am
> happy with. GutFeeling is that it should turn out really tight and neat,
> thanks to contextual fragments and use of entities for state and
> valuecomposites for everything else.
> 
> Cheers
> Niclas
> 
> On Jul 18, 2009 10:36 PM, "Robert Krüger" <[email protected]> wrote:
> 
> 
> Hi,
> 
> I'm currently beginning to evaluate Qi4j and have stumbled across mentions
> of a web framework that's also being developed by the Qi4j team but have
> been unable to find any further information on that topic on the site or
> in
> the list archives.
> 
> Could anyone point me in the right direction or is it simply too early to
> ask?
> 
> Thanks in advance,
> 
> Robert
> 
> 
> 
> _______________________________________________
> qi4j-dev mailing list
> [email protected]
> http://lists.ops4j.org/mailman/listinfo/qi4j-dev
> 
> _______________________________________________
> qi4j-dev mailing list
> [email protected]
> http://lists.ops4j.org/mailman/listinfo/qi4j-dev
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Web-framework-tp24548505p24555233.html
Sent from the Qi4j-dev mailing list archive at Nabble.com.


_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev

Reply via email to