1 package org.starobjects.jpa.runtime.persistence.maps;
2
3 import org.hibernate.proxy.HibernateProxy;
4 import org.hibernate.proxy.LazyInitializer;
5 import org.nakedobjects.metamodel.adapter.NakedObject;
6 import org.nakedobjects.metamodel.adapter.oid.Oid;
7 import org.nakedobjects.metamodel.spec.NakedObjectSpecification;
8 import org.nakedobjects.metamodel.spec.feature.OneToOneAssociation;
9 import org.nakedobjects.runtime.persistence.adaptermanager.AdapterManagerDefault;
10 import org.nakedobjects.runtime.persistence.adaptermanager.AggregateAdapters;
11 import org.starobjects.jpa.metamodel.facets.object.entity.JpaEntityFacet;
12 import org.starobjects.jpa.metamodel.util.JpaPropertyUtils;
13 import org.starobjects.jpa.runtime.persistence.objectstore.hibspi.ResolveStateUtil;
14 import org.starobjects.jpa.runtime.persistence.oid.JpaOid;
15 import org.starobjects.jpa.runtime.persistence.oid.JpaOidGenerator;
16 import org.starobjects.jpa.runtime.util.JpaOidPropertyUtils;
17
18 public class JpaAdapterManager extends AdapterManagerDefault {
19
20
21
22
23 private long serviceId;
24
25 public JpaAdapterManager() {
26
27
28
29 }
30
31
32
33
34 @Override
35 public JpaOidGenerator getOidGenerator() {
36 return (JpaOidGenerator) super.getOidGenerator();
37 }
38
39 @Override
40 protected AggregateAdapters createOrRecreateRootAdapter(final Object pojo) {
41
42 final AggregateAdapters aggregateAdapters = super
43 .createOrRecreateRootAdapter(pojo);
44
45 final NakedObject rootAdapter = aggregateAdapters.getRootAdapter();
46
47
48 if (rootAdapter.getSpecification().isService()) {
49 final Long id = ++serviceId;
50 syncOidAndConvertToPersistent(rootAdapter, id);
51 ResolveStateUtil.markAsResolved(rootAdapter);
52 return aggregateAdapters;
53 }
54
55
56 updateResolveStateIfPersistent(rootAdapter);
57 return aggregateAdapters;
58 }
59
60 @Override
61 protected Oid createOid(final Object pojo) {
62 final JpaOid oid = getOidGenerator().createTransientOid(pojo);
63
64
65 final NakedObject adapter = getAdapterFactory()
66 .createAdapter(pojo, oid);
67 initializeOidIfEntity(adapter);
68
69 return oid;
70 }
71
72 private void initializeOidIfEntity(final NakedObject adapter) {
73 final NakedObjectSpecification noSpec = adapter.getSpecification();
74
75 if (!noSpec.containsFacet(JpaEntityFacet.class)) {
76 return;
77 }
78
79
80 final OneToOneAssociation idProperty = JpaPropertyUtils
81 .getIdPropertyFor(noSpec);
82 final NakedObject idAdapter = idProperty.get(adapter);
83
84
85 if (idAdapter != null) {
86 final Number id = (Number) idAdapter.getObject();
87 final JpaOid oid = (JpaOid) adapter.getOid();
88 convertToPersistentOid(oid, id);
89 }
90 }
91
92 private void convertToPersistentOid(final JpaOid oid, final Number id) {
93
94 oid.setNewId(id);
95 getOidGenerator().convertTransientToPersistentOid(oid);
96 }
97
98
99 private void updateResolveStateIfPersistent(final NakedObject adapter) {
100 if (adapter.getOid().isTransient()) {
101 return;
102 }
103
104 final Object pojo = adapter.getObject();
105 final LazyInitializer hibernateLazyInitializer = getHibernateLazyInitializer(pojo);
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 final Long id = JpaOidPropertyUtils.getIdFor(adapter);
123 if (id == null) {
124
125 return;
126 }
127
128
129
130
131
132
133
134
135
136 if (hibernateLazyInitializer != null &&
137 hibernateLazyInitializer.isUninitialized()) {
138 ResolveStateUtil.markAsGhost(adapter);
139 } else {
140 ResolveStateUtil.markAsResolved(adapter);
141 }
142
143 return;
144 }
145
146 private void syncOidAndConvertToPersistent(final NakedObject adapter,
147 final Number id) {
148 final JpaOid oid = (JpaOid) adapter.getOid();
149 convertToPersistentOid(oid, id);
150 }
151
152 private LazyInitializer getHibernateLazyInitializer(final Object pojo) {
153 if (!(pojo instanceof HibernateProxy)) {
154 return null;
155 }
156 final HibernateProxy hibernateProxy = (HibernateProxy) pojo;
157 return hibernateProxy.getHibernateLazyInitializer();
158 }
159 }
160
161
162