001// Copyright 2011 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.internal.jpa;
016
017import org.apache.tapestry5.http.services.Request;
018import org.apache.tapestry5.internal.services.SessionApplicationStatePersistenceStrategy;
019import org.apache.tapestry5.jpa.EntityManagerManager;
020import org.apache.tapestry5.services.ApplicationStateCreator;
021
022public class EntityApplicationStatePersistenceStrategy extends
023        SessionApplicationStatePersistenceStrategy
024{
025    private final EntityManagerManager entityManagerManager;
026
027    public EntityApplicationStatePersistenceStrategy(final Request request,
028            final EntityManagerManager entityManagerManager)
029    {
030        super(request);
031
032        this.entityManagerManager = entityManagerManager;
033    }
034
035    @Override
036    protected <T> T transformPersistedValue(Object value)
037    {
038        return value instanceof PersistedEntity ? (T) ((PersistedEntity) value).restore(entityManagerManager) : (T) value;
039    }
040
041    @Override
042    public <T> T get(final Class<T> ssoClass, final ApplicationStateCreator<T> creator)
043    {
044        final Object persistedValue = getOrCreate(ssoClass, creator);
045
046        if (persistedValue instanceof PersistedEntity)
047        {
048            final PersistedEntity persisted = (PersistedEntity) persistedValue;
049
050            final Object restored = transformPersistedValue(persisted);
051
052            // shall we maybe throw an exception instead?
053            if (restored == null)
054            {
055                set(ssoClass, null);
056                return (T) getOrCreate(ssoClass, creator);
057            }
058
059            return (T) restored;
060        }
061
062        return (T) persistedValue;
063    }
064
065    @Override
066    public <T> void set(final Class<T> ssoClass, final T sso)
067    {
068        final String key = buildKey(ssoClass);
069
070        Object entity;
071
072        if (sso != null)
073        {
074            try
075            {
076                entity = JpaInternalUtils.convertApplicationValueToPersisted(entityManagerManager,
077                        sso);
078            }
079            catch (final RuntimeException ex)
080            {
081                // if entity not attached to an EntityManager yet, store it as usual sso
082                entity = sso;
083            }
084        }
085        else
086        {
087            entity = sso;
088        }
089
090        getSession().setAttribute(key, entity);
091    }
092
093}