/
EntityApplicationStatePersistenceStrategy.java
87 lines (71 loc) · 2.64 KB
/
EntityApplicationStatePersistenceStrategy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2011 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.tapestry5.internal.jpa;
import org.apache.tapestry5.http.services.Request;
import org.apache.tapestry5.internal.services.SessionApplicationStatePersistenceStrategy;
import org.apache.tapestry5.jpa.EntityManagerManager;
import org.apache.tapestry5.services.ApplicationStateCreator;
public class EntityApplicationStatePersistenceStrategy extends
SessionApplicationStatePersistenceStrategy
{
private final EntityManagerManager entityManagerManager;
public EntityApplicationStatePersistenceStrategy(final Request request,
final EntityManagerManager entityManagerManager)
{
super(request);
this.entityManagerManager = entityManagerManager;
}
@Override
public <T> T get(final Class<T> ssoClass, final ApplicationStateCreator<T> creator)
{
final Object persistedValue = getOrCreate(ssoClass, creator);
if (persistedValue instanceof PersistedEntity)
{
final PersistedEntity persisted = (PersistedEntity) persistedValue;
final Object restored = persisted.restore(entityManagerManager);
// shall we maybe throw an exception instead?
if (restored == null)
{
set(ssoClass, null);
return (T) getOrCreate(ssoClass, creator);
}
return (T) restored;
}
return (T) persistedValue;
}
@Override
public <T> void set(final Class<T> ssoClass, final T sso)
{
final String key = buildKey(ssoClass);
Object entity;
if (sso != null)
{
try
{
entity = JpaInternalUtils.convertApplicationValueToPersisted(entityManagerManager,
sso);
}
catch (final RuntimeException ex)
{
// if entity not attached to an EntityManager yet, store it as usual sso
entity = sso;
}
}
else
{
entity = sso;
}
getSession().setAttribute(key, entity);
}
}