    001// Copyright 2009, 2014 The Apache Software Foundation
    002//
    003// Licensed under the Apache License, Version 2.0 (the "License");
    004// you may not use this file except in compliance with the License.
    005// You may obtain a copy of the License at
    006//
    007// http://www.apache.org/licenses/LICENSE-2.0
    008//
    009// Unless required by applicable law or agreed to in writing, software
    010// distributed under the License is distributed on an "AS IS" BASIS,
    011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012// See the License for the specific language governing permissions and
    013// limitations under the License.
    014
    015package org.apache.tapestry5.hibernate.web.internal;
    016
    017import org.apache.tapestry5.http.services.Request;
    018import org.apache.tapestry5.internal.services.SessionApplicationStatePersistenceStrategy;
    019import org.apache.tapestry5.services.ApplicationStateCreator;
    020import org.hibernate.Session;
    021
    022/**
    023 * Persists Hibernate entities as SSOs by storing their primary key in the {@link org.apache.tapestry5.http.services.Session}.
    024 *
    025 * @see org.apache.tapestry5.hibernate.web.internal.PersistedEntity
    026 */
    027public class EntityApplicationStatePersistenceStrategy extends SessionApplicationStatePersistenceStrategy
    028{
    029
    030    private final EntityPersistentFieldStrategy delegate;
    031
    032    public EntityApplicationStatePersistenceStrategy(Request request, Session hibernateSession)
    033    {
    034        super(request);
    035
    036        delegate = new EntityPersistentFieldStrategy(hibernateSession, null);
    037    }
    038
    039    @Override
    040    protected <T> T transformPersistedValue(Object value)
    041    {
    042        return value instanceof SessionRestorable ? (T) delegate.convertPersistedToApplicationValue(value) : (T) value;
    043    }
    044
    045    @Override
    046    @SuppressWarnings("unchecked")
    047    public <T> T get(Class<T> ssoClass, ApplicationStateCreator<T> creator)
    048    {
    049        final Object persistedValue = getOrCreate(ssoClass, creator);
    050
    051        if (persistedValue instanceof SessionRestorable)
    052        {
    053            Object restored = transformPersistedValue(persistedValue);
    054
    055            // Maybe throw an exception instead?
    056            if (restored == null)
    057            {
    058                set(ssoClass, null);
    059                return (T) getOrCreate(ssoClass, creator);
    060            }
    061
    062            return (T) restored;
    063        }
    064
    065        return (T) persistedValue;
    066    }
    067
    068    @Override
    069    public <T> void set(Class<T> ssoClass, T sso)
    070    {
    071        final String key = buildKey(ssoClass);
    072
    073        if (sso == null)
    074        {
    075            getSession().setAttribute(key, null);
    076            return;
    077        }
    078
    079        Object persistable = delegate.convertApplicationValueToPersisted(sso);
    080
    081        getSession().setAttribute(key, persistable);
    082    }
    083
    084}